From d00a6f792f7cae2279a4d7242d61ba57908c1fbb Mon Sep 17 00:00:00 2001 From: ZhangGe6 Date: Fri, 22 Apr 2022 20:27:50 +0800 Subject: [PATCH] a minimal netron for onnx --- __init__.py | 34 + __version__.py | 1 + app.js | 1033 ++ base.js | 678 + dagre.js | 2246 +++ electron.html | 315 + electron.js | 730 + favicon.ico | Bin 0 -> 34494 bytes flatbuffers.js | 392 + gzip.js | 234 + hdf5.js | 1464 ++ icon.png | Bin 0 -> 58106 bytes index.html | 374 + index.js | 1066 ++ json.js | 566 + numpy.js | 567 + onnx-metadata.json | 34063 +++++++++++++++++++++++++++++++++++++++++++ onnx-proto.js | 1737 +++ onnx-schema.js | 401 + onnx.js | 2490 ++++ pickle.js | 189 + protobuf.js | 1348 ++ python.js | 4090 ++++++ server.py | 298 + tar.js | 171 + text.js | 292 + view-grapher.css | 136 + view-grapher.js | 786 + view-sidebar.css | 89 + view-sidebar.js | 2277 +++ view.js | 2142 +++ weka.js | 256 + xml.js | 1790 +++ zip.js | 713 + 34 files changed, 62968 insertions(+) create mode 100644 __init__.py create mode 100644 __version__.py create mode 100644 app.js create mode 100644 base.js create mode 100644 dagre.js create mode 100644 electron.html create mode 100644 electron.js create mode 100644 favicon.ico create mode 100644 flatbuffers.js create mode 100644 gzip.js create mode 100644 hdf5.js create mode 100644 icon.png create mode 100644 index.html create mode 100644 index.js create mode 100644 json.js create mode 100644 numpy.js create mode 100644 onnx-metadata.json create mode 100644 onnx-proto.js create mode 100644 onnx-schema.js create mode 100644 onnx.js create mode 100644 pickle.js create mode 100644 protobuf.js create mode 100644 python.js create mode 100644 server.py create mode 100644 tar.js create mode 100644 text.js create mode 100644 view-grapher.css create mode 100644 view-grapher.js create mode 100644 view-sidebar.css create mode 100644 view-sidebar.js create mode 100644 view.js create mode 100644 weka.js create mode 100644 xml.js create mode 100644 zip.js diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..5456384 --- /dev/null +++ b/__init__.py @@ -0,0 +1,34 @@ + +import argparse +import sys +import os + +from .server import start +from .server import stop +from .server import status +from .server import wait +from .server import serve +from .__version__ import __version__ + +def main(): + parser = argparse.ArgumentParser(description='Viewer for neural network, deep learning and machine learning models.') + parser.add_argument('file', metavar='MODEL_FILE', help='model file to serve', nargs='?', default=None) + parser.add_argument('-v', '--version', help="print version", action='store_true') + parser.add_argument('-b', '--browse', help='launch web browser', action='store_true') + parser.add_argument('-p', '--port', help='port to serve', type=int) + parser.add_argument('--host', help="host to serve") + parser.add_argument('--log', help='log details to console', action='store_true') + args = parser.parse_args() + if args.file and not os.path.exists(args.file): + print("Model file '" + args.file + "' does not exist.") + sys.exit(2) + if args.version: + print(__version__) + sys.exit(0) + address = (args.host, args.port) if args.host else args.port if args.port else None + start(args.file, address=address, browse=args.browse, log=args.log) + wait() + sys.exit(0) + +if __name__ == '__main__': + main() diff --git a/__version__.py b/__version__.py new file mode 100644 index 0000000..6853c36 --- /dev/null +++ b/__version__.py @@ -0,0 +1 @@ +__version__ = '0.0.0' \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..ff99342 --- /dev/null +++ b/app.js @@ -0,0 +1,1033 @@ + +const electron = require('electron'); +const updater = require('electron-updater'); +const fs = require('fs'); +const os = require('os'); +const path = require('path'); +const process = require('process'); +const url = require('url'); + +class Application { + + constructor() { + + this._views = new ViewCollection(); + this._configuration = new ConfigurationService(); + this._menu = new MenuService(); + this._openQueue = []; + + electron.app.setAppUserModelId('com.lutzroeder.netron'); + electron.app.allowRendererProcessReuse = true; + + if (!electron.app.requestSingleInstanceLock()) { + electron.app.quit(); + return; + } + + electron.app.on('second-instance', (event, commandLine, workingDirectory) => { + const currentDirectory = process.cwd(); + process.chdir(workingDirectory); + const open = this._parseCommandLine(commandLine); + process.chdir(currentDirectory); + if (!open) { + if (this._views.count > 0) { + const view = this._views.item(0); + if (view) { + view.restore(); + } + } + } + }); + + electron.ipcMain.on('open-file-dialog', () => { + this._openFileDialog(); + }); + + electron.ipcMain.on('get-environment', (event) => { + event.returnValue = { + version: electron.app.getVersion(), + package: electron.app.isPackaged + }; + }); + electron.ipcMain.on('get-configuration', (event, obj) => { + event.returnValue = this._configuration.has(obj.name) ? this._configuration.get(obj.name) : undefined; + }); + electron.ipcMain.on('set-configuration', (event, obj) => { + this._configuration.set(obj.name, obj.value); + }); + electron.ipcMain.on('drop-paths', (event, data) => { + const paths = data.paths.filter((path) => { + if (fs.existsSync(path)) { + const stat = fs.statSync(path); + return stat.isFile() || stat.isDirectory(); + } + return false; + }); + this._dropPaths(event.sender, paths); + }); + electron.ipcMain.on('show-message-box', (event, options) => { + const owner = event.sender.getOwnerBrowserWindow(); + event.returnValue = electron.dialog.showMessageBoxSync(owner, options); + }); + electron.ipcMain.on('show-save-dialog', (event, options) => { + const owner = event.sender.getOwnerBrowserWindow(); + event.returnValue = electron.dialog.showSaveDialogSync(owner, options); + }); + + electron.app.on('will-finish-launching', () => { + electron.app.on('open-file', (event, path) => { + this._openPath(path); + }); + }); + + electron.app.on('ready', () => { + this._ready(); + }); + + electron.app.on('window-all-closed', () => { + if (process.platform !== 'darwin') { + electron.app.quit(); + } + }); + + electron.app.on('will-quit', () => { + this._configuration.save(); + }); + + this._parseCommandLine(process.argv); + this._checkForUpdates(); + } + + _parseCommandLine(argv) { + let open = false; + if (argv.length > 1) { + for (const arg of argv.slice(1)) { + if (!arg.startsWith('-') && arg !== path.dirname(__dirname)) { + const extension = path.extname(arg).toLowerCase(); + if (extension != '' && extension != 'js' && fs.existsSync(arg)) { + const stat = fs.statSync(arg); + if (stat.isFile() || stat.isDirectory()) { + this._openPath(arg); + open = true; + } + } + } + } + } + return open; + } + + _ready() { + this._configuration.load(); + if (!this._configuration.has('userId')) { + this._configuration.set('userId', this._uuid()); + } + if (this._openQueue) { + const queue = this._openQueue; + this._openQueue = null; + while (queue.length > 0) { + const file = queue.shift(); + this._openPath(file); + } + } + if (this._views.count == 0) { + this._views.openView(); + } + this._resetMenu(); + this._views.on('active-view-changed', () => { + this._updateMenu(); + }); + this._views.on('active-view-updated', () => { + this._updateMenu(); + }); + } + + _uuid() { + const buffer = new Uint8Array(16); + require("crypto").randomFillSync(buffer); + buffer[6] = buffer[6] & 0x0f | 0x40; + buffer[8] = buffer[8] & 0x3f | 0x80; + const code = Array.from(buffer).map((value) => value < 0x10 ? '0' + value.toString(16) : value.toString(16)).join(''); + return code.slice(0, 8) + '-' + code.slice(8, 12) + '-' + code.slice(12, 16) + '-' + code.slice(16, 20) + '-' + code.slice(20, 32); + } + + _openFileDialog() { + const showOpenDialogOptions = { + properties: [ 'openFile' ], + filters: [ + { name: 'All Model Files', extensions: [ + 'onnx', 'ort', 'pb', + 'h5', 'hd5', 'hdf5', 'json', 'keras', + 'mlmodel', 'mlpackage', + 'caffemodel', + 'model', 'dnn', 'cmf', 'mar', 'params', + 'pdmodel', 'pdparams', 'nb', + 'meta', + 'tflite', 'lite', 'tfl', + 'armnn', 'mnn', 'nn', 'uff', 'uff.txt', 'rknn', 'xmodel', 'kmodel', + 'ncnn', 'param', 'tnnproto', 'tmfile', 'ms', 'om', + 'pt', 'pth', 'ptl', 't7', + 'pkl', 'joblib', + 'pbtxt', 'prototxt', + 'cfg', 'xml', + 'zip', 'tar' ] } + ] + }; + const selectedFiles = electron.dialog.showOpenDialogSync(showOpenDialogOptions); + if (selectedFiles) { + for (const file of selectedFiles) { + this._openPath(file); + } + } + } + + _openPath(path) { + if (this._openQueue) { + this._openQueue.push(path); + return; + } + if (path && path.length > 0 && fs.existsSync(path)) { + const stat = fs.statSync(path); + if (stat.isFile() || stat.isDirectory()) { + // find existing view for this file + let view = this._views.find(path); + // find empty welcome window + if (view == null) { + view = this._views.find(null); + } + // create new window + if (view == null) { + view = this._views.openView(); + } + this._loadPath(path, view); + } + } + } + + _loadPath(path, view) { + const recents = this._configuration.get('recents').filter((recent) => path != recent.path); + view.open(path); + recents.unshift({ path: path }); + if (recents.length > 9) { + recents.splice(9); + } + this._configuration.set('recents', recents); + this._resetMenu(); + } + + _dropPaths(sender, paths) { + let view = this._views.from(sender); + for (const path of paths) { + if (view) { + this._loadPath(path, view); + view = null; + } + else { + this._openPath(path); + } + } + } + + _export() { + const view = this._views.activeView; + if (view && view.path) { + let defaultPath = 'Untitled'; + const file = view.path; + const lastIndex = file.lastIndexOf('.'); + if (lastIndex != -1) { + defaultPath = file.substring(0, lastIndex); + } + const owner = electron.BrowserWindow.getFocusedWindow(); + const showSaveDialogOptions = { + title: 'Export', + defaultPath: defaultPath, + buttonLabel: 'Export', + filters: [ + { name: 'PNG', extensions: [ 'png' ] }, + { name: 'SVG', extensions: [ 'svg' ] } + ] + }; + const selectedFile = electron.dialog.showSaveDialogSync(owner, showSaveDialogOptions); + if (selectedFile) { + view.execute('export', { 'file': selectedFile }); + } + } + } + + service(name) { + if (name == 'configuration') { + return this._configuration; + } + return undefined; + } + + execute(command, data) { + const view = this._views.activeView; + if (view) { + view.execute(command, data || {}); + } + this._updateMenu(); + } + + _reload() { + const view = this._views.activeView; + if (view && view.path) { + this._loadPath(view.path, view); + } + } + + _checkForUpdates() { + if (!electron.app.isPackaged) { + return; + } + const autoUpdater = updater.autoUpdater; + if (autoUpdater.app && autoUpdater.app.appUpdateConfigPath && !fs.existsSync(autoUpdater.app.appUpdateConfigPath)) { + return; + } + const promise = autoUpdater.checkForUpdates(); + if (promise) { + promise.catch((error) => { + /* eslint-disable */ + console.log(error.message); + /* eslint-enable */ + }); + } + } + + get package() { + if (!this._package) { + const file = path.join(path.dirname(__dirname), 'package.json'); + const data = fs.readFileSync(file); + this._package = JSON.parse(data); + this._package.date = new Date(fs.statSync(file).mtime); + } + return this._package; + } + + _about() { + let dialog = null; + const options = { + show: false, + backgroundColor: electron.nativeTheme.shouldUseDarkColors ? '#2d2d2d' : '#e6e6e6', + width: 400, + height: 250, + center: true, + minimizable: false, + maximizable: false, + useContentSize: true, + resizable: true, + fullscreenable: false, + webPreferences: { + nodeIntegration: true, + } + }; + if (process.platform === 'darwin') { + options.title = ''; + dialog = Application._aboutDialog; + } + else { + options.title = 'About ' + electron.app.name; + options.parent = electron.BrowserWindow.getFocusedWindow(); + options.modal = true; + options.showInTaskbar = false; + } + if (process.platform === 'win32') { + options.type = 'toolbar'; + } + if (!dialog) { + dialog = new electron.BrowserWindow(options); + if (process.platform === 'darwin') { + Application._aboutDialog = dialog; + } + dialog.removeMenu(); + dialog.excludedFromShownWindowsMenu = true; + dialog.webContents.on('new-window', (event, url) => { + if (url.startsWith('http://') || url.startsWith('https://')) { + event.preventDefault(); + electron.shell.openExternal(url); + } + }); + let content = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf-8'); + content = content.replace('{version}', this.package.version); + content = content.replace('Netron', ''); + content = content.replace('', ''); + content = content.replace(/)<[^<]*)*<\/script>/gi, ''); + content = content.replace(//gi, ''); + dialog.once('ready-to-show', () => { + dialog.resizable = false; + dialog.show(); + }); + dialog.on('close', function() { + electron.globalShortcut.unregister('Escape'); + Application._aboutDialog = null; + }); + dialog.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(content)); + electron.globalShortcut.register('Escape', function() { + dialog.close(); + }); + } + else { + dialog.show(); + } + } + + _updateMenu() { + const window = electron.BrowserWindow.getFocusedWindow(); + this._menu.update({ + window: window, + webContents: window ? window.webContents : null, + view: this._views.activeView + }, this._views.views.map((view) => view.window)); + } + + _resetMenu() { + const menuRecentsTemplate = []; + if (this._configuration.has('recents')) { + let recents = this._configuration.get('recents'); + recents = recents.filter((recent) => { + const path = recent.path; + if (fs.existsSync(path)) { + const stat = fs.statSync(path); + if (stat.isFile() || stat.isDirectory()) { + return true; + } + } + return false; + }); + if (recents.length > 9) { + recents.splice(9); + } + this._configuration.set('recents', recents); + for (let i = 0; i < recents.length; i++) { + const recent = recents[i]; + menuRecentsTemplate.push({ + path: recent.path, + label: Application.minimizePath(recent.path), + accelerator: ((process.platform === 'darwin') ? 'Cmd+' : 'Ctrl+') + (i + 1).toString(), + click: (item) => { this._openPath(item.path); } + }); + } + } + + const menuTemplate = []; + + if (process.platform === 'darwin') { + menuTemplate.unshift({ + label: electron.app.name, + submenu: [ + { + label: 'About ' + electron.app.name, + click: () => this._about() + }, + { type: 'separator' }, + { role: 'hide' }, + { role: 'hideothers' }, + { role: 'unhide' }, + { type: 'separator' }, + { role: 'quit' } + ] + }); + } + + menuTemplate.push({ + label: '&File', + submenu: [ + { + label: '&Open...', + accelerator: 'CmdOrCtrl+O', + click: () => { this._openFileDialog(); } + }, + { + label: 'Open &Recent', + submenu: menuRecentsTemplate + }, + { type: 'separator' }, + { + id: 'file.export', + label: '&Export...', + accelerator: 'CmdOrCtrl+Shift+E', + click: () => this._export(), + }, + { type: 'separator' }, + { role: 'close' }, + ] + }); + + if (process.platform !== 'darwin') { + menuTemplate.slice(-1)[0].submenu.push( + { type: 'separator' }, + { role: 'quit' } + ); + } + + if (process.platform == 'darwin') { + electron.systemPreferences.setUserDefault('NSDisabledDictationMenuItem', 'boolean', true); + electron.systemPreferences.setUserDefault('NSDisabledCharacterPaletteMenuItem', 'boolean', true); + } + + menuTemplate.push({ + label: '&Edit', + submenu: [ + { + id: 'edit.cut', + label: 'Cu&t', + accelerator: 'CmdOrCtrl+X', + click: () => this.execute('cut', null), + }, + { + id: 'edit.copy', + label: '&Copy', + accelerator: 'CmdOrCtrl+C', + click: () => this.execute('copy', null), + }, + { + id: 'edit.paste', + label: '&Paste', + accelerator: 'CmdOrCtrl+V', + click: () => this.execute('paste', null), + }, + { + id: 'edit.select-all', + label: 'Select &All', + accelerator: 'CmdOrCtrl+A', + click: () => this.execute('selectall', null), + }, + { type: 'separator' }, + { + id: 'edit.find', + label: '&Find...', + accelerator: 'CmdOrCtrl+F', + click: () => this.execute('find', null), + } + ] + }); + + const viewTemplate = { + label: '&View', + submenu: [ + { + id: 'view.toggle-attributes', + accelerator: 'CmdOrCtrl+D', + click: () => this.execute('toggle', 'attributes'), + }, + { + id: 'view.toggle-initializers', + accelerator: 'CmdOrCtrl+I', + click: () => this.execute('toggle', 'initializers'), + }, + { + id: 'view.toggle-names', + accelerator: 'CmdOrCtrl+U', + click: () => this.execute('toggle', 'names'), + }, + { + id: 'view.toggle-direction', + accelerator: 'CmdOrCtrl+K', + click: () => { this.execute('toggle', 'direction'); } + }, + { + id: 'view.toggle-mousewheel', + accelerator: 'CmdOrCtrl+M', + click: () => this.execute('toggle', 'mousewheel'), + }, + { type: 'separator' }, + { + id: 'view.reload', + label: '&Reload', + accelerator: (process.platform === 'darwin') ? 'Cmd+R' : 'F5', + click: () => this._reload(), + }, + { type: 'separator' }, + { + id: 'view.reset-zoom', + label: 'Actual &Size', + accelerator: 'Shift+Backspace', + click: () => this.execute('reset-zoom', null), + }, + { + id: 'view.zoom-in', + label: 'Zoom &In', + accelerator: 'Shift+Up', + click: () => this.execute('zoom-in', null), + }, + { + id: 'view.zoom-out', + label: 'Zoom &Out', + accelerator: 'Shift+Down', + click: () => this.execute('zoom-out', null), + }, + { type: 'separator' }, + { + id: 'view.show-properties', + label: '&Properties...', + accelerator: 'CmdOrCtrl+Enter', + click: () => this.execute('show-properties', null), + } + ] + }; + if (!electron.app.isPackaged) { + viewTemplate.submenu.push({ type: 'separator' }); + viewTemplate.submenu.push({ role: 'toggledevtools' }); + } + menuTemplate.push(viewTemplate); + + if (process.platform === 'darwin') { + menuTemplate.push({ + role: 'window', + submenu: [ + { role: 'minimize' }, + { role: 'zoom' }, + { type: 'separator' }, + { role: 'front'} + ] + }); + } + + const helpSubmenu = [ + { + label: '&Search Feature Requests', + click: () => { electron.shell.openExternal('https://www.github.com/' + this.package.repository + '/issues'); } + }, + { + label: 'Report &Issues', + click: () => { electron.shell.openExternal('https://www.github.com/' + this.package.repository + '/issues/new'); } + } + ]; + + if (process.platform != 'darwin') { + helpSubmenu.push({ type: 'separator' }); + helpSubmenu.push({ + label: 'About ' + electron.app.name, + click: () => this._about() + }); + } + + menuTemplate.push({ + role: 'help', + submenu: helpSubmenu + }); + + const commandTable = new Map(); + commandTable.set('file.export', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('edit.cut', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('edit.copy', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('edit.paste', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('edit.select-all', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('edit.find', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('view.toggle-attributes', { + enabled: (context) => { return context.view && context.view.path ? true : false; }, + label: (context) => { return !context.view || !context.view.get('attributes') ? 'Hide &Attributes' : 'Show &Attributes'; } + }); + commandTable.set('view.toggle-initializers', { + enabled: (context) => { return context.view && context.view.path ? true : false; }, + label: (context) => { return !context.view || context.view.get('initializers') ? 'Hide &Initializers' : 'Show &Initializers'; } + }); + commandTable.set('view.toggle-names', { + enabled: (context) => { return context.view && context.view.path ? true : false; }, + label: (context) => { return !context.view || context.view.get('names') ? 'Hide &Names' : 'Show &Names'; } + }); + commandTable.set('view.toggle-direction', { + enabled: (context) => { return context.view && context.view.path ? true : false; }, + label: (context) => { return !context.view || context.view.get('direction') === 'vertical' ? 'Show &Horizontal' : 'Show &Vertical'; } + }); + commandTable.set('view.toggle-mousewheel', { + enabled: (context) => { return context.view && context.view.path ? true : false; }, + label: (context) => { return !context.view || context.view.get('mousewheel') === 'scroll' ? '&Mouse Wheel: Zoom' : '&Mouse Wheel: Scroll'; } + }); + commandTable.set('view.reload', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('view.reset-zoom', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('view.zoom-in', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('view.zoom-out', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + commandTable.set('view.show-properties', { + enabled: (context) => { return context.view && context.view.path ? true : false; } + }); + + this._menu.build(menuTemplate, commandTable, this._views.views.map((view) => view.window)); + this._updateMenu(); + } + + static minimizePath(file) { + if (process.platform != 'win32') { + const homeDir = os.homedir(); + if (file.startsWith(homeDir)) { + return '~' + file.substring(homeDir.length); + } + } + return file; + } + +} + +class View { + + constructor(owner) { + this._owner = owner; + this._ready = false; + this._path = null; + this._properties = new Map(); + this._location = url.format({ protocol: 'file:', slashes: true, pathname: path.join(__dirname, 'electron.html') }); + + const size = electron.screen.getPrimaryDisplay().workAreaSize; + const options = { + show: false, + title: electron.app.name, + backgroundColor: electron.nativeTheme.shouldUseDarkColors ? '#1d1d1d' : '#e6e6e6', + icon: electron.nativeImage.createFromPath(path.join(__dirname, 'icon.png')), + minWidth: 600, + minHeight: 400, + width: size.width > 1024 ? 1024 : size.width, + height: size.height > 768 ? 768 : size.height, + webPreferences: { + preload: path.join(__dirname, 'electron.js'), + nodeIntegration: true + } + }; + if (this._owner.count > 0 && View._position && View._position.length == 2) { + options.x = View._position[0] + 30; + options.y = View._position[1] + 30; + if (options.x + options.width > size.width) { + options.x = 0; + } + if (options.y + options.height > size.height) { + options.y = 0; + } + } + this._window = new electron.BrowserWindow(options); + View._position = this._window.getPosition(); + this._updateCallback = (event, data) => { + if (event.sender == this._window.webContents) { + for (const entry of Object.entries(data)) { + this.update(entry[0], entry[1]); + } + this._raise('updated'); + } + }; + electron.ipcMain.on('update', this._updateCallback); + this._window.on('closed', () => { + electron.ipcMain.removeListener('update', this._updateCallback); + this._owner.closeView(this); + }); + this._window.on('focus', () => { + this._raise('activated'); + }); + this._window.on('blur', () => { + this._raise('deactivated'); + }); + this._window.webContents.on('did-finish-load', () => { + this._didFinishLoad = true; + }); + this._window.webContents.on('new-window', (event, url) => { + if (url.startsWith('http://') || url.startsWith('https://')) { + event.preventDefault(); + electron.shell.openExternal(url); + } + }); + this._window.once('ready-to-show', () => { + this._window.show(); + }); + this._window.loadURL(this._location); + } + + get window() { + return this._window; + } + + get path() { + return this._path; + } + + open(path) { + this._openPath = path; + if (this._didFinishLoad) { + this._window.webContents.send('open', { path: path }); + } + else { + this._window.webContents.on('did-finish-load', () => { + this._window.webContents.send('open', { path: path }); + }); + this._window.loadURL(this._location); + } + } + + restore() { + if (this._window) { + if (this._window.isMinimized()) { + this._window.restore(); + } + this._window.show(); + } + } + + match(path) { + if (this._openPath) { + if (path === null) { + return false; + } + if (path === this._openPath) { + return true; + } + } + return this._path == path; + } + + execute(command, data) { + if (this._window && this._window.webContents) { + this._window.webContents.send(command, data); + } + } + + update(name, value) { + if (name === 'path') { + if (value) { + this._path = value; + const title = Application.minimizePath(this._path); + this._window.setTitle(process.platform !== 'darwin' ? title + ' - ' + electron.app.name : title); + this._window.focus(); + } + this._openPath = null; + return; + } + this._properties.set(name, value); + } + + get(name) { + return this._properties.get(name); + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } +} + +class ViewCollection { + + constructor() { + this._views = []; + } + + get views() { + return this._views; + } + + get count() { + return this._views.length; + } + + item(index) { + return this._views[index]; + } + + openView() { + const view = new View(this); + view.on('activated', (sender) => { + this._activeView = sender; + this._raise('active-view-changed', { activeView: this._activeView }); + }); + view.on('updated', () => { + this._raise('active-view-updated', { activeView: this._activeView }); + }); + view.on('deactivated', () => { + this._activeView = null; + this._raise('active-view-changed', { activeView: this._activeView }); + }); + this._views.push(view); + this._updateActiveView(); + return view; + } + + closeView(view) { + for (let i = this._views.length - 1; i >= 0; i--) { + if (this._views[i] == view) { + this._views.splice(i, 1); + } + } + this._updateActiveView(); + } + + find(path) { + return this._views.find(view => view.match(path)); + } + + from(contents) { + return this._views.find(view => view && view.window && view.window.webContents && view.window.webContents == contents); + } + + get activeView() { + return this._activeView; + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } + + _updateActiveView() { + const window = electron.BrowserWindow.getFocusedWindow(); + const view = this._views.find(view => view.window == window) || null; + if (view != this._activeView) { + this._activeView = view; + this._raise('active-view-changed', { activeView: this._activeView }); + } + } +} + +class ConfigurationService { + + load() { + this._data = { 'recents': [] }; + const dir = electron.app.getPath('userData'); + if (dir && dir.length > 0) { + const file = path.join(dir, 'configuration.json'); + if (fs.existsSync(file)) { + const data = fs.readFileSync(file); + if (data) { + try { + this._data = JSON.parse(data); + } + catch (error) { + // continue regardless of error + } + } + } + } + } + + save() { + if (this._data) { + const data = JSON.stringify(this._data, null, 2); + if (data) { + const dir = electron.app.getPath('userData'); + if (dir && dir.length > 0) { + const file = path.join(dir, 'configuration.json'); + fs.writeFileSync(file, data); + } + } + } + } + + has(name) { + return this._data && Object.prototype.hasOwnProperty.call(this._data, name); + } + + set(name, value) { + this._data[name] = value; + } + + get(name) { + return this._data[name]; + } + +} + +class MenuService { + + build(menuTemplate, commandTable, windows) { + this._menuTemplate = menuTemplate; + this._commandTable = commandTable; + this._itemTable = new Map(); + for (const menu of menuTemplate) { + for (const item of menu.submenu) { + if (item.id) { + if (!item.label) { + item.label = ''; + } + this._itemTable.set(item.id, item); + } + } + } + this._rebuild(windows); + } + + update(context, windows) { + if (!this._menu && !this._commandTable) { + return; + } + if (this._updateLabel(context)) { + this._rebuild(windows); + } + this._updateEnabled(context); + } + + _rebuild(windows) { + this._menu = electron.Menu.buildFromTemplate(this._menuTemplate); + if (process.platform === 'darwin') { + electron.Menu.setApplicationMenu(this._menu); + } + else { + for (const window of windows) { + window.setMenu(this._menu); + } + } + } + + _updateLabel(context) { + let rebuild = false; + for (const entry of this._commandTable.entries()) { + const menuItem = this._menu.getMenuItemById(entry[0]); + const command = entry[1]; + if (command && command.label) { + const label = command.label(context); + if (label != menuItem.label) { + if (this._itemTable.has(entry[0])) { + this._itemTable.get(entry[0]).label = label; + rebuild = true; + } + } + } + } + return rebuild; + } + + _updateEnabled(context) { + for (const entry of this._commandTable.entries()) { + const menuItem = this._menu.getMenuItemById(entry[0]); + if (menuItem) { + const command = entry[1]; + if (command.enabled) { + menuItem.enabled = command.enabled(context); + } + } + } + } +} + +global.application = new Application(); diff --git a/base.js b/base.js new file mode 100644 index 0000000..cf15e17 --- /dev/null +++ b/base.js @@ -0,0 +1,678 @@ + +var base = base || {}; + +base.Int64 = class Int64 { + + constructor(low, high) { + this.low = low | 0; + this.high = high | 0; + } + + static create(value) { + if (isNaN(value)) { + return base.Int64.zero; + } + if (value <= -9223372036854776000) { + return base.Int64.min; + } + if (value + 1 >= 9223372036854776000) { + return base.Int64.max; + } + if (value < 0) { + return base.Int64.create(-value).negate(); + } + return new base.Int64((value % 4294967296) | 0, (value / 4294967296)); + } + + get isZero() { + return this.low === 0 && this.high === 0; + } + + get isNegative() { + return this.high < 0; + } + + negate() { + if (this.equals(base.Int64.min)) { + return base.Int64.min; + } + return this.not().add(base.Int64.one); + } + + not() { + return new Int64(~this.low, ~this.high); + } + + equals(other) { + if (!(other instanceof base.Int64) && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) { + return false; + } + return this.high === other.high && this.low === other.low; + } + + compare(other) { + if (this.equals(other)) { + return 0; + } + const thisNeg = this.isNegative; + const otherNeg = other.isNegative; + if (thisNeg && !otherNeg) { + return -1; + } + if (!thisNeg && otherNeg) { + return 1; + } + return this.subtract(other).isNegative ? -1 : 1; + } + + add(other) { + return base.Utility.add(this, other, false); + } + + subtract(other) { + return base.Utility.subtract(this, other, false); + } + + multiply(other) { + return base.Utility.multiply(this, other, false); + } + + divide(other) { + return base.Utility.divide(this, other, false); + } + + toInteger() { + return this.low; + } + + toNumber() { + if (this.high === 0) { + return this.low >>> 0; + } + if (this.high === -1) { + return this.low; + } + return (this.high * 4294967296) + (this.low >>> 0); + } + + toString(radix) { + const r = radix || 10; + if (r < 2 || r > 16) { + throw RangeError('radix'); + } + if (this.isZero) { + return '0'; + } + if (this.high < 0) { + if (this.equals(base.Int64.min)) { + const r = new Int64(radix, 0); + const div = this.divide(r); + const remainder = div.multiply(r).subtract(this); + return div.toString(r) + (remainder.low >>> 0).toString(r); + } + return '-' + this.negate().toString(r); + } + if (this.high === 0) { + return this.low.toString(radix); + } + return base.Utility.text(this, false, r); + } +}; + +base.Int64.min = new base.Int64(0, -2147483648); +base.Int64.zero = new base.Int64(0, 0); +base.Int64.one = new base.Int64(1, 0); +base.Int64.power24 = new base.Int64(1 << 24, 0); +base.Int64.max = new base.Int64(0, 2147483647); + +base.Uint64 = class Uint64 { + + constructor(low, high) { + this.low = low | 0; + this.high = high | 0; + } + + static create(value) { + if (isNaN(value)) { + return base.Uint64.zero; + } + if (value < 0) { + return base.Uint64.zero; + } + if (value >= 18446744073709552000) { + return base.Uint64.max; + } + if (value < 0) { + return base.Uint64.create(-value).negate(); + } + return new base.Uint64((value % 4294967296) | 0, (value / 4294967296)); + } + + get isZero() { + return this.low === 0 && this.high === 0; + } + + get isNegative() { + return false; + } + + negate() { + return this.not().add(base.Int64.one); + } + + not() { + return new base.Uint64(~this.low, ~this.high); + } + + equals(other) { + if (!(other instanceof base.Uint64) && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) { + return false; + } + return this.high === other.high && this.low === other.low; + } + + compare(other) { + if (this.equals(other)) { + return 0; + } + const thisNeg = this.isNegative; + const otherNeg = other.isNegative; + if (thisNeg && !otherNeg) { + return -1; + } + if (!thisNeg && otherNeg) { + return 1; + } + return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1; + } + + add(other) { + return base.Utility.add(this, other, true); + } + + subtract(other) { + return base.Utility.subtract(this, other, true); + } + + multiply(other) { + return base.Utility.multiply(this, other, true); + } + + divide(other) { + return base.Utility.divide(this, other, true); + } + + toInteger() { + return this.low >>> 0; + } + + toNumber() { + if (this.high === 0) { + return this.low >>> 0; + } + return ((this.high >>> 0) * 4294967296) + (this.low >>> 0); + } + + toString(radix) { + const r = radix || 10; + if (r < 2 || 36 < r) { + throw RangeError('radix'); + } + if (this.isZero) { + return '0'; + } + if (this.high === 0) { + return this.low.toString(radix); + } + return base.Utility.text(this, true, r); + } +}; + +base.Utility = class { + + static add(a, b, unsigned) { + const a48 = a.high >>> 16; + const a32 = a.high & 0xFFFF; + const a16 = a.low >>> 16; + const a00 = a.low & 0xFFFF; + const b48 = b.high >>> 16; + const b32 = b.high & 0xFFFF; + const b16 = b.low >>> 16; + const b00 = b.low & 0xFFFF; + let c48 = 0; + let c32 = 0; + let c16 = 0; + let c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 + b48; + c48 &= 0xFFFF; + return base.Utility._create((c16 << 16) | c00, (c48 << 16) | c32, unsigned); + } + + static subtract(a, b, unsigned) { + return base.Utility.add(a, b.negate(), unsigned); + } + + static multiply(a, b, unsigned) { + if (a.isZero) { + return base.Int64.zero; + } + if (b.isZero) { + return base.Int64.zero; + } + if (a.equals(base.Int64.min)) { + return b.isOdd() ? base.Int64.min : base.Int64.zero; + } + if (b.equals(base.Int64.min)) { + return b.isOdd() ? base.Int64.min : base.Int64.zero; + } + if (a.isNegative) { + if (b.isNegative) { + return this.negate().multiply(b.negate()); + } + else { + return this.negate().multiply(b).negate(); + } + } + else if (b.isNegative) { + return this.multiply(b.negate()).negate(); + } + if (a.compare(base.Int64.power24) < 0 && b.compare(base.Int64.power24) < 0) { + return unsigned ? base.Uint64.create(a.toNumber() * b.toNumber()) : base.Int64.create(a.toNumber() * b.toNumber()); + } + const a48 = a.high >>> 16; + const a32 = a.high & 0xFFFF; + const a16 = a.low >>> 16; + const a00 = a.low & 0xFFFF; + const b48 = b.high >>> 16; + const b32 = b.high & 0xFFFF; + const b16 = b.low >>> 16; + const b00 = b.low & 0xFFFF; + let c48 = 0; + let c32 = 0; + let c16 = 0; + let c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xFFFF; + return base.Utility._create((c16 << 16) | c00, (c48 << 16) | c32, unsigned); + } + + static divide(a, b, unsigned) { + if (b.isZero) { + throw Error('Division by zero.'); + } + if (a.isZero) { + return unsigned ? base.Uint64.zero : base.Int64.zero; + } + let approx; + let remainder; + let result; + if (!unsigned) { + if (a.equals(base.Int64.min)) { + if (b.equals(base.Int64.one) || b.equals(base.Int64.negativeOne)) { + return base.Int64.min; + } + else if (b.equals(base.Int64.min)) { + return base.Int64.one; + } + else { + const half = base.Utility._shiftRight(a, unsigned, 1); + const halfDivide = half.divide(b); + approx = base.Utility._shiftLeft(halfDivide, halfDivide instanceof base.Uint64, 1); + if (approx.eq(base.Int64.zero)) { + return b.isNegative ? base.Int64.one : base.Int64.negativeOne; + } + else { + remainder = a.subtract(b.multiply(approx)); + result = approx.add(remainder.divide(b)); + return result; + } + } + } + else if (b.equals(base.Int64.min)) { + return unsigned ? base.Uint64.zero : base.Int64.zero; + } + if (a.isNegative) { + if (b.isNegative) { + return this.negate().divide(b.negate()); + } + return a.negate().divide(b).negate(); + } + else if (b.isNegative) { + return a.divide(b.negate()).negate(); + } + result = base.Int64.zero; + } + else { + if (!(b instanceof base.Uint64)) { + b = new base.Uint64(b.low, b.high); + } + if (b.compare(a) > 0) { + return base.Int64.zero; + } + if (b.compare(base.Utility._shiftRight(a, unsigned, 1)) > 0) { + return base.Uint64.one; + } + result = base.Uint64.zero; + } + remainder = a; + while (remainder.compare(b) >= 0) { + let approx = Math.max(1, Math.floor(remainder.toNumber() / b.toNumber())); + const log2 = Math.ceil(Math.log(approx) / Math.LN2); + const delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48); + let approxResult = base.Int64.create(approx); + let approxRemainder = approxResult.multiply(b); + while (approxRemainder.isNegative || approxRemainder.compare(remainder) > 0) { + approx -= delta; + approxResult = unsigned ? base.Uint64.create(approx) : base.Int64.create(approx); + approxRemainder = approxResult.multiply(b); + } + if (approxResult.isZero) { + approxResult = base.Int64.one; + } + result = result.add(approxResult); + remainder = remainder.subtract(approxRemainder); + } + return result; + } + + static text(value, unsigned, radix) { + const power = unsigned ? base.Uint64.create(Math.pow(radix, 6)) : base.Int64.create(Math.pow(radix, 6)); + let remainder = value; + let result = ''; + for (;;) { + const remainderDiv = remainder.divide(power); + const intval = remainder.subtract(remainderDiv.multiply(power)).toInteger() >>> 0; + let digits = intval.toString(radix); + remainder = remainderDiv; + if (remainder.low === 0 && remainder.high === 0) { + return digits + result; + } + while (digits.length < 6) { + digits = '0' + digits; + } + result = '' + digits + result; + } + } + + static _shiftLeft(value, unsigned, shift) { + return base.Utility._create(value.low << shift, (value.high << shift) | (value.low >>> (32 - shift)), unsigned); + } + + static _shiftRight(value, unsigned, shift) { + return base.Utility._create((value.low >>> shift) | (value.high << (32 - shift)), value.high >> shift, unsigned); + } + + static _create(low, high, unsigned) { + return unsigned ? new base.Uint64(low, high) : new base.Int64(low, high); + } +}; + +base.Uint64.zero = new base.Uint64(0, 0); +base.Uint64.one = new base.Uint64(1, 0); +base.Uint64.max = new base.Uint64(-1, -1); + +if (!DataView.prototype.getFloat16) { + DataView.prototype.getFloat16 = function(byteOffset, littleEndian) { + const value = this.getUint16(byteOffset, littleEndian); + const e = (value & 0x7C00) >> 10; + let f = value & 0x03FF; + if (e == 0) { + f = 0.00006103515625 * (f / 1024); + } + else if (e == 0x1F) { + f = f ? NaN : Infinity; + } + else { + f = DataView.__float16_pow[e] * (1 + (f / 1024)); + } + return value & 0x8000 ? -f : f; + }; + DataView.__float16_pow = { + 1: 1/16384, 2: 1/8192, 3: 1/4096, 4: 1/2048, 5: 1/1024, 6: 1/512, 7: 1/256, 8: 1/128, + 9: 1/64, 10: 1/32, 11: 1/16, 12: 1/8, 13: 1/4, 14: 1/2, 15: 1, 16: 2, + 17: 4, 18: 8, 19: 16, 20: 32, 21: 64, 22: 128, 23: 256, 24: 512, + 25: 1024, 26: 2048, 27: 4096, 28: 8192, 29: 16384, 30: 32768, 31: 65536 + }; +} + +if (!DataView.prototype.setFloat16) { + DataView.prototype.setFloat16 = function(byteOffset, value, littleEndian) { + DataView.__float16_float[0] = value; + value = DataView.__float16_int[0]; + const s = (value >>> 16) & 0x8000; + const e = (value >>> 23) & 0xff; + const f = value & 0x7fffff; + const v = s | DataView.__float16_base[e] | (f >> DataView.__float16_shift[e]); + this.setUint16(byteOffset, v, littleEndian); + }; + DataView.__float16_float = new Float32Array(1); + DataView.__float16_int = new Uint32Array(DataView.__float16_float.buffer, 0, DataView.__float16_float.length); + DataView.__float16_base = new Uint32Array(256); + DataView.__float16_shift = new Uint32Array(256); + for (let i = 0; i < 256; ++i) { + const e = i - 127; + if (e < -27) { + DataView.__float16_base[i] = 0x0000; + DataView.__float16_shift[i] = 24; + } + else if (e < -14) { + DataView.__float16_base[i] = 0x0400 >> -e - 14; + DataView.__float16_shift[i] = -e - 1; + } + else if (e <= 15) { + DataView.__float16_base[i] = e + 15 << 10; + DataView.__float16_shift[i] = 13; + } + else if (e < 128) { + DataView.__float16_base[i] = 0x7c00; + DataView.__float16_shift[i] = 24; + } + else { + DataView.__float16_base[i] = 0x7c00; + DataView.__float16_shift[i] = 13; + } + } +} + +DataView.prototype.getInt64 = DataView.prototype.getInt64 || function(byteOffset, littleEndian) { + return littleEndian ? + new base.Int64(this.getUint32(byteOffset, true), this.getUint32(byteOffset + 4, true)) : + new base.Int64(this.getUint32(byteOffset + 4, true), this.getUint32(byteOffset, true)); +}; + +DataView.prototype.setInt64 = DataView.prototype.setInt64 || function(byteOffset, value, littleEndian) { + if (littleEndian) { + this.setUint32(byteOffset, value.low, true); + this.setUint32(byteOffset + 4, value.high, true); + } + else { + this.setUint32(byteOffset + 4, value.low, false); + this.setUint32(byteOffset, value.high, false); + } +}; + +DataView.prototype.getUint64 = DataView.prototype.getUint64 || function(byteOffset, littleEndian) { + return littleEndian ? + new base.Uint64(this.getUint32(byteOffset, true), this.getUint32(byteOffset + 4, true)) : + new base.Uint64(this.getUint32(byteOffset + 4, true), this.getUint32(byteOffset, true)); +}; + +DataView.prototype.setUint64 = DataView.prototype.setUint64 || function(byteOffset, value, littleEndian) { + if (littleEndian) { + this.setUInt32(byteOffset, value.low, true); + this.setUInt32(byteOffset + 4, value.high, true); + } + else { + this.setUInt32(byteOffset + 4, value.low, false); + this.setUInt32(byteOffset, value.high, false); + } +}; + +DataView.prototype.getBits = DataView.prototype.getBits || function(offset, bits /*, signed */) { + offset = offset * bits; + const available = (this.byteLength << 3) - offset; + if (bits > available) { + throw new RangeError(); + } + let value = 0; + let index = 0; + while (index < bits) { + const remainder = offset & 7; + const size = Math.min(bits - index, 8 - remainder); + value <<= size; + value |= (this.getUint8(offset >> 3) >> (8 - size - remainder)) & ~(0xff << size); + offset += size; + index += size; + } + return value; +}; + +base.BinaryReader = class { + + constructor(data) { + this._buffer = data instanceof Uint8Array ? data : data.peek(); + this._position = 0; + this._length = this._buffer.length; + this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength); + this._utf8 = new TextDecoder('utf-8'); + } + + get length() { + return this._length; + } + + get position() { + return this._position; + } + + seek(position) { + this._position = position >= 0 ? position : this._length + position; + if (this._position > this._length || this._position < 0) { + throw new Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + skip(offset) { + this._position += offset; + if (this._position > this._length) { + throw new Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + read(length) { + if (this._position === 0 && length === undefined) { + this._position = this._length; + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + return this._buffer.slice(position, this._position); + } + + byte() { + const position = this._position; + this.skip(1); + return this._buffer[position]; + } + + int8() { + const position = this._position; + this.skip(1); + return this._view.getInt8(position, true); + } + + int16() { + const position = this._position; + this.skip(2); + return this._view.getInt16(position, true); + } + + int32() { + const position = this._position; + this.skip(4); + return this._view.getInt32(position, true); + } + + int64() { + const position = this._position; + this.skip(8); + return this._view.getInt64(position, true).toNumber(); + } + + uint16() { + const position = this._position; + this.skip(2); + return this._view.getUint16(position, true); + } + + uint32() { + const position = this._position; + this.skip(4); + return this._view.getUint32(position, true); + } + + uint64() { + const position = this._position; + this.skip(8); + return this._view.getUint64(position, true).toNumber(); + } + + float32() { + const position = this._position; + this.skip(4); + return this._view.getFloat32(position, true); + } + + float64() { + const position = this._position; + this.skip(8); + return this._view.getFloat64(position, true); + } + + string() { + const length = this.uint32(); + const position = this._position; + this.skip(length); + const data = this._buffer.subarray(position, this._position); + return this._utf8.decode(data); + } +}; + +if (typeof window !== 'undefined' && typeof window.Long != 'undefined') { + window.long = { Long: window.Long }; + window.Int64 = base.Int64; + window.Uint64 = base.Uint64; +} + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.Int64 = base.Int64; + module.exports.Uint64 = base.Uint64; + module.exports.BinaryReader = base.BinaryReader; +} diff --git a/dagre.js b/dagre.js new file mode 100644 index 0000000..3eeda08 --- /dev/null +++ b/dagre.js @@ -0,0 +1,2246 @@ + +var dagre = dagre || {}; + +// Dagre graph layout +// https://github.com/dagrejs/dagre +// https://github.com/dagrejs/graphlib + +dagre.layout = (graph, options) => { + options = options || {}; + // options.time = true; + const time = (name, callback) => { + const start = Date.now(); + const result = callback(); + const duration = Date.now() - start; + if (options.time) { + /* eslint-disable */ + console.log(name + ': ' + duration + 'ms'); + /* eslint-enable */ + } + return result; + }; + + // Constructs a new graph from the input graph, which can be used for layout. + // This process copies only whitelisted attributes from the input graph to the + // layout graph. Thus this function serves as a good place to determine what + // attributes can influence layout. + const buildLayoutGraph = (graph) => { + const g = new dagre.Graph({ compound: true }); + g.options = Object.assign({}, { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: 'tb' }, graph.options); + for (const node of graph.nodes.values()) { + const v = node.v; + const label = node.label; + g.setNode(v, { + width: label.width || 0, + height: label.height || 0 + }); + g.setParent(v, graph.parent(v)); + } + for (const e of graph.edges.values()) { + const edge = e.label; + g.setEdge(e.v, e.w, { + minlen: edge.minlen || 1, + weight: edge.weight || 1, + width: edge.width || 0, + height: edge.height || 0, + labeloffset: edge.labeloffset || 10, + labelpos: edge.labelpos || 'r' + }); + } + return g; + }; + + const runLayout = (g, time) => { + let uniqueIdCounter = 0; + const uniqueId = (prefix) => { + const id = ++uniqueIdCounter; + return prefix + id; + }; + const flat = (list) => { + if (Array.isArray(list) && list.every((item) => !Array.isArray(item))) { + return list; + } + const target = []; + for (const item of list) { + if (!Array.isArray(item)) { + target.push(item); + continue; + } + for (const entry of item) { + target.push(entry); + } + } + return target; + }; + + // Adds a dummy node to the graph and return v. + const addDummyNode = (g, type, label, name) => { + let v; + do { + v = uniqueId(name); + } while (g.hasNode(v)); + label.dummy = type; + g.setNode(v, label); + return v; + }; + + const asNonCompoundGraph = (g) => { + const graph = new dagre.Graph({}); + graph.options = g.options; + for (const node of g.nodes.values()) { + const v = node.v; + if (g.children(v).length === 0) { + graph.setNode(v, node.label); + } + } + for (const e of g.edges.values()) { + graph.setEdge(e.v, e.w, e.label); + } + return graph; + }; + + const maxRank = (g) => { + let rank = Number.NEGATIVE_INFINITY; + for (const node of g.nodes.values()) { + const x = node.label.rank; + if (x !== undefined && x > rank) { + rank = x; + } + } + return rank === Number.NEGATIVE_INFINITY ? undefined : rank; + }; + + // Given a DAG with each node assigned 'rank' and 'order' properties, this function will produce a matrix with the ids of each node. + const buildLayerMatrix = (g) => { + const rank = maxRank(g); + const length = rank === undefined ? 0 : rank + 1; + const layering = Array.from(new Array(length), () => []); + for (const node of g.nodes.values()) { + const label = node.label; + const rank = label.rank; + if (rank !== undefined) { + layering[rank][label.order] = node.v; + } + } + return layering; + }; + + // This idea comes from the Gansner paper: to account for edge labels in our layout we split each rank in half by doubling minlen and halving ranksep. + // Then we can place labels at these mid-points between nodes. + // We also add some minimal padding to the width to push the label for the edge away from the edge itself a bit. + const makeSpaceForEdgeLabels = (g) => { + const graph = g.options; + graph.ranksep /= 2; + for (const e of g.edges.values()) { + const edge = e.label; + edge.minlen *= 2; + if (edge.labelpos.toLowerCase() !== 'c') { + if (graph.rankdir === 'TB' || graph.rankdir === 'BT') { + edge.width += edge.labeloffset; + } + else { + edge.height += edge.labeloffset; + } + } + } + }; + + const removeSelfEdges = (g) => { + for (const e of g.edges.values()) { + if (e.v === e.w) { + const label = e.vNode.label; + if (!label.selfEdges) { + label.selfEdges = []; + } + label.selfEdges.push({ e: e, label: e.label }); + g.removeEdge(e); + } + } + }; + + const acyclic_run = (g) => { + const fas = []; + const stack = new Set(); + const visited = new Set(); + const dfs = (v) => { + if (!visited.has(v)) { + visited.add(v); + stack.add(v); + for (const e of g.node(v).out) { + if (stack.has(e.w)) { + fas.push(e); + } + else { + dfs(e.w); + } + } + stack.delete(v); + } + }; + for (const v of g.nodes.keys()) { + dfs(v); + } + for (const e of fas) { + const label = e.label; + g.removeEdge(e); + label.forwardName = e.name; + label.reversed = true; + g.setEdge(e.w, e.v, label, uniqueId('rev')); + } + }; + const acyclic_undo = (g) => { + for (const e of g.edges.values()) { + const edge = e.label; + if (edge.reversed) { + edge.points.reverse(); + g.removeEdge(e); + const forwardName = edge.forwardName; + delete edge.reversed; + delete edge.forwardName; + g.setEdge(e.w, e.v, edge, forwardName); + } + } + }; + + // Returns the amount of slack for the given edge. + // The slack is defined as the difference between the length of the edge and its minimum length. + const slack = (g, e) => { + return e.wNode.label.rank - e.vNode.label.rank - e.label.minlen; + }; + + // Assigns a rank to each node in the input graph that respects the 'minlen' constraint specified on edges between nodes. + // This basic structure is derived from Gansner, et al., 'A Technique for Drawing Directed Graphs.' + // + // Pre-conditions: + // 1. Graph must be a connected DAG + // 2. Graph nodes must be objects + // 3. Graph edges must have 'weight' and 'minlen' attributes + // + // Post-conditions: + // 1. Graph nodes will have a 'rank' attribute based on the results of the + // algorithm. Ranks can start at any index (including negative), we'll + // fix them up later. + const rank = (g) => { + // Constructs a spanning tree with tight edges and adjusted the input node's ranks to achieve this. + // A tight edge is one that is has a length that matches its 'minlen' attribute. + // The basic structure for this function is derived from Gansner, et al., 'A Technique for Drawing Directed Graphs.' + // + // Pre-conditions: + // 1. Graph must be a DAG. + // 2. Graph must be connected. + // 3. Graph must have at least one node. + // 5. Graph nodes must have been previously assigned a 'rank' property that respects the 'minlen' property of incident edges. + // 6. Graph edges must have a 'minlen' property. + // + // Post-conditions: + // - Graph nodes will have their rank adjusted to ensure that all edges are tight. + // + // Returns a tree (undirected graph) that is constructed using only 'tight' edges. + const feasibleTree = (g) => { + const t = new dagre.Graph({ directed: false }); + // Choose arbitrary node from which to start our tree + const start = g.nodes.keys().next().value; + const size = g.nodes.size; + t.setNode(start, {}); + // Finds a maximal tree of tight edges and returns the number of nodes in the tree. + const tightTree = (t, g) => { + const stack = Array.from(t.nodes.keys()).reverse(); + while (stack.length > 0) { + const v = stack.pop(); + const node = g.node(v); + for (const e of node.in.concat(node.out)) { + const edgeV = e.v; + const w = (v === edgeV) ? e.w : edgeV; + if (!t.hasNode(w) && !slack(g, e)) { + t.setNode(w, {}); + t.setEdge(v, w, {}); + stack.push(w); + } + } + } + return t.nodes.size; + }; + while (tightTree(t, g) < size) { + // Finds the edge with the smallest slack that is incident on tree and returns it. + let minKey = Number.MAX_SAFE_INTEGER; + let edge = undefined; + for (const e of g.edges.values()) { + if (t.hasNode(e.v) !== t.hasNode(e.w)) { + const key = slack(g, e); + if (key < minKey) { + minKey = key; + edge = e; + } + } + } + const delta = t.hasNode(edge.v) ? slack(g, edge) : -slack(g, edge); + for (const v of t.nodes.keys()) { + g.node(v).label.rank += delta; + } + } + return t; + }; + // Initializes ranks for the input graph using the longest path algorithm. This + // algorithm scales well and is fast in practice, it yields rather poor + // solutions. Nodes are pushed to the lowest layer possible, leaving the bottom + // ranks wide and leaving edges longer than necessary. However, due to its + // speed, this algorithm is good for getting an initial ranking that can be fed + // into other algorithms. + // + // This algorithm does not normalize layers because it will be used by other + // algorithms in most cases. If using this algorithm directly, be sure to + // run normalize at the end. + // + // Pre-conditions: + // 1. Input graph is a DAG. + // 2. Input graph node labels can be assigned properties. + // + // Post-conditions: + // 1. Each node will be assign an (unnormalized) 'rank' property. + const longestPath = (g) => { + const visited = new Set(); + const dfs = (v) => { + const node = g.node(v); + if (visited.has(v)) { + return node.label.rank; + } + visited.add(v); + let rank = Number.MAX_SAFE_INTEGER; + for (const e of node.out) { + rank = Math.min(rank, dfs(e.w) - e.label.minlen); + } + if (rank === Number.MAX_SAFE_INTEGER) { + rank = 0; + } + node.label.rank = rank; + return rank; + }; + for (const node of g.nodes.values()) { + if (node.in.length === 0) { + dfs(node.v); + } + } + }; + // The network simplex algorithm assigns ranks to each node in the input graph + // and iteratively improves the ranking to reduce the length of edges. + // + // Preconditions: + // 1. The input graph must be a DAG. + // 2. All nodes in the graph must have an object value. + // 3. All edges in the graph must have 'minlen' and 'weight' attributes. + // + // Postconditions: + // 1. All nodes in the graph will have an assigned 'rank' attribute that has + // been optimized by the network simplex algorithm. Ranks start at 0. + // + // A rough sketch of the algorithm is as follows: + // 1. Assign initial ranks to each node. We use the longest path algorithm, + // which assigns ranks to the lowest position possible. In general this + // leads to very wide bottom ranks and unnecessarily long edges. + // 2. Construct a feasible tight tree. A tight tree is one such that all + // edges in the tree have no slack (difference between length of edge + // and minlen for the edge). This by itself greatly improves the assigned + // rankings by shorting edges. + // 3. Iteratively find edges that have negative cut values. Generally a + // negative cut value indicates that the edge could be removed and a new + // tree edge could be added to produce a more compact graph. + // + // Much of the algorithms here are derived from Gansner, et al., 'A Technique + // for Drawing Directed Graphs.' The structure of the file roughly follows the + // structure of the overall algorithm. + const networkSimplex = (g) => { + // Returns a new graph with only simple edges. Handles aggregation of data associated with multi-edges. + const simplify = (g) => { + const graph = new dagre.Graph(); + graph.options = g.options; + for (const node of g.nodes.values()) { + graph.setNode(node.v, node.label); + } + for (const e of g.edges.values()) { + const simpleEdge = graph.edge(e.v, e.w); + const simpleLabel = simpleEdge ? simpleEdge.label : { weight: 0, minlen: 1 }; + const label = e.label; + graph.setEdge(e.v, e.w, { + weight: simpleLabel.weight + label.weight, + minlen: Math.max(simpleLabel.minlen, label.minlen) + }); + } + return graph; + }; + const initLowLimValues = (tree, root) => { + const dfs = (tree, visited, nextLim, v, parent) => { + const low = nextLim; + const label = tree.node(v).label; + visited.add(v); + for (const w of tree.neighbors(v)) { + if (!visited.has(w)) { + nextLim = dfs(tree, visited, nextLim, w, v); + } + } + label.low = low; + label.lim = nextLim++; + if (parent) { + label.parent = parent; + } + else { + // TODO should be able to remove this when we incrementally update low lim + delete label.parent; + } + return nextLim; + }; + root = tree.nodes.keys().next().value; + const visited = new Set(); + dfs(tree, visited, 1, root); + }; + // Initializes cut values for all edges in the tree. + const initCutValues = (t, g) => { + const vs = []; + const visited = new Set(); + const stack = [ Array.from(t.nodes.keys()).reverse() ]; + while (stack.length > 0) { + const current = stack[stack.length - 1]; + if (Array.isArray(current)) { + const v = current.pop(); + if (current.length === 0) { + stack.pop(); + } + if (!visited.has(v)) { + visited.add(v); + const children = t.neighbors(v); + if (children.length > 0) { + stack.push(v); + stack.push(children.reverse()); + } + else { + vs.push(v); + } + } + } + else { + vs.push(stack.pop()); + } + } + for (const v of vs.slice(0, vs.length - 1)) { + // Given the tight tree, its graph, and a child in the graph calculate and + // return the cut value for the edge between the child and its parent. + const childLabel = t.node(v).label; + const parent = childLabel.parent; + // The graph's view of the tree edge we're inspecting + const edge = g.edge(v, parent); + // True if the child is on the tail end of the edge in the directed graph + const childIsTail = edge ? true : false; + // The accumulated cut value for the edge between this node and its parent + const graphEdge = edge ? edge.label : g.edge(parent, v).label; + let cutValue = graphEdge.weight; + const node = g.node(v); + for (const e of node.in.concat(node.out)) { + const isOutEdge = e.v === v; + const other = isOutEdge ? e.w : e.v; + if (other !== parent) { + const pointsToHead = isOutEdge === childIsTail; + cutValue += pointsToHead ? e.label.weight : -e.label.weight; + const edge = t.edge(v, other); + if (edge) { + const otherCutValue = edge.label.cutvalue; + cutValue += pointsToHead ? -otherCutValue : otherCutValue; + } + } + } + t.edge(v, parent).label.cutvalue = cutValue; + } + }; + const leaveEdge = (tree) => { + return Array.from(tree.edges.values()).find((e) => e.label.cutvalue < 0); + }; + const enterEdge = (t, g, edge) => { + let v = edge.v; + let w = edge.w; + // For the rest of this function we assume that v is the tail and w is the + // head, so if we don't have this edge in the graph we should flip it to + // match the correct orientation. + if (!g.edge(v, w)) { + v = edge.w; + w = edge.v; + } + const vLabel = t.node(v).label; + const wLabel = t.node(w).label; + let tailLabel = vLabel; + let flip = false; + // If the root is in the tail of the edge then we need to flip the logic that + // checks for the head and tail nodes in the candidates function below. + if (vLabel.lim > wLabel.lim) { + tailLabel = wLabel; + flip = true; + } + // Returns true if the specified node is descendant of the root node per the assigned low and lim attributes in the tree. + const isDescendant = (vLabel, rootLabel) => { + return rootLabel.low <= vLabel.lim && vLabel.lim <= rootLabel.lim; + }; + let minKey = Number.POSITIVE_INFINITY; + let minValue = undefined; + for (const edge of g.edges.values()) { + if (flip === isDescendant(t.node(edge.v).label, tailLabel) && + flip !== isDescendant(t.node(edge.w).label, tailLabel)) { + const key = slack(g, edge); + if (key < minKey) { + minKey = key; + minValue = edge; + } + } + } + return minValue; + }; + const exchangeEdges = (t, g, e, f) => { + t.removeEdge(e); + t.setEdge(f.v, f.w, {}); + initLowLimValues(t); + initCutValues(t, g); + // update ranks + const root = Array.from(t.nodes.keys()).find((v) => !g.node(v).label.parent); + const stack = [ root ]; + const visited = new Set(); + while (stack.length > 0) { + const v = stack.pop(); + if (!visited.has(v)) { + visited.add(v); + for (const w of t.neighbors(v).reverse()) { + stack.push(w); + } + } + } + const vs = Array.from(visited); + for (const v of vs.slice(1)) { + const parent = t.node(v).label.parent; + let edge = g.edge(v, parent); + let flipped = false; + if (!edge) { + edge = g.edge(parent, v); + flipped = true; + } + g.node(v).label.rank = g.node(parent).label.rank + (flipped ? edge.label.minlen : -edge.label.minlen); + } + }; + g = simplify(g); + longestPath(g); + const t = feasibleTree(g); + initLowLimValues(t); + initCutValues(t, g); + let e; + let f; + while ((e = leaveEdge(t))) { + f = enterEdge(t, g, e); + exchangeEdges(t, g, e, f); + } + }; + switch(g.options.ranker) { + case 'tight-tree': + longestPath(g); + feasibleTree(g); + break; + case 'longest-path': + longestPath(g); + break; + default: + networkSimplex(g); + break; + } + }; + + // Creates temporary dummy nodes that capture the rank in which each edge's label is going to, if it has one of non-zero width and height. + // We do this so that we can safely remove empty ranks while preserving balance for the label's position. + const injectEdgeLabelProxies = (g) => { + for (const e of g.edges.values()) { + const edge = e.label; + if (edge.width && edge.height) { + const v = e.vNode.label; + const w = e.wNode.label; + addDummyNode(g, 'edge-proxy', { rank: (w.rank - v.rank) / 2 + v.rank, e: e }, '_ep'); + } + } + }; + + const removeEmptyRanks = (g) => { + // Ranks may not start at 0, so we need to offset them + if (g.nodes.size > 0) { + let minRank = Number.MAX_SAFE_INTEGER; + let maxRank = Number.MIN_SAFE_INTEGER; + const nodes = Array.from(g.nodes.values()); + for (const node of nodes) { + const label = node.label; + if (label.rank !== undefined) { + minRank = Math.min(minRank, label.rank); + maxRank = Math.max(maxRank, label.rank); + } + } + const size = maxRank - minRank; + if (size > 0) { + const layers = new Array(size); + for (const node of nodes) { + const label = node.label; + if (label.rank !== undefined) { + const rank = label.rank - minRank; + if (!layers[rank]) { + layers[rank] = []; + } + layers[rank].push(node.v); + } + } + let delta = 0; + const nodeRankFactor = g.options.nodeRankFactor; + for (let i = 0; i < layers.length; i++) { + const vs = layers[i]; + if (vs === undefined && i % nodeRankFactor !== 0) { + delta--; + } + else if (delta && vs) { + for (const v of vs) { + g.node(v).label.rank += delta; + } + } + } + } + } + }; + + // A nesting graph creates dummy nodes for the tops and bottoms of subgraphs, + // adds appropriate edges to ensure that all cluster nodes are placed between + // these boundries, and ensures that the graph is connected. + // In addition we ensure, through the use of the minlen property, that nodes + // and subgraph border nodes do not end up on the same rank. + // + // Preconditions: + // 1. Input graph is a DAG + // 2. Nodes in the input graph has a minlen attribute + // + // Postconditions: + // 1. Input graph is connected. + // 2. Dummy nodes are added for the tops and bottoms of subgraphs. + // 3. The minlen attribute for nodes is adjusted to ensure nodes do not + // get placed on the same rank as subgraph border nodes. + // + // The nesting graph idea comes from Sander, 'Layout of Compound Directed Graphs.' + const nestingGraph_run = (g) => { + const root = addDummyNode(g, 'root', {}, '_root'); + const treeDepths = (g) => { + const depths = {}; + const dfs = (v, depth) => { + const children = g.children(v); + if (children && children.length > 0) { + for (const child of children) { + dfs(child, depth + 1); + } + } + depths[v] = depth; + }; + for (const v of g.children()) { + dfs(v, 1); + } + return depths; + }; + const dfs = (g, root, nodeSep, weight, height, depths, v) => { + const children = g.children(v); + if (!children.length) { + if (v !== root) { + g.setEdge(root, v, { weight: 0, minlen: nodeSep }); + } + return; + } + const top = addDummyNode(g, 'border', { width: 0, height: 0 }, '_bt'); + const bottom = addDummyNode(g, 'border', { width: 0, height: 0 }, '_bb'); + const label = g.node(v).label; + g.setParent(top, v); + label.borderTop = top; + g.setParent(bottom, v); + label.borderBottom = bottom; + for (const child of children) { + dfs(g, root, nodeSep, weight, height, depths, child); + const childNode = g.node(child).label; + const childTop = childNode.borderTop ? childNode.borderTop : child; + const childBottom = childNode.borderBottom ? childNode.borderBottom : child; + const thisWeight = childNode.borderTop ? weight : 2 * weight; + const minlen = childTop !== childBottom ? 1 : height - depths[v] + 1; + g.setEdge(top, childTop, { weight: thisWeight, minlen: minlen, nestingEdge: true }); + g.setEdge(childBottom, bottom, { weight: thisWeight, minlen: minlen, nestingEdge: true }); + } + if (!g.parent(v)) { + g.setEdge(root, top, { weight: 0, minlen: height + depths[v] }); + } + }; + const depths = treeDepths(g); + const height = Math.max(...Object.values(depths)) - 1; // Note: depths is an Object not an array + const nodeSep = 2 * height + 1; + g.options.nestingRoot = root; + // Multiply minlen by nodeSep to align nodes on non-border ranks. + for (const e of g.edges.values()) { + e.label.minlen *= nodeSep; + } + // Calculate a weight that is sufficient to keep subgraphs vertically compact + const weight = Array.from(g.edges.values()).reduce((acc, e) => acc + e.label.weight, 0) + 1; + // Create border nodes and link them up + for (const child of g.children()) { + dfs(g, root, nodeSep, weight, height, depths, child); + } + // Save the multiplier for node layers for later removal of empty border layers. + g.options.nodeRankFactor = nodeSep; + }; + const nestingGraph_cleanup = (g) => { + const graphLabel = g.options; + g.removeNode(graphLabel.nestingRoot); + delete graphLabel.nestingRoot; + for (const e of g.edges.values()) { + if (e.label.nestingEdge) { + g.removeEdge(e); + } + } + }; + + const assignRankMinMax = (g) => { + // Adjusts the ranks for all nodes in the graph such that all nodes v have rank(v) >= 0 and at least one node w has rank(w) = 0. + let min = Number.POSITIVE_INFINITY; + for (const node of g.nodes.values()) { + const rank = node.label.rank; + if (rank !== undefined && rank < min) { + min = rank; + } + } + for (const node of g.nodes.values()) { + const label = node.label; + if (label.rank !== undefined) { + label.rank -= min; + } + } + let maxRank = 0; + for (const node of g.nodes.values()) { + const label = node.label; + if (label.borderTop) { + label.minRank = g.node(label.borderTop).label.rank; + label.maxRank = g.node(label.borderBottom).label.rank; + maxRank = Math.max(maxRank, label.maxRank); + } + } + g.options.maxRank = maxRank; + }; + + // Breaks any long edges in the graph into short segments that span 1 layer each. + // This operation is undoable with the denormalize function. + // + // Pre-conditions: + // 1. The input graph is a DAG. + // 2. Each node in the graph has a 'rank' property. + // + // Post-condition: + // 1. All edges in the graph have a length of 1. + // 2. Dummy nodes are added where edges have been split into segments. + // 3. The graph is augmented with a 'dummyChains' attribute which contains + // the first dummy in each chain of dummy nodes produced. + const normalize = (g) => { + g.options.dummyChains = []; + for (const e of g.edges.values()) { + let v = e.v; + const w = e.w; + const name = e.name; + const edgeLabel = e.label; + const labelRank = edgeLabel.labelRank; + let vRank = g.node(v).label.rank; + const wRank = g.node(w).label.rank; + if (wRank !== vRank + 1) { + g.removeEdge(e); + let first = true; + vRank++; + while (vRank < wRank) { + edgeLabel.points = []; + delete e.key; + const attrs = { + width: 0, height: 0, + edgeLabel: edgeLabel, + edgeObj: e, + rank: vRank + }; + const dummy = addDummyNode(g, 'edge', attrs, '_d'); + if (vRank === labelRank) { + attrs.width = edgeLabel.width; + attrs.height = edgeLabel.height; + attrs.dummy = 'edge-label'; + attrs.labelpos = edgeLabel.labelpos; + } + g.setEdge(v, dummy, { weight: edgeLabel.weight }, name); + if (first) { + g.options.dummyChains.push(dummy); + first = false; + } + v = dummy; + vRank++; + } + g.setEdge(v, w, { weight: edgeLabel.weight }, name); + } + } + }; + + const denormalize = (g) => { + for (let v of g.options.dummyChains) { + let label = g.node(v).label; + const edgeLabel = label.edgeLabel; + const e = label.edgeObj; + g.setEdge(e.v, e.w, edgeLabel, e.name); + while (label.dummy) { + const w = g.successors(v)[0]; + g.removeNode(v); + edgeLabel.points.push({ x: label.x, y: label.y }); + if (label.dummy === 'edge-label') { + edgeLabel.x = label.x; + edgeLabel.y = label.y; + edgeLabel.width = label.width; + edgeLabel.height = label.height; + } + v = w; + label = g.node(v).label; + } + } + }; + + const removeEdgeLabelProxies = (g) => { + for (const node of g.nodes.values()) { + const label = node.label; + if (label.dummy === 'edge-proxy') { + label.e.label.labelRank = label.rank; + g.removeNode(node.v); + } + } + }; + + const parentDummyChains = (g) => { + // Find a path from v to w through the lowest common ancestor (LCA). Return the full path and the LCA. + const findPath = (g, postorderNums, v, w) => { + const vPath = []; + const wPath = []; + const low = Math.min(postorderNums[v].low, postorderNums[w].low); + const lim = Math.max(postorderNums[v].lim, postorderNums[w].lim); + // Traverse up from v to find the LCA + let parent = v; + do { + parent = g.parent(parent); + vPath.push(parent); + } + while (parent && (postorderNums[parent].low > low || lim > postorderNums[parent].lim)); + const lca = parent; + // Traverse from w to LCA + parent = w; + while ((parent = g.parent(parent)) !== lca) { + wPath.push(parent); + } + return { path: vPath.concat(wPath.reverse()), lca: lca }; + }; + const postorder = (g) => { + const result = {}; + let lim = 0; + const dfs = (v) => { + const low = lim; + for (const u of g.children(v)) { + dfs(u); + } + result[v] = { low: low, lim: lim++ }; + }; + for (const v of g.children()) { + dfs(v); + } + return result; + }; + const postorderNums = postorder(g); + for (let v of g.options.dummyChains || []) { + const node = g.node(v).label; + const edgeObj = node.edgeObj; + const pathData = findPath(g, postorderNums, edgeObj.v, edgeObj.w); + const path = pathData.path; + const lca = pathData.lca; + let pathIdx = 0; + let pathV = path[pathIdx]; + let ascending = true; + while (v !== edgeObj.w) { + const node = g.node(v).label; + if (ascending) { + while ((pathV = path[pathIdx]) !== lca && g.node(pathV).label.maxRank < node.rank) { + pathIdx++; + } + if (pathV === lca) { + ascending = false; + } + } + if (!ascending) { + while (pathIdx < path.length - 1 && g.node(pathV = path[pathIdx + 1]).label.minRank <= node.rank) { + pathIdx++; + } + pathV = path[pathIdx]; + } + g.setParent(v, pathV); + v = g.successors(v)[0]; + } + } + }; + + const addBorderSegments = (g) => { + const addBorderNode = (g, prop, prefix, sg, sgNode, rank) => { + const label = { width: 0, height: 0, rank: rank, borderType: prop }; + const prev = sgNode[prop][rank - 1]; + const curr = addDummyNode(g, 'border', label, prefix); + sgNode[prop][rank] = curr; + g.setParent(curr, sg); + if (prev) { + g.setEdge(prev, curr, { weight: 1 }); + } + }; + const queue = g.children(); + while (queue.length > 0) { + const v = queue.shift(); + const node = g.node(v).label; + if ('minRank' in node) { + node.borderLeft = []; + node.borderRight = []; + const maxRank = node.maxRank + 1; + for (let rank = node.minRank; rank < maxRank; rank++) { + addBorderNode(g, 'borderLeft', '_bl', v, node, rank); + addBorderNode(g, 'borderRight', '_br', v, node, rank); + } + } + const children = g.children(v); + if (children.length) { + for (const v of children) { + queue.push(v); + } + } + } + }; + + // Applies heuristics to minimize edge crossings in the graph and sets the best order solution as an order attribute on each node. + // + // Pre-conditions: + // 1. Graph must be DAG + // 2. Graph nodes must have the 'rank' attribute + // 3. Graph edges must have the 'weight' attribute + // + // Post-conditions: + // 1. Graph nodes will have an 'order' attribute based on the results of the algorithm. + const order = (g) => { + const sortSubgraph = (g, v, cg, biasRight) => { + // Given a list of entries of the form {v, barycenter, weight} and a constraint graph this function will resolve any conflicts between the constraint graph and the barycenters for the entries. + // If the barycenters for an entry would violate a constraint in the constraint graph then we coalesce the nodes in the conflict into a new node that respects the contraint and aggregates barycenter and weight information. + // This implementation is based on the description in Forster, 'A Fast and Simple Hueristic for Constrained Two-Level Crossing Reduction,' thought it differs in some specific details. + // + // Pre-conditions: + // 1. Each entry has the form {v, barycenter, weight}, or if the node has no barycenter, then {v}. + // + // Returns: + // A new list of entries of the form {vs, i, barycenter, weight}. + // The list `vs` may either be a singleton or it may be an aggregation of nodes ordered such that they do not violate constraints from the constraint graph. + // The property `i` is the lowest original index of any of the elements in `vs`. + const resolveConflicts = (entries, cg) => { + const mappedEntries = new Map(); + for (let i = 0; i < entries.length; i++) { + const entry = entries[i]; + const tmp = { indegree: 0, 'in': [], out: [], vs: [ entry.v ], i: i }; + if (entry.barycenter !== undefined) { + tmp.barycenter = entry.barycenter; + tmp.weight = entry.weight; + } + mappedEntries.set(entry.v, tmp); + } + for (const e of cg.edges.values()) { + const entryV = mappedEntries.get(e.v); + const entryW = mappedEntries.get(e.w); + if (entryV && entryW) { + entryW.indegree++; + entryV.out.push(entryW); + } + } + const sourceSet = Array.from(mappedEntries.values()).filter((entry) => !entry.indegree); + const results = []; + function handleIn(vEntry) { + return function(uEntry) { + if (uEntry.merged) { + return; + } + if (uEntry.barycenter === undefined || vEntry.barycenter === undefined || uEntry.barycenter >= vEntry.barycenter) { + let sum = 0; + let weight = 0; + if (vEntry.weight) { + sum += vEntry.barycenter * vEntry.weight; + weight += vEntry.weight; + } + if (uEntry.weight) { + sum += uEntry.barycenter * uEntry.weight; + weight += uEntry.weight; + } + vEntry.vs = uEntry.vs.concat(vEntry.vs); + vEntry.barycenter = sum / weight; + vEntry.weight = weight; + vEntry.i = Math.min(uEntry.i, vEntry.i); + uEntry.merged = true; + } + }; + } + function handleOut(vEntry) { + return function(wEntry) { + wEntry.in.push(vEntry); + if (--wEntry.indegree === 0) { + sourceSet.push(wEntry); + } + }; + } + while (sourceSet.length) { + const entry = sourceSet.pop(); + results.push(entry); + entry.in.reverse().forEach(handleIn(entry)); + entry.out.forEach(handleOut(entry)); + } + return results.filter((entry) => !entry.merged).map((entry) => { + const value = { + vs: entry.vs, + i: entry.i + }; + if (entry.barycenter !== undefined) { + value.barycenter = entry.barycenter; + } + if (entry.weight !== undefined) { + value.weight = entry.weight; + } + return value; + }); + }; + const barycenter = (g, movable) => { + return (movable || []).map((v) => { + const inV = g.node(v).in; + if (!inV.length) { + return { v: v }; + } + else { + const result = inV.reduce((acc, e) => { + const edge = e.label; + const nodeU = e.vNode.label; + return { + sum: acc.sum + (edge.weight * nodeU.order), + weight: acc.weight + edge.weight + }; + }, { sum: 0, weight: 0 }); + return { + v: v, + barycenter: result.sum / result.weight, + weight: result.weight + }; + } + }); + }; + const sort = (entries, biasRight) => { + const consumeUnsortable = (vs, unsortable, index) => { + let last; + while (unsortable.length && (last = unsortable[unsortable.length - 1]).i <= index) { + unsortable.pop(); + vs.push(last.vs); + index++; + } + return index; + }; + const compareWithBias = (bias) => { + return function(entryV, entryW) { + if (entryV.barycenter < entryW.barycenter) { + return -1; + } + else if (entryV.barycenter > entryW.barycenter) { + return 1; + } + return !bias ? entryV.i - entryW.i : entryW.i - entryV.i; + }; + }; + // partition + const parts = { lhs: [], rhs: [] }; + for (const value of entries) { + if ('barycenter' in value) { + parts.lhs.push(value); + } + else { + parts.rhs.push(value); + } + } + const sortable = parts.lhs; + const unsortable = parts.rhs.sort((a, b) => -a.i + b.i); + const vs = []; + let sum = 0; + let weight = 0; + let vsIndex = 0; + sortable.sort(compareWithBias(!!biasRight)); + vsIndex = consumeUnsortable(vs, unsortable, vsIndex); + for (const entry of sortable) { + vsIndex += entry.vs.length; + vs.push(entry.vs); + sum += entry.barycenter * entry.weight; + weight += entry.weight; + vsIndex = consumeUnsortable(vs, unsortable, vsIndex); + } + const result = { vs: flat(vs) }; + if (weight) { + result.barycenter = sum / weight; + result.weight = weight; + } + return result; + }; + const node = g.node(v); + const bl = node && node.label ? node.label.borderLeft : undefined; + const br = node && node.label ? node.label.borderRight: undefined; + const subgraphs = {}; + const movable = bl ? g.children(v).filter((w) => w !== bl && w !== br) : g.children(v); + const barycenters = barycenter(g, movable); + for (const entry of barycenters) { + if (g.children(entry.v).length) { + const result = sortSubgraph(g, entry.v, cg, biasRight); + subgraphs[entry.v] = result; + if ('barycenter' in result) { + if (entry.barycenter !== undefined) { + entry.barycenter = (entry.barycenter * entry.weight + result.barycenter * result.weight) / (entry.weight + result.weight); + entry.weight += result.weight; + } + else { + entry.barycenter = result.barycenter; + entry.weight = result.weight; + } + } + } + } + const entries = resolveConflicts(barycenters, cg); + // expand subgraphs + for (const entry of entries) { + entry.vs = flat(entry.vs.map((v) => subgraphs[v] ? subgraphs[v].vs : v)); + } + const result = sort(entries, biasRight); + if (bl) { + result.vs = flat([bl, result.vs, br]); + if (g.predecessors(bl).length) { + const blPred = g.node(g.predecessors(bl)[0]).label; + const brPred = g.node(g.predecessors(br)[0]).label; + if (!('barycenter' in result)) { + result.barycenter = 0; + result.weight = 0; + } + result.barycenter = (result.barycenter * result.weight + blPred.order + brPred.order) / (result.weight + 2); + result.weight += 2; + } + } + return result; + }; + const sweepLayerGraphs = (layerGraphs, biasRight) => { + const cg = new dagre.Graph(); + for (const lg of layerGraphs) { + const root = lg.options.root; + const sorted = sortSubgraph(lg, root, cg, biasRight); + const vs = sorted.vs; + const length = vs.length; + for (let i = 0; i < length; i++) { + lg.node(vs[i]).label.order = i; + } + // add subgraph constraints + const prev = {}; + let rootPrev; + let exit = false; + for (const v of vs) { + let child = lg.parent(v); + let prevChild; + while (child) { + const parent = lg.parent(child); + if (parent) { + prevChild = prev[parent]; + prev[parent] = child; + } + else { + prevChild = rootPrev; + rootPrev = child; + } + if (prevChild && prevChild !== child) { + cg.setEdge(prevChild, child, null); + exit = true; + break; + } + child = parent; + } + if (exit) { + break; + } + } + } + }; + // A function that takes a layering (an array of layers, each with an array of + // ordererd nodes) and a graph and returns a weighted crossing count. + // + // Pre-conditions: + // 1. Input graph must be simple (not a multigraph), directed, and include + // only simple edges. + // 2. Edges in the input graph must have assigned weights. + // + // Post-conditions: + // 1. The graph and layering matrix are left unchanged. + // + // This algorithm is derived from Barth, et al., 'Bilayer Cross Counting.' + const crossCount = (g, layering) => { + let count = 0; + for (let i = 1; i < layering.length; i++) { + const northLayer = layering[i - 1]; + const southLayer = layering[i]; + // Sort all of the edges between the north and south layers by their position in the north layer and then the south. + // Map these edges to the position of their head in the south layer. + const southPos = {}; + for (let i = 0; i < southLayer.length; i++) { + southPos[southLayer[i]] = i; + } + const southEntries = []; + for (const v of northLayer) { + const entries = []; + for (const e of g.node(v).out) { + entries.push({ + pos: southPos[e.w], + weight: e.label.weight + }); + } + entries.sort((a, b) => a.pos - b.pos); + for (const entry of entries) { + southEntries.push(entry); + } + } + // Build the accumulator tree + let firstIndex = 1; + while (firstIndex < southLayer.length) { + firstIndex <<= 1; + } + const treeSize = 2 * firstIndex - 1; + firstIndex -= 1; + const tree = Array.from(new Array(treeSize), () => 0); + // Calculate the weighted crossings + for (const entry of southEntries) { + let index = entry.pos + firstIndex; + tree[index] += entry.weight; + let weightSum = 0; + while (index > 0) { + if (index % 2) { + weightSum += tree[index + 1]; + } + index = (index - 1) >> 1; + tree[index] += entry.weight; + } + count += entry.weight * weightSum; + } + } + return count; + }; + // Assigns an initial order value for each node by performing a DFS search + // starting from nodes in the first rank. Nodes are assigned an order in their + // rank as they are first visited. + // + // This approach comes from Gansner, et al., 'A Technique for Drawing Directed + // Graphs.' + // + // Returns a layering matrix with an array per layer and each layer sorted by + // the order of its nodes. + const initOrder = (g) => { + const visited = new Set(); + const nodes = Array.from(g.nodes.keys()).filter((v) => !g.children(v).length); + let maxRank = undefined; + for (const v of nodes) { + if (!g.children(v).length > 0) { + const rank = g.node(v).label.rank; + if (maxRank === undefined || (rank !== undefined && rank > maxRank)) { + maxRank = rank; + } + } + } + if (maxRank !== undefined) { + const layers = Array.from(new Array(maxRank + 1), () => []); + for (const v of nodes.map((v) => [ g.node(v).label.rank, v ]).sort((a, b) => a[0] - b[0]).map((item) => item[1])) { + const queue = [ v ]; + while (queue.length > 0) { + const v = queue.shift(); + if (!visited.has(v)) { + visited.add(v); + const rank = g.node(v).label.rank; + layers[rank].push(v); + queue.push(...g.successors(v)); + } + } + } + return layers; + } + return []; + }; + // Constructs a graph that can be used to sort a layer of nodes. + // The graph will contain all base and subgraph nodes from the request layer in their original + // hierarchy and any edges that are incident on these nodes and are of the type requested by the 'relationship' parameter. + // + // Nodes from the requested rank that do not have parents are assigned a root node in the output graph, + // which is set in the root graph attribute. + // This makes it easy to walk the hierarchy of movable nodes during ordering. + // + // Pre-conditions: + // 1. Input graph is a DAG + // 2. Base nodes in the input graph have a rank attribute + // 3. Subgraph nodes in the input graph has minRank and maxRank attributes + // 4. Edges have an assigned weight + // + // Post-conditions: + // 1. Output graph has all nodes in the movable rank with preserved hierarchy. + // 2. Root nodes in the movable layer are made children of the node + // indicated by the root attribute of the graph. + // 3. Non-movable nodes incident on movable nodes, selected by the + // relationship parameter, are included in the graph (without hierarchy). + // 4. Edges incident on movable nodes, selected by the relationship parameter, are added to the output graph. + // 5. The weights for copied edges are aggregated as need, since the output graph is not a multi-graph. + const buildLayerGraph = (g, nodes, rank, relationship) => { + let root; + while (g.hasNode((root = uniqueId('_root')))) { + // continue + } + const graph = new dagre.Graph({ compound: true }); + graph.options = { root: root }; + graph.setDefaultNodeLabel((v) => { const node = g.node(v); return node ? node.label : undefined; }); + const length = nodes.length; + let i = 0; + while (i < length) { + const node = nodes[i++]; + const label = node.label; + if (label.rank === rank || 'minRank' in label && 'maxRank' in label && label.minRank <= rank && rank <= label.maxRank) { + const v = node.v; + graph.setNode(v); + const parent = g.parent(v); + graph.setParent(v, parent || root); + // This assumes we have only short edges! + if (relationship) { + for (const e of node.in) { + graph.setEdge(e.v, v, { weight: e.label.weight }); + } + } + else { + for (const e of node.out) { + graph.setEdge(e.w, v, { weight: e.label.weight }); + } + } + if ('minRank' in label) { + graph.setNode(v, { + borderLeft: label.borderLeft[rank], + borderRight: label.borderRight[rank] + }); + } + } + } + return graph; + }; + let layering = initOrder(g); + const assignOrder = (g, layering) => { + for (const layer of layering) { + for (let i = 0; i < layer.length; i++) { + g.node(layer[i]).label.order = i; + } + } + }; + assignOrder(g, layering); + const rank = maxRank(g) || 0; + const downLayerGraphs = new Array(rank); + const upLayerGraphs = new Array(rank); + const nodes = Array.from(g.nodes.values()); + for (let i = 0; i < rank; i++) { + downLayerGraphs[i] = buildLayerGraph(g, nodes, i + 1, true); + upLayerGraphs[i] = buildLayerGraph(g, nodes, rank - i - 1, false); + } + let bestCC = Number.POSITIVE_INFINITY; + let best; + for (let i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) { + sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2); + layering = buildLayerMatrix(g); + const cc = crossCount(g, layering); + if (cc < bestCC) { + lastBest = 0; + const length = layering.length; + best = new Array(length); + for (let j = 0; j < length; j++) { + best[j] = layering[j].slice(); + } + bestCC = cc; + } + } + assignOrder(g, best); + }; + + const insertSelfEdges = (g) => { + const layers = buildLayerMatrix(g); + for (const layer of layers) { + let orderShift = 0; + layer.forEach(function(v, i) { + const label = g.node(v).label; + label.order = i + orderShift; + if (label.selfEdges) { + for (const selfEdge of label.selfEdges) { + addDummyNode(g, 'selfedge', { + width: selfEdge.label.width, + height: selfEdge.label.height, + rank: label.rank, + order: i + (++orderShift), + e: selfEdge.e, + label: selfEdge.label + }, '_se'); + } + delete label.selfEdges; + } + }); + } + }; + + const coordinateSystem_swapWidthHeight = (g) => { + for (const node of g.nodes.values()) { + const label = node.label; + const w = label.width; + label.width = label.height; + label.height = w; + } + for (const e of g.edges.values()) { + const label = e.label; + const w = label.width; + label.width = label.height; + label.height = w; + } + }; + const coordinateSystem_adjust = (g) => { + const rankDir = g.options.rankdir.toLowerCase(); + if (rankDir === 'lr' || rankDir === 'rl') { + coordinateSystem_swapWidthHeight(g); + } + }; + const coordinateSystem_undo = (g) => { + const rankDir = g.options.rankdir.toLowerCase(); + if (rankDir === 'bt' || rankDir === 'rl') { + for (const node of g.nodes.values()) { + node.label.y = -node.label.y; + } + for (const e of g.edges.values()) { + const edge = e.label; + for (const attr of edge.points) { + attr.y = -attr.y; + } + if ('y' in edge) { + edge.y = -edge.y; + } + } + } + if (rankDir === 'lr' || rankDir === 'rl') { + const swapXYOne = (attrs) => { + const x = attrs.x; + attrs.x = attrs.y; + attrs.y = x; + }; + for (const node of g.nodes.values()) { + swapXYOne(node.label); + } + for (const e of g.edges.values()) { + const edge = e.label; + for (const e of edge.points) { + swapXYOne(e); + } + if (edge.x !== undefined) { + swapXYOne(edge); + } + } + coordinateSystem_swapWidthHeight(g); + } + }; + + const position = (g) => { + const addConflict = (conflicts, v, w) => { + if (v > w) { + const tmp = v; + v = w; + w = tmp; + } + let conflictsV = conflicts[v]; + if (!conflictsV) { + conflicts[v] = conflictsV = {}; + } + conflictsV[w] = true; + }; + const hasConflict = (conflicts, v, w) => { + if (v > w) { + const tmp = v; + v = w; + w = tmp; + } + return conflicts[v] && w in conflicts[v]; + }; + const buildBlockGraph = (g, layering, root, reverseSep) => { + const nodeSep = g.options.nodesep; + const edgeSep = g.options.edgesep; + const blockGraph = new dagre.Graph(); + for (const layer of layering) { + let u; + for (const v of layer) { + const vRoot = root[v]; + blockGraph.setNode(vRoot, {}); + if (u) { + const uRoot = root[u]; + const vLabel = g.node(v).label; + const wLabel = g.node(u).label; + let sum = 0; + let delta; + sum += vLabel.width / 2; + if ('labelpos' in vLabel) { + switch (vLabel.labelpos) { + case 'l': delta = -vLabel.width / 2; break; + case 'r': delta = vLabel.width / 2; break; + } + } + if (delta) { + sum += reverseSep ? delta : -delta; + } + delta = 0; + sum += (vLabel.dummy ? edgeSep : nodeSep) / 2; + sum += (wLabel.dummy ? edgeSep : nodeSep) / 2; + sum += wLabel.width / 2; + if ('labelpos' in wLabel) { + switch (wLabel.labelpos) { + case 'l': delta = wLabel.width / 2; break; + case 'r': delta = -wLabel.width / 2; break; + } + } + if (delta) { + sum += reverseSep ? delta : -delta; + } + const edge = blockGraph.edge(uRoot, vRoot); + const max = Math.max(sum, edge ? edge.label : 0); + if (edge) { + edge.label = max; + } + else { + blockGraph.setEdge(uRoot, vRoot, max); + } + } + u = v; + } + } + return blockGraph; + }; + // Try to align nodes into vertical 'blocks' where possible. + // This algorithm attempts to align a node with one of its median neighbors. + // If the edge connecting a neighbor is a type-1 conflict then we ignore that possibility. + // If a previous node has already formed a block with a node after the node we're trying to form a block with, + // we also ignore that possibility - our blocks would be split in that scenario. + const verticalAlignment = (layering, conflicts, neighborFn) => { + const root = {}; + const align = {}; + const pos = {}; + // We cache the position here based on the layering because the graph and layering may be out of sync. + // The layering matrix is manipulated to generate different extreme alignments. + for (const layer of layering) { + let order = 0; + for (const v of layer) { + root[v] = v; + align[v] = v; + pos[v] = order; + order++; + } + } + for (const layer of layering) { + let prevIdx = -1; + for (const v of layer) { + let ws = neighborFn(v); + if (ws.length > 0) { + ws = ws.sort((a, b) => pos[a] - pos[b]); + const mp = (ws.length - 1) / 2.0; + const il = Math.ceil(mp); + for (let i = Math.floor(mp); i <= il; i++) { + const w = ws[i]; + if (align[v] === v && prevIdx < pos[w] && !hasConflict(conflicts, v, w)) { + align[w] = v; + align[v] = root[v] = root[w]; + prevIdx = pos[w]; + } + } + } + } + } + return { root: root, align: align }; + }; + const horizontalCompaction = (g, layering, root, align, reverseSep) => { + // This portion of the algorithm differs from BK due to a number of problems. + // Instead of their algorithm we construct a new block graph and do two sweeps. + const xs = {}; + const blockG = buildBlockGraph(g, layering, root, reverseSep); + const borderType = reverseSep ? 'borderLeft' : 'borderRight'; + const iterate = (setXsFunc, nextNodesFunc) => { + let stack = Array.from(blockG.nodes.keys()); + const visited = new Set(); + while (stack.length > 0) { + const v = stack.pop(); + if (visited.has(v)) { + setXsFunc(v); + } + else { + visited.add(v); + stack.push(v); + stack = stack.concat(nextNodesFunc(v)); + } + } + }; + // First pass, places blocks with the smallest possible coordinates. + const pass1 = (v) => { + let max = 0; + for (const e of blockG.node(v).in) { + max = Math.max(max, xs[e.v] + e.label); + } + xs[v] = max; + }; + // Second pass, removes unused space by moving blocks to the greatest coordinates without violating separation. + const pass2 = (v) => { + let min = Number.POSITIVE_INFINITY; + for (const e of blockG.node(v).out) { + min = Math.min(min, xs[e.w] - e.label); + } + const label = g.node(v).label; + if (min !== Number.POSITIVE_INFINITY && label.borderType !== borderType) { + xs[v] = Math.max(xs[v], min); + } + }; + iterate(pass1, blockG.predecessors.bind(blockG)); + iterate(pass2, blockG.successors.bind(blockG)); + // Assign x coordinates to all nodes + for (const v of Object.values(align)) { + xs[v] = xs[root[v]]; + } + return xs; + }; + // Marks all edges in the graph with a type-1 conflict with the 'type1Conflict' property. + // A type-1 conflict is one where a non-inner segment crosses an inner segment. + // An inner segment is an edge with both incident nodes marked with the 'dummy' property. + // + // This algorithm scans layer by layer, starting with the second, for type-1 + // conflicts between the current layer and the previous layer. For each layer + // it scans the nodes from left to right until it reaches one that is incident + // on an inner segment. It then scans predecessors to determine if they have + // edges that cross that inner segment. At the end a final scan is done for all + // nodes on the current rank to see if they cross the last visited inner segment. + // + // This algorithm (safely) assumes that a dummy node will only be incident on a + // single node in the layers being scanned. + const findType1Conflicts = (g, layering) => { + const conflicts = {}; + if (layering.length > 0) { + let prev = layering[0]; + for (let k = 1; k < layering.length; k++) { + const layer = layering[k]; + // last visited node in the previous layer that is incident on an inner segment. + let k0 = 0; + // Tracks the last node in this layer scanned for crossings with a type-1 segment. + let scanPos = 0; + const prevLayerLength = prev.length; + const lastNode = layer[layer.length - 1]; + for (let i = 0; i < layer.length; i++) { + const v = layer[i]; + const w = g.node(v).label.dummy ? g.predecessors(v).find((u) => g.node(u).label.dummy) : null; + if (w || v === lastNode) { + const k1 = w ? g.node(w).label.order : prevLayerLength; + for (const scanNode of layer.slice(scanPos, i + 1)) { + // for (const scanNode of layer.slice(scanPos, scanPos + 1)) { + for (const u of g.predecessors(scanNode)) { + const uLabel = g.node(u).label; + const uPos = uLabel.order; + if ((uPos < k0 || k1 < uPos) && !(uLabel.dummy && g.node(scanNode).label.dummy)) { + addConflict(conflicts, u, scanNode); + } + } + } + // scanPos += 1; + scanPos = i + 1; + k0 = k1; + } + } + prev = layer; + } + } + return conflicts; + }; + const findType2Conflicts = (g, layering) => { + const conflicts = {}; + const scan = (south, southPos, southEnd, prevNorthBorder, nextNorthBorder) => { + let v; + for (let i = southPos; i < southEnd; i++) { + v = south[i]; + if (g.node(v).labeldummy) { + for (const u of g.predecessors(v)) { + const uNode = g.node(u).label; + if (uNode.dummy && (uNode.order < prevNorthBorder || uNode.order > nextNorthBorder)) { + addConflict(conflicts, u, v); + } + } + } + } + }; + if (layering.length > 0) { + let north = layering[0]; + for (let i = 1; i < layering.length; i++) { + const south = layering[i]; + let prevNorthPos = -1; + let nextNorthPos; + let southPos = 0; + south.forEach(function(v, southLookahead) { + if (g.node(v).label.dummy === 'border') { + const predecessors = g.predecessors(v); + if (predecessors.length) { + nextNorthPos = g.node(predecessors[0]).label.order; + scan(south, southPos, southLookahead, prevNorthPos, nextNorthPos); + southPos = southLookahead; + prevNorthPos = nextNorthPos; + } + } + scan(south, southPos, south.length, nextNorthPos, north.length); + }); + north = south; + } + } + return conflicts; + }; + + g = asNonCompoundGraph(g); + const layering = buildLayerMatrix(g); + const ranksep = g.options.ranksep; + // Assign y-coordinate based on rank + let y = 0; + for (const layer of layering) { + const maxHeight = layer.reduce((a, v) => Math.max(a, g.node(v).label.height), 0); + for (const v of layer) { + g.node(v).label.y = y + maxHeight / 2; + } + y += maxHeight + ranksep; + } + // Coordinate assignment based on Brandes and Köpf, 'Fast and Simple Horizontal Coordinate Assignment.' + const conflicts = Object.assign(findType1Conflicts(g, layering), findType2Conflicts(g, layering)); + const xss = {}; + for (const vertical of ['u', 'd']) { + let adjustedLayering = vertical === 'u' ? layering : Object.values(layering).reverse(); + for (const horizontal of ['l', 'r']) { + if (horizontal === 'r') { + adjustedLayering = adjustedLayering.map((layer) => Object.values(layer).reverse()); + } + const neighborFn = (vertical === 'u' ? g.predecessors : g.successors).bind(g); + const align = verticalAlignment(adjustedLayering, conflicts, neighborFn); + const xs = horizontalCompaction(g, adjustedLayering, align.root, align.align, horizontal === 'r'); + if (horizontal === 'r') { + for (const entry of Object.entries(xs)) { + xs[entry[0]] = -entry[1]; + } + } + xss[vertical + horizontal] = xs; + } + } + // Find smallest width alignment: Returns the alignment that has the smallest width of the given alignments. + let minWidth = Number.POSITIVE_INFINITY; + let minValue = undefined; + for (const xs of Object.values(xss)) { + let max = Number.NEGATIVE_INFINITY; + let min = Number.POSITIVE_INFINITY; + for (const entry of Object.entries(xs)) { + const v = entry[0]; + const x = entry[1]; + const halfWidth = g.node(v).label.width / 2; + max = Math.max(x + halfWidth, max); + min = Math.min(x - halfWidth, min); + } + const width = max - min; + if (width < minWidth) { + minWidth = width; + minValue = xs; + } + } + // Align the coordinates of each of the layout alignments such that + // left-biased alignments have their minimum coordinate at the same point as + // the minimum coordinate of the smallest width alignment and right-biased + // alignments have their maximum coordinate at the same point as the maximum + // coordinate of the smallest width alignment. + const alignTo = minValue; + const range = (values) => { + let min = Number.POSITIVE_INFINITY; + let max = Number.NEGATIVE_INFINITY; + for (const value of values) { + if (value < min) { + min = value; + } + if (value > max) { + max = value; + } + } + return [ min, max ]; + }; + const alignToRange = range(Object.values(alignTo)); + for (const vertical of ['u', 'd']) { + for (const horizontal of ['l', 'r']) { + const alignment = vertical + horizontal; + const xs = xss[alignment]; + let delta; + if (xs !== alignTo) { + const vsValsRange = range(Object.values(xs)); + delta = horizontal === 'l' ? alignToRange[0] - vsValsRange[0] : alignToRange[1] - vsValsRange[1]; + if (delta) { + const list = {}; + for (const key of Object.keys(xs)) { + list[key] = xs[key] + delta; + } + xss[alignment] = list; + } + } + } + } + // balance + const align = g.options.align; + if (align) { + const xs = xss[align.toLowerCase()]; + for (const v of Object.keys(xss.ul)) { + g.node(v).label.x = xs[v]; + } + } + else { + for (const v of Object.keys(xss.ul)) { + const xs = [ xss.ul[v], xss.ur[v], xss.dl[v], xss.dr[v] ].sort((a, b) => a - b); + g.node(v).label.x = (xs[1] + xs[2]) / 2; + } + } + }; + + const positionSelfEdges = (g) => { + for (const node of g.nodes.values()) { + const label = node.label; + if (label.dummy === 'selfedge') { + const v = node.v; + const selfNode = g.node(label.e.v).label; + const x = selfNode.x + selfNode.width / 2; + const y = selfNode.y; + const dx = label.x - x; + const dy = selfNode.height / 2; + g.setEdge(label.e.v, label.e.w, label.label); + g.removeNode(v); + label.label.points = [ + { x: x + 2 * dx / 3, y: y - dy }, + { x: x + 5 * dx / 6, y: y - dy }, + { x: x + dx , y: y }, + { x: x + 5 * dx / 6, y: y + dy }, + { x: x + 2 * dx / 3, y: y + dy } + ]; + label.label.x = label.x; + label.label.y = label.y; + } + } + }; + + const removeBorderNodes = (g) => { + for (const node of g.nodes.values()) { + const v = node.v; + if (g.children(v).length) { + const label = node.label; + const t = g.node(label.borderTop).label; + const b = g.node(label.borderBottom).label; + const l = g.node(label.borderLeft[label.borderLeft.length - 1]).label; + const r = g.node(label.borderRight[label.borderRight.length - 1]).label; + label.width = Math.abs(r.x - l.x); + label.height = Math.abs(b.y - t.y); + label.x = l.x + label.width / 2; + label.y = t.y + label.height / 2; + } + } + for (const node of g.nodes.values()) { + if (node.label.dummy === 'border') { + g.removeNode(node.v); + } + } + }; + + const fixupEdgeLabelCoords = (g) => { + for (const e of g.edges.values()) { + const edge = e.label; + if ('x' in edge) { + if (edge.labelpos === 'l' || edge.labelpos === 'r') { + edge.width -= edge.labeloffset; + } + switch (edge.labelpos) { + case 'l': edge.x -= edge.width / 2 + edge.labeloffset; break; + case 'r': edge.x += edge.width / 2 + edge.labeloffset; break; + } + } + } + }; + + const translateGraph = (g) => { + let minX = Number.POSITIVE_INFINITY; + let maxX = 0; + let minY = Number.POSITIVE_INFINITY; + let maxY = 0; + const getExtremes = (attrs) => { + const x = attrs.x; + const y = attrs.y; + const w = attrs.width; + const h = attrs.height; + minX = Math.min(minX, x - w / 2); + maxX = Math.max(maxX, x + w / 2); + minY = Math.min(minY, y - h / 2); + maxY = Math.max(maxY, y + h / 2); + }; + for (const node of g.nodes.values()) { + getExtremes(node.label); + } + for (const e of g.edges.values()) { + const edge = e.label; + if ('x' in edge) { + getExtremes(edge); + } + } + for (const node of g.nodes.values()) { + node.label.x -= minX; + node.label.y -= minY; + } + for (const e of g.edges.values()) { + const edge = e.label; + for (const p of edge.points) { + p.x -= minX; + p.y -= minY; + } + if ('x' in edge) { + edge.x -= minX; + } + if ('y' in edge) { + edge.y -= minY; + } + } + const graphLabel = g.options; + graphLabel.width = maxX - minX; + graphLabel.height = maxY - minY; + }; + + const assignNodeIntersects = (g) => { + // Finds where a line starting at point ({x, y}) would intersect a rectangle + // ({x, y, width, height}) if it were pointing at the rectangle's center. + const intersectRect = (rect, point) => { + const x = rect.x; + const y = rect.y; + // Rectangle intersection algorithm from: http://math.stackexchange.com/questions/108113/find-edge-between-two-boxes + const dx = point.x - x; + const dy = point.y - y; + let w = rect.width / 2; + let h = rect.height / 2; + if (!dx && !dy) { + throw new Error('Not possible to find intersection inside of the rectangle'); + } + let sx; + let sy; + if (Math.abs(dy) * w > Math.abs(dx) * h) { + // Intersection is top or bottom of rect. + if (dy < 0) { + h = -h; + } + sx = h * dx / dy; + sy = h; + } + else { + // Intersection is left or right of rect. + if (dx < 0) { + w = -w; + } + sx = w; + sy = w * dy / dx; + } + return { x: x + sx, y: y + sy }; + }; + for (const e of g.edges.values()) { + const edge = e.label; + const vNode = e.vNode.label; + const wNode = e.wNode.label; + let p1; + let p2; + if (!edge.points) { + edge.points = []; + p1 = wNode; + p2 = vNode; + } + else { + p1 = edge.points[0]; + p2 = edge.points[edge.points.length - 1]; + } + edge.points.unshift(intersectRect(vNode, p1)); + edge.points.push(intersectRect(wNode, p2)); + } + }; + + time(' makeSpaceForEdgeLabels', () => { makeSpaceForEdgeLabels(g); }); + time(' removeSelfEdges', () => { removeSelfEdges(g); }); + time(' acyclic_run', () => { acyclic_run(g); }); + time(' nestingGraph_run', () => { nestingGraph_run(g); }); + time(' rank', () => { rank(asNonCompoundGraph(g)); }); + time(' injectEdgeLabelProxies', () => { injectEdgeLabelProxies(g); }); + time(' removeEmptyRanks', () => { removeEmptyRanks(g); }); + time(' nestingGraph_cleanup', () => { nestingGraph_cleanup(g); }); + time(' assignRankMinMax', () => { assignRankMinMax(g); }); + time(' removeEdgeLabelProxies', () => { removeEdgeLabelProxies(g); }); + time(' normalize', () => { normalize(g); }); + time(' parentDummyChains', () => { parentDummyChains(g); }); + time(' addBorderSegments', () => { addBorderSegments(g); }); + time(' order', () => { order(g); }); + time(' insertSelfEdges', () => { insertSelfEdges(g); }); + time(' coordinateSystem_adjust', () => { coordinateSystem_adjust(g); }); + time(' position', () => { position(g); }); + time(' positionSelfEdges', () => { positionSelfEdges(g); }); + time(' removeBorderNodes', () => { removeBorderNodes(g); }); + time(' denormalize', () => { denormalize(g); }); + time(' fixupEdgeLabelCoords', () => { fixupEdgeLabelCoords(g); }); + time(' coordinateSystem_undo', () => { coordinateSystem_undo(g); }); + time(' translateGraph', () => { translateGraph(g); }); + time(' assignNodeIntersects', () => { assignNodeIntersects(g); }); + time(' acyclic_undo', () => { acyclic_undo(g); }); + }; + + // Copies final layout information from the layout graph back to the input graph. + // This process only copies whitelisted attributes from the layout graph to the input graph, + // so it serves as a good place to determine what attributes can influence layout. + const updateSourceGraph = (graph, g) => { + for (const node of graph.nodes.values()) { + const label = node.label; + if (label) { + const v = node.v; + const layoutLabel = g.node(v).label; + label.x = layoutLabel.x; + label.y = layoutLabel.y; + if (g.children(v).length) { + label.width = layoutLabel.width; + label.height = layoutLabel.height; + } + } + } + for (const e of graph.edges.values()) { + const label = g.edge(e.v, e.w).label; + e.label.points = label.points; + if ('x' in label) { + e.label.x = label.x; + e.label.y = label.y; + } + } + graph.options.width = g.options.width; + graph.options.height = g.options.height; + }; + + time('layout', () => { + const layoutGraph = + time(' buildLayoutGraph', () => { return buildLayoutGraph(graph); }); + time(' runLayout', () => { runLayout(layoutGraph, time); }); + time(' updateSourceGraph', () => { updateSourceGraph(graph, layoutGraph); }); + }); +}; + +dagre.Graph = class { + + constructor(options) { + options = options || {}; + this._directed = 'directed' in options ? options.directed : true; + this._compound = 'compound' in options ? options.compound : false; + this._label = undefined; + this._defaultNodeLabelFn = () => { + return undefined; + }; + this.nodes = new Map(); + this.edges = new Map(); + if (this._compound) { + this._parent = {}; + this._children = {}; + this._children['\x00'] = {}; + } + } + + set options(value) { + this._label = value; + } + + get options() { + return this._label; + } + + isDirected() { + return this._directed; + } + + isCompound() { + return this._compound; + } + + setDefaultNodeLabel(newDefault) { + this._defaultNodeLabelFn = newDefault; + } + + setNode(v, label) { + const node = this.nodes.get(v); + if (node) { + if (label) { + node.label = label; + } + } + else { + const node = { label: label ? label : this._defaultNodeLabelFn(v), in: [], out: [], predecessors: {}, successors: {}, v: v }; + this.nodes.set(v, node); + if (this._compound) { + this._parent[v] = '\x00'; + this._children[v] = {}; + this._children['\x00'][v] = true; + } + } + } + + node(v) { + return this.nodes.get(v); + } + + hasNode(v) { + return this.nodes.has(v); + } + + removeNode(v) { + const node = this.nodes.get(v); + if (node) { + if (this._compound) { + delete this._children[this._parent[v]][v]; + delete this._parent[v]; + for (const child of this.children(v)) { + this.setParent(child); + } + delete this._children[v]; + } + for (const edge of node.in) { + this.removeEdge(edge); + } + for (const edge of node.out) { + this.removeEdge(edge); + } + this.nodes.delete(v); + } + } + + setParent(v, parent) { + if (!this._compound) { + throw new Error('Cannot set parent in a non-compound graph'); + } + if (parent) { + for (let ancestor = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) { + if (ancestor === v) { + throw new Error('Setting ' + parent + ' as parent of ' + v + ' would create a cycle.'); + } + } + this.setNode(parent); + } + else { + parent = '\x00'; + } + delete this._children[this._parent[v]][v]; + this._parent[v] = parent; + this._children[parent][v] = true; + } + + parent(v) { + if (this._compound) { + const parent = this._parent[v]; + if (parent !== '\x00') { + return parent; + } + } + } + + children(v) { + if (this._compound) { + return Object.keys(this._children[v === undefined ? '\x00' : v]); + } + else if (v === undefined) { + return this.nodes.keys(); + } + else if (this.hasNode(v)) { + return []; + } + } + + predecessors(v) { + return Object.keys(this.nodes.get(v).predecessors); + } + + successors(v) { + return Object.keys(this.nodes.get(v).successors); + } + + neighbors(v) { + return Array.from(new Set(this.predecessors(v).concat(this.successors(v)))); + } + + edge(v, w) { + return this.edges.get(this._edgeKey(this._directed, v, w)); + } + + setEdge(v, w, label, name) { + const key = this._edgeKey(this._directed, v, w, name); + const edge = this.edges.get(key); + if (edge) { + edge.label = label; + } + else { + if (!this._directed && v > w) { + const tmp = v; + v = w; + w = tmp; + } + const edge = { label: label, v: v, w: w, name: name, key: key, vNode: null, wNode: null }; + this.edges.set(key, edge); + this.setNode(v); + this.setNode(w); + const wNode = this.nodes.get(w); + const vNode = this.nodes.get(v); + edge.wNode = wNode; + edge.vNode = vNode; + const incrementOrInitEntry = (map, k) => { + if (map[k]) { + map[k]++; + } + else { + map[k] = 1; + } + }; + incrementOrInitEntry(wNode.predecessors, v); + incrementOrInitEntry(vNode.successors, w); + wNode.in.push(edge); + vNode.out.push(edge); + } + } + + removeEdge(edge) { + const key = edge.key; + const v = edge.v; + const w = edge.w; + const decrementOrRemoveEntry = (map, k) => { + if (!--map[k]) { + delete map[k]; + } + }; + const wNode = edge.wNode; + const vNode = edge.vNode; + decrementOrRemoveEntry(wNode.predecessors, v); + decrementOrRemoveEntry(vNode.successors, w); + wNode.in = wNode.in.filter((edge) => edge.key !== key); + vNode.out = vNode.out.filter((edge) => edge.key !== key); + this.edges.delete(key); + } + + _edgeKey(isDirected, v, w, name) { + if (!isDirected && v > w) { + return name ? w + ':' + v + ':' + name : w + ':' + v + ':'; + } + return name ? v + ':' + w + ':' + name : v + ':' + w + ':'; + } + + toString() { + return [ + '[nodes]', Array.from(this.nodes.values()).map(n => JSON.stringify(n.label)).join('\n'), + '[edges]', Array.from(this.edges.values()).map(e => JSON.stringify(e.label)).join('\n'), + '[parents]', JSON.stringify(this._parent, null, 2), + '[children]', JSON.stringify(this._children, null, 2) + ].join('\n'); + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports = dagre; +} diff --git a/electron.html b/electron.html new file mode 100644 index 0000000..ecc5765 --- /dev/null +++ b/electron.html @@ -0,0 +1,315 @@ + + + + + + +Netron + + + + + +
+ +
+ +
+ + + + + +
+ + + \ No newline at end of file diff --git a/electron.js b/electron.js new file mode 100644 index 0000000..8daf5d5 --- /dev/null +++ b/electron.js @@ -0,0 +1,730 @@ + +var host = host || {}; + +const electron = require('electron'); +const fs = require('fs'); +const http = require('http'); +const https = require('https'); +const process = require('process'); +const path = require('path'); +const querystring = require('querystring'); + +host.ElectronHost = class { + + constructor() { + process.on('uncaughtException', (err) => { + this.exception(err, true); + }); + this._document = window.document; + this._window = window; + this._window.eval = global.eval = () => { + throw new Error('window.eval() not supported.'); + }; + this._window.addEventListener('unload', () => { + if (typeof __coverage__ !== 'undefined') { + const file = path.join('.nyc_output', path.basename(window.location.pathname, '.html')) + '.json'; + /* eslint-disable no-undef */ + fs.writeFileSync(file, JSON.stringify(__coverage__)); + /* eslint-enable no-undef */ + } + }); + this._environment = electron.ipcRenderer.sendSync('get-environment', {}); + this._queue = []; + } + + get window() { + return this._window; + } + + get document() { + return this._document; + } + + get version() { + return this._environment.version; + } + + get type() { + return 'Electron'; + } + + get agent() { + return 'any'; + } + + initialize(view) { + this._view = view; + electron.ipcRenderer.on('open', (_, data) => { + this._openPath(data.path); + }); + return new Promise((resolve /*, reject */) => { + const accept = () => { + if (this._environment.package) { + this._telemetry = new host.Telemetry('UA-54146-13', this._getConfiguration('userId'), navigator.userAgent, this.type, this.version); + } + resolve(); + }; + const request = () => { + this._view.show('welcome consent'); + const acceptButton = this.document.getElementById('consent-accept-button'); + if (acceptButton) { + acceptButton.addEventListener('click', () => { + this._setConfiguration('consent', Date.now()); + accept(); + }); + } + }; + const time = this._getConfiguration('consent'); + if (time && (Date.now() - time) < 30 * 24 * 60 * 60 * 1000) { + accept(); + } + else { + this._request('https://ipinfo.io/json', { 'Content-Type': 'application/json' }, 2000).then((text) => { + try { + const json = JSON.parse(text); + const countries = ['AT', 'BE', 'BG', 'HR', 'CZ', 'CY', 'DK', 'EE', 'FI', 'FR', 'DE', 'EL', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'NO', 'PL', 'PT', 'SK', 'ES', 'SE', 'GB', 'UK', 'GR', 'EU', 'RO']; + if (json && json.country && !countries.indexOf(json.country) !== -1) { + this._setConfiguration('consent', Date.now()); + accept(); + } + else { + request(); + } + } + catch (err) { + request(); + } + }).catch(() => { + request(); + }); + } + }); + } + + start() { + if (this._queue) { + const queue = this._queue; + delete this._queue; + if (queue.length > 0) { + const path = queue.pop(); + this._openPath(path); + } + } + + electron.ipcRenderer.on('export', (_, data) => { + this._view.export(data.file); + }); + electron.ipcRenderer.on('cut', () => { + this._view.cut(); + }); + electron.ipcRenderer.on('copy', () => { + this._view.copy(); + }); + electron.ipcRenderer.on('paste', () => { + this._view.paste(); + }); + electron.ipcRenderer.on('selectall', () => { + this._view.selectAll(); + }); + electron.ipcRenderer.on('toggle', (sender, name) => { + this._view.toggle(name); + this._update(Object.assign({}, this._view.options)); + }); + electron.ipcRenderer.on('zoom-in', () => { + this.document.getElementById('zoom-in-button').click(); + }); + electron.ipcRenderer.on('zoom-out', () => { + this.document.getElementById('zoom-out-button').click(); + }); + electron.ipcRenderer.on('reset-zoom', () => { + this._view.resetZoom(); + }); + electron.ipcRenderer.on('show-properties', () => { + this.document.getElementById('menu-button').click(); + }); + electron.ipcRenderer.on('find', () => { + this._view.find(); + }); + this.document.getElementById('menu-button').addEventListener('click', () => { + this._view.showModelProperties(); + }); + + const openFileButton = this.document.getElementById('open-file-button'); + if (openFileButton) { + openFileButton.style.opacity = 1; + openFileButton.addEventListener('click', () => { + electron.ipcRenderer.send('open-file-dialog', {}); + }); + } + const githubButton = this.document.getElementById('github-button'); + const githubLink = this.document.getElementById('logo-github'); + if (githubButton && githubLink) { + githubButton.style.opacity = 1; + githubButton.addEventListener('click', () => { + this.openURL(githubLink.href); + }); + } + + this.document.addEventListener('dragover', (e) => { + e.preventDefault(); + }); + this.document.addEventListener('drop', (e) => { + e.preventDefault(); + }); + this.document.body.addEventListener('drop', (e) => { + e.preventDefault(); + const paths = Array.from(e.dataTransfer.files).map(((file) => file.path)); + if (paths.length > 0) { + electron.ipcRenderer.send('drop-paths', { paths: paths }); + } + return false; + }); + + this._view.show('welcome'); + } + + environment(name) { + return this._environment[name]; + } + + error(message, detail) { + electron.ipcRenderer.sendSync('show-message-box', { + type: 'error', + message: message, + detail: detail, + }); + } + + confirm(message, detail) { + const result = electron.ipcRenderer.sendSync('show-message-box', { + type: 'question', + message: message, + detail: detail, + buttons: ['Yes', 'No'], + defaultId: 0, + cancelId: 1 + }); + return result === 0; + } + + require(id) { + try { + return Promise.resolve(require(id)); + } + catch (error) { + return Promise.reject(error); + } + } + + save(name, extension, defaultPath, callback) { + const selectedFile = electron.ipcRenderer.sendSync('show-save-dialog', { + title: 'Export Tensor', + defaultPath: defaultPath, + buttonLabel: 'Export', + filters: [ { name: name, extensions: [ extension ] } ] + }); + if (selectedFile) { + callback(selectedFile); + } + } + + export(file, blob) { + const reader = new FileReader(); + reader.onload = (e) => { + const data = new Uint8Array(e.target.result); + fs.writeFile(file, data, null, (err) => { + if (err) { + this.exception(err, false); + this.error('Error writing file.', err.message); + } + }); + }; + + let err = null; + if (!blob) { + err = new Error("Export blob is '" + JSON.stringify(blob) + "'."); + } + else if (!(blob instanceof Blob)) { + err = new Error("Export blob type is '" + (typeof blob) + "'."); + } + + if (err) { + this.exception(err, false); + this.error('Error exporting image.', err.message); + } + else { + reader.readAsArrayBuffer(blob); + } + } + + request(file, encoding, base) { + return new Promise((resolve, reject) => { + const pathname = path.join(base || __dirname, file); + fs.stat(pathname, (err, stat) => { + if (err && err.code === 'ENOENT') { + reject(new Error("The file '" + file + "' does not exist.")); + } + else if (err) { + reject(err); + } + else if (!stat.isFile()) { + reject(new Error("The path '" + file + "' is not a file.")); + } + else if (stat && stat.size < 0x7ffff000) { + fs.readFile(pathname, encoding, (err, data) => { + if (err) { + reject(err); + } + else { + resolve(encoding ? data : new host.ElectronHost.BinaryStream(data)); + } + }); + } + else if (encoding) { + reject(new Error("The file '" + file + "' size (" + stat.size.toString() + ") for encoding '" + encoding + "' is greater than 2 GB.")); + } + else { + resolve(new host.ElectronHost.FileStream(pathname, 0, stat.size, stat.mtimeMs)); + } + }); + }); + } + + openURL(url) { + electron.shell.openExternal(url); + } + + exception(error, fatal) { + if (this._telemetry && error && error.telemetry !== false) { + try { + const name = error && error.name ? error.name + ': ' : ''; + const message = error && error.message ? error.message : '(null)'; + const description = [ name + message ]; + if (error.stack) { + const format = (file, line, column) => { + return file.split('\\').join('/').split('/').pop() + ':' + line + ':' + column; + }; + const match = error.stack.match(/\n {4}at (.*) \((.*):(\d*):(\d*)\)/); + if (match) { + description.push(match[1] + ' (' + format(match[2], match[3], match[4]) + ')'); + } + else { + const match = error.stack.match(/\n {4}at (.*):(\d*):(\d*)/); + if (match) { + description.push('(' + format(match[1], match[2], match[3]) + ')'); + } + } + } + this._telemetry.exception(description.join(' @ '), fatal); + } + catch (e) { + // continue regardless of error + } + } + } + + screen(name) { + if (this._telemetry) { + try { + this._telemetry.screenview(name); + } + catch (e) { + // continue regardless of error + } + } + } + + event(category, action, label, value) { + if (this._telemetry) { + try { + this._telemetry.event(category, action, label, value); + } + catch (e) { + // continue regardless of error + } + } + } + + _context(location) { + const basename = path.basename(location); + const stat = fs.statSync(location); + if (stat.isFile()) { + const dirname = path.dirname(location); + return this.request(basename, null, dirname).then((stream) => { + return new host.ElectronHost.ElectronContext(this, dirname, basename, stream); + }); + } + else if (stat.isDirectory()) { + const entries = new Map(); + const walk = (dir) => { + for (const item of fs.readdirSync(dir)) { + const pathname = path.join(dir, item); + const stat = fs.statSync(pathname); + if (stat.isDirectory()) { + walk(pathname); + } + else if (stat.isFile()) { + const stream = new host.ElectronHost.FileStream(pathname, 0, stat.size, stat.mtimeMs); + const name = pathname.split(path.sep).join(path.posix.sep); + entries.set(name, stream); + } + } + }; + walk(location); + return Promise.resolve(new host.ElectronHost.ElectronContext(this, location, basename, null, entries)); + } + throw new Error("Unsupported path stat '" + JSON.stringify(stat) + "'."); + } + + _openPath(path) { + if (this._queue) { + this._queue.push(path); + return; + } + if (path && this._view.accept(path)) { + this._view.show('welcome spinner'); + this._context(path).then((context) => { + this._view.open(context).then((model) => { + this._view.show(null); + const options = Object.assign({}, this._view.options); + if (model) { + options.path = path; + } + this._update(options); + }).catch((error) => { + const options = Object.assign({}, this._view.options); + if (error) { + this._view.error(error, null, null); + options.path = null; + } + this._update(options); + }); + }).catch((error) => { + this._view.error(error, 'Error while reading file.', null); + this._update({ path: null }); + }); + } + } + + _request(location, headers, timeout) { + return new Promise((resolve, reject) => { + const url = new URL(location); + const protocol = url.protocol === 'https:' ? https : http; + const options = {}; + options.headers = headers; + if (timeout) { + options.timeout = timeout; + } + const request = protocol.request(location, options, (response) => { + if (response.statusCode !== 200) { + const err = new Error("The web request failed with status code " + response.statusCode + " at '" + location + "'."); + err.type = 'error'; + err.url = location; + err.status = response.statusCode; + reject(err); + } + else { + let data = ''; + response.on('data', (chunk) => { + data += chunk; + }); + response.on('err', (err) => { + reject(err); + }); + response.on('end', () => { + resolve(data); + }); + } + }); + request.on("error", (err) => { + reject(err); + }); + request.on("timeout", () => { + request.destroy(); + const error = new Error("The web request timed out at '" + location + "'."); + error.type = 'timeout'; + error.url = url; + reject(error); + }); + request.end(); + }); + } + + _getConfiguration(name) { + return electron.ipcRenderer.sendSync('get-configuration', { name: name }); + } + + _setConfiguration(name, value) { + electron.ipcRenderer.sendSync('set-configuration', { name: name, value: value }); + } + + _update(data) { + electron.ipcRenderer.send('update', data); + } +}; + +host.Telemetry = class { + + constructor(trackingId, clientId, userAgent, applicationName, applicationVersion) { + this._params = { + aip: '1', // anonymizeIp + tid: trackingId, + cid: clientId, + ua: userAgent, + an: applicationName, + av: applicationVersion + }; + } + + screenview(screenName) { + const params = Object.assign({}, this._params); + params.cd = screenName; + this._send('screenview', params); + } + + event(category, action, label, value) { + const params = Object.assign({}, this._params); + params.ec = category; + params.ea = action; + params.el = label; + params.ev = value; + this._send('event', params); + } + + exception(description, fatal) { + const params = Object.assign({}, this._params); + params.exd = description; + if (fatal) { + params.exf = '1'; + } + this._send('exception', params); + } + + _send(type, params) { + params.t = type; + params.v = '1'; + for (const param in params) { + if (params[param] === null || params[param] === undefined) { + delete params[param]; + } + } + const body = querystring.stringify(params); + const options = { + method: 'POST', + host: 'www.google-analytics.com', + path: '/collect', + headers: { 'Content-Length': Buffer.byteLength(body) } + }; + const request = https.request(options, (response) => { + response.on('error', (/* error */) => {}); + }); + request.setTimeout(5000, () => { + request.destroy(); + }); + request.on('error', (/* error */) => {}); + request.write(body); + request.end(); + } +}; + +host.ElectronHost.BinaryStream = class { + + constructor(buffer) { + this._buffer = buffer; + this._length = buffer.length; + this._position = 0; + } + + get position() { + return this._position; + } + + get length() { + return this._length; + } + + stream(length) { + const buffer = this.read(length); + return new host.ElectronHost.BinaryStream(buffer.slice(0)); + } + + seek(position) { + this._position = position >= 0 ? position : this._length + position; + if (this._position > this._buffer.length) { + throw new Error('Expected ' + (this._position - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + skip(offset) { + this._position += offset; + if (this._position > this._buffer.length) { + throw new Error('Expected ' + (this._position - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + peek(length) { + if (this._position === 0 && length === undefined) { + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + const end = this._position; + this.seek(position); + return this._buffer.subarray(position, end); + } + + read(length) { + if (this._position === 0 && length === undefined) { + this._position = this._length; + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + return this._buffer.subarray(position, this._position); + } + + byte() { + const position = this._position; + this.skip(1); + return this._buffer[position]; + } +}; + +host.ElectronHost.FileStream = class { + + constructor(file, start, length, mtime) { + this._file = file; + this._start = start; + this._length = length; + this._position = 0; + this._mtime = mtime; + } + + get position() { + return this._position; + } + + get length() { + return this._length; + } + + stream(length) { + const file = new host.ElectronHost.FileStream(this._file, this._position, length, this._mtime); + this.skip(length); + return file; + } + + seek(position) { + this._position = position >= 0 ? position : this._length + position; + } + + skip(offset) { + this._position += offset; + if (this._position > this._length) { + throw new Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + peek(length) { + length = length !== undefined ? length : this._length - this._position; + if (length < 0x10000000) { + const position = this._fill(length); + this._position -= length; + return this._buffer.subarray(position, position + length); + } + const position = this._position; + this.skip(length); + this.seek(position); + const buffer = new Uint8Array(length); + this._read(buffer, position); + return buffer; + } + + read(length) { + length = length !== undefined ? length : this._length - this._position; + if (length < 0x10000000) { + const position = this._fill(length); + return this._buffer.subarray(position, position + length); + } + const position = this._position; + this.skip(length); + const buffer = new Uint8Array(length); + this._read(buffer, position); + return buffer; + } + + byte() { + const position = this._fill(1); + return this._buffer[position]; + } + + _fill(length) { + if (this._position + length > this._length) { + throw new Error('Expected ' + (this._position + length - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + if (!this._buffer || this._position < this._offset || this._position + length > this._offset + this._buffer.length) { + this._offset = this._position; + this._buffer = new Uint8Array(Math.min(0x10000000, this._length - this._offset)); + this._read(this._buffer, this._offset); + } + const position = this._position; + this._position += length; + return position - this._offset; + } + + _read(buffer, offset) { + const descriptor = fs.openSync(this._file, 'r'); + const stat = fs.statSync(this._file); + if (stat.mtimeMs != this._mtime) { + throw new Error("File '" + this._file + "' last modified time changed."); + } + try { + fs.readSync(descriptor, buffer, 0, buffer.length, offset + this._start); + } + finally { + fs.closeSync(descriptor); + } + } +}; + +host.ElectronHost.ElectronHost = class { + + constructor(host, folder, identifier, stream, entries) { + this._host = host; + this._folder = folder; + this._identifier = identifier; + this._stream = stream; + this._entries = entries || new Map(); + } + + get identifier() { + return this._identifier; + } + + get stream() { + return this._stream; + } + + get entries() { + return this._entries; + } + + request(file, encoding, base) { + return this._host.request(file, encoding, base === undefined ? this._folder : base); + } + + require(id) { + return this._host.require(id); + } + + exception(error, fatal) { + this._host.exception(error, fatal); + } +}; + +window.addEventListener('load', () => { + global.protobuf = require('./protobuf'); + global.flatbuffers = require('./flatbuffers'); + const view = require('./view'); + window.__view__ = new view.View(new host.ElectronHost()); +}); diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..c30ac5a9b11e3ab802e35348aa94adb004a5b9ea GIT binary patch literal 34494 zcmeHw2b5Js*6y|KLEbEu<2a7va$L&1<@33G{(`7T6c7}|NDvhf11d;Pl0%a_KXVEqG@sv5m9t>)KSM_9A@_WzPh(Ar*GeGx>4qzS?fP*?OS!usoMM7b?Vfq zuy z(^q$zOk>Ad&+mQQWD1^OGTjRvND7YD>j-*#xFDhH9e3Pu2QgT zj()tXq^PhoJM#O2WOpGk&TG7f%uadScH3?J%1TSU_gcIpYU5}LUG-om;x*o58m4ss z;Zj{)<((U~L?YKcWT&W2BP7N96$xK^`5MzOtr16FUfyGs*}f99X{e1Jk*Ez1OIpAi zz%W$O{9iTP8!Al8JT?q2Z7ut<%^u?=Ve2r-3Z5e=es4&u=L|^?UnZF$??|H0EQ#Lq zh`P@-Ov^mPFlyAO{>{~?-F<@{UrJ8!|<*q83R?#5&0cUnH49Z%TIf97*zfO>(1FN_N;g zDjoAMZ+Ut7oz3N;lIi=HqMVQyBrSY_q{nQK%*e%(jxtCOeM`)NuS-GPDk+Sfr_wPG z^Ujztqu-I{oNsdj$4Y+4M1?;qVwt2NPf^M?Nd@k_sMjSc{0+(WA0tdhVjkvY8GQWF z>Ga~LXQVLnaVbrlFQrNIB|Bm^^j{@8k#i+C`fczYr|vTi(=v~KhUd!4iT)1#> zQc}{I+}zycqN1V_663tad*IXG#*2BK5iWN+n5Lh*yZhs1Wn~pjjn$V*GXo_nVue`z z=O|*F*LaUWZu(_KMMZK$MTW#~87JXuhIFAYTq7CpF)i~jFL~HxD7-dV3|vb}O5FEX zWl7BD5fZij;Z78Z^Vn_U(H1S2*d3E40`0Xi4f8NBd61WZuabjr$f$#D`+iK?IRS0* zBb{mEBNDm(At{PqD?!T#OJUp^#osX<^O6U7fxoeQTw7aPi;EN2NSxcFikLS7m?QB$ z2w6J>?fOenV(~<~@~G@JZ?j(8Jk*|E=OYjD0)PAS4hjmo=jfr7_Qanc5f+cgPt-<3#|UE=m( z_PpdlUgT*jBbSbjj_``Kjgsmy(oW^6YosXdec788EWul+NHTa8C9JcTf5M)ZBz((w zNe+5XvLok9X7H={ehpXo$%{NS{}U%pWS1l@lC<5UK%;FTziT7K5;9M6%svwDH&1fo z*GO@~N-2t+E9Tu3B{O`U>`mV-;p;|8y8l~}6*N`lB`@;S{7;`gon2;LESWy|*4ZfC zd$c6G4;OPVzB8%5DEkG9e?jaDe6!w`IQJ(dJ!~G{<2_dX0dGlu_;i(@yvS4Y|KNiU zLhEw3NDjVpq-?)2!t%+DUMaO0B&bl&UN@$&Fhq ziF+nV7Rsj7vQE-NW=U?$dx~!XzRx-jL-HW6s;a7i;Lk(7zn_2pdG7wAtx_B@zB7f7 zm;BIic&>A#GHbVbzsfAzm7ZPFA}@K6m*%S^;0Mop@MoW%`LQg1ij+j7Kj27F6QnAA zld7Mk=JisHeu85f<|PmEvhgQ-dpq&P7hh~Rd!iQof~TcC_Q@_(ZJr@bMVqD8@>-XB z%)`9oVdJU!QieLFPMzxd%{SkKpF34A)#hnZnJ}qW#I(%Ayf3`)g0bx+Use7(eD&2= z+1FV2^_S;=JW#n)s*|4qq0i!!o)M;DTIQKKb7m)AnkR+e(DzgvdOq;izy3A*o3B2- zbmnlD9IEw{))KUhAjWx(_n3xh9r9~_40}3UE?l^92Y9^wx4-=@@W&s2OeZnUYoC4g z*&WE-rJNlYYTVZ(6m*uPR!sjiZ8lvr-DUd5^efX@(+!HMOxK$JB(=#9G zl4*?MMV{o%e%L_JSkM*vVMx)Z<4WbxCuY4&tYxxKcJqDr-FMH56)T?HxpU{sB*uA- z_drae(9nuutPjex%)`9oL0)~p;Bx!zx8G7)T54%;ZBxh;$dkOUfT17e z5jGt@)Pe8y>z(?+#y&CQ?AIoFPLVRpF3FEuCn3PVYmVu7PG00m-jL7!r?bNabn`Y^(pjE*Q+@adpd`{IC+vcWk5LVw&T%% z|Ni}jZH>j~I}ejs^r1DyAReX`}Sq&?y66&^Eig&N#2wJ z!F(r-hl{_z|Im{sPfA|II}*R;QAKeGW4DaNe92si-8or;*E}czs~?rLu;r2-xms!p zBheRHg}#uFWXEp6{UHh?^JyN&ki5}{5XypZV;Q(MG&J}gK2WRr+`3Pnux+?B6opFS zt|?NCx}1}^6LMEd=!OwWe~t^1c2AWkw^5ShGgHh#^Cbjj6N`BvjwLi7d$@hLPzGf| zzOg^h|LD=9himhFJN5Uo0;fqf`oAn+_D?JGBP4XoWN}|S2=hUkr8sq`dIr^)|1x_| zlVqQl#1gt#LV%**|TRQJ7|hnJTbm7P?rBwQkSy@W4m>dmFOv{ksBq>_gx8IH&p5hLR8trZk;6c zB{8T2YbDulwuEkdMoKaRq&&?X?}(3>y(X&h4f#?AWl^S;xa3w&ppiYGGP}&}hPj?QJjObO8k>D=rv700> z8FP8TZ=-MSfxiDHiNQQ()Rv(byF6!=58bu=hJxL~aTWPyK?Y?}rj}n^Ts-8{Pd~*t zYAVJh)^QTUEY3j&OqPm_&5{M(GZQ@|8)H?D9YZ$_llp>Cd->NCM`2969&@E{NtFBZ zcsGNkCfiH)6$eOO=(B3P!|_*s_%xv`%GBkL`JS86C%jaVvPg0Q#v15xX)L~6|8mV9 zlHmJ_RPRkdU9{GH>PV`~-z}-TM~d0+Mc`Pio}n7|b>cTxD1)*nQ_Dx#?~_kHX>TbH zmOPBZG?m1?(&_zcD2bzT zkh?<46JNo2a-3uZJ|Sgk>!i@ULF)EK+uPrg^sSNuoeRS*=QZ+SoV+QcuCDHW2l*@t zSBxJs&m6Cpy^&9VCUi?2e-=be>D2x-7JCEp_-^x%=b2;m9HVAZ76m#SF6b}a_vb(V z`G@9Gk1k_S$1!PX4BDK0e^vMR{cYO26LZ>K$F4dbd6G9}K(4XujW9TFTKe_ZD5K1U zQiic?PgIxwmb6v)Nn6=2DT|rdZCdgoPx9_Tw!(4q%{OzNI`XS8Ka=|Gg;EheQBkEs zsIOlsBoFc;&tL!g*Urk_sK1J1?!F(&DiV|I(pKpuRhYZ4N}AM1dP?Ob5Awp8!1%s( zk?DBhcfb3c3otLfc=6&7AAis!jd@F?24nHw5YsXb^ODD{x8B;_^L7;Lc{!62eFXYj4TR z<8_{MUF+5%LxwyY5D>5q>uDvmwY8tw=6pEs(}j4CX_%IIn3p_kytGU$3zzMI?)WfrN-3zzate?^S*8t*ZUPS3pLL0;r( zPacN(ifqhfT|%El&EwSVO_bcoWw3jgAVDk8@9u$^hH06H zb3o)lUgSyMlmRlnBUi#jC7FhYF*L?mPjAB-+;Y^5uQ)%*xt}tuMa8&{$5;&OwIGIl zi8C*GkQaH9H)T*3NWUA1szc-9i7{@y>-?AL`rUHNEq9{M#`D=9Ja|y*ien{q>jbR5 zbhf>@+H>+EPx7V=%KFW3esd>fgRX>?_q(HhfB3^6{x`<7CGG9)ad?l2;WT}P(j!O-=$zy0kUm^&_ExwN;|OUmwPUFH&Lv*c`kF&y(8 zdQHC5`aG^Py)hr-`aEUNG$o5NDVrFG#fAxiIy6pw?ilj^Ef`D39y)XgYv>JA?^?fX|7b|21(%iEaxG;;$iWbu4EGfu>d2# zk&ZaEPR7s`Yj%UNUj7y4)1)8;S!H%ZO8a^5u-b|NaY@iop}b8ejF9)WrK5X=>OFMU87%FURk#P`fLkHnC5DF|&Y zZ3NXkw&F|P-4iho3o#KJuPJ4)J8k~sfv@tGaKl+E|D5bLf|!pT7`x7PQ&4&iBL4hPF1E61I<&qphV_&l#iYMd<2b(4|ny z3*s>UXvM{{^+lUmRTv|_t42ulwh0pN{xs&^@r=;+6vHMdDR2SSD;|U`&%>%r61Kr+ z$RNjAoS2A>7=hpMJ*E!7L|-Z!b|_Mkv{I6Hz_!SN^1@%2igXWUXB3F~9Ef&3ANE}_ zKJ&zL@&6OQm4n24`2&&?zFMMoza{(16QmhbUl=NfnhGUu=aUkSaxXM*m(;Lj5(pil z@GdBuC)y-A$TOZ36R{B^usYTu;=dW|Jm=4x`9Nq7McY|N$_ai^8VbWYwJD{rr>QAT zk&^5XP_U%MY?b=TY{`W^Gj-E#Pb}u*T52p3hUdd_&rR4U8BuF4>wssV*G0@&m!R#F zQGNz65gRcQD@fNNRe$EspMTGXAATsu4>w~?19n#+hMnUhcj`zJY|MjTMw{aR}+lHxmE>5$?v!iK?$h1iIZSPkWmsL2)U(zCgK(p(v@)>O2e zb@w651!V-g>6FOzk{<6N`|FEk`-1z$bJ>GZQ4k@ATgy}(%7o3?q5TEuyJ8Jv3)YIF z7E5(ugjD2*NrL}eX|Bu1n)Do{YvhJ8a=58T%zGwF3heKU@`;TYiItcM90#9)E5@bs zxc;&q^ZMyn54sX@&B)?40^@+8Qk3o^6-8$8**slJ@?xZ-FkV8i))ljTl$61)tr|9A znf}iJ5A69oQLmwQ9O_8O2DHavi=?$GwNpK#oj^^lJKAvA=^3yP8!-|qG25^sFuCH} zFz@{N^9p|k*Ocspbt@sX1f zy^F;=nt-)5Vvj_=K&-v_>&R(r`+2XXl&+r+o){_!}WYgzH|1TQ1k1 zj(z#4_p>B${cwEa7D-h>xT56X_oTJ4pi})h(p)S#Ad;lhOra;UjL*)wuY&5_6}&3mj=!&b1ZE){)*T~eAKuk0z!SRc+!-XRelGb9M# zs#^Q|%W}28zbEnjR-zA@5&n)uZW%5S`2MD2EMJ=Gh3`bD9Bs*$qs^JJH}*A!*-<{R z5hJnM@Y~y;ty{Ma#2SozaH8FAm#Nn2Y&M#V(>C(({*2D;=c&UIuw{z)tsX31=r`0= zq)W8-+j7j%e(Ji>f%YQ_bw3sBwy~%~*|1Y6PTh*J(Fo-$LB9t_nZ|QsBSvC%#E8GDaei+tTZT=I+aBu8P+xLs>_EzHetF}B9?M*n^lrZ&7_SfcOoqD(J`(;hX z@7Lj`Oi9Ii6OZpxfq65&!y6?9Hn$3Qz-6Beqf8wqCSoH-VzuE%VDEeOL)&Wk;~)Ph zRT-Ra%}7FBh($l99^;O%tz%)^{gh;dOjEkW zqU|Y#&1-JlGW1cfmJ8hZ!Ix!dS~rORF6ww?1@KeVsWUw!ozo_l~= zcjek^SK?YQuOq~|FxQ5OB^~Wke(XHxksxK+KB_-p9KTe<=EQ>bmuZ==D_>`_uvQK% z#6)bwXv2%Zu7}pg1@>Tru{QVPg- z+cItAcimPEf-hKsvi)Y7-tl`vVIUS_0#0MuYuH*Jdk7nxJCWy0=nT8|RZG|$F5Jl5>{ zK&3HHVEujv-toQot`^9VeX&B@0$%S8AIkh3vWbCMY}g4~>!9PtkZ~R^*Ijp=E9|^| zzyJPw`Q+SjsZ4&klYgDj|E}ja?C#@8m;Lpw6sIi8q-}oelf^iPw}UgR&@-vR{1hMXpoXF*%7-58mgYLmhWny?XTktYxQj zP5sL+K9z?2)l!uRn}=&awFHruzOJD za-=0qs#2zR^1F2O!|cmBd66f}oH8hjG7b1TBkFvnb?Y&Nt+XrJuo>Y0NBAqqpT4*t z?KQztoia^o%&`B^L|c$6#clVQmpsUeJjt6fD9eVuM@$C&bom=Yr}2h{hTHL8Mban( z2g>RzIe9Q&_7$y{n$+o1mom9WVj8Ap9_A$vG9pj%cEIEmgF_me_URx`SIi~d2V0iN zzyJO3-`ea<&|Z8e7d}1#d#-Xh(UvAhn=p3_Vw~4_k7;yz<|PmEB2SRU+ZRC0qx0#w z4#~$^TwkNj%FU>+v+(Sbf#>W${_zj#iI|3IVY@Voc@6ySJo|V}V{{D3Phab8+!brm zx1ygu0y<4a8#0T;IIm%Slxyv}oO{c&HyEx~_kV|cs*wLvf2`(0G$7((46CkJ=l_J^ z60cu`(GtUNV0^@|3Pwu|-C$qDkOhDrBu9qJA?%Z2p#(p6hId^K!3K;>@UiL~p5^%s zCiq}oo6u37K6k{YaXab*6_uWLy{l~H07j}@bUEpA6I(fUDPL=O-wSqQL2jU4*l8iv z{~Xt_1RUEFwsp?PG2JzUbj?|7U8pDfTxCz9x%)`8z z52F9}hsMfr3H$fdmHOTDi(mX=FxD8K!+tyqeSCbjhlhvzWB-D1663tadrZT$%wx+- z9^|FRI+XG6c4)o%%=nCNM_={6dGqGYgpEWv=5<@J55f1ePjhB}XS~KVOv^mXOCIC} zp0sx%Z_3c}5dCjGG!8x+*4u&O$B&;77Z(=_yM%Mv9?uw3SF3$oJMAwN(JqPCc+Z$t z$H|L4$(u54vi`g419j*7$#?b6DO09Q%F4>hh7K3$&#H-bV~5(CqycO8rC3+Ui&%oW zx>w;3`!wdk#w%i+*K%=>X_)rVfhJ{hrui@=Z_1!7%CyPWZ3IsLWe0UNU9z;6iE*Dp9*;&b|?nh$)6G@FY;`yFH$nJJjx~p zVgV7;wX6&F`tIuLdbinZ_6Lqjtl!H1vZY3H!r;%@%^p8L*LBJ`2w7S#F%SzeL4UrB z*P;xx?l<0f=biUqd|AwAO*#ZWR#__@n~{BFda1rNXmZl9KUsC~a=2EJjnTZP?KxAv=!_ne&@uQ6^sr>(2w zIj&JQF=#x*MvOmm8SvQ;7&vg?z4#6_(Ee1}y*5|E$Mh*>H>qq|VZ+qt5ISjF*e+o& z`6ce0B<;YWaS3&{=p4RroFsa^0{`qW;s;xeV3Y^zk+K=?1Ak&V*ixlzi-{He1aD%u>2Nh= zz&tl$|AU#}cj?HHBXXpp88!h=^~z4TCwt@w*inS9A0{oZ-8*}#19r$cD%@XTmH^mx zu^y*4scej` z_Q{DXg>&CiX#t z2C*&Z>wCcRqMdvw`6xT%T;u+tu(!4EHF~AEb&osi9vpaIV8a~)+t0J74jOc~`lPcg zcs&CCBeXfT?#*!}{$20a0ISAL9jFVaw{?Ke{+Dy-&K(6F7h#`_zIYvMIY-*}_i)^+ zqq}pp2MBGbL9DCVj@s-y3%0viw&$*Ow}!l)g8N$k?s)ffomh#P*r@|`p-!OQv;*qd z<2#aa{J63WhOhnt*c8)FoO=om1zF?jn$2#Uc(lIixow6mJ9UTcWdxpiDB83jyw~2# z9*}^I*hB0jpMPiFdH&(acI+Vt+ibKER(o=*O*!S#uKBWjtL=Js=LOLVlx;S3pf1q8 zH+4Y0_p@iuei->K(zd&O|K4tG!4)RxO`LpQ)R9D6XPz@udZVrPM%(VaYK)X%pTYK~ za!@Jk&3rnQ0rw?os>xDi#IoX^jkIgG>TTGgh0noZ&lbmX+J6%}b)YWP31x7lc7Xcd zR9af*&Anx4S6vvh&}K)j?a42P)Sr8nBzit0HF^HhTxFJ$v|SRoc9gQG55V_r_tO6o z{|%F+x;Q~v8ww@bYc}lK?-!r76JWn_PCh)*CI@QMRoGISA%|M4un*&O@rAD(?ZRo3 z%(@lFy*g|*?X;`5+P3!`68p)Mr)ZmueV5DlJ><8br+2^=dmi16Z^s$xezaph?9YdH z-IvC;R}J5Tyr}o&+y_Ub)VxJ90^f#BskfYk?XvH>i4y2GRrXb7g3={o=QQz0yH0y| zuO$!2{+b*K*)|pX^h}hj&^IL!{t#>%{ZV!uEfo^s{wnOwnI3y2p?)cwew*I*{c)V_ znPa>L?9_p};2U;^xraO+0XM`pp7}pAQg7>`Kh~=O?@PM>{9$^Nz~Ty_U9e2W(4|Kr4qdx z_Ry-#9#ZcZ>$vg>pgmd-xQzFx1NEm))D8M~_YP410od2?+;RX`@+X$_p0%7ps7rDEqNIJ6c+Wpqt6Gwd$hw3 z-!xq58n|}29N1T^_6ubhuS**M=q^qjv_900I)a?N1H5)K`p|W-i;)vY z4~Tgud<|&x-wl-{E>U)jUEA?Na`I3;?5z{T9JWfrcD{tN7%FSt_`k{)*nK7Jkg8I# z&rbl#?sET!=d^R@JHoxfgVsKZIgdO^4ql?(1^QUH)Qvhqe`jq#zs;LB4`n)1O^%0kk6fG22K_eFzPW~T`Ux}? zc*==`wb;k@Wn~waLEjC0+YhzXiO0%us{N<$%${{mOG|yB>{vVmHka>9dtLYaAMQ8L z{jkE(9@2Ni7xQ1V+02ODD0{#w2;WoI5#Eo4&s;3N4ctpg>5sj*6jqHFz6R7q>qQ-* zKgV^BN571Wj2ZN!AeAI8wfT5(e=Yi1SohhohSqcIKEfklYr0%cb(Etnbil?o58w6@ z3H4g2#$zEnXUT?l?nk^*_N*DJ-s5=piK?H|^}QLa+JT0OG>M1bl=n*bf^Z%Q-+(>% z9#$2_N>;)Sd{3W*Uveb;FiP>wO_o&unecDH-uieC=o``*??|PK){8nqacBDXhaKfo z>?<#%yolF2`DhsZHM)z_Z-G7(VQ%BFZ(EIQUVuHXDl(;IU%5nhFBC8K1)z^7{0L7T zYsGW%>-666yuBd>&VDH3L7?%&T>Tmlm0gW>sx@f(qBlPDt!13shb(c-_Lt)yS;3@X~o2~mfb5G}< z$%6YV1J=cq;Q3OP7b))RrpvLzZSX@$lr8TKLj8Y4n(OnGf6~FmOw18UM{-tUHmIL*REf(S4lqt#QOjU8oav zqmEGA**CC1=GAx7kL9C}&PjIQ)17==jQ%b?#ktot>p(Ex&&r~BIec)xte=Z-zvnyB z+*pclUzLRHcv<;4MQ?dTQhlD5{OCE7?)Qw^$6EX1q}b|GBIg=?vdFRZbt- zr@pU7{TXkmG^0)|MEQ^Gf}6TfC+bEWp}(`SP=Bnk?4%!z(m&{FgMW_EZ>Re>%f*5{ z)#w8gi1y2>f0I>zuXm(%UzxOHjwWo^%i@pzeF(nkacB!NLf?=C`0>!^CmwYt5$!nj zW}Lc5qL1x|^bxzJ^{7Ah3uhg?>{n#Pnhjm36Lq7GJv{$S^rK-p<%LeQ?=i0T8g~}b z?}vNkanHRJ_+1e@eUK6Y=1F;eln|mllM1z0=2?w|`C5X%f8cIVnopjW)Cj-~KfGpUGkg zST`L0cksWiNR^Mzc5VM^vNk9z$}h@*k$9;Kb*FCB5sEu&|LH3@m3~O1GV{{Tep2uO z>g2bCbIP*dxtE%k$#M9PoIQ;`Y+bfAHxx^NJJxA1mW%d&Lp+xcL3vckp0(q|e+}lg z;Df>T&hZ`QIs3u;tJATM{agum!@CdvnK--$f$Oj@WJx@HWZW@7^1K|Ziq6Goe_7j@BkQAg(6yL!Vd(8#^hoz$et)K6#rxMVfX!CpH1+s3|oRZ z4?k(D%|YMysC@XrF^mhtdhGvZ2S1CwEwG0u+H&rj*^ItoF8-4t0c{KSinRNqT>+oQ zPF<)Ib)$~Zzq|3z0DL2>nD+F^4*05#bLy|AeaC7tR`xjlsVhs8oP_oGe~Q@>xqYno ztQvx`eHq^Gr{UwD;dK0Sq&Z9ZikZ=thQiOY4SNA(#jI5RSA6EmFU*M#oyOEP9EUcu zL+MZ5pnG@YKio3)!(7pJ`jfG~6vw_QdGO=16Mf$d=QZg|dK~{o?tqU!o|)DE4L-kk zrg2`+%Hg(333VGS9q?J?`>w~oM_aNa!+!$OLwD>Ee6XolG9nkL{Cwur&AKC(0)u9d|na@!5s* z*ry?0qCB3JNVNHh=$9v>Pm_T6IS>C^Qi460!||O<^_`4;Ip^Six1NQ)!$_4TY|CSq z2P?zA&2w>WlzQgWJ3pBJQPn%YI-QO>Xnm*~byS)<|8cqDh8u1~{XflpM^7JbgC89B z4uzkcGop;TtmPQ@be{h@e->*cVK1vaRu5oLix%{66MQi~WWNCVhoE2E+K?wy{7=gq3C5TEp(-tFSDRy%7`Op9mj7 z16}Fdnuq@rW7UKDWCf0qwBVO)-#OH6LWMAYN@l*oym%vUPZ2p}+ zwR_atc(~wyA?}5L!Ve%hb*u^d`%V(&14%zfMc8xL9_Ka3bDho>s{M(r|Cv*hKEKEH zukw_6Qi$i?Gk-^Um(Pinn2DV_P!}lIQ@dx>opIF;pxiv+PontjEAfSIr|tjw_@6vF z&d~APc(2R-3D_HYy87;2p8M3#-#Y)dH+HJ^e|;SJba(C`pIE6ou~P@=)0_9cOBpO% zw(J)0ISpSG`2ye1iqv=eR@r9W#YIlxAY#7fM>4rN&HuU3Ec9$?(Kfj+0CQ%4)5JpMT+{|nXif25rK zx0LZ3_v+?3ByKzWLAF_xirlp5E8`KCuv!#z(BgOze)zT;;jTf&~k1LVHrCWw5L(l3u)~KEu87 zNhStj(YS~a`#4=`o%d>r!DZXFZ8xJ`DT8mMkj{Su-|6(Z*TmLl{`bFUd$10^$v>E%9gzOo@;rOjkVgb!~*)c+B&f6 z)j@eK@LOWLxD&44KiGV})jk1dKj^@k1l|kFY^jCscuz#%a^rn{&b;J7Ufg>?^QSDz zq-gG7|?ih1?B@od83hy4RcH1T4C%wp`up8? z->W`4`|0ej@fz3KWq?d)8Y1#Y-vPyPO|E4v%>kdyGt--2$-(NAT+dIRIU#(PY|w9Et2 z`JM6pPrRn}xl;I_sN4Tw{tqkxwS9o|pNn@{uVY^V9v8V$1CFyMllnUtH*U~?gGwW~ z#BhvCrV@U`^IxgtDnk{|vGs!+LuAw;Pr(4&8t~YxL&XjZbqMJ^?s5#37zpZXcvi79 z-Jrw1=GW;R@#u4nTZdXN9janu!MT}mpggJ=qI`6CmggvFB?#rH7~dtbqwLKR|9K-AUcc3dD{~v}pwf6u3 literal 0 HcmV?d00001 diff --git a/flatbuffers.js b/flatbuffers.js new file mode 100644 index 0000000..f56031a --- /dev/null +++ b/flatbuffers.js @@ -0,0 +1,392 @@ + + +var flatbuffers = {}; +var json = json || require('./json'); + +flatbuffers.get = (name) => { + flatbuffers._map = flatbuffers._map || new Map(); + if (!flatbuffers._map.has(name)) { + flatbuffers._map.set(name, {}); + } + return flatbuffers._map.get(name); +}; + +flatbuffers.BinaryReader = class { + + static open(data) { + return new flatbuffers.BinaryReader(data); + } + + constructor(data) { + const buffer = data instanceof Uint8Array ? data : data.peek(); + this._buffer = buffer; + this._position = 0; + this._dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + } + + get root() { + return this.int32(this._position) + this._position; + } + + get identifier() { + if (this._buffer.length >= 8) { + const buffer = this._buffer.slice(4, 8); + if (buffer.every((c) => c >= 32 && c <= 128)) { + return String.fromCharCode(...buffer); + } + } + return ''; + } + + bool(offset) { + return !!this.int8(offset); + } + + bool_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.bool(position + offset) : defaultValue; + } + + int8(offset) { + return this.uint8(offset) << 24 >> 24; + } + + int8_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.int8(position + offset) : defaultValue; + } + + uint8(offset) { + return this._buffer[offset]; + } + + uint8_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.uint8(position + offset) : defaultValue; + } + + int16(offset) { + return this._dataView.getInt16(offset, true); + } + + int16_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.int16(position + offset) : defaultValue; + } + + uint16(offset) { + return this._dataView.getUint16(offset, true); + } + + uint16_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.uint16(position + offset) : defaultValue; + } + + int32(offset) { + return this._dataView.getInt32(offset, true); + } + + int32_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.int32(position + offset) : defaultValue; + } + + uint32(offset) { + return this._dataView.getUint32(offset, true); + } + + uint32_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.int32(position + offset) : defaultValue; + } + + int64(offset) { + return this._dataView.getInt64(offset, true); + } + + int64_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.int64(position + offset) : defaultValue; + } + + uint64(offset) { + return this._dataView.getUint64(offset, true); + } + + uint64_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.uint64(position + offset) : defaultValue; + } + + float32(offset) { + return this._dataView.getFloat32(offset, true); + } + + float32_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.float32(position + offset) : defaultValue; + } + + float64(offset) { + return this._dataView.getFloat64(offset, true); + } + + float64_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.float64(position + offset) : defaultValue; + } + + string(offset, encoding) { + offset += this.int32(offset); + const length = this.int32(offset); + var result = ''; + var i = 0; + offset += 4; + if (encoding === 1) { + return this._buffer.subarray(offset, offset + length); + } + while (i < length) { + var codePoint; + // Decode UTF-8 + const a = this.uint8(offset + i++); + if (a < 0xC0) { + codePoint = a; + } + else { + const b = this.uint8(offset + i++); + if (a < 0xE0) { + codePoint = ((a & 0x1F) << 6) | (b & 0x3F); + } + else { + const c = this.uint8(offset + i++); + if (a < 0xF0) { + codePoint = ((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F); + } + else { + const d = this.uint8(offset + i++); + codePoint = ((a & 0x07) << 18) | ((b & 0x3F) << 12) | ((c & 0x3F) << 6) | (d & 0x3F); + } + } + } + // Encode UTF-16 + if (codePoint < 0x10000) { + result += String.fromCharCode(codePoint); + } + else { + codePoint -= 0x10000; + result += String.fromCharCode((codePoint >> 10) + 0xD800, (codePoint & ((1 << 10) - 1)) + 0xDC00); + } + } + + return result; + } + + string_(position, offset, defaultValue) { + offset = this._offset(position, offset); + return offset ? this.string(position + offset) : defaultValue; + } + + bools_(position, offset) { + offset = this._offset(position, offset); + if (offset) { + const length = this._length(position + offset); + offset = this._vector(position + offset); + const array = new Array(length); + for (let i = 0; i < length; i++) { + array[i] = this.uint8(offset + i + 4) ? true : false; + } + return array; + } + return []; + } + + int64s_(position, offset) { + offset = this._offset(position, offset); + if (offset) { + const length = this._length(position + offset); + offset = this._vector(position + offset); + const array = new Array(length); + for (let i = 0; i < length; i++) { + array[i] = this.int64(offset + (i << 3)); + } + return array; + } + return []; + } + + uint64s_(position, offset) { + offset = this._offset(position, offset); + if (offset) { + const length = this._length(position + offset); + offset = this._vector(position + offset); + const array = new Array(length); + for (let i = 0; i < length; i++) { + array[i] = this.uint64(offset + (i << 3)); + } + return array; + } + return []; + } + + strings_(position, offset) { + offset = this._offset(position, offset); + if (offset) { + const length = this._length(position + offset); + offset = this._vector(position + offset); + const array = new Array(length); + for (let i = 0; i < length; i++) { + array[i] = this.string(offset + i * 4); + } + return array; + } + return []; + } + + struct(position, offset, decode) { + offset = this._offset(position, offset); + return offset ? decode(this, position + offset) : null; + } + + table(position, offset, decode) { + offset = this._offset(position, offset); + return offset ? decode(this, this._indirect(position + offset)) : null; + } + + union(position, offset, decode) { + const type_offset = this._offset(position, offset); + const type = type_offset ? this.uint8(position + type_offset) : 0; + offset = this._offset(position, offset + 2); + return offset ? decode(this, this._union(position + offset), type) : null; + } + + typedArray(position, offset, type) { + offset = this._offset(position, offset); + return offset ? new type(this._buffer.buffer, this._buffer.byteOffset + this._vector(position + offset), this._length(position + offset)) : new type(0); + } + + unionArray(/* position, offset, decode */) { + throw new flatbuffers.Error('Not implemented.'); + } + + structArray(position, offset, size, decode) { + offset = this._offset(position, offset); + const length = offset ? this._length(position + offset) : 0; + const list = new Array(length); + for (let i = 0; i < length; i++) { + list[i] = decode(this, this._indirect(this._vector(position + offset) + i * 4)); + } + return list; + } + + tableArray(position, offset, decode) { + offset = this._offset(position, offset); + const length = offset ? this._length(position + offset) : 0; + const list = new Array(length); + for (let i = 0; i < length; i++) { + list[i] = decode(this, this._indirect(this._vector(position + offset) + i * 4)); + } + return list; + } + + _offset(bb_pos, vtableOffset) { + var vtable = bb_pos - this.int32(bb_pos); + return vtableOffset < this.int16(vtable) ? this.int16(vtable + vtableOffset) : 0; + } + + _indirect(offset) { + return offset + this.int32(offset); + } + + _vector(offset) { + return offset + this.int32(offset) + 4; + } + + _length(offset) { + return this.int32(offset + this.int32(offset)); + } + + _union(offset) { + return offset + this.int32(offset); + } +}; + +flatbuffers.TextReader = class { + + static open(obj) { + return new flatbuffers.TextReader(obj); + } + + constructor(obj) { + this._root = obj; + } + + get root() { + return this._root; + } + + value(obj, defaultValue) { + return obj !== undefined ? obj : defaultValue; + } + + object(obj, decode) { + return obj !== undefined ? decode(this, obj) : obj; + } + + array(obj) { + if (Array.isArray(obj)) { + const target = new Array(obj.length); + for (let i = 0; i < obj.length; i++) { + target[i] = obj[i]; + } + return target; + } + if (!obj) { + return []; + } + throw new flatbuffers.Error('Inalid value array.'); + } + + typedArray(obj, type) { + if (Array.isArray(obj)) { + const target = new type(obj.length); + for (let i = 0; i < obj.length; i++) { + target[i] = obj[i]; + } + return target; + } + if (!obj) { + return new type(0); + } + throw new flatbuffers.Error('Inalid typed array.'); + } + + objectArray(obj, decode) { + if (Array.isArray(obj)) { + const target = new Array(obj.length); + for (let i = 0; i < obj.length; i++) { + target[i] = decode(this, obj[i]); + } + return target; + } + if (!obj) { + return []; + } + throw new flatbuffers.Error('Inalid object array.'); + } +}; + +flatbuffers.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'FlatBuffers Error'; + this.message = message; + } +}; + +if (typeof module !== "undefined" && typeof module.exports === "object") { + module.exports.BinaryReader = flatbuffers.BinaryReader; + module.exports.TextReader = flatbuffers.TextReader; + module.exports.get = flatbuffers.get; +} diff --git a/gzip.js b/gzip.js new file mode 100644 index 0000000..2e3180b --- /dev/null +++ b/gzip.js @@ -0,0 +1,234 @@ + +var gzip = gzip || {}; +var zip = zip || require('./zip'); + +gzip.Archive = class { + + static open(data) { + const stream = data instanceof Uint8Array ? new gzip.BinaryReader(data) : data; + const signature = [ 0x1f, 0x8b ]; + if (stream.length > 18 && stream.peek(2).every((value, index) => value === signature[index])) { + return new gzip.Archive(stream); + } + return null; + } + + constructor(stream) { + const position = stream.position; + const entry = new gzip.Entry(stream); + this._entries = new Map([ [ entry.name, entry.stream ] ]); + stream.seek(position); + } + + get entries() { + return this._entries; + } +}; + +gzip.Entry = class { + + constructor(stream) { + const signature = [ 0x1f, 0x8b ]; + if (stream.position + 2 > stream.length || + !stream.read(2).every((value, index) => value === signature[index])) { + throw new gzip.Error('Invalid gzip signature.'); + } + const string = () => { + let content = ''; + while (stream.position < stream.length) { + const value = stream.byte(); + if (value === 0x00) { + break; + } + content += String.fromCharCode(value); + } + return content; + }; + const reader = new gzip.BinaryReader(stream.read(8)); + const compressionMethod = reader.byte(); + if (compressionMethod != 8) { + throw new gzip.Error("Invalid compression method '" + compressionMethod.toString() + "'."); + } + const flags = reader.byte(); + reader.uint32(); // MTIME + reader.byte(); // XFL + reader.byte(); // OS + if ((flags & 4) != 0) { // FEXTRA + const xlen = stream.byte() | (stream.byte() << 8); + stream.skip(xlen); + } + this._name = (flags & 8) != 0 ? string() : ''; // FNAME + if ((flags & 16) != 0) { // FCOMMENT + string(); + } + if ((flags & 1) != 0) { // FHCRC + stream.skip(2); + } + this._stream = new gzip.InflaterStream(stream); + } + + get name() { + return this._name; + } + + get stream() { + return this._stream; + } +}; + +gzip.InflaterStream = class { + + constructor(stream) { + this._stream = stream.stream(stream.length - stream.position - 8); + const reader = new gzip.BinaryReader(stream.read(8)); + reader.uint32(); // CRC32 + this._length = reader.uint32(); // ISIZE + this._position = 0; + } + + get position() { + return this._position; + } + + get length() { + return this._length; + } + + seek(position) { + if (this._buffer === undefined) { + this._inflate(); + } + this._position = position >= 0 ? position : this._length + position; + } + + skip(offset) { + if (this._buffer === undefined) { + this._inflate(); + } + this._position += offset; + } + + stream(length) { + return new gzip.BinaryReader(this.read(length)); + } + + peek(length) { + const position = this._position; + length = length !== undefined ? length : this._length - this._position; + this.skip(length); + const end = this._position; + this.seek(position); + if (position === 0 && length === this._length) { + return this._buffer; + } + return this._buffer.subarray(position, end); + } + + read(length) { + const position = this._position; + length = length !== undefined ? length : this._length - this._position; + this.skip(length); + if (position === 0 && length === this._length) { + return this._buffer; + } + return this._buffer.subarray(position, this._position); + } + + byte() { + const position = this._position; + this.skip(1); + return this._buffer[position]; + } + + _inflate() { + if (this._buffer === undefined) { + const buffer = this._stream.peek(); + this._buffer = new zip.Inflater().inflateRaw(buffer, this._length); + delete this._stream; + } + } +}; + +gzip.BinaryReader = class { + + constructor(buffer) { + this._buffer = buffer; + this._length = buffer.length; + this._position = 0; + this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + } + + get position() { + return this._position; + } + + get length() { + return this._length; + } + + create(buffer) { + return new gzip.BinaryReader(buffer); + } + + stream(length) { + return this.create(this.read(length)); + } + + seek(position) { + this._position = position >= 0 ? position : this._length + position; + } + + skip(offset) { + this._position += offset; + } + + peek(length) { + if (this._position === 0 && length === undefined) { + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + const end = this._position; + this.seek(position); + return this._buffer.subarray(position, end); + } + + read(length) { + if (this._position === 0 && length === undefined) { + this._position = this._length; + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + return this._buffer.subarray(position, this._position); + } + + byte() { + const position = this._position; + this.skip(1); + return this._buffer[position]; + } + + uint16() { + const position = this._position; + this.skip(2); + return this._view.getUint16(position, true); + } + + uint32() { + const position = this._position; + this.skip(4); + return this._view.getUint32(position, true); + } +}; + +gzip.Error = class extends Error { + constructor(message) { + super(message); + this.name = 'Gzip Error'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.Archive = gzip.Archive; +} \ No newline at end of file diff --git a/hdf5.js b/hdf5.js new file mode 100644 index 0000000..695662f --- /dev/null +++ b/hdf5.js @@ -0,0 +1,1464 @@ + +// Experimental HDF5 JavaScript reader + +var hdf5 = hdf5 || {}; +var zip = zip || require('./zip'); + +hdf5.File = class { + + static open(data) { + const buffer = data instanceof Uint8Array ? data : data.peek(); + const reader = new hdf5.Reader(buffer, 0); + if (reader.match('\x89HDF\r\n\x1A\n')) { + return new hdf5.File(reader); + } + return null; + } + + constructor(reader) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html + this._globalHeap = new hdf5.GlobalHeap(reader); + const version = reader.byte(); + switch (version) { + case 0: + case 1: { + this._freeSpaceStorageVersion = reader.byte(); + this._rootGroupEntryVersion = reader.byte(); + reader.skip(1); + this._sharedHeaderMessageVersionFormat = reader.byte(); + reader.initialize(); + reader.skip(1); + this._groupLeafNodeK = reader.uint16(); // 0x04? + this._groupInternalNodeK = reader.uint16(); // 0x10? + reader.skip(4); + if (version > 0) { + this._indexedStorageInternalNodeK = reader.uint16(); + this.seek(2); // Reserved + } + this._baseAddress = reader.offset(); + reader.offset(); // Address of File Free space Info + this._endOfFileAddress = reader.offset(); + reader.offset(); // Driver Information Block Address + if (this._baseAddress != 0) { + throw new hdf5.Error('Base address is not zero.'); + } + const rootGroupEntry = new hdf5.SymbolTableEntry(reader); + this._rootGroup = new hdf5.Group(reader, rootGroupEntry, null, this._globalHeap, '', ''); + break; + } + case 2: + case 3: { + reader.initialize(); + reader.byte(); + this._baseAddress = reader.offset(); + this._superBlockExtensionAddress = reader.offset(); + this._endOfFileAddress = reader.offset(); + const rootGroupObjectHeader = new hdf5.DataObjectHeader(reader.at(reader.offset())); + this._rootGroup = new hdf5.Group(reader, null, rootGroupObjectHeader, this._globalHeap, '', ''); + break; + } + default: + throw new hdf5.Error('Unsupported Superblock version ' + version + '.'); + } + } + + get rootGroup() { + return this._rootGroup; + } +}; + +hdf5.Group = class { + + constructor(reader, entry, objectHeader, globalHeap, parentPath, name) { + this._reader = reader; + this._entry = entry; + this._dataObjectHeader = objectHeader; + this._globalHeap = globalHeap; + this._name = name; + this._path = parentPath == '/' ? (parentPath + name) : (parentPath + '/' + name); + } + + get name() { + return this._name; + } + + get path() { + return this._path; + } + + group(path) { + this._decodeGroups(); + if (this._groups.has(path)) { + return this._groups.get(path); + } + const index = path.indexOf('/'); + if (index !== -1) { + const group = this.group(path.substring(0, index)); + if (group) { + return group.group(path.substring(index + 1)); + } + } + return null; + } + + get groups() { + this._decodeGroups(); + return this._groups; + } + + get attributes() { + this._decodeDataObject(); + return this._attributes; + } + + get value() { + this._decodeDataObject(); + return this._value; + } + + _decodeDataObject() { + if (!this._dataObjectHeader) { + const reader = this._reader.at(this._entry.objectHeaderAddress); + this._dataObjectHeader = new hdf5.DataObjectHeader(reader); + } + if (!this._attributes) { + this._attributes = new Map(); + for (const attribute of this._dataObjectHeader.attributes) { + const name = attribute.name; + const value = attribute.decodeValue(this._globalHeap); + this._attributes.set(name, value); + } + this._value = null; + const datatype = this._dataObjectHeader.datatype; + const dataspace = this._dataObjectHeader.dataspace; + const dataLayout = this._dataObjectHeader.dataLayout; + const filterPipeline = this._dataObjectHeader.filterPipeline; + if (datatype && dataspace && dataLayout) { + this._value = new hdf5.Variable(this._reader, this._globalHeap, datatype, dataspace, dataLayout, filterPipeline); + } + } + } + + _decodeGroups() { + if (!this._groups) { + this._groups = new Map(); + if (this._entry) { + if (this._entry.treeAddress || this._entry.heapAddress) { + const heap = new hdf5.Heap(this._reader.at(this._entry.heapAddress)); + const tree = new hdf5.Tree(this._reader.at(this._entry.treeAddress)); + for (const node of tree.nodes) { + for (const entry of node.entries) { + const name = heap.getString(entry.linkNameOffset); + const group = new hdf5.Group(this._reader, entry, null, this._globalHeap, this._path, name); + this._groups.set(name, group); + } + } + } + } + else { + this._decodeDataObject(); + for (const link of this._dataObjectHeader.links) { + if (Object.prototype.hasOwnProperty.call(link, 'objectHeaderAddress')) { + const name = link.name; + const objectHeader = new hdf5.DataObjectHeader(this._reader.at(link.objectHeaderAddress)); + const linkGroup = new hdf5.Group(this._reader, null, objectHeader, this._globalHeap, this._path, name); + this._groups.set(name, linkGroup); + } + } + } + } + } +}; + +hdf5.Variable = class { + + constructor(reader, globalHeap, datatype, dataspace, dataLayout, filterPipeline) { + this._reader = reader; + this._globalHeap = globalHeap; + this._datatype = datatype; + this._dataspace = dataspace; + this._dataLayout = dataLayout; + this._filterPipeline = filterPipeline; + } + + get type () { + return this._datatype.type; + } + + get littleEndian() { + return this._datatype.littleEndian; + } + + get shape() { + return this._dataspace.shape; + } + + get value() { + const data = this.data; + if (data) { + const reader = new hdf5.Reader(data); + const array = this._dataspace.read(this._datatype, reader); + return this._dataspace.decode(this._datatype, array, array, this._globalHeap); + } + return null; + } + + get data() { + switch (this._dataLayout.layoutClass) { + case 1: // Contiguous + if (this._dataLayout.address) { + return this._reader.at(this._dataLayout.address).read(this._dataLayout.size); + } + break; + case 2: { // Chunked + const dimensionality = this._dataLayout.dimensionality; + if (dimensionality === 2) { + const tree = new hdf5.Tree(this._reader.at(this._dataLayout.address), dimensionality); + const itemsize = this._dataLayout.datasetElementSize; + const shape = this._dataspace.shape; + const size = shape.reduce((a, b) => a * b, 1) * itemsize; + const data = new Uint8Array(size); + for (const node of tree.nodes) { + if (node.filterMask !== 0) { + return null; + } + const start = node.fields.slice(0, 1).reduce((a, b) => a * b, 1) * itemsize; + let chunk = node.data; + if (this._filterPipeline) { + for (const filter of this._filterPipeline.filters) { + chunk = filter.decode(chunk); + } + } + data.set(chunk, start); + } + return data; + } + break; + } + default: { + throw new hdf5.Error("Unknown data layout class '" + this.layoutClass + "'."); + } + } + return null; + } +}; + +hdf5.Reader = class { + + constructor(buffer) { + if (buffer) { + this._buffer = buffer; + this._dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + this._position = 0; + this._offset = 0; + } + } + + initialize() { + this._offsetSize = this.byte(); + this._lengthSize = this.byte(); + } + + skip(offset) { + this._offset += offset; + if (this._position + this._offset > this._buffer.length) { + throw new hdf5.Error('Expected ' + (this._position + this._offset - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + read(length) { + const offset = this._offset; + this.skip(length); + return this._buffer.subarray(this._position + offset, this._position + this._offset); + } + + int8() { + const offset = this._offset; + this.skip(1); + return this._dataView.getInt8(this._position + offset); + } + + byte() { + const offset = this._offset; + this.skip(1); + return this._dataView.getUint8(this._position + offset); + } + + int16() { + const offset = this._position + this._offset; + this.skip(2); + return this._dataView.getInt16(offset, true); + } + + uint16() { + const offset = this._position + this._offset; + this.skip(2); + return this._dataView.getUint16(offset, true); + } + + int32() { + const offset = this._position + this._offset; + this.skip(4); + return this._dataView.getInt32(offset, true); + } + + uint32() { + const offset = this._position + this._offset; + this.skip(4); + return this._dataView.getUint32(offset, true); + } + + int64() { + const offset = this._position + this._offset; + this.skip(8); + return this._dataView.getInt64(offset, true).toNumber(); + } + + uint64() { + const offset = this._position + this._offset; + this.skip(8); + return this._dataView.getUint64(offset, true).toNumber(); + } + + uint(type) { + switch (type) { + case 0: return this.byte(); + case 1: return this.uint16(); + case 2: return this.uint32(); + case 3: return this.uint64(); + } + } + + float16() { + const offset = this._offset; + this.skip(2); + const value = this._dataView.getUint16(this._position + offset, true); + // decode float16 value + const s = (value & 0x8000) >> 15; + const e = (value & 0x7C00) >> 10; + const f = value & 0x03FF; + if(e == 0) { + return (s ? -1 : 1) * Math.pow(2, -14) * (f / Math.pow(2, 10)); + } + else if (e == 0x1F) { + return f ? NaN : ((s ? -1 : 1) * Infinity); + } + return (s ? -1 : 1) * Math.pow(2, e-15) * (1 + (f / Math.pow(2, 10))); + } + + float32() { + const offset = this._position + this._offset; + this.skip(4); + return this._dataView.getFloat32(offset, true); + } + + float64() { + const offset = this._position + this._offset; + this.skip(8); + return this._dataView.getFloat64(offset, true); + } + + string(size, encoding) { + if (!size || size == -1) { + let position = this._position + this._offset; + while (this._buffer[position] != 0) { + position++; + } + size = position - this._position - this._offset + 1; + } + const data = this.read(size); + return hdf5.Reader.decode(data, encoding); + } + + static decode(data, encoding) { + let content = ''; + if (encoding == 'utf-8') { + if (!hdf5.Reader._utf8Decoder) { + hdf5.Reader._utf8Decoder = new TextDecoder('utf-8'); + } + content = hdf5.Reader._utf8Decoder.decode(data); + } + else { + if (!hdf5.Reader._asciiDecoder) { + hdf5.Reader._asciiDecoder = new TextDecoder('ascii'); + } + content = hdf5.Reader._asciiDecoder.decode(data); + } + return content.replace(/\0/g, ''); + } + + offset() { + switch (this._offsetSize) { + case 8: { + const position = this._position + this._offset; + this.skip(8); + const value = this._dataView.getUint64(position, true); + if (value.low === -1 && value.high === -1) { + return undefined; + } + return value.toNumber(); + } + case 4: { + const value = this.uint32(); + if (value === 0xffffffff) { + return undefined; + } + return value; + } + } + throw new hdf5.Error('Unsupported offset size \'' + this._offsetSize + '\'.'); + } + + length() { + switch (this._lengthSize) { + case 8: { + const position = this._position + this._offset; + this.skip(8); + const value = this._dataView.getUint64(position, true); + if (value.low === -1 && value.high === -1) { + return undefined; + } + return value.toNumber(); + } + case 4: { + const value = this.uint32(); + if (value === 0xffffffff) { + return undefined; + } + return value; + } + } + throw new hdf5.Error('Unsupported length size \'' + this._lengthSize + '\'.'); + } + + at(position) { + const reader = new hdf5.Reader(null); + reader._buffer = this._buffer; + reader._dataView = this._dataView; + reader._position = position; + reader._offset = 0; + reader._offsetSize = this._offsetSize; + reader._lengthSize = this._lengthSize; + return reader; + } + + clone() { + const reader = new hdf5.Reader(this._buffer, this._position); + reader._buffer = this._buffer; + reader._dataView = this._dataView; + reader._position = this._position; + reader._offset = this._offset; + reader._offsetSize = this._offsetSize; + reader._lengthSize = this._lengthSize; + return reader; + } + + align(mod) { + if (this._offset % mod != 0) { + this._offset = (Math.floor(this._offset / mod) + 1) * mod; + } + } + + match(text) { + if (this._position + this._offset + text.length > this._buffer.length) { + return false; + } + const offset = this._offset; + const buffer = this.read(text.length); + for (let i = 0; i < text.length; i++) { + if (text.charCodeAt(i) != buffer[i]) { + this._offset = offset; + return false; + } + } + return true; + } + + get position() { + return this._position + this._offset; + } + + get size() { + return this._buffer.length; + } +}; + +hdf5.SymbolTableNode = class { + + constructor(reader) { + if (!reader.match('SNOD')) { + throw new hdf5.Error("Not a valid 'SNOD' block."); + } + const version = reader.byte(); + if (version == 1) { + reader.skip(1); + const entriesUsed = reader.uint16(); + this.entries = []; + for (let i = 0; i < entriesUsed; i++) { + const entry = new hdf5.SymbolTableEntry(reader); + this.entries.push(entry); + } + } + else { + throw new hdf5.Error('Unsupported symbol table node version \'' + version + '\'.'); + } + } +}; + +hdf5.SymbolTableEntry = class { + + constructor(reader) { + this.linkNameOffset = reader.offset(); + this.objectHeaderAddress = reader.offset(); + const cacheType = reader.uint32(); + reader.skip(4); // Reserved + switch (cacheType) { + case 0: + break; + case 1: { + const scratchReader = reader.clone(); + this.treeAddress = scratchReader.offset(); + this.heapAddress = scratchReader.offset(); + break; + } + default: + throw new hdf5.Error('Unsupported cache type \'' + cacheType + '\'.'); + } + reader.skip(16); // Scratch-pad space + } +}; + +hdf5.DataObjectHeader = class { + + constructor(reader) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html#ObjectHeader + this.attributes = []; + this.links = []; + this.continuations = []; + const version = reader.match('OHDR') ? reader.byte() : reader.byte(); + switch (version) { + case 1: { + reader.skip(1); + const messageCount = reader.uint16(); + reader.uint32(); + const objectHeaderSize = reader.uint32(); + reader.align(8); + let end = reader.position + objectHeaderSize; + for (let i = 0; i < messageCount; i++) { + const type = reader.uint16(); + const size = reader.uint16(); + const flags = reader.byte(); + reader.skip(3); + reader.align(8); + const next = this._readMessage(reader, type, size, flags); + if ((!next || reader.position >= end) && this.continuations.length > 0) { + const continuation = this.continuations.shift(); + reader = reader.at(continuation.offset); + end = continuation.offset + continuation.length; + } + else { + reader.align(8); + } + } + break; + } + case 2: { + const flags = reader.byte(); + if ((flags & 0x20) != 0) { + reader.uint32(); // access time + reader.uint32(); // modification time + reader.uint32(); // change time + reader.uint32(); // birth time + } + if ((flags & 0x10) != 0) { + reader.uint16(); // max compact attributes + reader.uint16(); // min compact attributes + } + const order = (flags & 0x04) != 0; + const size = reader.uint(flags & 0x03); + let next = true; + let end = reader.position + size; + while (next && reader.position < end) { + const type = reader.byte(); + const size = reader.uint16(); + const flags = reader.byte(); + if (reader.position < end) { + if (order) { + reader.uint16(); // creation order + } + next = this._readMessage(reader, type, size, flags); + } + if ((!next || reader.position >= end) && this.continuations.length > 0) { + const continuation = this.continuations.shift(); + reader = reader.at(continuation.offset); + end = continuation.offset + continuation.length; + if (!reader.match('OCHK')) { + throw new hdf5.Error('Invalid continuation block signature.'); + } + next = true; + } + } + break; + } + default: { + throw new hdf5.Error("Unsupported data object header version '" + version + "'."); + } + } + } + + _readMessage(reader, type, size, flags) { + switch(type) { + case 0x0000: // NIL + return false; + case 0x0001: // Dataspace + this.dataspace = (size != 4 || flags != 1) ? new hdf5.Dataspace(reader.clone()) : null; + break; + case 0x0002: // Link Info + this.linkInfo = new hdf5.LinkInfo(reader.clone()); + break; + case 0x0003: // Datatype + this.datatype = new hdf5.Datatype(reader.clone()); + break; + case 0x0004: + case 0x0005: // Fill Value + this.fillValue = new hdf5.FillValue(reader.clone(), type); + break; + case 0x0006: // Link + this.links.push(new hdf5.Link(reader.clone())); + break; + case 0x0008: // Data Layout + this.dataLayout = new hdf5.DataLayout(reader.clone()); + break; + case 0x000A: // Group Info + this.groupInfo = new hdf5.GroupInfo(reader.clone()); + break; + case 0x000B: // Filter Pipeline + this.filterPipeline = new hdf5.FilterPipeline(reader.clone()); + break; + case 0x000C: // Attribute + this.attributes.push(new hdf5.Attribute(reader.clone())); + break; + case 0x000D: // Object Comment Message + this.comment = reader.string(-1, 'ascii'); + break; + case 0x0010: // Object Header Continuation + this.continuations.push(new hdf5.ObjectHeaderContinuation(reader.clone())); + break; + case 0x0011: // Symbol Table + this.symbolTable = new hdf5.SymbolTable(reader.clone()); + break; + case 0x000E: // Object Modification Time (Old) + case 0x0012: // Object Modification Time + this.objectModificationTime = new hdf5.ObjectModificationTime(reader.clone(), type); + break; + case 0x0015: // Attribute Info + this.attributeInfo = new hdf5.AttributeInfo(reader.clone()); + break; + default: + throw new hdf5.Error('Unsupported message type \'' + type + '\'.'); + } + reader.skip(size); + return true; + } +}; + +hdf5.Message = class { + + constructor(type, data, flags) { + this._type = type; + this._data = data; + this._flags = flags; + } +}; + +hdf5.Dataspace = class { + + constructor(reader) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html#DataspaceMessage + this._sizes = []; + const version = reader.byte(); + switch (version) { + case 1: + this._dimensions = reader.byte(); + this._flags = reader.byte(); + reader.skip(1); + reader.skip(4); + for (let i = 0; i < this._dimensions; i++) { + this._sizes.push(reader.length()); + } + if ((this._flags & 0x01) != 0) { + this._maxSizes = []; + for (let j = 0; j < this._dimensions; j++) { + this._maxSizes.push(reader.length()); + if (this._maxSizes[j] != this._sizes[j]) { + throw new hdf5.Error('Max size is not supported.'); + } + } + } + if ((this._flags & 0x02) != 0) { + throw new hdf5.Error('Permutation indices not supported.'); + } + break; + case 2: + this._dimensions = reader.byte(); + this._flags = reader.byte(); + this._type = reader.byte(); // 0 scalar, 1 simple, 2 null + for (let k = 0; k < this._dimensions; k++) { + this._sizes.push(reader.length()); + } + if ((this._flags & 0x01) != 0) { + this._maxSizes = []; + for (let l = 0; l < this._dimensions; l++) { + this._maxSizes.push(reader.length()); + } + } + break; + default: + throw new hdf5.Error("Unsupported dataspace message version '" + version + "'."); + + } + } + + get shape() { + return this._sizes; + } + + read(datatype, reader) { + if (this._dimensions == 0) { + return datatype.read(reader); + } + return this._readArray(datatype, reader, this._sizes, 0); + } + + _readArray(datatype, reader, shape, dimension) { + const array = []; + const size = shape[dimension]; + if (dimension == shape.length - 1) { + for (let i = 0; i < size; i++) { + array.push(datatype.read(reader)); + } + } + else { + for (let j = 0; j < size; j++) { + array.push(this._readArray(datatype, reader, shape, dimension + 1)); + } + } + return array; + } + + decode(datatype, data, globalHeap) { + if (this._dimensions == 0) { + return datatype.decode(data, globalHeap); + } + return this._decodeArray(datatype, data, globalHeap, this._sizes, 0); + } + + _decodeArray(datatype, data, globalHeap, shape, dimension) { + const size = shape[dimension]; + if (dimension == shape.length - 1) { + for (let i = 0; i < size; i++) { + data[i] = datatype.decode(data[i], globalHeap); + } + } + else { + for (let j = 0; j < size; j++) { + data[j] = this._decodeArray(datatype, data[j], shape, dimension + 1); + } + } + return data; + } +}; + +hdf5.LinkInfo = class { + + constructor(reader) { + const version = reader.byte(); + switch (version) { + case 0: { + const flags = reader.byte(); + if ((flags & 1) != 0) { + this.maxCreationIndex = reader.uint64(); + } + this.fractalHeapAddress = reader.offset(); + this.nameIndexTreeAddress = reader.offset(); + if ((flags & 2) != 0) { + this.creationOrderIndexTreeAddress = reader.offset(); + } + break; + } + default: + throw new hdf5.Error("Unsupported link info message version '" + version + "'."); + } + } +}; + +hdf5.Datatype = class { + + constructor(reader) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html#DatatypeMessage + const format = reader.byte(); + const version = format >> 4; + this._class = format & 0xf; + switch (version) { + case 1: + case 2: { + this._flags = reader.byte() | reader.byte() << 8 | reader.byte() << 16; + this._size = reader.uint32(); + switch (this._class) { + case 0: { + this._bitOffset = reader.uint16(); + this._bitPrecision = reader.uint16(); + break; + } + case 8: { + this._base = new hdf5.Datatype(reader); + this._names = []; + this._values = []; + const count = this._flags & 0xffff; + for (let i = 0; i < count; i++) { + const name = reader.clone().string(-1, 'ascii'); + this._names.push(name); + reader.skip(Math.round((name.length + 1) / 8) * 8); + } + for (let i = 0; i < count; i++) { + this._values.push(this._base.read(reader)); + } + break; + } + } + break; + } + default: + throw new hdf5.Error('Unsupported datatype version \'' + version + '\'.'); + } + } + + get type() { + switch (this._class) { + case 0: // fixed-point + if ((this._flags & 0xfff6) === 0) { + if ((this._flags && 0x08) !== 0) { + switch (this._size) { + case 1: return 'int8'; + case 2: return 'int16'; + case 4: return 'int32'; + case 8: return 'int64'; + } + } + else { + switch (this._size) { + case 1: return 'uint8'; + case 2: return 'uint16'; + case 4: return 'uint32'; + case 8: return 'uint64'; + } + } + } + break; + case 1: // floating-point + if (this._size == 2 && this._flags == 0x0f20) { + return 'float16'; + } + else if (this._size == 4 && this._flags == 0x1f20) { + return 'float32'; + } + else if (this._size == 8 && this._flags == 0x3f20) { + return 'float64'; + } + break; + case 3: // string + return 'string'; + case 5: // opaque + return 'uint8[]'; + case 8: // enumerated + if (this._base.type === 'int8' && + this._names.length === 2 && this._names[0] === 'FALSE' && this._names[1] === 'TRUE' && + this._values.length === 2 && this._values[0] === 0 && this._values[1] === 1) { + return 'boolean'; + } + break; + case 9: // variable-length + if ((this._flags & 0x0f) == 1) { // type + return 'char[]'; + } + break; + } + throw new hdf5.Error('Unsupported datatype class \'' + this._class + '\'.'); + } + + get littleEndian() { + switch (this._class) { + case 0: // fixed-point + case 1: // floating-point + return (this.flags & 0x01) == 0; + } + return true; + } + + read(reader) { + switch (this._class) { + case 0: // fixed-point + if (this._size == 1) { + return ((this._flags & 0x8) != 0) ? reader.int8() : reader.byte(); + } + else if (this._size == 2) { + return ((this._flags & 0x8) != 0) ? reader.int16() : reader.uint16(); + } + else if (this._size == 4) { + return ((this._flags & 0x8) != 0) ? reader.int32() : reader.uint32(); + } + else if (this._size == 8) { + return ((this._flags & 0x8) != 0) ? reader.int64() : reader.uint64(); + } + throw new hdf5.Error('Unsupported fixed-point datatype.'); + case 1: // floating-point + if (this._size == 2 && this._flags == 0x0f20) { + return reader.float16(); + } + else if (this._size == 4 && this._flags == 0x1f20) { + return reader.float32(); + } + else if (this._size == 8 && this._flags == 0x3f20) { + return reader.float64(); + } + throw new hdf5.Error('Unsupported floating-point datatype.'); + case 3: // string + switch ((this._flags >> 8) & 0x0f) { // character set + case 0: + return hdf5.Reader.decode(reader.read(this._size), 'ascii'); + case 1: + return hdf5.Reader.decode(reader.read(this._size), 'utf-8'); + } + throw new hdf5.Error('Unsupported character encoding.'); + case 5: // opaque + return reader.read(this._size); + case 8: // enumerated + return reader.read(this._size); + case 9: // variable-length + return { + length: reader.uint32(), + globalHeapID: new hdf5.GlobalHeapID(reader) + }; + } + throw new hdf5.Error('Unsupported datatype class \'' + this._class + '\'.'); + } + + decode(data, globalHeap) { + switch (this._class) { + case 0: // fixed-point + return data; + case 1: // floating-point + return data; + case 3: // string + return data; + case 5: // opaque + return data; + case 8: // enumerated + return data; + case 9: { // variable-length + const globalHeapObject = globalHeap.get(data.globalHeapID); + if (globalHeapObject != null) { + const characterSet = (this._flags >> 8) & 0x0f; + switch (characterSet) { + case 0: + return hdf5.Reader.decode(globalHeapObject.data, 'ascii'); + case 1: + return hdf5.Reader.decode(globalHeapObject.data, 'utf-8'); + } + throw new hdf5.Error('Unsupported character encoding.'); + } + break; + } + default: + throw new hdf5.Error('Unsupported datatype class \'' + this._class + '\'.'); + } + return null; + } +}; + +hdf5.FillValue = class { + + constructor(reader, type) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html#FillValueMessage + switch (type) { + case 0x0004: { + const size = reader.uint32(); + this.data = reader.read(size); + break; + } + case 0x0005: + default: { + const version = reader.byte(); + switch (version) { + case 1: + case 2: { + reader.byte(); + reader.byte(); + const valueDefined = reader.byte(); + if (version === 1 || valueDefined === 1) { + const size = reader.uint32(); + this.data = reader.read(size); + } + break; + } + case 3: { + const flags = reader.byte(); + if ((flags & 0x20) !== 0) { + const size = reader.uint32(); + this.data = reader.read(size); + } + break; + } + default: + throw new hdf5.Error('Unsupported fill value version \'' + version + '\'.'); + } + break; + } + } + } +}; + +hdf5.Link = class { + + constructor(reader) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html#FillValueMessage + const version = reader.byte(); + switch (version) { + case 1: { + const flags = reader.byte(); + this.type = (flags & 0x08) != 0 ? reader.byte() : 0; + if ((flags & 0x04) != 0) { + this.creationOrder = reader.uint32(); + } + const encoding = ((flags & 0x10) != 0 && reader.byte() == 1) ? 'utf-8' : 'ascii'; + this.name = reader.string(reader.uint(flags & 0x03), encoding); + switch (this.type) { + case 0: // hard link + this.objectHeaderAddress = reader.offset(); + break; + case 1: // soft link + break; + } + break; + } + default: + throw new hdf5.Error('Unsupported link message version \'' + version + '\'.'); + } + } +}; + +hdf5.DataLayout = class { + + constructor(reader) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html#LayoutMessage + const version = reader.byte(); + switch (version) { + case 1: + case 2: { + this.dimensionality = reader.byte(); + this.layoutClass = reader.byte(); + reader.skip(5); + switch (this.layoutClass) { + case 1: + this.address = reader.offset(); + this.dimensionSizes = []; + for (let i = 0; i < this.dimensionality - 1; i++) { + this.dimensionSizes.push(reader.int32()); + } + break; + case 2: // Chunked + this.address = reader.offset(); + this.dimensionSizes = []; + for (let i = 0; i < this.dimensionality - 1; i++) { + this.dimensionSizes.push(reader.int32()); + } + this.datasetElementSize = reader.int32(); + break; + default: + throw new hdf5.Error('Unsupported data layout class \'' + this.layoutClass + '\'.'); + } + break; + } + case 3: { + this.layoutClass = reader.byte(); + switch (this.layoutClass) { + case 0: // Compact + this.size = reader.uint16(); + reader.skip(2); + this.address = reader.position; + break; + case 1: // Contiguous + this.address = reader.offset(); + this.size = reader.length(); + break; + case 2: // Chunked + this.dimensionality = reader.byte(); + this.address = reader.offset(); + this.dimensionSizes = []; + for (let i = 0; i < this.dimensionality - 1; i++) { + this.dimensionSizes.push(reader.int32()); + } + this.datasetElementSize = reader.int32(); + break; + default: + throw new hdf5.Error('Unsupported data layout class \'' + this.layoutClass + '\'.'); + } + break; + } + default: + throw new hdf5.Error('Unsupported data layout version \'' + version + '\'.'); + } + } +}; + +hdf5.GroupInfo = class { + + constructor(reader) { + const version = reader.byte(); + switch (version) { + case 0: { + const flags = reader.byte(); + if ((flags & 0x01) != 0) { + this.maxCompactLinks = reader.uint16(); + this.minDenseLinks = reader.uint16(); + } + if ((flags & 0x02) != 0) { + this.estimatedEntriesNumber = reader.uint16(); + this.estimatedLinkNameLengthEntires = reader.uint16(); + } + break; + } + default: + throw new hdf5.Error('Unsupported group info version \'' + version + '\'.'); + } + } +}; + +hdf5.FilterPipeline = class { + + constructor(reader) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html#FilterMessage + const version = reader.byte(); + switch (version) { + case 1: { + this.filters = []; + const numberOfFilters = reader.byte(); + reader.skip(2); + reader.skip(4); + for (let i = 0; i < numberOfFilters; i++) { + this.filters.push(new hdf5.Filter(reader)); + reader.align(8); + } + break; + } + default: + throw new hdf5.Error('Unsupported filter pipeline message version \'' + version + '\'.'); + } + } +}; + +hdf5.Filter = class { + + constructor(reader) { + this.id = reader.int16(); + const nameLength = reader.int16(); + this.flags = reader.int16(); + const clientDataSize = reader.int16(); + this.name = reader.string(nameLength, 'ascii'); + this.clientData = reader.read(clientDataSize * 4); + } + + decode(data) { + switch (this.id) { + case 1: { // gzip + const archive = zip.Archive.open(data); + return archive.entries.get('').peek(); + } + default: + throw hdf5.Error("Unsupported filter '" + this.name + "'."); + } + } +}; + +hdf5.Attribute = class { + + constructor(reader) { + const version = reader.byte(); + switch (version) { + case 1: { + reader.skip(1); + const nameSize = reader.uint16(); + const datatypeSize = reader.uint16(); + const dataspaceSize = reader.uint16(); + this.name = reader.string(nameSize, 'utf-8'); + reader.align(8); + this._datatype = new hdf5.Datatype(reader.clone()); + reader.skip(datatypeSize); + reader.align(8); + this._dataspace = new hdf5.Dataspace(reader.clone()); + reader.skip(dataspaceSize); + reader.align(8); + this._data = this._dataspace.read(this._datatype, reader); + break; + } + case 3: { + reader.byte(); + const nameSize = reader.uint16(); + const datatypeSize = reader.uint16(); + const dataspaceSize = reader.uint16(); + const encoding = reader.byte() == 1 ? 'utf-8' : 'ascii'; + this.name = reader.string(nameSize, encoding); + this._datatype = new hdf5.Datatype(reader.clone()); + reader.skip(datatypeSize); + this._dataspace = new hdf5.Dataspace(reader.clone()); + reader.skip(dataspaceSize); + this._data = this._dataspace.read(this._datatype, reader); + break; + } + default: + throw new hdf5.Error('Unsupported attribute message version \'' + version + '\'.'); + } + } + + decodeValue(globalHeap) { + if (this._data) { + return this._dataspace.decode(this._datatype, this._data, globalHeap); + } + return null; + } +}; + +hdf5.ObjectHeaderContinuation = class { + + constructor(reader) { + this.offset = reader.offset(); + this.length = reader.length(); + } +}; + +hdf5.SymbolTable = class { + + constructor(reader) { + this.treeAddress = reader.offset(); // hdf5.Tree pointer + this.heapAddress = reader.offset(); // hdf5.Heap pointer + } +}; + +hdf5.ObjectModificationTime = class { + + constructor(reader, type) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html#ModificationTimeMessage + switch (type) { + case 0x000E: { + this.year = reader.uint32(); + this.month = reader.uint16(); + this.day = reader.uint16(); + this.hour = reader.uint16(); + this.minute = reader.uint16(); + this.second = reader.uint16(); + reader.skip(2); + break; + } + case 0x0012: { + const version = reader.byte(); + reader.skip(3); + switch (version) { + case 1: + this.timestamp = reader.uint32(); + break; + default: + throw new hdf5.Error('Unsupported object modification time message version \'' + version + '\'.'); + } + break; + } + } + } +}; + +hdf5.AttributeInfo = class { + + constructor(reader) { + const version = reader.byte(); + switch (version) { + case 0: { + const flags = reader.byte(); + if ((flags & 1) != 0) { + this.maxCreationIndex = reader.uint64(); + } + this.fractalHeapAddress = reader.offset(); + this.attributeNameTreeAddress = reader.offset(); + if ((flags & 2) != 0) { + this.attributeCreationOrderTreeAddress = reader.offset(); + } + break; + } + default: + throw new hdf5.Error('Unsupported attribute info message version \'' + version + '\'.'); + } + } +}; + +hdf5.Tree = class { + + constructor(reader, dimensionality) { + // https://support.hdfgroup.org/HDF5/doc/H5.format.html#V1Btrees + if (!reader.match('TREE')) { + throw new hdf5.Error("Not a valid 'TREE' block."); + } + this.type = reader.byte(); + this.level = reader.byte(); + const entriesUsed = reader.uint16(); + reader.offset(); // address of left sibling + reader.offset(); // address of right sibling + this.nodes = []; + switch (this.type) { + case 0: // Group nodes + for (let i = 0; i < entriesUsed; i++) { + reader.length(); + const childPointer = reader.offset(); + if (this.level == 0) { + const node = new hdf5.SymbolTableNode(reader.at(childPointer)); + this.nodes.push(node); + } + else { + const tree = new hdf5.Tree(reader.at(childPointer)); + this.nodes.push(...tree.nodes); + } + } + break; + case 1: // Raw data chunk nodes + for (let i = 0; i < entriesUsed; i++) { + const size = reader.int32(); + const filterMask = reader.int32(); + const fields = []; + for (let j = 0; j < dimensionality; j++) { + fields.push(reader.uint64()); + } + const childPointer = reader.offset(); + if (this.level == 0) { + const data = reader.at(childPointer).read(size); + this.nodes.push({ data: data, fields: fields, filterMask: filterMask }); + } + else { + const tree = new hdf5.Tree(reader.at(childPointer), dimensionality); + this.nodes.push(...tree.nodes); + } + } + break; + default: + throw new hdf5.Error('Unsupported B-Tree node type \'' + this.type + '\'.'); + } + } +}; + +hdf5.Heap = class { + + constructor(reader) { + this._reader = reader; + if (!reader.match('HEAP')) { + throw new hdf5.Error("Not a valid 'HEAP' block."); + } + const version = reader.byte(); + switch (version) { + case 0: { + reader.skip(3); + this._dataSize = reader.length(); + this._offsetToHeadOfFreeList = reader.length(); + this._dataAddress = reader.offset(); + break; + } + default: { + throw new hdf5.Error('Unsupported Local Heap version \'' + version + '\'.'); + } + } + } + + getString(offset) { + const reader = this._reader.at(this._dataAddress + offset); + return reader.string(-1, 'utf-8'); + } +}; + +hdf5.GlobalHeap = class { + + constructor(reader) { + this._reader = reader; + this._collections = new Map(); + } + + get(globalHeapID) { + const address = globalHeapID.address; + if (!this._collections.has(address)) { + this._collections.set(address, new hdf5.GlobalHeapCollection(this._reader.at(address))); + } + return this._collections.get(globalHeapID.address).getObject(globalHeapID.objectIndex); + } +}; + +hdf5.GlobalHeapCollection = class { + + constructor(reader) { + const startPosition = reader.position; + if (!reader.match('GCOL')) { + throw new hdf5.Error("Not a valid 'GCOL' block."); + } + const version = reader.byte(); + switch (version) { + case 1: { + reader.skip(3); + this._objects = new Map(); + const size = reader.length(); + const endPosition = startPosition + size; + while (reader.position < endPosition) { + const index = reader.uint16(); + if (index == 0) { + break; + } + this._objects.set(index, new hdf5.GlobalHeapObject(reader)); + reader.align(8); + } + break; + } + default: { + throw new hdf5.Error('Unsupported global heap collection version \'' + version + '\'.'); + } + } + } + + getObject(objectIndex) { + if (this._objects.has(objectIndex)) { + return this._objects.get(objectIndex); + } + return null; + } +}; + +hdf5.GlobalHeapObject = class { + + constructor(reader) { + reader.uint16(); + reader.skip(4); + const length = reader.length(); + this.data = reader.read(length); + } +}; + +hdf5.GlobalHeapID = class { + + constructor(reader) { + this.address = reader.offset(); + this.objectIndex = reader.uint32(); + } +}; + +hdf5.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'HDF5 Error'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.File = hdf5.File; +} \ No newline at end of file diff --git a/icon.png b/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..75a1a0a6a04333b4aa936e5c76380996124e7045 GIT binary patch literal 58106 zcmXt9Wl&sA)5YE0-642zcXxLPiv$P|EVyfMcemgc2<{NvgS+eEu-Jb4yj9<=+P(YZ z*3L}dp6+u__e5)`$)lr?pg=)Ep(`rLXhA_iLw-U-At69+Cf_S=ufOTT>=5()8jc%sN+OI{f9r6)-N zT^LGRtYhe0`(Iob!;(D#%twZe+j&R_vFRaBgB2Hk+{^3Tm#L1K)C@; zv{;#ws=jThncGdm{~kbsJdhPRha^U6Nl5c(H6MR3OnE?S4R)5b+?#o7Ba+iZ0t@8Y z5ZOZ6BFH8Gl?+Mid6fzySJ?;iQNsR>tiXz8=*G8r4S0>-DCfL+UBDaAjve8|_=2>L zwsYTa2S-F_mUNtTzd0ddA96~aQTR4FV4iLPdXqVu8J)g!y3X;-Ab-x|@V_SZ{y3^$ z7Kp`;;< zbjan9t@o8kPG+Gc3X29#fjSJKFtrKFm^rP5jWu7XY%d*vK4R6lGCQkb?(7Vd@bY@H zu(dtzE0T`1nh|?-1lE~$B8*|RU9zU=FH^xHZ5@9h`2haC%Yb>>cS+`)hEgM!qj@Pb zqDNq7&N|`TdD)8Wb~I^k_w{S`%F2pBZB32iS2~<5J0iK?sZCM~ zEn3tv(W%@b>1jGj^lTF(O>7RcO*H`vyuLWmLyf_~!L&|JPLVk|ISH4Woo(-pqZGnD zmV*(Q#-6OR3>2>G);!@<(hdHn)V&cB!LP0Y(2!Tbh*=_db^GfAgk*N-fBfs>_9(}> zuKoRSy<$2~*lX=zEQL@ zm{)G^NskkKw6(RB_vzCo;irpLx95!Bx5FgceDCrcw7J)F2QDG$YjN6{?H!;i6Ar{% zqWkUU#R^I@;MpbRocONVkMkAzw|?wThNBXi8EI=bSGKgcms=90l%|$}P?8h+Q2V2? zUdE2JnVi>K5w(BmoX^hA2G`ZqsS0~utlVz6&5%D&d-W`!l>Tjz)Zjtl4llIftiz9m zhWNo=VPXr1+`ucnzfqZakDvFHy!gwUcZPWYs2CZSf$gVUi5uFW9STlz!7M6HVMck7 zwYVmN2Gjf4&P;|pOWIco3W}}ut*xGK3+1XgA9vzlx2FYl@lDm%F|0*2xdp6|-ytbM zt_Ymmu` z&I}Z9Q^Fhj3L>d9qhZ7(%IhUVGcEKpzpTtLyckcx2l>NlhX4SYw==S&BIVm63P5; z5nKe%p+J=d$J6uk^_D>Jhi%~N`gYIbLw(>o69&)q+9Jx^C*DD*RW#@kVo40h2#!oL z&%;>V5$8KT+pk;t;8XQLpE3Fjlkds^z@e7uGNX4Ef%LT?OPBCBc)@ud;Z6IdhZ(v6 za9g>3>*M`>SC{|mV+DoJNs&Hf;M)xbcf6K5D^VPAp8zcrWKZ`6r%?L=Z^;aj3f>MF zGd5fX(c2c6mpL#a2b;Um(zKY$MshV!`|ZL`IVM&IfV~`WA5YOTR_5~ZqYE?Q;5Tir zE$?GJDz);R>2b@FphcjyApj)?!yw;6+ng&W)GuuyV8cCKk zx-^Ak6$S;mT-G$DB$YA+`jpJ_aDd@TYn&875C{+ROVM3bdcgy2kjk2fNG{4nK!y&d zpHqsc2utp7A5!?-!^S0@n(ce2J#O?+J|rrQ27o5aKR$Y1uY@HSMl9&$H2xYsms`U) z%ONDHZ(`!~^ie++4lVaQbpt>7LHyt~e)N{L{ZRKD%CjTKZllK6I4x9zrX zSiSh$^n5pa0Fr$LEBUtXMd| zSlAM$nf`r%Dq5D#=wB5O)uwuYM;NHA%2bU zAS>h@)d8d}HZ-b&jsgxldB&XU!-RnQl8LRm{HHe$C#OpkW`JPs+M=mmkss`8s}sV@ z>*@JrQAu-3-4%sU1dcnMichK}BlSy|q{+#U>94B_*kIbR_q54h{#ZJXuTYc|iWT#x zVRs7FW~59s<21!8EV{`?X~lA-l?f+{8DW*Rbwxcr@#AM-$H!R{wQQO=nndNlly14L zw|@C=(m(d183SG#6Z|jGxz!{Ruru*6*lA|AQ3=@H5j0{ufKMmqX4!}oKPw3NVvENgHod^>V3azl<;!% zaj)DGth$#4;R@dUcOELSh6>85c2jlqV=B3swV6p2H0p6wV(nCz66Q7Z<-hF1u~b(h z%hL!t>EV57^1`gQzOJvYQ@$Q@1cFk0f!$vG#LU2Dq>x})XBMX&DI2Kp(w>56QQ_^r zywTp{d|!rb$U~I&U%z%pFFzQ$l=AEy9QVIJ4N6PjyL-&9t&KNMe~ZVU6R;sA!^M6H z>o4S-&##&Evre2%1A>$CZKYlC2w&Kr#WLpbBDX=G<`?OLaR6Fn$trZyGFeti_L<1P zmsIjavd-3ois|X6y*>;hQevgAvk*+>6DZz6u$Us(%|O3n;ZN3kNLto!u>B_k{Na2q z)Z9UHr9H<>Gg?o!Gx?^E&-)3z&Q}}lfutzmHUneTFOoT8Nl^=%bBfB!JX4o>WW*}2 zI^lv0#rMdec=ODq8ms3)uq@tOa2fT~I@Ixr>@EQ!6|lX7Xk_JBT6~8r!`7cwQWUs< zPZs{_Ngq>HUHrF%ixRmBRG*ACh{C2z8H4TgynOi>Ph+LrZgbua$`P?6_(47Di~_OY z7bwYJ=60Z3HMy6ckZ5*a%fIQqAbNOs=o~qRxS^0dS`je6zM$&h#v^ZJBuJ#}vVyTd zQ*w{}eaT0@ECZTU#54=0)qB^yWb8w#^)73a<61M*WinhTS*0~Hivf3%DS^}DR)*p8 zXH8S>pI9_`;4uUe@25rs$A@MJ2Fgog)vu$MzF=5wMvw#t_0kZc;r>Y^3MzO;^?vf$ z1_U1Q6xhGK9gBmW6 z9C+3clw@`-_Dml9M02g%2+B2mqxenC^i?;+{%cfL#PDPXOSyW1+~AO6n7>hfbci{R zwh}ry1Q&oi!5^=I*R{_6)x@@bnLQ4rt08`A0vx+i7EmCa55LwPFJ!6~o2hJkeKYSv z+wR28l<2u9Fh$g`o!IwGSo!#-Hje24{4!Y?Vi_-3Cl`Ek^Q491-zzLtHR2@!`kb5X zaoa$CreUil(IK{oEK_2ee=W?ss(EUL-t9TVzaETfl2+OvC=C3R?NA4TO{CkWyXOfh zblwT!LZ0?Q;LIKe>OXn(ee4nT3hZw|JiV>8)w2vsYNFzbkO%X!`~33sX!Zm4!r>cW zrooC5&@#cv43}NKXNy#nh;d1cgk{ti!f)CK1DyHthSHEKAwO5RH)$Evt~qwWoRTT# z#-O|O`CGbJ#V+!Y<)5{nrPWo?Nhqe+bKi3B$3y7BS6B!DdjBl!oYe=Kgp$(+b^j8b z8LY22Zo4x5NyzbG#e|oQ!B{89QZ|3LEV^lWvE7*=IyJhQmUxkJcprtO6nu{a)+sVj zeRJ;@3^RvrbrFz?Q{pxr&ZKP3TIUTmbUV^8)jp>aMPBGTrW0v#-5;svFmAm_utpLe z{)-wn?Z8kNmw%8OMs2ZP^$IkwBixjorzM6{uP^p||^8vo%nCJ!sirqf79u*)W5c*Uc z7OEHZcxC33KtV%%URK(YYRBs{;D9djNn`n;s=U11SPV3#{?E(Z-O+nLjz8irTsXYy zl%K2!aL13erMD|w?*gP7@bH&5fvx*6-MGg@OY6H}?w_fXwwtHR)v3C{DaSa63+%)D z65*(YvfHl3Z#|>}B z1KR6Q{fwfbqA7LJheiEu@W)f{1%W$VN}n@kS# z%oBdPgJi|$wl$%ohm&ZT+|bv@#xL8@tr#qx<0w9yCh@pL*H>P{*zUoP-qiMl5GSLBt`r6~N z!OI%8^k(DKB#Cm=9gf@zyw{)SZmbAZNyf~QS5e4flw*ufHY31afv$A&vZ=I+aXpV; z(8wLAarl_(|C;%78i7Lx>3CI9mzS5#!Tf=D{J6e8K73AVE%s3+*<<=c8{_#UE@GTP zI|9f!y^;~n-p{*{9IyXH6AcfK2UU$OuNAjzZ_ke-RZ_!de~#*i!7geRVk{${53x2o z(9hiMn zh)NRHFKEuiM$~^=q;S6}c^I9pB zuu8RuxIh-7`h!> z5{)Q=5L6aK9?z=lKlVQ_o7~B_aWYJ^NzQUWg?mv7RqUA9K!Kvf{|F~|5kuHSm+#g_ zJD92zZM}5?ad;XQ*1ff@+2fqR%=z3FH?8HTh7CpFWVMc+NbKjLxP31)H_2|Lrl6>>ukHiueU`w5V_?DTh?~tiEwV59;Hpa45a}1Xl4LGWZu!LSRaUnb({Tg95 zXjYJ|*c}}W*I6*?o3YOFqP3Gw@<(~!57#M~Ziq~=rtY_go5s=mcIz-THMNy~nCR5Z zG!0wO*ZvnOhB^Qii}K?-k~HAvr@3EqQPFNalq=2ogQ#)fphJDriv((QjS@z*9l^W_ zkrRb4Zx^FDrdT|9@md)VMY0pgb%>YfoAdD5x02$jWV@D9s2<1bT6R204*dBILbQn? z1J*lkc6dbHr6T7#pjpuEJ^8*IX7t>b*>69O@b?%S8hXcj`l!?S2NG?{@=R!jDuNAY_3D!lRT0OV(${Y_Vs_GOs1q&)WCRR|xIN}hb(f0;P0Z*@G zWo7y=Wj)T2ZwhX5mNL35g@(fi)vgvN%s{pD#ta<11C`5uq+5jp-^{!Kx+j;$A0syf z6mMJD(xb?3BJ_%6!m+WK8ru39Tc-jOVea6=P!|Ca!0CLM@>d6kW%2j>g}hu_$hekJ z!&tr0*ZY*<=iAP!j&t861#8Qqhuu>Z|LmaD=V_r-29vQb`M7G4x3RUJazteVtF@%)3FB!r)gS{#oi zv+BWDUcJK8-a~NH@CX`gH`N8;w`2XyfY(bfBN@ibqx@ss(LJuztlt6BxtV{Ot|;HR zEIl&)oX8S;r9Dd9D9Xlhs>rj!!DGvHnL4J9`HSnDEn+WRveb7S*=TeOOLB1)Z+m3? zw6t$o-}xiPY>m!3%zWo$3A;VEy1bo5KA!hK}-Qu+;p)#)hf`f zJWu9dLC98Ru176ot+H=83RHG_nmF&GvmUV>FFrIqcc{4+C^>MD(K$^1Tz~TL@Hqdt zuMqd>T=hZHh4P+d6Zqo7MXpbBX*fYwh%d2*-_yMDqNVM4G0V?c6!`9< zvye$r-Ew|F;cfKMXq?OyKF4uF_9W)Bs2%;ljtk7HYih?DiIS4i<~_)^;H{B4!jhNW zu*rHi7#8v4KdHph+(T!_Stut2Uj*HSffxdp-;=l1-(DbjWjqiP12@cdbv>ku@Qpz< zA3|&E2CuiQBHN5B<>O7BXE*85KC%k(o@Ko=S8qG_+Aef9F=^rjJ3%C)fdTu3C~GpA z1|rdp;!>)*_y7Q)=bm;3BW!S&c^UG`79l?Cs;jK5JpOc6!xPLi<|MN-zB)-u5_k6c zc=>u;s+e-c{>V#24d*nP(D<0P{6nm~IdcB~b`yc68Jzx>>V)N z<4>TjV^zQTN3YLgxi?gtfsaaU=j7!7{DJkYIg zs!&4+;VMrYB;=P)B`8dP?T}I!R6867BhV?`D1BcIp6#g>7%Rx9 z!;p9M-$NrkN-JK?e(Z+h|A%o2sl&Lm?3`Ptw#TwP>aR^yY!g@{pM*ZyIUe+WaFdI_19dQFZbMdx(F-TU6;;L-`&hELyriLJbVkRrgLa>fOf3T;|HO)&%D|Bu^S{sZF`oEVoc31kd;Xm$?h3*?eelYA5jMBV#Y#yBTxuDD!bT^>z(HHRwvS1{5mXL0Vfxs_Mhf2ub5ZYQQ{`L zy7+4lS*`aoA74O2=9F+AQm^H|TEjt}(TLT`arGO#e5vB1!oN%cVbg#&3{By+GHJeV zuQUy511(OG!l+30$vCFcxFMwg9&4jaCCgP52=%#dgxdw~sq}0$&n+A0ZeId5YAlkX zT<^k=Cbk~=Xp-zS<0T=k>g3e>UqA+5TwbceAakopIvY1G^pvffT0u$fJrzvnw7#}E ztv}xUWbZg}DEXXOL2Qsd{GxAmv?`rf)6B|)pq)RH){DoYp_!EAVhI08Kkh@>Kzm0k z{B-TBZH0obPy(4{6CQv%&igZft%miBr!MpFGRuMN=7;t&yin$!`>?K_p6CtDJR|)d z)GEf73a9hEf2mLj7;Br%|6BsmH`~7YZwS<;S7)$GFMG%dMGo{jZ9ZPEgKzeJa(rO& z3xg*mu8EppK8t=9$V2Pv3_%>2gT#+mgFka^tG~yuBT$>)@nNKy>K1(3 zEw^;oja&XeFtUE-gXqgiiC4gND5c4`{yUN{INJ5lFC>jCirr#I$C3@Qi+Z^uGa6o(8|p2s@&}~<4xrxHhSqU zrOr`xyLj2lfuPD!-&A^8j;=@kw4FaFxK|D(Qjsqa5oBE;UTDW?vRzIQm1tNz&r zz)5HfsuI#vqWE}Al}m@Pmhu$c2_qx-VD+LuyU`Adrv*gNaE*zxw{w5^X2v8!yV+2--@fFR!PM& zUJEe#_mzt*U)$>JT->+TMk$Oj)@M#X@SG{G@|D-lmNn~N#UH8Z0YwI0U`J1*(6QBx zR_ca0kWW?WtF2Er#=4vWaB-_AxIX2S0<`hDp6{2MnJ~}a0=8WtTDMQrQtgj_Z?P`X zhcHfos?0)Z)7d{#V^njPKCzZ`baXIPSbX`iA8GD$TIS*GyS^UqvlCWt2c?Ss3(OaL znAp-6(xA7EV=?~yc;Tz(_`35Y+Rrikn;9w$-VbRR-bFlz(+|Fb%)N4S^Xf56nJB-0 zzzaMufeM`7pti;pf!zawO>ejv>ib7o=yacK)x#6(<~H#6*VSu}UuU|fSzi<8K4aMm z*teq%FxVTR4QZ2|UfEt=5twawhv|%4Jr0r3M8w;(@-nykN#UD@x;}bf(u7ODVc=e$ z(R2IM+$s;CR=;-#Z`Zm{S|>LJt1+nO$CjTDXSO#%Y~S=lqCfx;%et}^?>Y7ePAM@?~H%K0&IiCDE$0keX@8F z7}OqVeyC_~$Op{Y{EJAyo8-=~!^JzcwmIbo;&)haX+Vc3%8qb%3f%3?{UshaU+>t0K$l{jXnn72E|`j3`}2Kdh!>ZM%bg)z z*1)R;mZ#hMr+O>G;_ZSKm@2pIgHRxK-G(&vu}xc_q^P63T!Ol}sI=7j!(-XB?P*l} z<3{Z^62&)jR0fp@6#+}YPa@4zk~0UaeX>m$kaAIn%s0qu9aT{-O@S`sg!fJq!^=?D=Qjv9YLThXHGbQC$tQU4mL)^{2~Lpnq11~?hG~e?XK+aVtzMxSkqfb zp*MKuzJ8w~gI+8g#$iFcHY!QAK;r`8I_|vy0 zPTC9SlNO(*r1g1Y@PJu+UVb*A)Z?_ntoz4})nzaug@NqIKcuhFUtlUa$PC~D5I;lx zPb$lSgw4o#Wgb}S-u}bx*T)MRX$Q>p3mpF0QSlsSc?N<}O zW5R&6GCKAg6KO>SY3A9_akgy~)_1!YQZFk80o11ZH|V}AzFnhv3_2?H>;Owsc;9{>cG)ZEyJJgmVw*XhwlqSx)%3K9Z!ZJYtUL)$WoGvtk-GcA{0l` zbuEi(6_b$LsLKH-u-|h^pTjS1v+duJ)A8{&T;_I5)u~MfkZSwKMvA}fIziz2GT#1!dYP8vW>dI6qKA-<3#xN?9BDa;gn-trTDixq z*L!<{IrO+ELV^=3Lz-+Q2eRNDNUQ1T2S4JP@0{BF6$e}ggJE+;)`|-bHQDI?K!nmj zw#@2dlomT>ZvCY=P5}CehEP5FndAB`f(^^(fehlQ!kU_kY|8f&dEnp;vyV|wo9o|9 zJLoT#FweT+`>I|Dr1ZU&B{aQn3;3qbiAyce;DwnJ|IjcIPp~okB=hwbRrMeW0}5vV z*MNcO^0lv&F6b6J2*3*2!bqKUfvQTLt)5)DEo!FLa1yv7j~~=VzE*Zp#dwQp5xkV*0ZJCU*3OmD=zF(;po9msAHPNj!l;s(1pe_vrK;^{DlAmS5kaza zQ(86hia=8E7wy0bYypSMz~X0k3zn5cHo!z(_xxB=txF=IomPM(hI;}JwH%!6~zG9zxe8N=`-0+;y6A~R>a zhZ7YV@*Uzcx^MY9u5{HYk_h$R^Oxoalwp_(0sB}5GG{lL*dP?HiLS`rFg(}4T& zqsys`N2L77%fPDV&$%l>=v*u9{-j%q`hu&7lMu1HJi+fSXFC_T_3{o)@t&`VTbOwN z6cTtq?zM5Fsk-p>ebnpt?&CkkCN`dawjwc$D{e=|q)r4&B53OSMXJ@7y1sAp~t?x`7q?*d2v$aNC-Y+%N=Q5eH*ZG3}E z8g_JJW)jXbr^%zs^?dtA(y1Y&zW0i}P0tNrjYzS1F*e23NtadM67ZhCp@VXTebsc{JD_a;S@{J5SuJ@$Kh?wE^yu7?Tz3(83|C#J2 z=lQfFL)>RKZ*JWF{V)$AupczPhiV2PJ^}aRrX?|qEE$jH&NEe%k2ZWWG*EW&`#In@ zC5ap{>%VjX;b1iFEOG&Fty=6(zTQWBUT6y{M@qk17SoVA5F!-~r}fOG6w3eU78YR8 zO9)OxFfON7WT1?y{){jB!}ABmC`kSpWqS2dJnvZ(1$~%Wcrv|C660!HKb1pbp(O@V zP}@?pNq16d3DV|*4v`cW`L9H-tFK>wjdEEZB#t40)ZZr&OaA)UET|iDSbAG@E!Zj& z8{*DVFsWRDYnbucMOA@035zKBXgE+{lbGK!`8914O1aJHg*5US9%Wr6DdwT+xKA=d zn*k}+I|dA5;9Tp#mN*G_ZVG1=QURn#V`Ah3O3}JUD=60;G6W7I7ao@|^w7 zQ^5gVS~hg2$v6{6#(RDLQ-Y|9;<#>O(`1oCs3m$@$@8^FSSs1 zV2n;x%(e%xWG~^sPmxlZRK9a)lC`;So$#!-`RaR;h^IV>F7Vb~{5LP8b_NNJL#Ax( z+*srtMnCljeN55&*@5irY%fX6obA`m<%Pj%Y5CRs7zDUp#zO`1nTev%+Ppgxz+)Tj z_Nz)qF0@a?wJ@}>XF8+7pP@LKFWVIp`{QK%&cM7h6)fPXRl`0Jtf(m(=BPar$F8(8 zhouxCi{}ee7<$~3>1RUIcvGrG`*oFH%*PQwD=b&}r21Uut>2UQDmr>Ram8#BHNWe$ zHeahQg15ZZCz*sX$;;X*d>4@)j^GLMK8z#%w;=ZgC)SI-V{ z88`j!H1zQ#1wEQ+7ca!n%Tc0}OKNO&?%IxAb<;q4@jrKZVjc^|mF%ppned5CY+Ky6 z-27%DX+OpfboyEU$!~db#pwbJS_cwIoLk%*I=cx?V|YuH*XN$kQ=o-yb4@beDTg>T z-PcKs5iGXzMe*S31z0XopQ)f<_UWQ1W6aTsT3+8J#cx^7LE|;X-`WpZwZ^q({zblJ zOE`P+bM!XGEe&dF;TaKSueN9&`kVg=3ZE}9n1~hqh?N;jkpcu}eZxPbWDlz4+?S?% z>4<)M;uH}H)8e}$aroo5TK1MeT5q+w@k>UU+Hrk#exk*()Sc$uo!Nl1Jzo_uVPQ@@ z%rki{2qw9F%$IQHsJ^}%&scC;+<4W`Yw4Wqid<%aH*9f(i>jX#b)eR?2jk`wdC%-i zAl(~R*&G(oa(qY*(okS;lbMe~=f?4$`mZ*}dwHr`t@rT9@EMcfAx2XtO??qDQT?TQ z-MRN&&vf}*bdQ4p2S9hW>mr6v3K_tzqR-DX8oN0><_YW@#QNW z2Wu9(C>fp#_+VMAmm(k%pNULc*pi06zps=J zM((}YY}$yurfkn|%w2NA8gms@rPkMUMHr&%(rGbXVK$`E0F2mrtr4EO?pPRK)CH^2 z3)cRGF6Fd!8n+?)S#4X|1Sp=?H>(eLV@|Jsh)cd{Nfy*LI%zZn0?jH^XftQOnMl0< z*IZKl^HTwV25&E`9PG6^2b>pZNw3CW?)oMn@U6O|Zu!XXWC5-VUCex|D=i~}hAv$F z1@UK%-;8|6Q$Q39GCZgNu~%60?bKcJ&1^5|{u)UTF<9K(>#sMhEL6JeS0=fxwx2RJ z2|q+66>v>Nzu-849r9Scl`)rf;=W#bD4(mU5#+F3d?GX{zI_-f8uA9Sd zbEVDJZ9T=Q^v#TOYdn2L@nWgt=m*FlTJgl#e7s^^j?@J`hf2lN+S5zf$C38!l6bp%(>C-2t zjV|xI6yK*S^6xy8UIBfTv4sYZX02ONsQ-%F+n@bk?uAiY*}%=F!mW2!%9Oq}E*8JH z?LL_*^eerN-HFU(h&=~bzfmBsvwUIo`6)Xey7*LEGG@E(Czz=B@rC*7uZ>Vr15% zxD3FHnT>{l(PaIog9S6u`imv{IzC+c7Y9dCt#e|2k$~cY=cTJHeJ?q{}zi{(TdqDZ5DQ$IS zumTOZ4w86?et}i3;k(2>HI&)SP$*I26tcM^H z2iMfqVL)_$mok1NlmI3;*v5cI3VTa0mWv-9;l19W+woc-_mScAf-|8oLbQ?1c`hFw z#!A8V#!T?ksup(1w~1;7DF)*@#CW3&SNpt0FTJ=J@u>jZIeOGH)<301A@Cmf zyy@~$B-55zO3cZv#lB4aA6L+=zn>a^*Cy2M(q+3~Oy9d^nTWE9USCcs!+WwQex0R$ z(_sk*tn;R4xC{;uZ}@;r%9r&^`Zj}?K__v8wQrvL7-nl8|2bYns>~qvy&bX~9g0+l z76vWx2_s;Y3>ZfprlkA@o=)ZGyi1e=q1quip(=eouVQJg8#FI-R0Nt-M}+;lL96O` z%WP@0_H~3PB4~@!{2=~3b@;>#+imA=#<(C9?hCujP@sJC`KV*ChxeQ>O5{?pMC* zx|@Cw;I&VhfRnRE7`B7@IJ2G-Gz)xx0M7H)$^cw}8zBrh$d8m_16Mi%gN!P4?`!V4&gNwr+c1+lsW8>iXAv)#xN2NT9j>NVH}N*NS0YI_$bjUhTS@ zUsb89>uV*~HCd)pT(^GjDG}+hM6ACMAF!ZNR9Ik&mg6cevrDQhN%qB%0SFHjC~=h( zrK@lIkY_JyFufo|Ae{=~muYIH%g(2rX(=fUaGTnc;oYQ0L-DG1oP!a)9p60zZr+SK zUac;ifV-?pu7dzng;JF*Zf6AWTgC+Fwq#~*OFZi2jLOJ3fgqj=ukyYhnw{V<6NzPI zCh-&eXd$?Hp#XHl&mAC-x)ON6#A7oeT_b@?=kfC0= z_euLqA&cO(1EB%m?i(}D&0G8`tz$TGLdpq{dTUwQ&kk$7HB;MsihP3oFh3{VWvc5m zwY1`vHzJuCqK)>ofm`CuYrOOPolKFa$l`G7Drrp2cCiSF?U}==KJzMY6G$sPM?P)! z?X7AzI_B(J$8dbyb5WSKA*(L@V&*8}b+kCi_EpK0Pzq*;@9;gyuwN&01Ri?jc;3yr z)i?T>^Mno9!9?I9|GN|&Dy>2}QvF6}Z=~|8`Y5>^*->OsO5G7jI8BCU0LgSljL#%G zR_0oGL8iO_pGCPcR|RalO~?;3`uz<*)Ep{QNy`C)yS6-c{OpX47i5D7(Ig-Kv%Bvy z@d*ezMoitJH2r$(P23ML%rg@kZTGlYa$5NI zPCK~efRT39(b)B_M((P^N(etm<7JUdRSB{^8C5$FB2Hq^an-X=vP+xjq@)4JWl304 zNGc0byJ3AOJMI3X&$}=0L&@d1X#1^^O-_?Cxr-LDI)0ft<1))|gsN4?qWY8j+1?A4 zEYXQgdGI0~izgIhRuA!^Z{PMYJX>17tnx*a7ljWJ(E~8%d|AF5Wee5XR3xK${Z3J! z`vG3cxW#03RtW@Cs@LzXtJg`Tgt*9}$9-O6%hVX$Q1no#VW|*tgrtu!`#GM}!uHOB z%UW`>i?a%je3@ll#^TvpeeQqK+(}d<+|LDpuA;!6TPv0IfmvS9HqYyX1d)lNc(Z|B zq@--2*kl~Is{r^dE^HX{+#q#_+c*tdzTJaSiA2YlgJ)KP-d}Kp#ID~842O-kKRn+) ze#0n7E>-DPiv?_YEWcAkUf+>(UCHOb4?jbRp!dC2L$5*F?r+(t!>+6C`lSiWQtv&z z+zLx_O6rcc#c9*`1ymdiN=^()RxN_xx_0u zwtU6@cWJ+C=M@`fZ)G^`o>8P+rA*6~zx7v`J$vMEfph_yAI3n;3ug&C^i>~NC~i-q znR#QG915EmEuc6-S98vmB^dq0ofrryCUPgWS?pcESALJB;)zuJ!nCl&Psuof9$cVrE^XkuY3pM`wm#>`!js+mzdd{dve02Azl9+`K!(^dD^dI|aZ)+g}oncpL&EsFKG( zs#Rjjz~TMnu)QdI7l;Xe`uJ4-n#wYr>Hq#;uL$Z7lu^W;iX}~dsFJ!tR}QdE!P>Fd zQ@cH@Ud?lEYSV^!lAm_q>(iCxL(;d$aw$7v)BecfC>%Vto*6z|(A`v6eg`o#=ck|e zZVcCr!_8P%?&)=kR)dNGFME0)1>!ZfOVhM1qY;Wm1nJdR#^Zu`&CX0?|Bf&|y52c` zjXJaR^d-OOGN(J#t5>tzY8hr`cv4swR{T7W5Qp^nMHSt(3TlJyRh6Ire$L?Wm2&F1 z&{D|me0>IHv|aB|E?-an>%K?B{BnY@JO7;qf9Zt24CzyjEo!iDZpOX{uEbYPx$iDP z3JS+PPn&&RV{t1=bNocdti1s-=~rR{b2w7?y7BfU_9q=`3jDL~utldxze?;$BCrQI zrA|767kR_l>bn;cvt^K?7u{nO@QNIhf8o_%2rDhlw!6R3sjQdR)udM2d~Uq7BOUd5 z(Hx`GT2As|+SCs45Z))dW1`R4X^wxQC<4oq@!MLw*Y6KHD9CorHd7t;VZiz6-nw`UjC-^> zOWDmS>MpX)m~uq-h9Z>DCK?&H+Ki-d-Z44YL@lVV=y=iIlKdRoThBlMyu_O&CDPLZLp94(>zztUeeQ`vj|fKc^%=ZB@!U*kTT+tBuo#N8aT@TRa?hgU9CkakQh2sj31v)LLeef4au=rc<)M z`mogVuK*Zo2--};u}ZDp=^_y-qhy}9S)DDucJIqxoy6UwZHq|TIwoo;GAG5Hwb03< zlqOQq+hyM^r@U#o{f3o)kXKlSOJx71@0OSqfUi{!8wkOss))vlr7Cr~OK=kQbVKuk z+5(GPgZ=T}6d16yio*xu84@W-)h?SyI<^^ieDn8fpB@7BO#T`E&!Rg&Zj>4_3G`IH ze*G>!)2m{D6kKupJhAJp|jG z!c9b71OsEecd@!t*{3?Idi&^=g7Xv-A$Sq6y5WDBw()(~Ppdribv@88`nN9zHcB|M z{;~j{7z}-0mujDuT3ttZS@+wPplAB`Ns?3UTzBGm*m>_OCf0aXbxe6|CCiFrM=`(H z;9UXznfwq54IGq4_i3GY&qpD(X&oFmcHF-jvDfbjfBBV~*2SH{2~f7yZ+X^2Wn>JT zrWiaTz?Tx&7Nj9HT3f`z&WbqxoMZcJ~dGEW^;0l$5!Yl=56mRh4b z&)OGYV8VIs3fXdG0y&~nhv!$dpR3!<+}3I(G)o;#U8#Sr0h5L@>s#s{H=C ztoGC*B>*i$QLh<-2Kpnl#Pb02j(HIMEbO)qw7yY>7Hu6V>Y?@#5{_2O%GQ7JJ;ajY zw4Kg%2xjbJV;VpsnnK*Mg|Y(O9)5r^PSM-yj#zEKS2*k zZnDk>wNEmxmyaLpcWGY%WmBr=%INF`Tlaa4F=lyE1puc&L{pUf&F@j`b@}q%bZ7z; zAKjxu8dBKI%(Oa2@dOy1Y9d{YSLuSrIp51)O&F;1r5A%zm(9}P zBs=30qWZ~HJ9v%Xid4*-`(_>K0Qo;(k*EcowU%EYA=UT`_c!@PpkI8?{NvD^z|?aU;Dt)f zi|IQ1%27s#ozU#Pj`8na(3q+qMktLiW^qpBm@4$c-s!?{%g}4eauL zv8;g+MhoR-51ByK!Q!haeY%r20ebVt4wulUJO%IYeaR=f?(=YU-$nn&?pxoJGR7w! zj|?pf3`}ewP?mXkd$TL7jUzMKesv{%ia($&W*M^$2U$K!XNjhCsL6@o^XF^OdA-Hq zVRM#-W(d|m+`|l~@BF{OOjHq1XI+Sct(t|EH!@-e;)(JPX!Wct9GHEn4WOFf z|Mvo*Mv{w%W?9=RlYZKK?*csHNpJ#x0NV@Tgv0%ykMW4y%!O}sI3p2g-RehnQNrS274AN#IxR*ZbjBo&el3Wm} ze5oiCry_v#Wh)?IH!bAo3SezOmDaaKmb^>f8-b$1I!-kz9=ViRZ#w@Dr(jlEi}^^3 zh0d^g%N4UbUruAu6PRy&6Sw$hW(Q1m5LznB+Y!_V%<Lbt6H#CeCj^E+$-*pXtwe%8FmPI1g zN20eIWx2Om7=wGAngE@Qc@k&NyUcSVe>$Kb5+RlIc`85D^*Uz8< ze|ya(yzlbaShh;ff@nf;xTBACds^6Z@HqS1`xs7{?o_UzDjH_tD3u}WEC?!GJ7-bdC`~DvOyyE~#)8xzVzloai zGLUf3^0yM!HGZ~cGx1m-N-4sXHAqdm{tkyRI^#n6`k!n{6T87(CPCF9;`s zKaQX|2_0>1G)|c4zAj9EE{*;GClojsD3Q|`BRK*6W`O$Cr#>|?8ja2^kUkBzZPV7# zNpn)ii%ZYpb|TPV!GZ}fo_`&b$1{WL+C~b%?Kfl5Hb`(H^OL!;ZC&)H?mIz+D?E_X=hh*Fh*)<04XJ( zy#6Yt)>o4aLqpdj z2Ri!r*WbOuKRvpZ?tvk6&DmlsVrcx`TNg01x}22dIuSjeMSAMci6a1oUQ$lDs)k4? z#QFox{I6gAiI(m@G)-bFxcmC6_~Z>&Ba|XB5F_5xfn`}JW&4&odyY46Tk#-Kxjn6X zF2l1p{!SLZK+$bUj`s~RI1tM}6W}YKVHgvZFJC^(#W3n_!G%nKpzR>%&!2yp5JIzU zJGT(N`@Y^DS_YB~*wI`9%VBgbNe11erwGCMRRP3P7N%tr)}8R*%4nFVp{I9H7)ww} zQ63Fr=#qHKB9StA=i-Zb%e)!1boFw$y^90w-5l!~ptZN3_WnT<$rOp9Axt-l(ph}t z1mI|zj?@h&eqPgw_xF;F4>PN=hL2x=HQ?;d^ZJ2i{_}-bk=W?bGL)t>y}Fc-E?t2OZ;B7QilwjJ5y3zKwYc-*0ne(Q%o6U!h@x}l8BmeZ5H#4KLmXu}lk+&}#b7y|{7 z>NaL#i#|^l#2|2^@42?hEixb8#fB_|F8BEgG+e<}dRh~o{ z-+&N8n?Ha4Wxy&o$1Fh}#NzM$AgAz~09t*0{S{e-EdU)I9kiJmHX<|sA1G)pc-%aL zI6Hj-+pADugkQCC2V>_@g29wYS=c$Zeo{>tWzi75Ly1xAbfTb1$*hJdpREUBLuW!w z6_e_!Su+3s=k2`%>^jQ3|IeIy@7>-dZBt)$S;$4QE#roTA#OBdo00$t34sva^n_3X zNeGYvd3ggVFX5#IgK@#Q<8HY~vMtH7q*a%8wY%EBx1Kh?Kjxfs%kE0Dz5b28y7!(l z=ggcr&ph+A`94dO`_6I6IW7~~JbfeM96de8(cy6h#;52XQ;fm!1M*H5KQz__g&baX z3fnaK_f`;4b^{PSIpFqO@bNTo27HGtrjjXgBiq$qekhOW`uKg!?U`3z&3 z0;c}j)gJ*QOoJUqhxmuPpXE#MyA0bjaQuLcUCnG+*vdl(PhlF7T|?2Jh|qsZ^j#xa zMFL=Aj92=G_=_L?iZ9*#c9tz@!S#IJf5~~+hR(m-{|x!e6oCv#HMD?XAVfeQBd0h9Nv<6&_;o;MCb}yXe9>vn!+`NGhv74Fg0CA(U zNdTpPyz`y!Y)GY2OQv7QB#?Jpa*oGDHpj`4agGd)acp>;QzK&>K6;e?EWCfq`D|IU9LMt!Lh$eR zJi@k@c9F1ceA6I1KFaE@`MkBKLnR3f?GoR7U^|18**6^glrVLk+TX|R+g{~2uULoc zDcj2RtGjsm*br_Y34{#y8W8I15a9ww@d9SDN`)VfjnF$d%Afz}KECw+t*q*3!}WaL zb;H$ha-fbs;X+qJKy=vhI{Y5_cYVz^Q;m;Wfx+x;l>+p>@o~v8dPaz z3_v!WW@swMWT2Hj{j>>L@qDvA_q5})-Dj4EC@r2Ju&aNJ3p!iyP@?cvYr1*i$iO*V zeeT8ed^UEsvbeQQL3tTEdykypi{HPK&bDS2&Tpl=wTaH=M&>ovQD2qBGK|pDO^gCm zoz$pF+H|!vuw~_9q=cN~kSjQhr8BhF*C}96O=I7&lRWy;9#WQxuq^V00&Y6PrI(#Y zO~NMcdRT_egD)TB`NIR4a~1h$X#CH!`?!2{H{H#3xPHL0mO56o*0a5D1j`6rni4fZ zWsznCAfd+)tNoW%}sCO2Sw(W+Iex?}7=X6HPa^b~^`hfI)|(d0NAXFMw-oULBT%T)a+ zuc#EBBDdesH^Te!F1D!vJl?vho7$1)9^a2xIuyZ5p4 z&~f&h90cuEOw%NlOp>&1+UjapGQXLwmPR_8>S=GRV_riYwJ94@*U^NAL=d2M^%=TO zZB;5vAub7gpRRc=eE%;$$;jjsCx*v4I5^6w@d>V6zZ}>15kmR*@7#W{Y#iTc(S%?) zo#UZhNBEtqH$b2m>-il`Y(F_%vN{t=0FhUy)We~^nRWtE--H++p>J%GKmYNs_?!1` z<$|T16kMNc&g;Q6b^h+oM;Xs%k@7T&#%2uDF5X89R4hfva3ULbM7Nc?wkZ2}Q|Pa> zyk7bT1r;`!&5@a!n#KTT!U|lwc5OEbE3kC(PkZ&144_h65P;Uv(Xq0u(kz^3Y;24X zPem^og6ZClS)N$VGSbrqbe0jC%|ILLVp4_Z9vYqE#l8`?ES`ts`!v-g`OVAL^2Ixz zo{^)z*5Y_RZ(p~NOICEoe)f^^NgmqOOKnq22&1JYh3iY4Y>u%^j#vAKkSGZ9lx>r+ zOy<|uu&AYhj^+kB8)|58tf94`p8D!4qV1zbxKCWuG?JD@V{HvRo$Xws34A|@qx%e9 zWB=d?2L?t%U;W$_L)Um>-$`!0bTziA;K5hVYapTP_|b|V&GvA^kqBWcBU4#)-mq*E z_02Fo%+O?pzx>I4eDS^4u(@X;1<&J(HA}H{oiE+-Yeq6z7#$|r(2Q=`@jZl4?m>x$ z5JAjrfRYI-%82sKroXwMe^0`Qt1&h<#=Q29Y0`IP3FmAnD$tB-K*RtlMTAKF*8sh? zwsxiJ(9A}UsEmz|bK28qfAHuzXJd`C{hqDN+CcH6w2B`{?%Z>f^E;ar%C6^g_1cB( z>l@*R&+NrC^w+&QI-XC@{04r1>qfLNp?tJnEUOjT0n#LCFsu~saqv;?7fiIDP za&Hm%NZ+U6d*m}NBU4knbo3;F?;*2kw7}=;x2)#xf9F;-RLJ$AzEk82E(=;3sY)cU z3_bLh%d&)E^fbh1(xyU^0UY zhDbJ+z>Wt|Go|jo(iv8g!$PWa=L`o>SKL94PA3wpXKu!_`-WGrLC?CHv|`T9G9nGIe_cBI8ze{z^Z9bqNxDg zGC)fxhCrBgNCpI+OW?Z%o=0JPl&tSln@aG}cU*xL#)F?2Jk2Nn`9C=|Hc4YcJ;rbFw*Jw=W3{7RxFi`s) zlnM|K#$U)VeYS{FqJ=B94A2Zy0gH@}GLg&jxu4$8XK%Wix1HBR!S%RcX%~O{{;hoO z_J`;jn}U&09k7e4rwAR7v8qwQ1d1;(+W&Bdcm8G!+iNM|N;c{XCevA5*Dc={FfC#c z5{X2|#*G_mcI?f%M6~ip5MbJ;KJ}@EmtA(*hoj8Ds-5X7Kqj5$rTvF^ zaZ2FIQ1Ck2M4F-Fb5hFgTYP&F;KYHUan{VMXI_o+Ve6X4rseGN-J)-7f|rk;jKH%MAr#QcrezCQ(cTn$J{~`Cl2emeWw9>RwMCJB zijx(lq2g#7nw0?0BcCtu^!}r?)mE`G^bRa&s%L%YJa!)GV`MT7uA|_^Victrmb_Wt zrzrXNO!dyOoGV?w7Wx;;7`VjtS<=}~B9SOLC(7;-ZW5AXV`IO1s8jD<#^?$i0KW4^-KD! zYl5-7%RfH0o0H>dEM1Fp+!kfHfUW zeD21J`J0=!FhBIiM-QC^uXNv$Vl?XEFG?T8x4Zw)4 zy)z6*c_(K)URl4wHWDAN{2l+Cp}w*~DdUf+YaAI(^S2Ma$e&%co<+@d6g(f-^I6f+ z%opFgg=42Dcl)BgRpII%5uYMOCq04ZsEs~<@Eez7gd2tq zoE+fa?t21+z(`hyvGo!eW%CIFNV#~1O%SN?XHyf@rfh;U5$o$L5-<#%+N2fUUl|}r zPY+Uq0k)~;kAEV%Uy#La-;QBtx z<~Q>Xw_VF;fAS!&_Me8)(VK!~$gn{gvjN zn7N&}V#WPl3;j!62@@V<(&@O}()*P5SJO1TySsZn(+DPq z08CCzFHYK$48!ComXugkH8FESfRoMQ_PN?gmG1MeNDr`U9Fc`}g;v}A$rqR(@^O|fM=Uk-3whZPsR^x{gA?vuD7&uKn zS3u^n=$4I@XMhG>@KuZR98e8J?@lsZ0(H!dozt*zZm_XI?@lrY@}FbxFs zum0+<-d|N!wW_?|<(x+dh5!HTP>vB_E7S32DwW5lSte#Vex?CZf8k1BsneBIR(v2# zcA*I-^Da*u>|=B)OJ_q34JlhqW~s#fK&oxAx~?nzSrTUFRWbXPVK9--Fglf{wkib( z&g)sq3w!tT#LKVp{Hq69y`-CtmS!B+qobvfEvuGr{PZYChDMN{13`dcSqM#6hQEnX z3Xa1K7p&o9Z`+I)sH7f0c=S1b`qU0A)55B0K$sz}euNz$h@amOf~{+p6kUY^9@>2r zKag{3a`6Mnl9qbjfAPwY!KlfcFimt#Fp%BL^TXJhy?Kn z7Ol9?Um(!!Bm_R5>+r(C6KGQMmL(kofut^#WYe-P4)hIid}JKyItt9xG?7SrDe?V) zl&Q12wT`zgYU3R%+PQv3JMUc4&Na&xaM{9E&YRajM_m$A6HMkE@}5s*&MNICVrCV7 zR{Ep7PUY`&p??WfYd)*%OuD+ei@lj8F2{A-UG3`pf_jG7|$R1p(DR``OQa z#J26uvW`{Q0s`M>fA2xIjksh2t>oOyQ+M{Wl}DmAK~xM-dA;fUxOk+PaI(Dp=zc;d z&cEl>D3AA^AmG&4B)@v@W&ZAmckv+U@8#5cnZYg|OtZT(*msEg{gY1Ox$r>#+UsNfmjyv=b?zI+@_&o-Pg! z40G)C7^ORgNA?`zM^C@RcOT!$!@Cb~dSsMSqZ9bP&z5yPc%H|?_IWq~ys-Zexm=DX z_8q|wBpa4=sVuvi#w9D4&{&mZ=aG|S^Ld=fF}y+++p_uV_ikm)!ghinAf3zexgY(K zV?$#~^sfnV_B1vAMf?CY_L>kBT#uIOBaD#^Nq3%GL4Qm$UJjAiqhNSG#5xjgAyfqXuPU&!O-vII^68A>Qc zzCqpJ+CqPPkx4_OLePr_v7cA{U2uRr$m-H;;*y&LYoH~u>yIjA1Ie&W1`CPGV z0re?`(-^^=l~Pnd5duvUf`%%ajh)S0+|`2T2OJ(vs}$|i7P#{AX3@VyVhUJQEm*v; z3)8e_tRG1LQp%~H|NQ573=a=G6g>fwV!psK22hy=NZfMEE%UZ+-TEP2*K5^-P4@x_ zA;@Jj>^g9mU1@MB^#a6CQ1azWqZ6K=UHN})CE{};yjk>#QWv6MGSu^gP}bkIKkulkMnfzNnSoV#K2^RAOO>{F-#o-pI1%{(9_<^vi5m+zR#LP zUF<$F#L3g62;XDJi9wDHjj~}$C$*^rULaY&sDo7@^9K zOpv)22uwDIVxM<}=+x|AF`J;u)7tCuHUIY|HLBp|6x zB}rN)AP8hY(l%K>zlFE1TEf=#%UIXhMqMgF&T*N_7RcuEc!eB6E{k8tBYiJ?Ia(|d z>$bvp4BP^{j-6t7GQ&m77pnJW7;IU!gyrq6VXF5C&p>jZZ-l#d9OQ>jzrue%v74Vg zw~srw_wvh~huD6opOYg~*oIDXZ7K#mky5g>rIuB#b?oXNVLay+$1gU%R4$IC1jd}v z|6yJ+Xo;uPUQsqIfJ~v?98Lh~HH)RaS`E7p_8D zsHYYaWU7@(Xu8^&<~f|6m?E9a^VYS?@dL?%=6ZA?*mmedoPBs`GR+fv4$@jzO;39p zt{>1?Q_U6Ut>V(PJ#~uj4GBghK zkMPa=ckt~;UgDYFe)`7J)%3RIZ$ik3QFI6g$$^@{31s8g0uSx7_E>9&$e&z1fK;2~IIxvc4Kr@FRw z_A6DxT5oUfU3cDj=ShkZK%@f9Dgnd{;G-Y?=!%}6p7%)^+Gb~n0}w(meEKvm9_y#K zpsQpNXMM2Z`7(Yn+dQ7-MzdW%%{wsb^(6omVLcfcqbLTYXb6NJa@jaxXfZJ+2!+_A z=}6CEf8QV-jdiTWMsjNGOK{}u3i9Nj#1e~|H6Csr7+E`Z|hwpy% z$9M72^DmJ|r9xahBYvO=ZcB@~zfx-4^aRf2IE8}4%Jx?N_=d|_&{!8sZ$Thg&{)G; zy4yH3I7Z)iTG>F(qPRvPzb4@N4a@k`>o-wbm54VR^^Z>S~~SU->MImE-edpSHXOy2dVNhCuj((7{QN-DJ2=LpdykW{3|4hC1IpDqzcxin%4XZKp*W*qY!DRXUVt-2lfEAM z_oQHv?X$Y0mHLLpS$747f5wRuC+_+2kAHlaauwi=3?Q}veBu+ISl89nbzOARX%7%K zG&C^C_TxhwbM&&C%_{m-JYal!2A!wu__X_!AE=!8mF-sq1y(*x(eM=gE)(&JIq*b$ zPK(C?AE2pW4rc~E;#6x64yPaz~eh!=H$R28+(?J zOjrbgaxV^#Px7rh9$;j063b3t*h!TvR45gNPy&$Qdyqkx9Vk7C=lfi_Y6)MuPj~@iI)}^>&RToLapnojQM9#tUgEA%>NJ0c*1vS2EK@*?ax{(iDwg$^o z5{02@r1J&7{ENrg(?5)Dnqih<1Kmz4MvzE_DaLgq0aLjGdrl1S_$!Ba;N|`7?;9eM zcc@NSR3$77U02zMG>!QUbzHPuF@r5D7BRn}76FWBb7XRP3b`DClSc+V`9gtrU$BN- zuDDRebA$}?dyntpUmx5-!3!{T?TnMMgd8w5je_U1?LZ$=O5U<~9)x4EsJWKWbdJ3P z6X;rrph=AH97>cWZOsAwOQ7BKS+k&trk0k9K2P@s3=IuE_}%Y*cVBquj0|9!5%3TH z@DDF)Yipa93AmEeoa*mq=gARHx;i?Nv-v?QOVj~bF`=WyEmoB(`%&_sYOrUI1BhOT zluSDtiP7=BkR|uk%2!=PY&48I7_G)afM!?(uEXe5n$zP`T(!Oj3AEPNkg`o4-_whb z6052jzmUhBn&Q;R7}st(kEZ(CIJ~qbm1NV(r5qd>rhh62o&ypLGf`w#;h*$eoQY8i zxeS^Rd~nMK{`kf#s7WPahH}ReZNowH(+Qw6WJ_N=`1bPDZX&qZ%|v6LQ08c=-m1Ie*X2r7vt6awU$W2 zdhzHW^(l*W-EH`xm!PYmnrDs-kaqn@I4n^iBO0k-0yC7?LjQq;1*Xs1c@4DAn+Hs{ z&6WNh9UXo0JKy=v%gkZ~JmYRa+qP#^GAcQ!<2n?ilKo4wIhUo5rgViC73a(E7uT1v zqJC5=uB+svv$P+5kI?DL>X#05tk=m9hF%3z1yZOa6jG|Jx>CiQ6RyAnpqUm{Z9ReS z^XMxFx&5h~eBg=;aXg+$XT zpW+9PJx>q>3{7VE-D@wUE|tLbd>qfGu{z1SF6!a>^Ouo#Je)89N*4+dIBA)&3J^s; zTDnH>sS*D1{%!0$K19N@lz~pyV-<-=G>*h#ArQKTX_%PFDgxifcM4ce9^WZ2na}g< zJ-s~o%0X(A308NuvAJg<=P&7`yQPs-C?V;Z#=^E{mMv)I=1VtnYHWh1_Z}o=nY1@I z;`>VC+1EeJzyJDWfHxEcsR{6f;6Hx7i`5-1tn6sU^8?!JtGIgE0)DXb5V~n7bwChd zlg4(6h%)Db{w0!v6gc5tk}`Uo;de5btSz}^>HG$Y5`b3n8Se!A%x6Axc|$|P=BV;) zUVszFj=e83g!lfxX91apBS~S{rH+0##X! zAAN?WcfHDn<%bTD;w+It z=;POQG6jb_ckJh@_if|Q$Rr8NLWC&Bkt;GzCoYO=k`N_hOc=IoS{QZ`BUy!R+8DZy zE>t$pV)FhRAF+kB|Z7Rw7#T_i~n5Wd3uzcgb?d<6r zo)feuO$aiM%Ve&=6|1^p39vq4@n~-!ZfI|qCS8`&*BsElM9`!MtZ%8Ny|Z)1`la)9 za&mI}H^2GKXP9mS0H)&sloiu76Q#dr)15ru#|^W>mYG4%_TbZcUjDPV5Lcc!^L6La ze%W^AQcG@FQ4^w=wKbLu!X0Bli7pXly+M@hJ(m)@wvNCpFp|mePj^4cH$L_rEYqN~ zwTVx?^D;jDF9&IBtm6-FxC%4W?GGI94ZB>#$3V!F>Ei7zp z^yXWzx~;R?C%>=nEzEZn8|9S zlXH91-waElVxHB?{R~};TN5S3IXF>%=*Y&cCXTt;VJRh&T zV#%#ak5@JoQ^1U!n>MeDlKGj-IkjID&C1&^=ia5UViAXzT9}DeMwbc{=ABBBEdtPV z1G}ak>3TeO@HpRp^f^9$?PWNw%hn4w@cyeVVb!7zmbA~q@jN`==b!F)kl{>$gl%*C zGcR*;EX(IUa6L;FwBooPThCuZM@u80`@cVB`>VYq6E+{d_9{Mg)72y`Qw8H_I`=%c zi?84PC}Zgy3CqH+sYg#F;&Cwz9VykG{U;Ch(cM_f`p!0%&1+;{Z57oCWtDe4kMT@_ zzVRtu?H^%J-)V-kIW$ejvMfZb=a;xdMUnf8SP3pQ;)IJt$x5L@e@j6_HFP_PZYPj| zB=8*kLJr?4;5rWbP7bpB*eSmE*zkmH6SATRD>y|9!16#Mm{Lb}#zJ1>l z{P(Y)$Cna2VPjV}pj%c^I1?hw+5>i&y!7~ZhQp%=(Ow42Fybh|Snu}(G@)S_8kUua z#d;ugi-v|jtwc={rThCt+4!k>AwdMi`7!NNB140cW+u>01&|VWE`A}0RVd&(c@7PZ zu=`j)J?(Aubj%}g0`iW}t9>K0ywf*UbWP*MV}p3%PD?2zi<)X<$xlQ~pi&wRN208} z9{QI8PpUb0E+w49vnm;9WB^*Y+h8_hMM=yt!A$dhTC=mtIOkS>wgIcWeh%9&krP59 zRx6rPWmOVNzeF;UG5)=p1V*X`tB_|ZJ;gu#EEKZx{8FYjCh7&@Z$bMeCv+; z)Mle(3ahFX-zgw6lbjeD(F$a$p0DptE1=C)Gq*%rTiOP@75+0L?W?B`Zp)%=8f>tPlX^g8pSmb#>{?aR70D zHLVg4u>5(>^st1x1!sx?jE<{DLwvXLWwQJ^X8e;g%k!1>W)xnEr)NJf2komTC3JES zxf-Jm#j9#8EkKmHRtgZhj#XWc9{B8hU$-_tCS$lBvH(C{9GmbX^eD^+AkF>;ujin{8F)6N>TK(3Y4*sk`;HRQmPlr;{8h( zYKdAs%+KLZV%DGuOlN`pp&nNB*YDIT)3S-!snPsGNxb$e_Z~a3&B$QSv3K7uT;8 zX^KxYic-u(8XeIq;Y>}C%jfBAZs4DO_hvr0^)e)&>ncq6Prvm&zJK3i z=$c{zt&R2k{YT%+Z*RQ>0XVq~`H4|{&y7XxNJ~-bOpzf3fyD6w98ZPMn!1jqDG(GX zQE+4m!#ke%@$gAm_8IM|SHeg*z~O|BCT=uFB9cHvF?=>+CNU2P)d3MQn!t1LvQy;7 zhRKecCOCsUJ#wO7;4H-zPY>T$~sM5qJ@`ChSm5Yl=H-nue3hP{>T-2LT_tVl(G2>&EpxbfNLJAN-tEi#oaG zn#*xLkDIUD#IySj@zAq7(M14>DmJfN%I80LW9$aBO@mjC_VHK${v)=(a)5iD-oeSy z2|n|lcaX46Ohf0B*I&iLwkH1Z&PN!}(KgIlpS}t7O!=eRkXu8H9z55zo+_js5 z(Fs0q`6c|xZ8uU^oy75cOvB**7k2aa|MM$OjgC`OSBKCwe(=~1PK-?Q+1sw8qoono z^SNR320EJ?`SMR6;6VRrq#Q#A601fB$gRMWkd+&!PwKJM_JAcitjTZa0+H!$Lt zH(dlMPRvTqSI)Vn{VuXV^--#4l=Q23yO~9RIcq<kO(O^m zd;fm#WBl8_kKuS8$y5rfx{knga58Bg+kJrk|NR3#f7^AOzkD%{=dxjGH{bmDd-=-k z5AxXF!-&i{NDorAtZi@OBNwk?V|OcNm_xt|BwmEgSN7OJ%-~vTlH9Ov5mzj4=fQm^ z`0>ue3}*{*wjwn&vMjDe2B9<{)D7hfD})4O;1f7`ynGJ7P{8*bwW0?B)d`C=?eo~Y zyqgP_cCut%Gu25O3HW|7|Dg~9*YolGfMxR=xp-MS5AHdNJx9Snt{?n5#K&iD$h1r> zG5sbnM=$?b{8I7YF|$0I^=8^iGF$z*ZNG@CALSXKxZ)A0;0%dcxiV7(Qu5KOFJe{q ze4G$n_8WIU!m;6TQgsc0v_R(SiFNe&JS^XY%Tojnw_iQU51-nJ zpPu5@jVt->Yc^4zuyK7K#}7C6m^z*xoOLr$!ShL)2Jbm<3Foyp^Nq*%upY-*D>8y7BM^YU)aU(#N-8ORR;d?_(C4G%aq zGRBj;dr8_RH(b0y;TC9u4_>yK9Y+S3$T@Eq2-OcH^(mVVZCQgJ!iniZ@aU_@8P7Xv zKLp`3ib{#FQgnU&^e+KhfMrd`1Dr`h59jbqtG|Tl5L^T%;%27!=KtKorqw<4EbPSdeLP>K74JQElFxnjc3wI@gl(CaRW+EY z8qlJEBY|P3&A-pNf zFDQ8T*Qp8wA(Q{@E7q~PvjxZVF*J?7u_+#S3S1PQ9{d5`q%3A1NtJjpG*b%x|dWlkd0`Bb*eyCkOer`<_4uFiji3kXMmwy2<4m z*3jHg7fTqLrZG4&#_xaq+Z^aSMNLC9c5OXIqAKpOjJyp(L)Q}|ObabNNg+#e3gQTSG0b=kuORHXvnap${act}&G>@Y8Mk_~na-I5m+`TW$3sI?zPcp>I6R z3r7aHYv)0F<~4H5#VfgDO*axH64xax{^Y9j$UED4{={jlFy|qFkV-WQJWow_Cx`Di zc)o}0`k0zVQ%#DuESk?n%evXHa6WAfwb-VCA4(z7_TPf*aI}Afr(ZqH^S#G;_2eLv znJhuzVQ3n*Z8J1J#ed%S7@z;pyCCe}4HxtRwJ z&sXzYz>lBZOaEk6ZHN*|E)??UR^a8Ea~8)WnHZ6o=`v8`pB#+8!Lw zBLF`9)^#*gCHVFuFEN_QW1Rykkd#VFs!!Q`{L1y*bYTynkVnEWc=(mW+_U?z+Q}IX zR!pTay^10Yy>zX79D3M9e>6v&fxc(v-YRCl8cjHhRG%c;JXfPdYppma{c)$x%Mq9aa#OQ&wfqDDIi@J-}iC7 zfTV4xU3<&AxM*1?tGnjWSe*)WX+M-~rOMQlE%54z0iN1-gzX1UaAatdOu<1D3V|qT zC(tb$-Lw&gg0pZlllZwbuJ7}X^H;OFdjX!WR_chM+;rh`R<$?r{ij~x=>vVF^RCMH zB+3QpwSnKe=Hk#hBG`50 zB>(x)(**gfVidw;b;mqDf7^AeUEGD^xmbq5;l2U>^jqH}S19oP&wYm4>MB|r>-eMh zy@NmaKR?FJ=h3Y!dR1*qWN}=56!{gdP)4c>%`lJ?qrCHi)g*1Dw;Q^~v7vFke8X&xDv1yRFYv=aWzdAiSUSt@WBok6?Vm81;Jt^xpX`0(LL-}5uNt~W$CR_&BOnQ2+n z+7^~@aD!r8xtwJTVx4G~Sv}L?8OL%qt{~6cetc-k)=riTw*y_s*{_asIxjcz9~ zA}DMjh&Rw6RQ!GDk&wE{>SfD#$N6jVLx4=j_4(Hac5rND3fp{Rt})BhdH9uMtm$m# z)=Srf{&tNIUvUA8+nTs=Sr^OOTd7G#nRt{=Z)gf-d3Z9-ONUSL%>JY7JbaRY@hS3- zi>_(thE6I~rEmmIOM#(=0=DwLDw~WmHL2XKVYc8ue)m=^Q$dnVWpaG|r}uIG@`c=R z$y*2{u^P0ZqnTBmEfidjyz7&9TmWp#AYm8^ZAhwIf}S6sYZ{t>hj$<18xL>ibfysF zAw*+O>Ax~efGlAqlf&z#e+iZ*skJT4QZK+PDuCyCnZt(-T(M?J7`UVJ z-TQydGcWD}KftQ1<702%!pGln8J1~;mVUuce!Y!GK=f%S(&@+?hmW`(AN@t81fhJI(BmqdBKzdxhY9V!12@06)K#$1G@WHw6z$u_X_k;K z>FyTk5?H!JK)OM?n*~IqyQI6jJETQgq`SKtmUo`_IR0Pv%g)?0_dQpf*YE6opk4Nj ztvl`IG8CN|o-#)ozUJ(Ii7YGsO1GxhyyQ=$oX@DkNUw@=_LpCoH=@2XPG#K--v=Zs z7(a2*cD_0aFLdWL>B`Y?Jb*>vy=)1jofj6l64gaZ74h{-<#r7rQRX9m{$3xZ=Q_G2 zoQrj3JbO?KfkP6FVzr?cU@S0)^gUBnn(L=GoMe*PCEl$@W2*@GICW)F09w6`_W?mA z5iTl$B7eshW(2T8vp`igSy5gXvA+LzBVB8!NA)|xT0zpQKS7R{#)v;76Z$1qd#yYF zRd$~BGuCEgd@JyMO&(8H*mpfl@Md)=r?N@;W){Ls9zeO~zKXfkhGEZE_3~&x@A_JN4f-W=hB&`zbnX{IA$mv34XRC)t1K5> zIl+6h3xzyA!dIRciYpkfHEGE~A6DltI@X`K1WIyggVKGde?`Lbf5cv*rv-93g;e)} z#lY*B;jCp}qYQbslziLZpm5gM6M#oo|02&P%Etq(jK=}TPOUO231Bv5 z-0JNaBYVL&PLn%4zI>06gWP;MUPHelK)_MVQ2>yp)3POs=da0(qEAV{+fFzO@CyHe z_l>#O$fiLrY|se}X60DL!mlv0rjzcao(8*pAiUuyihwPZ)6r=bVY{eOmzdnY-UgiD z#zH(JoM9A1tp*TStFU&iAE8T2OYa#Vf8(l;HR*8w9yxtYu@Xo{1{nQzf|>*GCqJ!5 zwB)5w47`J5RdQNgVqa?tz$UxZl1&;pQV6H0Msh~m^mn7Ob}Zf68$b41PA@V?Lp&-^ zkTv%53x+)OaS;7XGhIz^=zbAusdavoAf4~$dH19od$mikI%ejJ!D#EDh5uQL&6+r+ z82f>|>$T+ZpF!-!cg)q0edNrR!i@DrACx!0`vR{vR%z<*4kf15g*Fm4JbC3aYDP-F zlF{;(Wj4V0(VvP}8X{Npggo2e;k)b&{|s%@Q0cbL?TZ2#qn-aA3?QBfS2RRCOA`#W ziLaHAG)%+k`g1|2u)Q0>!D|WvluOFWWlcTLcM6ZERjH--dA8^=%2$AE3tBT7e+0lr z&}usHKxKlLZopXgJ+e3hs3*y2Uvw6yf6p`{Egj+McVGqE^;tM^k8jRypBz(IuJ;mY zAQG-l$?5Q7|84sv6;=tQ<0X=V}4>m!uVmVx7)iO#m#k|R&f_sGZ zha97;Ur@=;>-=|qn`V5o(@TXn{fvXOo;X4b@_irz&Mjh)P2){B3H`jkHIxB!U{_9KLY%i&b^UuZ zUSN0xeh@}EN@-dw#ZpZVo;z*_mHq8@V>YT#tJrLoTkZ?$wn~HDcl8jM_eqteU|7wh zsqiReMA)+(B4ze(SivgYW@r@cUo}nyPf5tPIl7&Ot{N3#2Zv=+Rmoi@Fe&{-`*@LXQz?X$Y z?>`c?Pmrmc?%0Y!0xuO)q<#%mmJch^g0Od{`UNNxw9>j2s4_M)diS0FwXjqg<$MZ?RqX7;)RJ7@d$g zldF>ndRAVOc)}o<53-zA>IrljILW0>%_<4*g{%rmnbr!*^(vkzcDkN3S*Hd0u}Q$U zDMAhh#WEOy2?ypAI}G(cB5z6fs=+!1S&5aUCoHS@R89oxiK3^wmiwG<&6@A61c8#; z<4Q;MhNQNBn91Vi9=vTRw;&TdZzQjf#yOfvMA^`I&J&4}M^%O|MJteLDX<+*FMb zxoV$7jd>cYgZ%X%$z%~tV&R5Yluh}sfAFs>UEQhggfKs(aQLt`c8IlkpP%clj{)bz zny(4we06qgbtzFwmHpdxY$r0d4o^SNV~&__l%v>_a7ts?(3aFJ&}$CHs~+np*!95k zjn_o7Nhc*I26ASjVaRjWJ#|QKi7R_f`=JZ;jG-q#Ela$VIGr5)1qrBBCCi&P2sF+J z3ps(BCrnxO_{(;Pnj(5*P({*Nd`@l4C5r11{U!1bh_mg(dYduaZsZJcKA%&RC=up< zhna0`Am4SB;z<_kek5M=9TiAoMKhRP@IGLKysSQZZ(nekPBX^4A$>GGf1#Gugc}$< zomBXY)6J3Tf0G^Y;MWY&V(fhe+Q z#9jYeY<>(3N`8X-f|Iqb=QIml$%y<$&9dWU9ad!xYI%J&P7{8v&2OywO)XhvX*`IZ zP|802PSsm-JtP6iE20sbVv9mBi?1X7apkLz00>Dj+cs%Enk~;Q4mK(L|iXd zLsh*6#cPVp@aVL73yB3h&HL>vHQ~Mcwr=RlJL$MMG&$d+=w^;3k_DS5IIQ2SuAt$+ zfopLr_B@E0b@bc%PAnT|a+wicsB=sZ?MFccr81n&d%U4g#+`-%w=AJ`SGMy`YkazX z+I-v2?sHDaT~|)S@j)P)^4!NR_4yv<_=?J~`tft`zY=`{LMZIau5^Oe?c6LGeeQ=z#)QB)j8LoGb@%L68C*uutrDzZqg%p4{* z7jS>S5ab8GEFWj7s}kiaV>|mRmPdF0uLH{Oelg}ezX;bo;rX&#GV@Jv=G=KtmnO5; zA%XDl{R0JD)Q3oat!ZI!t4(4WY=!JP39S))0iSrRpR1ScTS>lsm>Tq?6Q7V^X6SHs zx4JW0n|>E&!MrF?=1I6RE3?L@@v^p>5PiD`R#okVudMw+!v`Mj3h)~ypR{a1Hmh5x zFB6QCqW)Wyz;oPxgI{bK9c+|ErWa~SxuMGbRp0YiVc#ch(IrUPXa4j9)OCrZ%?3;e zMwa^?2=TU~5qrJG7|u9-0QTyqmhA&h$Zew4_u@HbjBNcDMJL(dI7U-$seXp}ifpop8L6=V{B(?Aa_Zu&Ix-U>% zzT=RwDba4L2^IHP+WvSqdnI9779n9}CfJJsAS9&RqnksMQe$gXxeqBO^V%QbLxNcq zu=j?P-0qDagvk<=3K&;}SAU1iUUrckcGBk*n20aWu_`(`dR}RfW-4xU3P)h)e#@N> zrOUK!MT=@%`yZx}rvlOCm<7}C(o$-Kb5UQ|%doSy1E|ZjLqMZRMi#G6J7H!%aIBrm zgg)+Z80{&Xcl}B3Z+)ysYkk|bKRKg-HN)fhoM*Av?`k*q&IVge3#X2eOGH|xIOe;( z5Sl3r!QNUrmK%-{)&tKfCe7hkYgbyi1y` z$W>IxmYCng6-{Sf(bm1`Jah$KiMuf*?X7=-g;54+sc7PjsK@_0fO(Y29x~7DdE8La zZ+F4|p72R=-s|qx!0+(v#~}q713@fKnXS~cyH$%;XVz-NjH@WlCRmDK!k@3QrDYl! z42x{tiFXO>@o!y|89QeqSvLfN(kPAi?&qJVe-?gzek@uuLCEQesu}R0fXdhBd&kqW zv+U+sMM-#9Q#gYK!L{5P!?ZWXg1}961y3vot1r60b42@Vb(YkJ+YH9Dug5pMV$Mao z23tN0jp9selAo_mHnCtj&FNk<5d)-0K?j|tv>NCRwwr;Bd{DIwEspb69b?t44MEGt z4>e8U-oS3=wMbjmQ8A!ameM$FaQ~5}lY)!?8N{mKgTVpZZnuuPlUwF1;3T{_Ke-u% z9dJH0mNvf>VxtRa0*wz>qzy%;#8}Rc2>KgaO?4T{bVBk?hPf6xdt!r4aKiaTk0WZe zE`M1J_9^9V;lq9|lMQf-ljs(=^-u44ol>gQ6`YiW4)Zzg*FYZ7Lh0+9;lCIZWKpQK zgJM^I;29c;9+MkylbNSf*Pdfgw!%lcgjBQUqeIJGwHx5yaP(joaI{8vcoezldq@S{ ztXYy7U_J^2el56TzQ4OW(*qzXpS9+s*#1e%%W?lSbzqiOALX`0BJe_;Za|-AiIMvn zW!KJDS(#mhmx#ljIWJ+rD$&O|{qcK=&wbtT!ANCRG#V_pf2)L}tB+133&c=Ai4Q0O zUXtezGiL{a?>@KdWgL99DePjB^ymKccXWUyoq9k3UP{w= zP(*$$m?zQ~W5Ar&(A**I%;c{q8XzOkM7JjTq^DD-QX%cM}ppM>k%^ z4toy6`c{A7$7@7MDDbCv3Krnm~#`tJEGRTZOXl^`(k5@m|C)&`2m;~+K zI1JBr9pBbD-cRnGCRw>hgS3#DvKqXFcbcni5J2nt?W})WS+ad#h~fT<>sidR9F1cs ztKdM3LJH}BX2c}O77Md;>+2(P^>fziHQOx`9#DtPT386uR3Saa>WBpIeetcBY zJMl2b{A1-`m^6?m_vJY8@3F3Qg)~h~P+t=kq<9{_@^jTwIh>UzueVKSMCfSy{M~rF zW~D8&9tKA3^8%y7G6@%YVz&3G-`m8S>G?tXUeQfG+=9<%*P6`7>r(m9^>4uwa+pCgedI&GSC@BsHL%HKY* z1IlSnbVgWmpM$(jJKhc!@>mWGkDb_AXjM70(!2M9X7DFtS|Q?OrB2(4X3FoXQX+?UjB_+2{-V@zc_F7heDD`+2I?ox$gfc`h8%wLH@EAVC)UIm2 zE0AhdHD%4%-st7@eL`ArwF9Q*3rLSBiXF|P{6=$im`#X6-=z%-hcZ3(apdf6X^pi# zb!rh6_d&eRmlrzU7&AO5q|~&i4}Cn)W3C5MvN~&a9UDjU4-HsA}w2q%deBpb4|O)-U;(Nslfe?Tkkoo+l0Ni^4I;g zJFv;&)pT|Ev#&>L<~=^oM{`xJCJ9)gp<$r&W$<6o(J8mC{6tK%1p&SLdkN^|{Cs`k z?_alVH#OOe^bG6BQ;ARi(iD3tzHk_tU<;T@=&z;ft;QwnNky3tq!518+Ps)Tz-`;& zaajw;pRHeTw<}acR%d)46{X_hH6?wMI(A{c*%}_`wzm87SoH$T7SLnY;YVUCS-jr)3wOKc6tpFjD@vx&2>Pq*h>3Wlbr*QuIEpM zs+_EH)Ad;8!R3bs>7lB4H?7Kwb6rxIhF8k579RX;Y;ZL2c4A&pj!-``$YWgw(@*;S zd)AFa$By6{V4>wbf=kIwJl{ZXR|iPYlT~e+kkccd=^2dftGn%%eqYuYTUfEJSaJrwE~D+@-hQ;lL9p>4*ki z)l(qS1x}H2>W4KmjtepJ0OGOwuz|#ZBVu7m^37tCdyf}+0i!J!=x#@V>cJh5{}zBg zXW>6o9CXNcF#!nx0QAdp=bE{_-A$>|VuewfE>Vw24vO4AB_<#;@H;vPsOoi!ooZX3FWAPEkgId^pBbZ8JV# z{x}e6A;+3JgGQ9`=R?A{Dcn~n1}$xw`s#Rd1DZ?450{|=10UE90G!OI;EfIrIF?Ft zPNUR&E!8JPwmv4T`nusZ!^^zCLW<{IKWE!*dy{^*3kqF-VG@~))&?|nedElE4|`fO zq*f_!GJE~EnE%NaQJ+Qmqb+Pyx^MX=g4bIRMFn-n#kxDf=D)7{jz)h{*L<0x%R&fQ zX^faJn||b2?e`MJhmrM4s%dfD=SGIzO|WrZC*lqNV8n~4)crfEE&8PA#%p!rv?g)k z*y9!BD13uAlOTo5lz;nfgz~CGUXZSK*|)y?4IfWs^nm$v?Dnj>jk5#~T0W1V{}T?% zMmvseJy|G6tD%#kh5M$dRiwjuCvvx7){z$Ug84xM@cVrD*V662GiC(6BDwVHEck5J zY?UIfpn-dh`+AbeBeKZ3()#LSVv+CL^W@b(4~{-IA&?K;p0>jNG?Y%YhMhgIQ7oi3kmjU7_%ZPVya@QDaYmM!VE4LZWu*C=%=_#=`Fr%!3AcX7b*WPylkaN`zS}HbViK!6 zLpO@iyyhPjbl!%j3>RmItIY~_i@%)jNWoxf&%dD!0BFp@t|27O7-P}Rbp%@mwf z6U~8a>#aZi!}f!7l92DMCGSNrH!@m(3JnddU?B(D&}8U+IZ!^Q<+WB%fKAuyi`L3X zLnSW{E^&EwXOF!4iatjz=q&M}d@xXp`h#WC-#)*%FAw3XFB#c^VF; zLLNA!seTYgn;oe^598n6#9BOdRZP-gH%*?h(He~3`gtjb>aiP2S6g#tvlyola2e2$ z#bQNTlXJOh*n{1Z{a^e2PV@G?117QavzIG-5Ic@($kuw2$Xd)82o&_2T(FIvdf%C= zJ1g4cg!ygnt6Y6!Fp|K9V>^6Mj1NMO31Sb+d^(A!GXgM0}{Y zdr{mpGDU_tRX*lvYUKCVnkGd6DFQ3!lPhKa*i#t-hI#)$l0vovFv5(u&yW`Akss4q z%bTMKGp;|BrQDZ@)!jbMPwVGU=2sXZ&<2(ko2CO<49@UJSlp){o&9EUn!Gx$Wz zG=0cce_k0UWHK@{|8zYA^P_sg1AXV_&9$<up~@w&U7uZA^daew;Om zo7}~ksh{;JBmNa`vfn<`o?n^YYS$B~C|c_)&oR8LH0@T{$SDtQJ`?*jI7Tp~o+GV> z_t~xK@=pp`l(MB-psa9EIU^|%`Tpga$jK>1o+x<{)q5K3=v?mzoXxwya*&EAC-=|& zvb8S1*Hn^+ONHe$#&ju~_E(Gvl~!HfB;rI4U{K8A?aepLa>`7&udMO1;}?D=2k(^vmKQ3SMB2AvXYXv}ed_gisN zWwmyzog6Y*mVwr8KnnnQ+S%F3?spsIcJAe^*mkDYCUaU zo+z)V(EErFi-qBRD&&$@WoC&l;42u6)yZC5O6b6$Db{OBl9Dxc`nxC#1M3q#_ICLE z{i0f+r5rl&QKiu7h*C)LSiFSA>$_M$$nZQZaP*2&F9S)VjPa<3ShD=VgS^M%_?z@5 zYOz)k#ZGL?0?!gO5J^+1KAu`W_Xd7iZCL3uEf$#c9( z!0nQQ;fi~hfxjfW%FYOZ!cGJo{T29Wn10dSEo{H}e1Djv`RcTU_p)~{i3!bE zIRYEFOBffFuK#HPF7bZowAmVF5*2ZBN%wKK?rv$l9QxMK5l^dO?Vy6YU;2$Mt_h7K z=kch4FRbR0k=AApgzm~07b}fMn_}bUjNTEi?=JYlMR22L$!zwXgU&i)BSL_fu=0c| zS$UowCg!ZFr{^sd@Nj*8T>w$g!)T3Z%J;$~?(o$E-Pw|*(ZZ9_)Iexr!&c~r@=E(+%h zl`?I-bmX8tFY%ALK~Ft*BVECB+CnMRaB4dB_|k}&>@%#C%-t(SUR3O2N zCd?jGa6H)^p0t-3Q(iD>(|m{ci6@^jzB9lpP8$;gOYM}g^=`V0%xtQW6^K{ApWani zm)obGkX|D02I$o~XF}rehGj-oqD<0|S`2+Ka{T9$t;_v)_*2HO`8>t`6lApEPHR$+ z`rH&!9OSbu0h7h&=Ns%;2>y2l=9u9y_{}W9`&`~6_6^F0!ltIPkW?(1$YgfU!pmav z9AUTK{mR7b`C4At15EvW3zbVBM*KTJzhB*cRK7mrE@BXghV=_0E0cu6?5&scx!!~H z)d^*`N8ab4vo;dRBv`*7sfp(GK8KaKw3jY&$v)8-c5Er#%YB^-BUUk6vsZB}VF0Mg zKc7bG?iI#1CtC;_%iw=mnwy9wtloN|TO|UFgaqZPjRPC~rYTK_e`#wyGmbZ-IDwJy z!D^YuF@H{LSIdiOY7-Oh$PczVzh}AeSj6Fc_Gr0&X%<0`QlXBst_$fgL&I0nn&O#5 z^P>9Rdo_TAs5BSGe}5X7_SIWTf4D)}i;{U$6fbR*WrYIhDxdIDa{LmL;lhin{?S)Tpu3_Pk^h=XupjhVlBcan65)2+6>i z7%$adoBbp+-^sKJaW`PdnWT8!0|r!Y361=m@iIWIIZWSzmBBFO*s!OAghrVU{|ycs z3TB;gjh7?6-AQ6(EI}G$*VJf8VXN(gO_q;2(O6lkJDO}oRA3MaHD=cq?=YhQMnm|W zB2Iq-tr_;j(bv7?ReyBF9hEQ;QOc?9#ScD%-e@b18>tBWyb;WRTl8@2B!PGCtyCKO zExgXB0%NV#HhdBw49r;WlyNEr#7a|g8(qfovrM85iULH&9)G`Gl|=ESCjdhguAd^} z|4rX>g4<22i2Z#DTcYLa-l>i>3Rvxw-@iI+^XK1)-o>uylZgBW4^WIr!oFQWh^K}4 z7a(F~QdFia+QFT#-fZ^Ncfe%=n;5$Vi@iZ7y*-Y}^V6kiwPLds5&li5K9AWyzpk@i z`JO;v9$Eg01-@uddEKrzz(V?-J{(h^U~4(K>m^-l-+5t(H$@cN{D(Psgx>eOIh0`c zKPrww`%#==0{!kg*2&IKPW+ZNHAg21v8aUpPTuvg_7BRl=Pn7@y$^aPOE5>v6-$|_ zKpC{YyYmI6Euk-OlZ)MGMozP3`=RMC9Y3T`--Ci|uXieh?Z>1z17%T`cs&>P?)75YK_=pEbKc1`7r`-0oX6&3fqZ02D@ zES5&WJmZpnj(nI>y%xGcRsF?XT7i&Wf%<-cI)M^gj;Mg7NOt0I3sGN@HaN}O%Z zowL6MJ_S($X{(vw1pZiiSUI!rv&{Q3{LC!+h4c*GKX>#|rKqWFr#k@h&P7h8gbuLxxcU1%)J+!c;Bfff{ z|FzAYK_C5~BYfSL(U6r|lYYkJjyIG;nBPMr4~&7$CjFt8Dv+nyild{W6cq*}AfC{H zP}E=&GJVmUaLK)_ecQP4z4%)cb?K_}DZI+&T!dR{nv>r#fTf(8lTD~`E**TT-(C4l z;1GeXzj3lB3vm|jyO)%igMY%Yw#W7JeAd_x4m~*q|AYja<=0`i=zSm0YwfplHJ231 zz2Xj+&bKvwzbF|#`|UXcct2siG zbu$k_1iK&B-5hi zx`CgiA9lu7Xd#DMY{9}~@VK0%PPl=nlOktg9wxyaSq(|MF zZqMQb1_cGltiM6;-{R!&Aiq~3?Og5m!a^|O$kc&X3uduh*X0$}PkdvI1Dd0)MM-u( zyB6KoR{@6P+*5d%QN^=GnpdBp(Zu~V`r=Gvk$%G?zZ_ zvy-j8kTJq{NDTGpG5c`du%>^?RN|t*c4q)GR$vt&0kiWjz{$PfG4t+6xyqx|^tEb` z2S-U@=rS+q25TnIp$U4O7wx;u9Tl0Yqi}GA7c-+rt6UB2@wMJRY9ER?;c0w1XpbR? z&Kt&Ut_f5?&eYt!!gkN~xUV1ecBP3X774=+c z#+Hc>ZEP(a=)r>J1!btXuBP%u4B%^2fH4IY%ljtty`2& zX)s>%ES?*+fBXeE&b^)p#YYn|=S#1B0aeY}BcY;zaFDnf{^L%fO2Ah8akVcD$zkL| zN}vf@ud^#mpC`x#25t%o8Tp4}_v0RkX`Rk@HPwq&2`w#zg!sMyLkcE=j!-8S!Kxqh z^_i(?FBo0&gX|l#>MhwnUM5aAXdX57r8ES%g@@k!9t5Dfb*YT$I0kd}yXjxgEz1kMX3dn@g$FG& zFfn2-c2dYH&sUY|%%`R^smNkY(g?5(wksb`SDenceU|iyCif0Vi0N|1Xd=DMM+r3W z_N96=L}?}X!};YAo5@ei`ZA?c+?&jF=*K$t>R-hveo93xJ9ehMxvYNgyk4Qxug#6N zuvhwHTU}C77K6xkTX|r;^$XFp&BTH)mEEC)?*0CPbxPhSoDLG0!Nrj}|9bF#y|;t? z3wW9*{D`>bOk{)t@@#b?ASnA+X#qR@QNEMXG%~Q*2<3n>6U?G)faoJKq4=Nr+v(yvO4dB>Yb@K52BM8;Q?Tt8ZhiSM6mMd_o1^?kE%@Xvw4_6-i`QAwRWlRnq;X+pKnh_QSZ$CKsRRQS5eb4nT>fyGmTn<>_l%;GB;UWTf-wlKJ`z<+zm5Ai>a zUh#s;M}*1&%}a}6QRsuvy=yKc&6-D|vKxgBXHbEH1cXhXdDkWru{#ob-=zq}tb}N( zUShQ(tte_+!b4*UAPN$m>bu2YC05<`zi2szGRonQy4m;b&pYIL&#}atejjnf^{mm7M z?Qzeky1S0i_DB9`H*Y%lF_wq!?fucv!n5Jx5gkQazQ1}XwkIVaw{nmq8+T_?Su>nk z8fdb*ciP-yFKh&0XTKKA|LuVy5KjWxx-q-+k4DskpwnDPGQ+KRE4zWUUHBh`eujzX z+BS=bk8+L@zC#TSw^-!%)0`LI*p*b2a%b0H7%HC-ASEZ=Z8&dFu$?s68#tf!?)D`7 zFNgs)r=`_#nQdbgzJa+1)+!y$T>QFj-p1K^{XM=HuUVkr1UK4+XJ(hdN50>5ECkbk zXSj<_&WE5K=iSdxXdg0#uvF3QdgGPY-(u%Y!A}F8?y%E9we8^Ubh&4FX2!)mPqlP4 z5X-zlH`_k|{;l33uD}~tOLK^)f=O@AceV25vBqSJB+e_{ zXAM?U9fI*g+WE}YeqVNxRQy$YMhSjwSG+>H#efS&F<%qh9zP2bcr#qu-@mz6tLXjU;<|{WpoVQJ6a8My&YnjkySk11Pbv7PzA|nmI9~WE;#7-X4%%pQA z(d{VADmqm6*J>F`}zz$grDsrYi zTy7E#WFc#QRxotl=xIW;=^vyV$hjShUo+Hah&3Kx=alq4rinT3jH>oc>xck;sLIXF z&CA5ag%d2}#u)SiJ>0M)41y@^R3IZ|w$D-7y@AUp-P;E*VpTSsynM!!h2~_6t{bFS z(mw@=3vFNMWANv#s2jd#U&oQ0WV+SAaD`WpHlo1rb3ManYa8-A90nHSvW)Uc*OiPC zD6JL}Qun^L?ayVaQ*+uGDY|CVD>h_e5z)Jc6Qx$^4?MFA#kvx>=N%K%F`&1=+yN50 zpm%y+7lUj7Xsk{$;!yA_ZK$TZXshM>oQ}t@u@^QG64RXC7s**G_cJ8_H3F%L9xst! zGT|@7hP+p+R(uXoCyf4D9*Y-}piKo>t`1fCJ^qLVs!23B(b!dv;?r(2R{MXFx3Uje zvZ>+;uf?5JhQN=~=gbI_$NFgOU;CnSB5O&(Ds(U7jS!7rN?BZ@jh29sXNs9_GL)&$ zFn@#m24RA+=Am=tF{k9!$4vBTR+vt%gJ z)M}_S@HZH{qr}PWfnVKpLIRK#eu4->VpRSM2!ndeefM4KVuN*{I~I(BXh1=~a2iz(J;U1OLAHzkJ>PYzR<<&814Z zxFj{_ITa&A-GZCO2whGW$E?c~oXx$Gf~KA86BG5@?8yI%Fc`~~u-vtFr>6KJp6fmV z4=}|HHjbG)uVp1TYW+q(P)$ubf$Lq6dga$onIb`}gRIK$N@__LJ zY)wR1Qtz>%hlA>*lJ8Ylx}2;M;r-Xp;MugO6_;b2QLRKWF<}MK##^3Mi5yUKC({C<_Z!d0!;-Hy zMyGn7$!OyVrgINu@$8m)MFA7iuG`*Yht|I-StG@K)}wjmOF9u4g`Pv(u4Po}Y}UD` z+rQFm@PI*jCSSiKMEl0Ncv)vg5_n=wpL)TC>kKppEi_L`2N=MIJNE65mmT-0iuTOK zi*a)FHDNHl7JqrWXlU9v(d=7SeBxpoEpj!}Uz`O$^EmVv+rD|8tvovZhbNvFNR@wB zdfTWLW5ebrhr{yw!OfnVmzM__PycuxAw@lUxDrw@4H`P2?2<%uk#^_Pcg%-loz$fK z66vDyCiVCc8%D~=XrH>Y3t&PcbZx#gYBx7!t4j;R#k-skGBXHMPl*K-l4zT&hLC|Y zqv=k1eu+v5Pl^56u5s9Ztm@!IPn;-5nh9qNwZV=)$n2_(Bw^Q)qt6$a-`)Y0j~ zD&6- zGvsaj!=&(ssOTGZ*tEKnb$4Z_n+TVkhK9;k7LL?!Ak;+538bDLYIPc21cLu>hV^kLX ze|z$mu!-pbLVI7S;lrLk)6#+Ehj*7Iv=RBBLY_ks^4*{Fo|k`9_|irQ4y=F2$#YM22d`@e&EHL zF+*hDK92d;s|_2#`;NyzUMv2IIS2(Vp06i4f%T$TQpXFr8fNRX6Z4KRZUpsj@+5zp zwe6yJp@@G}7Kcp(obGy$<|-Wj@vsSgM5D2LZ{-(O_05}pd})-~ zR();5L2x&0?ARN%cw^=70)}wMJ*0sPwSzU(p}t!VyUxYrpoJDRzt~*6oH*#YU+S zbYwj!NH)uN4GK-M6%}T_cGU3gJLN^y9K}<8wE|x zii&nz5T30kOyOv>c{{_iq$qJbSCq@=sSsJvOBpG`V1P{%fXG5QUG)-<2um&CmdUV5ClYgml>w^@}uffO; z@%FMBl-xQM5cWi!&AXizimDY9m9MW>Pnp{ODVrQOdh(-#RcZvDP4O--q*vHludmV}c8&xoM&= z&~#J4!tusxo(!oi40fI~qfH32laARdAp7X0$*{njzuuSW;V-`LR@J z%g>;nI>V)?0Xny^+p>L&y@!MLhi4IoBR6X5z-PWch(v*pz3`ZRd>P|0W#xi&gN>|_ z0(-492YcpuoJhXjT} zK1}ZAN;icR+9twK+Evu=wKt}sD#=H_!kZB_w0XCKL8x?zfbyyN8*cNos;vRs*vCKs zmkGUG7YqCX>&GI~((>;~WxmHm?laGUK4(oha1A9EuK;m7bu3!sq8EOBaNViD1A@h8 zrLW8bGW^Kii_rRdx6DT&*I326c2i{{X{yVhtN8OCyvHBI?|)xK4V~)6*k;v_C4KAFH>vbFkF9*Z(&WZT9;L zHwcvsRWw&qkCBS$!|iRg5XF5Pr{uo9Fng*G^Al!00=_?5PksSX94M|kchd#Fd1NE( z9jqGVff_@|7FCB#nU$7Dwcn3=1Qytsq1ExR2O2eK7 zi!{?ED054vyY6VMW&ul~FzB(LYpQ<_p4T~W>beO2+-S7@WOsk=?`iHIOXnV*qupO~ za({{y6vv3=#`hU-xxdCd9b{LK1qidpLM9#J6H2RR{u%3$pyuju6A?}3p>EPd!BD*v zeByI$MDIL0On>7ud!@@|2|jLDXZ~ zlaPD7*a(V_C*Z1>x3;$Cs8q6wkO69JZfUR#U?H3cH{m}q596HIYzjI5fvkOee<=!K ze)6pOir9p{}zl+)GO2vMOJit0>F5 zsf#L_i470zWapL;uRndw?6zL|+cMJr-DZH;>or7pGDq>Pm`?Wt!$dk_Rb#VC%>z6M zhmWsskNdoVXB&{IX1#?>y!Ko0O2OQB3|Oh4i$Cr#hMyCvIgK>zmalczd@6#cE7472@kAcgH@j|}Z7to&CqPLfty zHDF9X?)TG1|-Ko{z+vgdXBol@Q;SAvZjqh1MI{D%5Dn3u-5DM?2r@v`T_cYRCYhBzu zx&uQ(vF+_X9Al1EKyTx_1XTy1BCF~7?;16S9y5|xDJ*3WKmnK}$l-d9UGA^{g-S8f z1p$N12r#5-xm8(cgcG|n%?P2|44p3cEOPF9m7o0`DJ{?#zRn3Ly&@k!$ctgOYb(Wg z!Y+EtgYfXOSK4z%C9tUgrJT!aIb!DQ>K6D$U{Re%am~L^J>`!*P}0`as;zKL8-I?$ znbxw>N#5pgJrek~_ojK{C_7qoio&kXpZc%)uHK;gvomSHS@-Qfy?LQ`D!a!Jbtz-s z6N!GGlnqbVu<-EkQ*A@<-CIDN)yEmdjSb^R89y`Ok}gXt`yTg%WOO#?L&Y9GI$`WD zA(43>T4B9U7nOIN=yO6@uO0Wlci)zd?H#{hT&g0xnkBMkCz_0|NDI;VNSOsFh^W|$ zh_7+N*(}HQ;Grj??X|cJ3;pv#l1VSWsEO?w74&I~~C+B2C{_)&8diP)Nmz9_*AAYuXSaj^-8>?S76ldfB3ITxzsI7h90w|Mw<$ z4&@X=fPt$eemD;JtCYbTZ2)<_l?05I9nDay`?Hd)dE{C16B(7JgyLwdMy^~>4?h?E zhHDqouWm!nGG8pn-c(i!^!o5b$ll_(o|NSLW(Z#H9%^RiVT}aFdGs6dGs22Zza%sd z;gP+w0g8@c!6y0Hf$pk(oQFS`dyUpy1YD02vso@!IxsJ*WFYoGUR{J?4fGHZ)lyV% z*!bRt7khwLuJku@Dxb^56`eUTvB;p&M?UY~XgIs>hpk9Rz%ZF-=Yc+k%rgr0n;dS? z;te_q4W#x!40;*s`F3^=J?k7A8cOavC#fgM(uT7Td|{F~Hzu!$dKF=B7anwN zn435}?fGL|1c#7`HbW05Bg4S;E)fuV7z?(c6HMA0JKMt7P!JhgbBk^3hZWN?{Uop@ zY`HGF*O%5-$QplQLET=2gSeh2l9K+Ens+-{0CnRR;Oro+ZC#7)y8hZU$L_R}3mr?D zEUD@;lTTUCde&hbil>n|>$;gy=&>OR9Z0OPj;ih zQ@$(}4Z$oD6XkQ7q6(w;Yvz2>FV{%N$0a?D%vd$qXv847`$MmTijsA;&GPl ztMBiV`VNeqNRb|nQ_(+}nwh&~^9+T zQ(d3m?K>~lvum2{tN+zR>~DP7N1nf)O>M!wj5G53h7)mw_!@@+VS|1fhg@tX&2{iP)#Du4Y{0=x$@G(;&J z3V$QH7Tqcl&DMh>!-}WH1pCB9E=YS8>i@5!>yBsZd&7~UX3bhvF-z@LwO8$}sM^(1 zRkQZqd$so0UPbNNtAtRaiV~X$Ma_yGB;G=k0Jz}h>D{}s!jp5om5C)Y_(xBPh>y!`&1IBsfAugriPhvio(wYE}I zb}PRjc@O7*`r-G?g&6wxNQV_L-ej9n+RNmzCYQ~j&iU@jw5fO+&d29b{)O21uxj?e%ItVvW9XOufrUO-(gLb!?<4?EU>)P#h44GK#>uzb69C z&=G(#fWTw)KHrN2^DeL5Wn+_Mg3M6G=vw?w8s$}RHrxA)(x1xhM#BP~;+~L>dr4A7 z;&fm1n6rt^8Gthtd>@_Jlvlg|QkLe=DLqm{3hRIC&Ax3UHh=y>bW~Er>hz`Od<@~i zug%=*;u>C>EB1y1=|P;|rGM|~=A&!wQx)dT_ER<@g%?V!oLxmsqZ40#4=)AoPeS|V z(p60QW3!%q*gd%dAXk=W8=JE`$}v~1%9yDKcZ*TLphI!!hv)3TArsw^K@Asw%=pM#Bsue;Kt5b9*8ZVD%N%$7 zE>i+Kz(U3crAFQ%s0u#ube<;QimXXz2d;nu2WOhEgA zKW5MM&p0|WWR(Z(&Wx?w?^nolo8LX5Ux!=r0~pRHlINmwn>Hw4|uw-iqdo z@Bumoq_m8Tg0N9AP-Swk+8J5oD&NNusCKZ)IFq%zQe-a>OB}qDMTVre4}Ci!(o1}U z!4KvPk=@hlH{W zog;FEL>y&A?tZ|jl?_XYJZg((Ty5@3BuJ#AiC3w}#6$yzr6TuOWG~D9nHcG-si`Go zY+sN{-{y;M#u{;hJGe%$8b7jiHc$Qij{qC!`ksrWM58L>Wt}s;>gqJDa>pqxyHf%m z3i=?oF2%}_Y)Z0Wsr2v{ZDKdAVpX`N)o7YTb;&2uJ$DG7zST9E$}f}AR&KKsH528@ znV#FL;mxfmsmd)|@H8qRR*=j6{pXs-I@ISI_$i&iC#nYKzZNdHTy0i^PY~YUFtzvS z1T9qtIw2kwf{HLnn7XDSTZ{RODXQ7*UoK(Z92shD!3e{47^c92ysL4Z$5ld^g5!j8w0s?6-_*Kwmf~*0Y~O{5Mm} z{<#R30OLdy^CU24LZ;Qzu5zX)+#bC_Eq2uRM z>09@eoe%RCp+rd~mf5^PSqp!W4?YWZFC&9D&vpE*vXHGzUY{Ut!{}xAhdZ+Htu_8Q zBh2k#cVGiutuW~+g%8qAHRuU!RzXKbog`CG4*%4*&$`>mpo>s^KG9d(84Bu}aT z0Dn_6>?||9%30O`royT-*a{@M0=EN^m{nN5lxIC?Fey%vUyV2Cv+h$s1n&8&cE^mg z3lex7)gVmvshIUi^+iubXB`jJvThX^ZQ{FI+JvSD@(!%vNsLTK;-&R(n#r%j|q!2>vhgt^}lJkNJ9YZnCcs-QBWlv72-uHy+K#lw0$>q8ypt z+`ONVxAU0 zCeXfeQr_F!%e*Dn+xij*f3C-k7k1J~Z*rUPEcRX|kgG&vXH9K_^{lL{OskAfKyJL- zC9G6Dw222y6L`j#-;f<9;*_-ui8$!#N6vPH z^?Ae4*o*fm7y(gSmc9P$GX2xVZ7eC>r~DEag>ze|mzUR=Y41sOdbX1xIK#gIQ3%FWArn%{a-J4QXK0ykDE{#hqVT_RXeA$E`0w&@Lz`e=qp z8B&K!Y(gzZ%UX`+8jXq(DW!|iiIp0j$K~DJ8lAgoXj&TiArqvqb7loUfo6!ZEDb-R zU&Mi^Umr7UY;4H%0)n{EJs^%p`N$wTZ*OcNxbzfMXzA8CJIFv2AEp|7&^S!kz3mHa za$am2E1i1xOn}|sQ8zfZKD}NWo@L-T%_T$Ftdj+va)Xt=>4>r);~iENdki!A5?v(o z?7AVD5r^}`nU81cBo6&Ip>8_4m3!aH%WIs2x~kwM7g^}k%M44w_HE!)m~!!5`7pfi z+HCys;Ix8G;oB8{@!S(%(V19C$mLE10jb3K*_kCv;`cpW&$oC2CMF!2^>6f)%jE81 z)I7ais=Y;PVkKaT&7_jX-yCN&7ZhnxknyYZ6bow_80m zCuRTmc$GRXeQ?A8jPh+Dst0|cAG~MupkSk*o;PEzRYk9&^H2^F(~!{h`iD;l9G8cL zW=JfD_OFOTJwv!1gbLL?_g6@U3#+umnE)E{FBAw(+;Eq}3b#(%9`RaYHolT>2`dnO zHwnD3s`xOA84L=F`vv@`gn(6-SKR|ENz3;F#ag*Bz+ph?n)~ zTqy)75ALGdL2miuG`N?*rb6A2yiulDI7rcjoid;K{GaMzWp?)5LtlR~SA~7bn6KlV z>V`69;`b`;@*O=Vag|qSgOvDEvp>%$#ml@|ybfm;WWZsDK9#RYPw-^@WWJpMvww!R z5fxziEe)|o3@uOWmDbcG+VTF4MhQB-h#K0?05&I3Uhv=_g;?nZFyW>=>bJ$dkbI>P z3Ws}mVT{z_>MZ`Bh0;f5p|8IuoW2cEA-X7C{LwGH^jw;Bn)il=AVqIM>L7#Hhwy%w z$z!i9+&xcLYq`6dye;3AK)1TM%H~jQEi&z3XVk}Kwn*H z>FYf!fL2@nCl{K}A9Ew#j1@}+^_{6Gg7><-_X&03LD%`76J}=FdcK{~;W<2+EAIg5 zk)#~s!_VJ)Q)N}*(p4&qt=H5SeLor|-wOe^?1uq(Nfnch4~8@l`G&;%q}x*qD=_1s zx3fzr!>`Hik=Nm3LoZ{O3(Dkq6!v7wQXvzvV2}|Fp6~Ar0GisK^@ZQjukG1Po4CHd zX2~WpP84Mpe6IES7~gxBHc`xQDV;ZV_=IdrtL@Iu#{rGVjHm4yj@;Tg+3}}So-!TB zwSryJGMW|el7QLu;huUP5+JZIQ~xM`6fG3Ap3;5T#rJO0Q*8{p?(v8k^hFn+RS_Sc z@H{7h+#$`prKhfF`e;6>F}c8tx(;?F-h#KTR!R2cRQ&MYYZ@tOKzSOW{t0u}oG09| z)nT6*`}9WpZomQAD3!^2HGyu>0oFgixg#-au5+hLe(S0ikrZkJ7pDbWWY8$w#a#uU z$ymKbyK7WC&&j~<#*p4;d;-b&-z(&Ne*&nwAi)DM+vPQ9sDujgvtc;Qyg7WRTg>9OJQ zJBhne5V8rm+L8e}tqCR#Z|vsq-bIw|Q$636UfTa%`!0csD(%@Df+jA~r_KKVnP1-_ z65|%*gC9o0XE3_BR!sX7Y?_zKMg`Y+&?PVxJzvs+sW37>pXCMkPvsK#O+Xuug_)RI zoj(hzEc_Z>G!{{K3@mneys*8aV_d86-$s`i|64Jkz#*39_|x~(vSFN@4gZ>-o}N-!uWU802z|8)3qkGHs31}}4MO_875AbJ{HV_j|EX>myB z(r!|M5Z8t{PD>E$9mG4xPGa6*n};_T6{hd{Uvike`RMQmed;G)ghU?)`XLOid(f*G zqrgB6Dj*=hl}fd-ZrVDsGlGXinJ2329lqQLJh^kF&tlSo6{4LXcMcQ$<-gsRU!3sD zm|xG@mxP`=ZP6E&IShwj-+7Z!xb5`m&-)YypTCcrdbdmBpQ&U>iH|pGcLKQST)vur zMj^LZI9dq^(p0rWUO5th49xLKMDhRG*oupYe7H=JIgC>;o3e1st>>+~!%F|)T|G@> zo1tRO2z%8V)8=ro*?}LEnU0;%l+C8S3jhA_j{AVw`~kjC z=Vh3u4;vk5;55kH&TexZfWHp@g&zU1wMY8-z;UE}llf6)#ql!S=(3^lv89H6pB?7j z!mzzQ&tBPK$ZOJ2&E83AHiLD%@4GJa>nnpVOSbZ|0F~7*mUWQ@cpSv{X-R7p)obw= zCJG_k5&f*Rs+uV&DTj>A%t7jg8Du2GCo!t8h%S7ZF*sGRKchd!H zuR>*ObnRY~@YyI~mZ!V&poJz!oioQ1`wbZ<$^6F_Fr}4wWKxmK4s0k-=Ii@e-l*>y zh6*7K{R$J4it^vHbsl|W0YrYgbr$U`zq}EeLG4he-6#dW93CsrrUi@ZX`+<-THod1 z;9$dDLPF)KcV{oM>>Yib`x+7}X~+f?iK3}Ks!lC)zP8e4ph_9iF@N@a%C?`^yr?+F z>CQk4y|^Ql|BA1t7|Y79c-`QKFA+;5B1{AW!X~ZPDVYJU%(CR=hr5#6|5U>nKi z$PNBQ(ZIUZ&jlEN)9`O?XL>jn9EQzT50Zf{nkVI#MTLcr@_7=T8aGj=I+QHNa-|!x zj*qWnjlnhrIYVyHynesw-bc?WU?=1;nel0zI!+C{HGHt9$H+E9GPr!1+@rPOr$dd$ zF=}4ov(J*gQ<9xTd)I;OR^>I*CYa>{&Wccg{3&uI9pRK|-O!22X1)q9cfzBTEMjda zeucLhj7GRU(Un=5pQpdC+E}#F!R}N{d(ylO<{x9DrK9>7zvGgyb}>h^Gzg*{S5%%W z-ct*4Pm|GBEw#H(GbS(Z`#4)apRaE#ksf6e0IVK4HNcyC$0^|zrmoI!?LyD_z(w7{ ziIlxj+C~AIk*IFjugD4l{VUiD1ttkiFXMZNwQ8#<*9k#$vS_CPtEde`#4{gQ$)&g| zBSUTo0{_Eizxw+6gj5U<3{6^DjSOVJr=uhHlDKzr3H_~MKM$CDb9B~f#Ka^+MB-6! zCLWSD6NAAhtg<_WTF3boy6WnIHeez9h6dro1ifVY&10e8;&qk|%+Pgbk4KJV32K37|aUmS3bN zBP^+Ye?48UjXR#!7P?v9Z-C0*!(6%3gf8Y_UHQ53KqRUlo)&O^BOW_g9JKeh9l0tl zDcQkC9gmGF?j5=#!iR>IzB1>VBDcUM$up;ScF{#z z{F6jxTN2kqCjxB2?|;w)*u z=ZWh^+5{D+O~r&2JO_gYgp2KIKf@h=a&jeE6%NFYO67M*#*PAg>Sj~uf62t`j`Pg9}Gg~QAkijaGdj)Cy6GgmYYXBYg0$$x~5GE$3LwbC+>gwtS zRRizxCW=~H2EZ+yQMEtFd}vFG*ujAl>u(O*X^9$3i!d{LruI~c4pF@R^6wX&XMY{K zOqpvNj=GF5%pW;Z=Vl2IDCMdZ^p!|atGK>HsKOx0qDGVIOZ=csl6IJGk}UpW2gYTpq!2})4%5*rZIN#7r6FAWL))6pyWwkkAo^dD4OK-qkzu8*mqZ@hYl#Rrr zK2fR&k3`x^ePuSCeqZg^1bT@e&;r1%)IkBA6QsVO0SP+|(wX5l%h+MS;baYk9=X| zVI&tzOWMtdaMbKC&xja%Oki}%__KzPc04RUm@z9lKUHtb!m&HaQq@w1#CKbMKmtjS zG71Ir=YD4ezUHN1rU51KI9VnRaMUYMc-Re~WhS%GxYQ!H)||IuMQ!jUh-PbW-V3?C z+D08LweB-Gm)`jvjJ_$w@RiQ`zUCqZDRH^tMF$1-J;wi8C>Q9;$i#HL420?~>+T(k z!esAq|40q3CGhRVJp_f>=L+lIsc?k-691A5V2R>gLQd7_&tEhiGks2q@t+OLm}0FV zoUX<*gO}_*5{)4qZyEO*Xlc+Ul=wgv&H9X^Z<Eh(M75z;Tjeo6^#d&fk zyep1jv($={($nISDUpu38kO+S-2eW&+yRhHksbGzxGkXST=f5@b93L}Fwm9@*ksP+ zpXvfT8nY=upf*>;(&ZqHzVqSVzdZ`n@y?t6@;WP#PMY{a#v5W7?^3go&Uq%_PVLRU z@{_6>{xPbWF)b@Ag8)3Tle#=mlH)jBQXYre-tI;=@YH(Yu^xGlg522L9q#cgP*CE2 z@HNyffAc>LKK8@16mADLd5&Zz-=A3{R}RpHvyPqYs<0m~0(r!Ha=@jA@pwj%fxaq8 zw=ahY66hWSnlNR46{7rzSKwkmfuG9~d^VNrG+*B%1iTRLPoBDn>&_1rgDI!;Du7zj zN6)KfaQTuSe*ek?>VRgCj*e)X1Q|$(Z8&bhhBP3zOXBdNz%{x%Ocw*g^5-)~4}msY zS7Y_{^-w^8^Z;M!-J5rWF^*VPSJ>-2A}Gd~a9f5?_WtadTEW2=2a7fkAQ%h%hgMF% z>IQn~^Re~7e>PA-@!x|Dm_kIr$|1!azHA>z<4iyc6fE7qC@%o?w|@a}bVYHcmt~5q z?i`b+wTNfGKsk-?O8ng;wBQb;nnu=|S9TCKSye*=T2)h%a~aLFThvUI5mYMZ@(2RZ zcu();oqYyRXg)U;Wc~YPQ#l0h5;!Zid+L{iA!3lVN4h$9H6}xJM+mBh1d(<^- z+_dG7B+W)Xj#IC6dRrSA(vFFbI}QlHwKHSM=G2 z^IQq9xhGHS_{Zkp;t$ImrF9IA|J&$Ltnx-x1Ix(3#H5wqX;pmOA19b1HjyhELbI_)kF~>N?0AsMqpuW>o{t$J; zk6+2tfgn|miFk93!3^X{xK!^S93%~H9Bc5?kpikl4N2yd808Y4u_m@Gd;b_)KM(*? z93RQ7t$vM_0)AD;*z$DM^TpWye%1T78NrL zs>d)lm{tXS>sCd-Uw8Tpz4wKxMSK@!=GAQ1Y%it4E)@~FoIglt`wFyq}2kza1F zA%-tAjW}++)pSAi3f~U>JHtxnd$m3aoUf9GM4fT?bh@;LcQfraaJ&Z}4V2}xN-AX2 zBzSC_CO#MN;2Ny|tlB_zdsjslM90lbMbHqOS!hbm%jQx(Sf3i4TJi{_eRu1&hVN!I t*~6}98s+p79~ja}|M{(u6TS`MWvEKaJJj>^zg{5Vqp7N=Qm_0f;(rpT0g(Uz literal 0 HcmV?d00001 diff --git a/index.html b/index.html new file mode 100644 index 0000000..1c42609 --- /dev/null +++ b/index.html @@ -0,0 +1,374 @@ + + + + + + + +Netron + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+ + + + + + +
+
+
+ + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..68a4e90 --- /dev/null +++ b/index.js @@ -0,0 +1,1066 @@ + +/* eslint "no-global-assign": ["error", {"exceptions": [ "TextDecoder", "TextEncoder", "URLSearchParams" ] } ] */ +/* global view */ + +var host = {}; + +host.BrowserHost = class { + + constructor() { + this._document = window.document; + this._window = window; + this._navigator = navigator; + if (this._window.location.hostname.endsWith('.github.io')) { + this._window.location.replace('https://netron.app'); + } + this._window.eval = () => { + throw new Error('window.eval() not supported.'); + }; + this._meta = {}; + for (const element of Array.from(this._document.getElementsByTagName('meta'))) { + if (element.content) { + this._meta[element.name] = this._meta[element.name] || []; + this._meta[element.name].push(element.content); + } + } + this._type = this._meta.type ? this._meta.type[0] : 'Browser'; + this._version = this._meta.version ? this._meta.version[0] : null; + this._telemetry = this._version && this._version !== '0.0.0'; + this._environment = new Map(); + this._environment.set('zoom', 'scroll'); + // this._environment.set('zoom', 'drag'); + } + + get window() { + return this._window; + } + + get document() { + return this._document; + } + + get version() { + return this._version; + } + + get type() { + return this._type; + } + + get agent() { + const userAgent = this._navigator.userAgent.toLowerCase(); + if (userAgent.indexOf('safari') !== -1 && userAgent.indexOf('chrome') === -1) { + return 'safari'; + } + return 'any'; + } + + initialize(view) { + this._view = view; + return new Promise((resolve /*, reject */) => { + const accept = () => { + if (this._telemetry) { + const script = this.document.createElement('script'); + script.setAttribute('type', 'text/javascript'); + script.setAttribute('src', 'https://www.google-analytics.com/analytics.js'); + script.onload = () => { + if (this.window.ga) { + this.window.ga.l = 1 * new Date(); + this.window.ga('create', 'UA-54146-13', 'auto'); + this.window.ga('set', 'anonymizeIp', true); + } + resolve(); + }; + script.onerror = () => { + resolve(); + }; + this.document.body.appendChild(script); + } + else { + resolve(); + } + }; + const request = () => { + this._view.show('welcome consent'); + const acceptButton = this.document.getElementById('consent-accept-button'); + if (acceptButton) { + acceptButton.addEventListener('click', () => { + this._setCookie('consent', 'yes', 30); + accept(); + }); + } + }; + if (this._getCookie('consent')) { + accept(); + } + else { + this._request('https://ipinfo.io/json', { 'Content-Type': 'application/json' }, 'utf-8', 2000).then((text) => { + try { + const json = JSON.parse(text); + const countries = ['AT', 'BE', 'BG', 'HR', 'CZ', 'CY', 'DK', 'EE', 'FI', 'FR', 'DE', 'EL', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'NO', 'PL', 'PT', 'SK', 'ES', 'SE', 'GB', 'UK', 'GR', 'EU', 'RO']; + if (json && json.country && !countries.indexOf(json.country) !== -1) { + this._setCookie('consent', Date.now(), 30); + accept(); + } + else { + request(); + } + } + catch (err) { + request(); + } + }).catch(() => { + request(); + }); + } + }); + } + + start() { + this.window.addEventListener('error', (e) => { + this.exception(e.error, true); + }); + + const params = new URLSearchParams(this.window.location.search); + this._environment.set('zoom', params.has('zoom') ? params.get('zoom') : this._environment.get('zoom')); + + this._menu = new host.Dropdown(this, 'menu-button', 'menu-dropdown'); + this._menu.add({ + label: 'Properties...', + accelerator: 'CmdOrCtrl+Enter', + click: () => this._view.showModelProperties() + }); + this._menu.add({}); + this._menu.add({ + label: 'Find...', + accelerator: 'CmdOrCtrl+F', + click: () => this._view.find() + }); + this._menu.add({}); + this._menu.add({ + label: () => this._view.options.attributes ? 'Hide Attributes' : 'Show Attributes', + accelerator: 'CmdOrCtrl+D', + click: () => this._view.toggle('attributes') + }); + this._menu.add({ + label: () => this._view.options.initializers ? 'Hide Initializers' : 'Show Initializers', + accelerator: 'CmdOrCtrl+I', + click: () => this._view.toggle('initializers') + }); + this._menu.add({ + label: () => this._view.options.names ? 'Hide Names' : 'Show Names', + accelerator: 'CmdOrCtrl+U', + click: () => this._view.toggle('names') + }); + this._menu.add({ + label: () => this._view.options.direction === 'vertical' ? 'Show Horizontal' : 'Show Vertical', + accelerator: 'CmdOrCtrl+K', + click: () => this._view.toggle('direction') + }); + this._menu.add({ + label: () => this._view.options.mousewheel === 'scroll' ? 'Mouse Wheel: Zoom' : 'Mouse Wheel: Scroll', + accelerator: 'CmdOrCtrl+M', + click: () => this._view.toggle('mousewheel') + }); + this._menu.add({}); + this._menu.add({ + label: 'Zoom In', + accelerator: 'Shift+Up', + click: () => this.document.getElementById('zoom-in-button').click() + }); + this._menu.add({ + label: 'Zoom Out', + accelerator: 'Shift+Down', + click: () => this.document.getElementById('zoom-out-button').click() + }); + this._menu.add({ + label: 'Actual Size', + accelerator: 'Shift+Backspace', + click: () => this._view.resetZoom() + }); + this._menu.add({}); + this._menu.add({ + label: 'Export as PNG', + accelerator: 'CmdOrCtrl+Shift+E', + click: () => this._view.export(document.title + '.png') + }); + this._menu.add({ + label: 'Export as SVG', + accelerator: 'CmdOrCtrl+Alt+E', + click: () => this._view.export(document.title + '.svg') + }); + this.document.getElementById('menu-button').addEventListener('click', (e) => { + this._menu.toggle(); + e.preventDefault(); + }); + this._menu.add({}); + this._menu.add({ + label: 'About ' + this.document.title, + click: () => this._about() + }); + + this.document.getElementById('version').innerText = this.version; + + if (this._meta.file) { + const url = this._meta.file[0]; + if (this._view.accept(url)) { + this._openModel(this._url(url), null); + return; + } + } + + const url = params.get('url'); + if (url) { + const identifier = params.get('identifier') || null; + const location = url.replace(new RegExp('^https://github.com/([\\w]*/[\\w]*)/blob/([\\w/_.]*)(\\?raw=true)?$'), 'https://raw.githubusercontent.com/$1/$2'); + if (this._view.accept(identifier || location)) { + this._openModel(location, identifier); + return; + } + } + + const gist = params.get('gist'); + if (gist) { + this._openGist(gist); + return; + } + + const openFileButton = this.document.getElementById('open-file-button'); + const openFileDialog = this.document.getElementById('open-file-dialog'); + if (openFileButton && openFileDialog) { + openFileButton.addEventListener('click', () => { + openFileDialog.value = ''; + openFileDialog.click(); + }); + openFileDialog.addEventListener('change', (e) => { + if (e.target && e.target.files && e.target.files.length > 0) { + const files = Array.from(e.target.files); + const file = files.find((file) => this._view.accept(file.name)); + // console.log(files); + if (file) { + this._open(file, files); + } + } + }); + } + const githubButton = this.document.getElementById('github-button'); + const githubLink = this.document.getElementById('logo-github'); + if (githubButton && githubLink) { + githubButton.style.opacity = 1; + githubButton.addEventListener('click', () => { + this.openURL(githubLink.href); + }); + } + this.document.addEventListener('dragover', (e) => { + e.preventDefault(); + }); + this.document.addEventListener('drop', (e) => { + e.preventDefault(); + }); + this.document.body.addEventListener('drop', (e) => { + e.preventDefault(); + if (e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files.length > 0) { + const files = Array.from(e.dataTransfer.files); + const file = files.find((file) => this._view.accept(file.name)); + if (file) { + this._open(file, files); + } + } + }); + + this._view.show('welcome'); + } + + environment(name) { + return this._environment.get(name); + } + + error(message, detail) { + alert((message == 'Error' ? '' : message + ' ') + detail); + } + + confirm(message, detail) { + return confirm(message + ' ' + detail); + } + + require(id) { + const url = this._url(id + '.js'); + this.window.__modules__ = this.window.__modules__ || {}; + // console.log(this.window.__modules__) + // console.log(url) // file:///C:/Users/ZhangGe/Desktop/netron/source/./onnx.js + // console.log(this.window.__modules__[url]) // undefined + if (this.window.__modules__[url]) { + return Promise.resolve(this.window.__exports__[url]); + } + return new Promise((resolve, reject) => { + // console.log('here') + this.window.module = { exports: {} }; + const script = document.createElement('script'); + script.setAttribute('id', id); + script.setAttribute('type', 'text/javascript'); + script.setAttribute('src', url); + script.onload = (e) => { + if (this.window.module && this.window.module.exports) { + const exports = this.window.module.exports; + delete this.window.module; + this.window.__modules__[id] = exports; + // console.log('here') + // console.log(exports) + resolve(exports); + } + else { + reject(new Error('The script \'' + e.target.src + '\' has no exports.')); + } + }; + script.onerror = (e) => { + delete this.window.module; + reject(new Error('The script \'' + e.target.src + '\' failed to load.')); + }; + this.document.head.appendChild(script); + }); + } + + save(name, extension, defaultPath, callback) { + callback(defaultPath + '.' + extension); + } + + export(file, blob) { + const element = this.document.createElement('a'); + element.download = file; + element.href = URL.createObjectURL(blob); + this.document.body.appendChild(element); + element.click(); + this.document.body.removeChild(element); + } + + request(file, encoding, base) { + const url = base ? (base + '/' + file) : this._url(file); + return this._request(url, null, encoding); + } + + openURL(url) { + this.window.location = url; + } + + exception(error, fatal) { + if (this._telemetry && this.window.ga && error.telemetry !== false) { + const description = []; + description.push((error && error.name ? (error.name + ': ') : '') + (error && error.message ? error.message : '(null)')); + if (error.stack) { + const match = error.stack.match(/\n {4}at (.*)\((.*)\)/); + if (match) { + description.push(match[1] + '(' + match[2].split('/').pop() + ')'); + } + else { + description.push(error.stack.split('\n').shift()); + } + } + this.window.ga('send', 'exception', { + exDescription: description.join(' @ '), + exFatal: fatal, + appName: this.type, + appVersion: this.version + }); + } + } + + screen(name) { + if (this._telemetry && this.window.ga) { + this.window.ga('send', 'screenview', { + screenName: name, + appName: this.type, + appVersion: this.version + }); + } + } + + event(category, action, label, value) { + if (this._telemetry && this.window.ga) { + this.window.ga('send', 'event', { + eventCategory: category, + eventAction: action, + eventLabel: label, + eventValue: value, + appName: this.type, + appVersion: this.version + }); + } + } + + _request(url, headers, encoding, timeout) { + return new Promise((resolve, reject) => { + const request = new XMLHttpRequest(); + if (!encoding) { + request.responseType = 'arraybuffer'; + } + if (timeout) { + request.timeout = timeout; + } + const error = (status) => { + const err = new Error("The web request failed with status code " + status + " at '" + url + "'."); + err.type = 'error'; + err.url = url; + return err; + }; + request.onload = () => { + if (request.status == 200) { + if (request.responseType == 'arraybuffer') { + resolve(new host.BrowserHost.BinaryStream(new Uint8Array(request.response))); + } + else { + resolve(request.responseText); + } + } + else { + reject(error(request.status)); + } + }; + request.onerror = (e) => { + const err = error(request.status); + err.type = e.type; + reject(err); + }; + request.ontimeout = () => { + request.abort(); + const err = new Error("The web request timed out in '" + url + "'."); + err.type = 'timeout'; + err.url = url; + reject(err); + }; + request.open('GET', url, true); + if (headers) { + for (const name of Object.keys(headers)) { + request.setRequestHeader(name, headers[name]); + } + } + request.send(); + }); + } + + _url(file) { + let url = file; + if (this.window && this.window.location && this.window.location.href) { + let location = this.window.location.href.split('?').shift(); + if (location.endsWith('.html')) { + location = location.split('/').slice(0, -1).join('/'); + } + if (location.endsWith('/')) { + location = location.slice(0, -1); + } + url = location + '/' + (file.startsWith('/') ? file.substring(1) : file); + } + return url; + } + + _openModel(url, identifier) { + url = url + ((/\?/).test(url) ? '&' : '?') + 'cb=' + (new Date()).getTime(); + this._view.show('welcome spinner'); + this._request(url).then((buffer) => { + const context = new host.BrowserHost.BrowserContext(this, url, identifier, buffer); + this._view.open(context).then(() => { + this.document.title = identifier || context.identifier; + }).catch((err) => { + if (err) { + this._view.error(err, null, 'welcome'); + } + }); + }).catch((err) => { + this.error('Model load request failed.', err.message); + this._view.show('welcome'); + }); + } + + _open(file, files) { + this._view.show('welcome spinner'); + const context = new host.BrowserHost.BrowserFileContext(this, file, files); + // console.log(context); + // console.log(model); + context.open().then(() => { + // console.log(context); + // console.log(model); // model is not defined + return this._view.open(context).then((model) => { + // console.log("_open() in index.js is called"); + // console.log(model); + this._view.show(null); + this.document.title = files[0].name; + return model; + }); + }).catch((error) => { + this._view.error(error, null, null); + }); + } + + _openGist(gist) { + this._view.show('welcome spinner'); + const url = 'https://api.github.com/gists/' + gist; + this._request(url, { 'Content-Type': 'application/json' }, 'utf-8').then((text) => { + const json = JSON.parse(text); + if (json.message) { + this.error('Error while loading Gist.', json.message); + return; + } + const key = Object.keys(json.files).find((key) => this._view.accept(json.files[key].filename)); + if (!key) { + this.error('Error while loading Gist.', 'Gist does not contain a model file.'); + return; + } + const file = json.files[key]; + const identifier = file.filename; + const encoder = new TextEncoder(); + const buffer = encoder.encode(file.content); + const context = new host.BrowserHost.BrowserContext(this, '', identifier, buffer); + this._view.open(context).then(() => { + this.document.title = identifier; + }).catch((error) => { + if (error) { + this._view.show(error.name, error, 'welcome'); + } + }); + }).catch((err) => { + this._view.show('Model load request failed.', err, 'welcome'); + }); + } + + _setCookie(name, value, days) { + const date = new Date(); + date.setTime(date.getTime() + ((typeof days !== "number" ? 365 : days) * 24 * 60 * 60 * 1000)); + document.cookie = name + "=" + value + ";path=/;expires=" + date.toUTCString(); + } + + _getCookie(name) { + const cookie = '; ' + document.cookie; + const parts = cookie.split('; ' + name + '='); + return parts.length < 2 ? undefined : parts.pop().split(';').shift(); + } + + _about() { + const self = this; + const eventHandler = () => { + this.window.removeEventListener('keydown', eventHandler); + self.document.body.removeEventListener('click', eventHandler); + self._view.show('default'); + }; + this.window.addEventListener('keydown', eventHandler); + this.document.body.addEventListener('click', eventHandler); + this._view.show('about'); + } +}; + +host.Dropdown = class { + + constructor(host, button, dropdown) { + this._host = host; + this._dropdown = this._host.document.getElementById(dropdown); + this._button = this._host.document.getElementById(button); + this._items = []; + this._apple = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform); + this._acceleratorMap = {}; + this._host.window.addEventListener('keydown', (e) => { + let code = e.keyCode; + code |= ((e.ctrlKey && !this._apple) || (e.metaKey && this._apple)) ? 0x0400 : 0; + code |= e.altKey ? 0x0200 : 0; + code |= e.shiftKey ? 0x0100 : 0; + if (code == 0x001b) { // Escape + this.close(); + return; + } + const item = this._acceleratorMap[code.toString()]; + if (item) { + item.click(); + e.preventDefault(); + } + }); + this._host.document.body.addEventListener('click', (e) => { + if (!this._button.contains(e.target)) { + this.close(); + } + }); + } + + add(item) { + const accelerator = item.accelerator; + if (accelerator) { + let cmdOrCtrl = false; + let alt = false; + let shift = false; + let key = ''; + for (const part of item.accelerator.split('+')) { + switch (part) { + case 'CmdOrCtrl': cmdOrCtrl = true; break; + case 'Alt': alt = true; break; + case 'Shift': shift = true; break; + default: key = part; break; + } + } + if (key !== '') { + item.accelerator = {}; + item.accelerator.text = ''; + if (this._apple) { + item.accelerator.text += alt ? '⌥' : ''; + item.accelerator.text += shift ? '⇧' : ''; + item.accelerator.text += cmdOrCtrl ? '⌘' : ''; + const keyTable = { 'Enter': '⏎', 'Up': '↑', 'Down': '↓', 'Backspace': '⌫' }; + item.accelerator.text += keyTable[key] ? keyTable[key] : key; + } + else { + const list = []; + if (cmdOrCtrl) { + list.push('Ctrl'); + } + if (alt) { + list.push('Alt'); + } + if (shift) { + list.push('Shift'); + } + list.push(key); + item.accelerator.text = list.join('+'); + } + let code = 0; + switch (key) { + case 'Backspace': code = 0x08; break; + case 'Enter': code = 0x0D; break; + case 'Up': code = 0x26; break; + case 'Down': code = 0x28; break; + default: code = key.charCodeAt(0); break; + } + code |= cmdOrCtrl ? 0x0400 : 0; + code |= alt ? 0x0200 : 0; + code |= shift ? 0x0100 : 0; + this._acceleratorMap[code.toString()] = item; + } + } + this._items.push(item); + } + + toggle() { + + if (this._dropdown.style.display === 'block') { + this.close(); + return; + } + + while (this._dropdown.lastChild) { + this._dropdown.removeChild(this._dropdown.lastChild); + } + + for (const item of this._items) { + if (Object.keys(item).length > 0) { + const button = this._host.document.createElement('button'); + button.innerText = (typeof item.label == 'function') ? item.label() : item.label; + button.addEventListener('click', () => { + this.close(); + setTimeout(() => { + item.click(); + }, 10); + }); + this._dropdown.appendChild(button); + if (item.accelerator) { + const accelerator = this._host.document.createElement('span'); + accelerator.style.float = 'right'; + accelerator.innerHTML = item.accelerator.text; + button.appendChild(accelerator); + } + } + else { + const separator = this._host.document.createElement('div'); + separator.setAttribute('class', 'separator'); + this._dropdown.appendChild(separator); + } + } + + this._dropdown.style.display = 'block'; + } + + close() { + this._dropdown.style.display = 'none'; + } +}; + +host.BrowserHost.BinaryStream = class { + + constructor(buffer) { + this._buffer = buffer; + this._length = buffer.length; + this._position = 0; + } + + get position() { + return this._position; + } + + get length() { + return this._length; + } + + stream(length) { + const buffer = this.read(length); + return new host.BrowserHost.BinaryStream(buffer.slice(0)); + } + + seek(position) { + this._position = position >= 0 ? position : this._length + position; + } + + skip(offset) { + this._position += offset; + } + + peek(length) { + if (this._position === 0 && length === undefined) { + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + const end = this._position; + this.seek(position); + return this._buffer.subarray(position, end); + } + + read(length) { + if (this._position === 0 && length === undefined) { + this._position = this._length; + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + return this._buffer.subarray(position, this._position); + } + + byte() { + const position = this._position; + this.skip(1); + return this._buffer[position]; + } +}; + +host.BrowserHost.BrowserFileContext = class { + + constructor(host, file, blobs) { + this._host = host; + this._file = file; + this._blobs = {}; + for (const blob of blobs) { + this._blobs[blob.name] = blob; + } + } + + get identifier() { + return this._file.name; + } + + get stream() { + return this._stream; + } + + request(file, encoding, base) { + // console.log(this) + // console.log(file); + // console.log(encoding); + // console.log(base); + + if (base !== undefined) { + return this._host.request(file, encoding, base); + } + const blob = this._blobs[file]; + // console.log(blob); + + if (!blob) { + return Promise.reject(new Error("File not found '" + file + "'.")); + } + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = (e) => { + resolve(encoding ? e.target.result : new host.BrowserHost.BinaryStream(new Uint8Array(e.target.result))); + }; + reader.onerror = (e) => { + e = e || this.window.event; + let message = ''; + const error = e.target.error; + switch(error.code) { + case error.NOT_FOUND_ERR: + message = "File not found '" + file + "'."; + break; + case error.NOT_READABLE_ERR: + message = "File not readable '" + file + "'."; + break; + case error.SECURITY_ERR: + message = "File access denied '" + file + "'."; + break; + default: + message = error.message ? error.message : "File read '" + error.code.toString() + "' error '" + file + "'."; + break; + } + reject(new Error(message)); + }; + if (encoding === 'utf-8') { + reader.readAsText(blob, encoding); + } + else { + reader.readAsArrayBuffer(blob); + } + }); + } + + require(id) { + return this._host.require(id); + } + + exception(error, fatal) { + this._host.exception(error, fatal); + } + + open() { + return this.request(this._file.name, null).then((stream) => { + this._stream = stream; + // console.log(this) + }); + } +}; + +host.BrowserHost.BrowserContext = class { + + constructor(host, url, identifier, stream) { + this._host = host; + this._stream = stream; + if (identifier) { + this._identifier = identifier; + this._base = url; + if (this._base.endsWith('/')) { + this._base.substring(0, this._base.length - 1); + } + } + else { + const parts = url.split('?')[0].split('/'); + this._identifier = parts.pop(); + this._base = parts.join('/'); + } + } + + get identifier() { + return this._identifier; + } + + get stream() { + return this._stream; + } + + request(file, encoding, base) { + return this._host.request(file, encoding, base === undefined ? this._base : base); + } + + require(id) { + return this._host.require(id); + } + + exception(error, fatal) { + this._host.exception(error, fatal); + } +}; + +if (typeof TextDecoder === "undefined") { + TextDecoder = function TextDecoder(encoding) { + this._encoding = encoding; + }; + TextDecoder.prototype.decode = function decode(buffer) { + let result = ''; + const length = buffer.length; + let i = 0; + switch (this._encoding) { + case 'utf-8': + while (i < length) { + const c = buffer[i++]; + switch(c >> 4) { + case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: { + result += String.fromCharCode(c); + break; + } + case 12: case 13: { + const c2 = buffer[i++]; + result += String.fromCharCode(((c & 0x1F) << 6) | (c2 & 0x3F)); + break; + } + case 14: { + const c2 = buffer[i++]; + const c3 = buffer[i++]; + result += String.fromCharCode(((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0)); + break; + } + case 15: { + const c2 = buffer[i++]; + const c3 = buffer[i++]; + const c4 = buffer[i++]; + result += String.fromCodePoint(((c & 0x07) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F)); + } + } + } + break; + case 'ascii': + while (i < length) { + result += String.fromCharCode(buffer[i++]); + } + break; + } + return result; + }; +} + +if (typeof TextEncoder === 'undefined') { + TextEncoder = function TextEncoder() { + }; + TextEncoder.prototype.encode = function encode(str) { + "use strict"; + const length = str.length; + let resPos = -1; + const resArr = typeof Uint8Array === "undefined" ? new Array(length * 2) : new Uint8Array(length * 3); + for (let point = 0, nextcode = 0, i = 0; i !== length; ) { + point = str.charCodeAt(i); + i += 1; + if (point >= 0xD800 && point <= 0xDBFF) { + if (i === length) { + resArr[resPos += 1] = 0xef; resArr[resPos += 1] = 0xbf; + resArr[resPos += 1] = 0xbd; break; + } + nextcode = str.charCodeAt(i); + if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) { + point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000; + i += 1; + if (point > 0xffff) { + resArr[resPos += 1] = (0x1e<<3) | (point>>>18); + resArr[resPos += 1] = (0x2<<6) | ((point>>>12)&0x3f); + resArr[resPos += 1] = (0x2<<6) | ((point>>>6)&0x3f); + resArr[resPos += 1] = (0x2<<6) | (point&0x3f); + continue; + } + } + else { + resArr[resPos += 1] = 0xef; resArr[resPos += 1] = 0xbf; + resArr[resPos += 1] = 0xbd; continue; + } + } + if (point <= 0x007f) { + resArr[resPos += 1] = (0x0<<7) | point; + } + else if (point <= 0x07ff) { + resArr[resPos += 1] = (0x6<<5) | (point>>>6); + resArr[resPos += 1] = (0x2<<6) | (point&0x3f); + } + else { + resArr[resPos += 1] = (0xe<<4) | (point>>>12); + resArr[resPos += 1] = (0x2<<6) | ((point>>>6)&0x3f); + resArr[resPos += 1] = (0x2<<6) | (point&0x3f); + } + } + if (typeof Uint8Array!=="undefined") { + return new Uint8Array(resArr.buffer.slice(0, resPos+1)); + } + else { + return resArr.length === resPos + 1 ? resArr : resArr.slice(0, resPos + 1); + } + }; + TextEncoder.prototype.toString = function() { + return "[object TextEncoder]"; + }; + try { + Object.defineProperty(TextEncoder.prototype,"encoding", { + get:function() { + if (Object.prototype.isPrototypeOf.call(TextEncoder.prototype, this)) { + return"utf-8"; + } + else { + throw TypeError("Illegal invocation"); + } + } + }); + } + catch (e) { + TextEncoder.prototype.encoding = "utf-8"; + } + if (typeof Symbol !== "undefined") { + TextEncoder.prototype[Symbol.toStringTag] = "TextEncoder"; + } +} + +if (typeof URLSearchParams === 'undefined') { + URLSearchParams = function URLSearchParams(search) { + const decode = (str) => { + return str.replace(/[ +]/g, '%20').replace(/(%[a-f0-9]{2})+/ig, (match) => { return decodeURIComponent(match); }); + }; + this._dict = {}; + if (typeof search === 'string') { + search = search.indexOf('?') === 0 ? search.substring(1) : search; + const properties = search.split('&'); + for (const property of properties) { + const index = property.indexOf('='); + const name = (index > -1) ? decode(property.substring(0, index)) : decode(property); + const value = (index > -1) ? decode(property.substring(index + 1)) : ''; + if (!Object.prototype.hasOwnProperty.call(this._dict, name)) { + this._dict[name] = []; + } + this._dict[name].push(value); + } + } + }; + URLSearchParams.prototype.get = function(name) { + return Object.prototype.hasOwnProperty.call(this._dict, name) ? this._dict[name][0] : null; + }; +} + +if (!HTMLCanvasElement.prototype.toBlob) { + HTMLCanvasElement.prototype.toBlob = function(callback, type, quality) { + const canvas = this; + setTimeout(function() { + const data = atob(canvas.toDataURL(type, quality).split(',')[1]); + const length = data.length; + const buffer = new Uint8Array(length); + for (let i = 0; i < length; i++) { + buffer[i] = data.charCodeAt(i); + } + callback(new Blob([ buffer ], { type: type || 'image/png' })); + }); + }; +} + +if (!('scrollBehavior' in window.document.documentElement.style)) { + const __scrollTo__ = Element.prototype.scrollTo; + Element.prototype.scrollTo = function(options) { + if (options === undefined) { + return; + } + if (options === null || typeof options !== 'object' || options.behavior === undefined || arguments[0].behavior === 'auto' || options.behavior === 'instant') { + if (__scrollTo__) { + __scrollTo__.apply(this, arguments); + } + return; + } + const now = () => { + return window.performance && window.performance.now ? window.performance.now() : Date.now(); + }; + const ease = (k) => { + return 0.5 * (1 - Math.cos(Math.PI * k)); + }; + const step = (context) => { + const value = ease(Math.min((now() - context.startTime) / 468, 1)); + const x = context.startX + (context.x - context.startX) * value; + const y = context.startY + (context.y - context.startY) * value; + context.element.scrollLeft = x; + context.element.scrollTop = y; + if (x !== context.x || y !== context.y) { + window.requestAnimationFrame(step.bind(window, context)); + } + }; + const context = { + element: this, + x: typeof options.left === 'undefined' ? this.scrollLeft : ~~options.left, + y: typeof options.top === 'undefined' ? this.scrollTop : ~~options.top, + startX: this.scrollLeft, + startY: this.scrollTop, + startTime: now() + }; + step(context); + }; +} + +window.addEventListener('load', () => { + window.__view__ = new view.View(new host.BrowserHost()); +}); diff --git a/json.js b/json.js new file mode 100644 index 0000000..3ed10d1 --- /dev/null +++ b/json.js @@ -0,0 +1,566 @@ + +var json = json || {}; +var text = text || require('./text'); + +json.TextReader = class { + + static open(data) { + const decoder = text.Decoder.open(data); + let state = 'start'; + for (let i = 0; i < 0x1000; i++) { + const c = decoder.decode(); + if (c === undefined || c === '\0') { + if (i === 0) { + return null; + } + break; + } + if (c <= ' ') { + if (c !== ' ' && c !== '\n' && c !== '\r' && c !== '\t') { + return null; + } + continue; + } + switch (state) { + case 'start': + if (c === '#') { + state = 'comment'; + break; + } + if (c === '[') { + state = 'list'; + break; + } + if (c === '{') { + state = 'object'; + break; + } + if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { + state = ''; + break; + } + return null; + case 'list': + if (c === '"' || c === '-' || c === '+' || c === '{' || c === '[' || (c >= '0' && c <= '9')) { + state = ''; + break; + } + return null; + case 'object': + if (c != '"') { + return null; + } + state = ''; + continue; + } + } + return new json.TextReader(data); + } + + constructor(data) { + this._data = data; + this._escape = { '"': '"', '\\': '\\', '/': '/', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t' }; + } + + read() { + const decoder = text.Decoder.open(this._data); + const stack = []; + this._decoder = decoder; + this._position = 0; + this._char = decoder.decode(); + this._whitespace(); + let obj = undefined; + let first = true; + for (;;) { + if (Array.isArray(obj)) { + this._whitespace(); + let c = this._char; + if (c === ']') { + this._next(); + this._whitespace(); + if (stack.length > 0) { + obj = stack.pop(); + first = false; + continue; + } + if (this._char !== undefined) { + this._unexpected(); + } + return obj; + } + if (!first) { + if (this._char !== ',') { + this._unexpected(); + } + this._next(); + this._whitespace(); + c = this._char; + } + first = false; + switch (c) { + case '{': { + this._next(); + stack.push(obj); + const item = {}; + obj.push(item); + obj = item; + first = true; + break; + } + case '[': { + this._next(); + stack.push(obj); + const item = []; + obj.push(item); + obj = item; + first = true; + break; + } + default: { + obj.push(c === '"' ? this._string() : this._literal()); + break; + } + } + } + else if (obj instanceof Object) { + this._whitespace(); + let c = this._char; + if (c === '}') { + this._next(); + this._whitespace(); + if (stack.length > 0) { + obj = stack.pop(); + first = false; + continue; + } + if (this._char !== undefined) { + this._unexpected(); + } + return obj; + } + if (!first) { + if (this._char !== ',') { + this._unexpected(); + } + this._next(); + this._whitespace(); + c = this._char; + } + first = false; + if (c === '"') { + const key = this._string(); + switch (key) { + case '__proto__': + case 'constructor': + case 'prototype': + throw new json.Error("Invalid key '" + key + "'" + this._location()); + } + this._whitespace(); + if (this._char !== ':') { + this._unexpected(); + } + this._next(); + this._whitespace(); + c = this._char; + switch (c) { + case '{': { + this._next(); + stack.push(obj); + const value = {}; + obj[key] = value; + obj = value; + first = true; + break; + } + case '[': { + this._next(); + stack.push(obj); + const value = []; + obj[key] = value; + obj = value; + first = true; + break; + } + default: { + obj[key] = c === '"' ? this._string() : this._literal(); + break; + } + } + this._whitespace(); + continue; + } + this._unexpected(); + } + else { + const c = this._char; + switch (c) { + case '{': { + this._next(); + this._whitespace(); + obj = {}; + first = true; + break; + } + case '[': { + this._next(); + this._whitespace(); + obj = []; + first = true; + break; + } + default: { + const value = c === '"' ? this._string() : c >= '0' && c <= '9' ? this._number() : this._literal(); + this._whitespace(); + if (this._char !== undefined) { + this._unexpected(); + } + return value; + } + } + } + } + } + + _next() { + if (this._char === undefined) { + this._unexpected(); + } + this._position = this._decoder.position; + this._char = this._decoder.decode(); + } + + _whitespace() { + while (this._char === ' ' || this._char === '\n' || this._char === '\r' || this._char === '\t') { + this._next(); + } + } + + _literal() { + const c = this._char; + if (c >= '0' && c <= '9') { + return this._number(); + } + switch (c) { + case 't': this._expect('true'); return true; + case 'f': this._expect('false'); return false; + case 'n': this._expect('null'); return null; + case 'N': this._expect('NaN'); return NaN; + case 'I': this._expect('Infinity'); return Infinity; + case '-': return this._number(); + } + this._unexpected(); + } + + _number() { + let value = ''; + if (this._char === '-') { + value = '-'; + this._next(); + } + if (this._char === 'I') { + this._expect('Infinity'); + return -Infinity; + } + const c = this._char; + if (c < '0' || c > '9') { + this._unexpected(); + } + value += c; + this._next(); + if (c === '0') { + const n = this._char; + if (n >= '0' && n <= '9') { + this._unexpected(); + } + } + while (this._char >= '0' && this._char <= '9') { + value += this._char; + this._next(); + } + if (this._char === '.') { + value += '.'; + this._next(); + const n = this._char; + if (n < '0' || n > '9') { + this._unexpected(); + } + while (this._char >= '0' && this._char <= '9') { + value += this._char; + this._next(); + } + } + if (this._char === 'e' || this._char === 'E') { + value += this._char; + this._next(); + const s = this._char; + if (s === '-' || s === '+') { + value += this._char; + this._next(); + } + const c = this._char; + if (c < '0' || c > '9') { + this._unexpected(); + } + value += this._char; + this._next(); + while (this._char >= '0' && this._char <= '9') { + value += this._char; + this._next(); + } + } + return +value; + } + + _string() { + let value = ''; + this._next(); + while (this._char != '"') { + if (this._char === '\\') { + this._next(); + if (this._char === 'u') { + this._next(); + let uffff = 0; + for (let i = 0; i < 4; i ++) { + const hex = parseInt(this._char, 16); + if (!isFinite(hex)) { + this._unexpected(); + } + this._next(); + uffff = uffff * 16 + hex; + } + value += String.fromCharCode(uffff); + } + else if (this._escape[this._char]) { + value += this._escape[this._char]; + this._next(); + } + else { + this._unexpected(); + } + } + else if (this._char < ' ') { + this._unexpected(); + } + else { + value += this._char; + this._next(); + } + } + this._next(); + return value; + } + + _expect(value) { + for (let i = 0; i < value.length; i++) { + if (value[i] !== this._char) { + this._unexpected(); + } + this._next(); + } + } + + _unexpected() { + let c = this._char; + if (c === undefined) { + throw new json.Error('Unexpected end of JSON input.'); + } + else if (c === '"') { + c = 'string'; + } + else if ((c >= '0' && c <= '9') || c === '-') { + c = 'number'; + } + else { + if (c < ' ' || c > '\x7F') { + const name = Object.keys(this._escape).filter((key) => this._escape[key] === c); + c = (name.length === 1) ? '\\' + name : '\\u' + ('000' + c.charCodeAt(0).toString(16)).slice(-4); + } + c = "token '" + c + "'"; + } + throw new json.Error('Unexpected ' + c + this._location()); + } + + _location() { + let line = 1; + let column = 1; + this._decoder.position = 0; + let c; + do { + if (this._decoder.position === this._position) { + return ' at ' + line.toString() + ':' + column.toString() + '.'; + } + c = this._decoder.decode(); + if (c === '\n') { + line++; + column = 1; + } + else { + column++; + } + } + while (c !== undefined); + return ' at ' + line.toString() + ':' + column.toString() + '.'; + } +}; + +json.BinaryReader = class { + + static open(data) { + const buffer = data instanceof Uint8Array ? data : data.peek(); + return new json.BinaryReader(buffer); + } + + constructor(buffer) { + this._buffer = buffer; + } + + read() { + const buffer = this._buffer; + const length = buffer.length; + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + const asciiDecoder = new TextDecoder('ascii'); + const utf8Decoder = new TextDecoder('utf-8'); + let position = 0; + const skip = (offset) => { + position += offset; + if (position > length) { + throw new json.Error('Expected ' + (position + length) + ' more bytes. The file might be corrupted. Unexpected end of file.', true); + } + }; + const header = () => { + const start = position; + skip(4); + const size = view.getInt32(start, 4); + if (size < 5 || start + size > length || buffer[start + size - 1] != 0x00) { + throw new json.Error('Invalid file size.', true); + } + }; + header(); + const stack = []; + let obj = {}; + for (;;) { + skip(1); + const type = buffer[position - 1]; + if (type == 0x00) { + if (stack.length === 0) { + break; + } + obj = stack.pop(); + continue; + } + const start = position; + position = buffer.indexOf(0x00, start) + 1; + const key = asciiDecoder.decode(buffer.subarray(start, position - 1)); + let value = null; + switch (type) { + case 0x01: { // float64 + const start = position; + skip(8); + value = view.getFloat64(start, true); + break; + } + case 0x02: { // string + skip(4); + const size = view.getInt32(position - 4, true); + const start = position; + skip(size); + value = utf8Decoder.decode(buffer.subarray(start, position - 1)); + if (buffer[position - 1] != '0x00') { + throw new json.Error('String missing terminal 0.', true); + } + break; + } + case 0x03: { // object + header(); + value = {}; + break; + } + case 0x04: { // array + header(); + value = []; + break; + } + case 0x05: { // bytes + const start = position; + skip(5); + const size = view.getInt32(start, true); + const subtype = buffer[start + 4]; + if (subtype !== 0x00) { + throw new json.Error("Unknown binary subtype '" + subtype + "'.", true); + } + skip(size); + value = buffer.subarray(start + 5, position); + break; + } + case 0x08: { // boolean + skip(1); + value = buffer[position - 1]; + if (value > 1) { + throw new json.Error("Invalid boolean value '" + value + "'.", true); + } + value = value === 1 ? true : false; + break; + } + case 0x0A: + value = null; + break; + case 0x10: { + const start = position; + skip(4); + value = view.getInt32(start, true); + break; + } + case 0x11: { // uint64 + const start = position; + skip(8); + value = view.getUint64(start, true).toNumber(); + break; + } + case 0x12: { // int64 + const start = position; + skip(8); + value = view.getInt64(start, true).toNumber(); + break; + } + default: + throw new json.Error("Unknown value type '" + type + "'.", true); + } + if (Array.isArray(obj)) { + if (obj.length !== parseInt(key, 10)) { + throw new json.Error("Invalid array index '" + key + "'.", true); + } + obj.push(value); + } + else { + switch (key) { + case '__proto__': + case 'constructor': + case 'prototype': + throw new json.Error("Invalid key '" + key + "' at " + position.toString() + "'.", true); + } + obj[key] = value; + } + if (type === 0x03 || type === 0x04) { + stack.push(obj); + obj = value; + } + } + if (position !== length) { + throw new json.Error("Unexpected data at '" + position.toString() + "'.", true); + } + return obj; + } +}; + +json.Error = class extends Error { + + constructor(message, binary) { + super(message); + this.name = binary ? 'BSON Error' : 'JSON Error'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.TextReader = json.TextReader; + module.exports.BinaryReader = json.BinaryReader; +} \ No newline at end of file diff --git a/numpy.js b/numpy.js new file mode 100644 index 0000000..fa989fd --- /dev/null +++ b/numpy.js @@ -0,0 +1,567 @@ + +// Experimental + +var numpy = numpy || {}; +var python = python || require('./python'); + +numpy.ModelFactory = class { + + match(context) { + const stream = context.stream; + const signature = [ 0x93, 0x4E, 0x55, 0x4D, 0x50, 0x59 ]; + if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) { + return { name: 'npy' }; + } + const entries = context.entries('zip'); + if (entries.size > 0 && Array.from(entries.keys()).every((name) => name.endsWith('.npy'))) { + return { name: 'npz', value: entries }; + } + const obj = context.open('pkl'); + if (obj) { + if (numpy.Utility.isTensor(obj)) { + return { name: 'numpy.ndarray', value: obj }; + } + if (Array.isArray(obj) && obj.every((obj) => obj && obj.__class__ && obj.__class__.__name__ === 'Network' && (obj.__class__.__module__ === 'dnnlib.tflib.network' || obj.__class__.__module__ === 'tfutil'))) { + return { name: 'dnnlib.tflib.network', value: obj }; + } + const weights = numpy.Utility.weights(obj); + if (weights) { + return { name: 'pickle', value: weights }; + } + } + return undefined; + } + + open(context, match) { + let format = ''; + const graphs = []; + switch (match.name) { + case 'npy': { + format = 'NumPy Array'; + const execution = new python.Execution(null); + const stream = context.stream; + const buffer = stream.peek(); + const bytes = execution.invoke('io.BytesIO', [ buffer ]); + const array = execution.invoke('numpy.load', [ bytes ]); + const layer = { type: 'numpy.ndarray', parameters: [ { name: 'value', tensor: { name: '', array: array } } ] }; + graphs.push({ layers: [ layer ] }); + break; + } + case 'npz': { + format = 'NumPy Zip'; + const layers = new Map(); + const execution = new python.Execution(null); + for (const entry of match.value) { + if (!entry[0].endsWith('.npy')) { + throw new numpy.Error("Invalid file name '" + entry.name + "'."); + } + const name = entry[0].replace(/\.npy$/, ''); + const parts = name.split('/'); + const parameterName = parts.pop(); + const groupName = parts.join('/'); + if (!layers.has(groupName)) { + layers.set(groupName, { name: groupName, parameters: [] }); + } + const layer = layers.get(groupName); + const stream = entry[1]; + const buffer = stream.peek(); + const bytes = execution.invoke('io.BytesIO', [ buffer ]); + let array = execution.invoke('numpy.load', [ bytes ]); + if (array.dtype.byteorder === '|' && array.dtype.itemsize !== 1) { + if (array.dtype.kind !== 'O') { + throw new numpy.Error("Invalid data type '" + array.dataType + "'."); + } + const unpickler = python.Unpickler.open(array.data); + array = unpickler.load((name, args) => execution.invoke(name, args)); + } + layer.parameters.push({ + name: parameterName, + tensor: { name: name, array: array } + }); + } + graphs.push({ layers: Array.from(layers.values()) }); + break; + } + case 'pickle': { + format = 'NumPy Weights'; + const layers = new Map(); + const weights = match.value; + let separator = '.'; + if (Array.from(weights.keys()).filter((key) => key.indexOf('_') !== -1) && + Array.from(weights.keys()).every((key) => key.indexOf('_') > key.indexOf('.'))) { + separator = '_'; + } + for (const pair of weights) { + const name = pair[0]; + const array = pair[1]; + const parts = name.split(separator); + const parameterName = parts.length > 1 ? parts.pop() : '?'; + const layerName = parts.join(separator); + if (!layers.has(layerName)) { + layers.set(layerName, { name: layerName, parameters: [] }); + } + const layer = layers.get(layerName); + layer.parameters.push({ + name: parameterName, + tensor: { name: name, array: array } + }); + } + graphs.push({ layers: Array.from(layers.values()) }); + break; + } + case 'numpy.ndarray': { + format = 'NumPy NDArray'; + const layer = { + type: 'numpy.ndarray', + parameters: [ { name: 'value', tensor: { name: '', array: match.value } } ] + }; + graphs.push({ layers: [ layer ] }); + break; + } + case 'dnnlib.tflib.network': { + format = 'dnnlib'; + for (const obj of match.value) { + const layers = new Map(); + for (const entry of obj.variables) { + const name = entry[0]; + const value = entry[1]; + if (numpy.Utility.isTensor(value)) { + const parts = name.split('/'); + const parameterName = parts.length > 1 ? parts.pop() : '?'; + const layerName = parts.join('/'); + if (!layers.has(layerName)) { + layers.set(layerName, { name: layerName, parameters: [] }); + } + const layer = layers.get(layerName); + layer.parameters.push({ + name: parameterName, + tensor: { name: name, array: value } + }); + } + } + graphs.push({ name: obj.name, layers: Array.from(layers.values()) }); + } + break; + } + } + const model = new numpy.Model(format, graphs); + return Promise.resolve(model); + } +}; + +numpy.Model = class { + + constructor(format, graphs) { + this._format = format; + this._graphs = graphs.map((graph) => new numpy.Graph(graph)); + } + + get format() { + return this._format; + } + + get graphs() { + return this._graphs; + } +}; + +numpy.Graph = class { + + constructor(graph) { + this._name = graph.name || ''; + this._nodes = graph.layers.map((layer) => new numpy.Node(layer)); + } + + get name() { + return this._name; + } + + get inputs() { + return []; + } + + get outputs() { + return []; + } + + get nodes() { + return this._nodes; + } +}; + +numpy.Parameter = class { + + constructor(name, args) { + this._name = name; + this._arguments = args; + } + + get name() { + return this._name; + } + + get visible() { + return true; + } + + get arguments() { + return this._arguments; + } +}; + +numpy.Argument = class { + + constructor(name, initializer) { + if (typeof name !== 'string') { + throw new numpy.Error("Invalid argument identifier '" + JSON.stringify(name) + "'."); + } + this._name = name; + this._initializer = initializer || null; + } + + get name() { + return this._name; + } + + get type() { + return this._initializer.type; + } + + get initializer() { + return this._initializer; + } +}; + +numpy.Node = class { + + constructor(layer) { + this._name = layer.name || ''; + this._type = { name: layer.type || 'Module' }; + this._inputs = []; + for (const parameter of layer.parameters) { + const initializer = new numpy.Tensor(parameter.tensor.array); + this._inputs.push(new numpy.Parameter(parameter.name, [ + new numpy.Argument(parameter.tensor.name || '', initializer) + ])); + } + } + + get type() { + return this._type; + } + + get name() { + return this._name; + } + + get inputs() { + return this._inputs; + } + + get outputs() { + return []; + } + + get attributes() { + return []; + } +}; + +numpy.Tensor = class { + + constructor(array) { + this._type = new numpy.TensorType(array.dtype.name, new numpy.TensorShape(array.shape)); + this._data = array.tobytes(); + this._byteorder = array.dtype.byteorder; + this._itemsize = array.dtype.itemsize; + } + + get type(){ + return this._type; + } + + get state() { + return this._context().state; + } + + get value() { + const context = this._context(); + if (context.state) { + return null; + } + context.limit = Number.MAX_SAFE_INTEGER; + return this._decode(context, 0); + } + + toString() { + const context = this._context(); + if (context.state) { + return ''; + } + context.limit = 10000; + const value = this._decode(context, 0); + return numpy.Tensor._stringify(value, '', ' '); + } + + _context() { + const context = {}; + context.index = 0; + context.count = 0; + context.state = null; + if (this._byteorder !== '<' && this._byteorder !== '>' && this._type.dataType !== 'uint8' && this._type.dataType !== 'int8') { + context.state = 'Tensor byte order is not supported.'; + return context; + } + if (!this._data || this._data.length == 0) { + context.state = 'Tensor data is empty.'; + return context; + } + context.itemSize = this._itemsize; + context.dimensions = this._type.shape.dimensions; + context.dataType = this._type.dataType; + context.littleEndian = this._byteorder == '<'; + context.data = this._data; + context.rawData = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength); + return context; + } + + _decode(context, dimension) { + const littleEndian = context.littleEndian; + const shape = context.dimensions.length == 0 ? [ 1 ] : context.dimensions; + const results = []; + const size = shape[dimension]; + if (dimension == shape.length - 1) { + for (let i = 0; i < size; i++) { + if (context.count > context.limit) { + results.push('...'); + return results; + } + if (context.rawData) { + switch (context.dataType) { + case 'float16': + results.push(context.rawData.getFloat16(context.index, littleEndian)); + break; + case 'float32': + results.push(context.rawData.getFloat32(context.index, littleEndian)); + break; + case 'float64': + results.push(context.rawData.getFloat64(context.index, littleEndian)); + break; + case 'int8': + results.push(context.rawData.getInt8(context.index, littleEndian)); + break; + case 'int16': + results.push(context.rawData.getInt16(context.index, littleEndian)); + break; + case 'int32': + results.push(context.rawData.getInt32(context.index, littleEndian)); + break; + case 'int64': + results.push(context.rawData.getInt64(context.index, littleEndian)); + break; + case 'uint8': + results.push(context.rawData.getUint8(context.index, littleEndian)); + break; + case 'uint16': + results.push(context.rawData.getUint16(context.index, littleEndian)); + break; + case 'uint32': + results.push(context.rawData.getUint32(context.index, littleEndian)); + break; + } + context.index += context.itemSize; + context.count++; + } + } + } + else { + for (let j = 0; j < size; j++) { + if (context.count > context.limit) { + results.push('...'); + return results; + } + results.push(this._decode(context, dimension + 1)); + } + } + if (context.dimensions.length == 0) { + return results[0]; + } + return results; + } + + static _stringify(value, indentation, indent) { + if (Array.isArray(value)) { + const result = []; + result.push(indentation + '['); + const items = value.map((item) => numpy.Tensor._stringify(item, indentation + indent, indent)); + if (items.length > 0) { + result.push(items.join(',\n')); + } + result.push(indentation + ']'); + return result.join('\n'); + } + if (typeof value == 'string') { + return indentation + value; + } + if (value == Infinity) { + return indentation + 'Infinity'; + } + if (value == -Infinity) { + return indentation + '-Infinity'; + } + if (isNaN(value)) { + return indentation + 'NaN'; + } + return indentation + value.toString(); + } +}; + +numpy.TensorType = class { + + constructor(dataType, shape) { + this._dataType = dataType; + this._shape = shape; + } + + get dataType() { + return this._dataType || '?'; + } + + get shape() { + return this._shape; + } + + toString() { + return this.dataType + this._shape.toString(); + } +}; + +numpy.TensorShape = class { + + constructor(dimensions) { + this._dimensions = dimensions; + } + + get dimensions() { + return this._dimensions; + } + + toString() { + if (!this._dimensions || this._dimensions.length == 0) { + return ''; + } + return '[' + this._dimensions.join(',') + ']'; + } +}; + +numpy.Utility = class { + + static isTensor(obj) { + return obj && obj.__class__ && + ((obj.__class__.__module__ === 'numpy' && obj.__class__.__name__ === 'ndarray') || + (obj.__class__.__module__ === 'numpy.core.memmap' && obj.__class__.__name__ === 'memmap')); + } + + static weights(obj) { + const dict = (obj, key) => { + const dict = key === '' ? obj : obj[key]; + if (dict) { + const weights = new Map(); + if (dict instanceof Map) { + for (const pair of dict) { + const key = pair[0]; + const obj = pair[1]; + if (numpy.Utility.isTensor(obj)) { + weights.set(key, obj); + continue; + } + else if (obj instanceof Map && Array.from(obj).every((pair) => numpy.Utility.isTensor(pair[1]))) { + for (const pair of obj) { + weights.set(key + '.' + pair[0], pair[1]); + } + continue; + } + else if (key === '_metadata') { + continue; + } + return null; + } + return weights; + } + else if (!Array.isArray(dict)) { + const set = new Set([ 'weight_order', 'lr', 'model_iter', '__class__' ]); + for (const entry of Object.entries(dict)) { + const key = entry[0]; + const value = entry[1]; + if (key) { + if (numpy.Utility.isTensor(value)) { + weights.set(key, value); + continue; + } + if (set.has(key)) { + continue; + } + if (value && !Array.isArray(value) && Object.entries(value).every((entry) => numpy.Utility.isTensor(entry[1]))) { + const name = key; + for (const entry of Object.entries(value)) { + weights.set(name + '.' + entry[0], entry[1]); + } + continue; + } + } + return null; + } + return weights; + } + } + return null; + }; + const list = (obj, key) => { + const list = key === '' ? obj : obj[key]; + if (list && Array.isArray(list)) { + const weights = new Map(); + for (let i = 0; i < list.length; i++) { + const obj = list[i]; + if (numpy.Utility.isTensor(obj)) { + weights.set(i.toString(), obj); + continue; + } + else if (obj instanceof Map && Array.from(obj).every((pair) => numpy.Utility.isTensor(pair[1]))) { + for (const pair of obj) { + weights.set(i.toString() + '.' + pair[0], pair[1]); + } + continue; + } + return null; + } + return weights; + } + }; + const keys = [ '', 'blobs', 'model' ]; + for (const key of keys) { + const weights = dict(obj, key); + if (weights && weights.size > 0) { + return weights; + } + } + for (const key of keys) { + const weights = list(obj, key); + if (weights) { + return weights; + } + } + return null; + } +}; + +numpy.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'Error loading Chainer model.'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.ModelFactory = numpy.ModelFactory; +} diff --git a/onnx-metadata.json b/onnx-metadata.json new file mode 100644 index 0000000..2092508 --- /dev/null +++ b/onnx-metadata.json @@ -0,0 +1,34063 @@ +[ + { + "name": "Abs", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Absolute takes one input data (Tensor) and produces one output data\n(Tensor) where the absolute is, y = abs(x), is applied to\nthe tensor elementwise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "abs", + "code": "node = onnx.helper.make_node(\n 'Abs',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = abs(x)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_abs')" + } + ] + }, + { + "name": "Abs", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Absolute takes one input data (Tensor) and produces one output data\n(Tensor) where the absolute is, y = abs(x), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "abs", + "code": "node = onnx.helper.make_node(\n 'Abs',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = abs(x)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_abs')" + } + ] + }, + { + "name": "Abs", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Absolute takes one input data (Tensor) and produces one output data\n(Tensor) where the absolute is, y = abs(x), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "abs", + "code": "node = onnx.helper.make_node(\n 'Abs',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = abs(x)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_abs')" + } + ] + }, + { + "name": "Acos", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Calculates the arccosine (inverse of cosine) of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The arccosine of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "acos", + "code": "node = onnx.helper.make_node(\n 'Acos',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-0.5, 0, 0.5]).astype(np.float32)\ny = np.arccos(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_acos_example')\n\nx = np.random.rand(3, 4, 5).astype(np.float32)\ny = np.arccos(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_acos')" + } + ] + }, + { + "name": "Acosh", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Calculates the hyperbolic arccosine of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The hyperbolic arccosine values of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "acosh", + "code": "node = onnx.helper.make_node(\n 'Acosh',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([10, np.e, 1]).astype(np.float32)\ny = np.arccosh(x) # expected output [2.99322295, 1.65745449, 0.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_acosh_example')\n\nx = np.random.uniform(1.0, 10.0, (3, 4, 5)).astype(np.float32)\ny = np.arccosh(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_acosh')" + } + ] + }, + { + "name": "Adagrad", + "module": "ai.onnx.preview.training", + "version": 1, + "support_level": "common", + "description": "Compute one iteration of ADAGRAD, a stochastic gradient based optimization\n algorithm. This operator can conduct the optimization of multiple tensor variables.\n\n Let's define the behavior of this operator. As you can imagine, ADAGRAD requires\n some parameters:\n\n - The initial learning-rate \"R\".\n - The update count \"T\". That is, the number of training iterations conducted.\n - A L2-norm regularization coefficient \"norm_coefficient\".\n - A learning-rate decay factor \"decay_factor\".\n - A small constant \"epsilon\" to avoid dividing-by-zero.\n\n At each ADAGRAD iteration, the optimized tensors are moved along a direction\n computed based on their estimated gradient and accumulated squared gradient. Assume\n that only a single tensor \"X\" is updated by this operator. We need the value of \"X\",\n its gradient \"G\", and its accumulated squared gradient \"H\". Therefore, variables in\n this operator's input list are sequentially \"R\", \"T\", \"X\", \"G\", and \"H\". Other\n parameters are given as attributes because they are usually constants. Also, the\n corresponding output tensors are the new value of \"X\" (called \"X_new\"), and then\n the new accumulated squared gradient (called \"H_new\"). Those outputs are computed\n from the given inputs following the pseudo code below.\n\n Let \"+\", \"-\", \"*\", and \"/\" are all element-wise arithmetic operations with\n numpy-style broadcasting support. The pseudo code to compute those outputs is:\n\n // Compute a scalar learning-rate factor. At the first update of X, T is generally\n // 0 (0-based update index) or 1 (1-based update index).\n r = R / (1 + T * decay_factor);\n\n // Add gradient of 0.5 * norm_coefficient * ||X||_2^2, where ||X||_2 is the 2-norm.\n G_regularized = norm_coefficient * X + G;\n\n // Compute new accumulated squared gradient.\n H_new = H + G_regularized * G_regularized;\n\n // Compute the adaptive part of per-coordinate learning rate. Note that Sqrt(...)\n // computes element-wise square-root.\n H_adaptive = Sqrt(H_new) + epsilon\n\n // Compute the new value of \"X\".\n X_new = X - r * G_regularized / H_adaptive;\n\n If one assign this operators to optimize multiple inputs, for example, \"X_1\" and \"X_2\", the same\n pseudo code may be extended to handle all tensors jointly. More specifically, we can view \"X\" as a\n concatenation of \"X_1\" and \"X_2\" (of course, their gradient and accumulate gradient should\n be concatenated too) and then just reuse the entire pseudo code.\n\n Note that ADAGRAD was first proposed in http://jmlr.org/papers/volume12/duchi11a/duchi11a.pdf.\n In that reference paper, this operator is a special case of the Figure 1's composite mirror\n descent update.\n", + "attributes": [ + { + "name": "decay_factor", + "type": "float32", + "required": false, + "description": "The decay factor of learning rate after one update.The effective learning rate is computed by r = R / (1 + T * decay_factor). Default to 0 so that increasing update counts doesn't reduce the learning rate." + }, + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999974752427e-07, + "description": "Small scalar to avoid dividing by zero." + }, + { + "name": "norm_coefficient", + "type": "float32", + "required": false, + "description": "Regularization coefficient in 0.5 * norm_coefficient * ||X||_2^2. Default to 0, which means no regularization." + } + ], + "inputs": [ + { + "name": "R", + "type": "T1", + "description": "The initial learning rate." + }, + { + "name": "T", + "type": "T2", + "description": "The update count of \"X\". It should be a scalar." + }, + { + "name": "inputs", + "type": "T3", + "list": true, + "description": "The current values of optimized tensors, followed by their respective gradients, followed by their respective accumulated squared gradients.For example, if two tensor \"X_1\" and \"X_2\" are optimized, The input list would be [\"X_1\", \"X_2\", gradient of \"X_1\", gradient of \"X_2\", accumulated squared gradient of \"X_1\", accumulated squared gradient of \"X_2\"]." + } + ], + "min_input": 3, + "max_input": 2147483647, + "outputs": [ + { + "name": "outputs", + "type": "T3", + "list": true, + "description": "Updated values of optimized tensors, followed by their updated values of accumulated squared gradients. For example, if two tensor \"X_1\" and \"X_2\" are optimized, the output list would be [new value of \"X_1,\" new value of \"X_2\" new accumulated squared gradient of \"X_1\", new accumulated squared gradient of \"X_2\"]." + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "3 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input types to float scalars.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain input types to 64-bit integer scalars.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int64)" + ] + }, + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T3", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "adagrad", + "code": "# Define operator attributes.\nnorm_coefficient = 0.001\nepsilon = 1e-5\ndecay_factor = 0.1\n\n# Create operator.\nnode = onnx.helper.make_node('Adagrad',\n inputs=['R', 'T', 'X', 'G', 'H'],\n outputs=['X_new', 'H_new'],\n norm_coefficient=norm_coefficient,\n epsilon=epsilon,\n decay_factor=decay_factor,\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN\n )\n\n# Define operator inputs.\nr = np.array(0.1, dtype=np.float32) # scalar\nt = np.array(0, dtype=np.int64) # scalar\nx = np.array([1.0], dtype=np.float32)\ng = np.array([-1.0], dtype=np.float32)\nh = np.array([2.0], dtype=np.float32)\n\n# Compute expected outputs of Adagrad.\nx_new, h_new = apply_adagrad(r, t, x, g, h,\n norm_coefficient, epsilon, decay_factor)\n\n# Check results.\nexpect(node, inputs=[r, t, x, g, h],\n outputs=[x_new, h_new], name='test_adagrad',\n opset_imports=[onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)])" + }, + { + "summary": "adagrad_multiple", + "code": "# Define operator attributes.\nnorm_coefficient = 0.001\nepsilon = 1e-5\ndecay_factor = 0.1\n\nnode = onnx.helper.make_node('Adagrad',\n inputs=['R', 'T', 'X1', 'X2',\n 'G1', 'G2', 'H1', 'H2'],\n outputs=['X1_new', 'X2_new',\n 'H1_new', 'H2_new'],\n norm_coefficient=norm_coefficient,\n epsilon=epsilon,\n decay_factor=decay_factor,\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN\n )\n\n# Define operator inputs.\nr = np.array(0.1, dtype=np.float32) # scalar\nt = np.array(0, dtype=np.int64) # scalar\n\nx1 = np.array([1.0], dtype=np.float32)\ng1 = np.array([-1.0], dtype=np.float32)\nh1 = np.array([2.0], dtype=np.float32)\n\nx2 = np.array([1.0, 2.0], dtype=np.float32)\ng2 = np.array([-1.0, -3.0], dtype=np.float32)\nh2 = np.array([4.0, 1.0], dtype=np.float32)\n\n# Compute expected outputs of Adagrad.\nx1_new, h1_new = apply_adagrad(r, t, x1, g1, h1,\n norm_coefficient, epsilon, decay_factor)\nx2_new, h2_new = apply_adagrad(r, t, x2, g2, h2,\n norm_coefficient, epsilon, decay_factor)\n\n# Check results.\nexpect(node, inputs=[r, t, x1, x2, g1, g2, h1, h2],\n outputs=[x1_new, x2_new, h1_new, h2_new], name='test_adagrad_multiple',\n opset_imports=[onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)])" + } + ] + }, + { + "name": "Adam", + "module": "ai.onnx.preview.training", + "version": 1, + "support_level": "common", + "description": "Compute one iteration of Adam, a stochastic gradient based optimization\n algorithm. This operator can conduct the optimization of multiple tensor variables.\n\n Let's define the behavior of this operator. First of all, Adam requires\n some parameters:\n\n - The learning-rate \"R\".\n - The update count \"T\". That is, the number of training iterations conducted.\n - A L2-norm regularization coefficient \"norm_coefficient\".\n - A small constant \"epsilon\" to avoid dividing-by-zero.\n - Two coefficients, \"alpha\" and \"beta\".\n\n At each Adam iteration, the optimized tensors are moved along a direction\n computed based on their exponentially-averaged historical gradient and\n exponentially-averaged historical squared gradient. Assume that only a tensor\n \"X\" is being optimized. The rest of required information is\n\n - the value of \"X\",\n - \"X\"'s gradient (denoted by \"G\"),\n - \"X\"'s exponentially-averaged historical gradient (denoted by \"V\"), and\n - \"X\"'s exponentially-averaged historical squared gradient (denoted by \"H\").\n\n Some of those parameters are passed into this operator as input tensors and others\n are stored as this operator's attributes. Specifically, this operator's input tensor\n list is [\"R\", \"T\", \"X\", \"G\", \"V\", \"H\"]. That is, \"R\" is the first input, \"T\" is\n the second input, and so on. Other parameters are given as attributes because they\n are constants. Moreover, the corresponding output tensors are\n\n - the new value of \"X\" (called \"X_new\"),\n - the new exponentially-averaged historical gradient (denoted by \"V_new\"), and\n - the new exponentially-averaged historical squared gradient (denoted by \"H_new\").\n\n Those outputs are computed following the pseudo code below.\n\n Let \"+\", \"-\", \"*\", and \"/\" are all element-wise arithmetic operations with\n numpy-style broadcasting support. The pseudo code to compute those outputs is:\n\n // Add gradient of 0.5 * norm_coefficient * ||X||_2^2, where ||X||_2 is the 2-norm.\n G_regularized = norm_coefficient * X + G\n\n // Update exponentially-averaged historical gradient.\n V_new = alpha * V + (1 - alpha) * G_regularized\n\n // Update exponentially-averaged historical squared gradient.\n H_new = beta * H + (1 - beta) * G_regularized * G_regularized\n\n // Compute the element-wise square-root of H_new. V_new will be element-wisely\n // divided by H_sqrt for a better update direction.\n H_sqrt = Sqrt(H_new) + epsilon\n\n // Compute learning-rate. Note that \"alpha**T\"/\"beta**T\" is alpha's/beta's T-th power.\n R_adjusted = T > 0 ? R * Sqrt(1 - beta**T) / (1 - alpha**T) : R\n\n // Compute new value of \"X\".\n X_new = X - R_adjusted * V_new / H_sqrt\n\n // Post-update regularization.\n X_final = (1 - norm_coefficient_post) * X_new\n\n If there are multiple inputs to be optimized, the pseudo code will be applied\n independently to each of them.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 0.8999999761581421, + "description": "Coefficient of previously accumulated gradient in running average. Default to 0.9." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 0.9990000128746033, + "description": "Coefficient of previously accumulated squared-gradient in running average. Default to 0.999." + }, + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999974752427e-07, + "description": "Small scalar to avoid dividing by zero." + }, + { + "name": "norm_coefficient", + "type": "float32", + "required": false, + "description": "Regularization coefficient of 0.5 * norm_coefficient * ||X||_2^2. Default to 0, which means no regularization." + }, + { + "name": "norm_coefficient_post", + "type": "float32", + "required": false, + "description": "Regularization coefficient of 0.5 * norm_coefficient * ||X||_2^2. Default to 0, which means no regularization." + } + ], + "inputs": [ + { + "name": "R", + "type": "T1", + "description": "The initial learning rate." + }, + { + "name": "T", + "type": "T2", + "description": "The update count of \"X\". It should be a scalar." + }, + { + "name": "inputs", + "type": "T3", + "list": true, + "description": "The tensors to be optimized, followed by their respective gradients, followed by their respective accumulated gradients (aka momentum), followed by their respective accumulated squared gradients. For example, to optimize tensors \"X_1\" and \"X_2,\", the input list would be [\"X_1\", \"X_2\", gradient of \"X_1\", gradient of \"X_2\", accumulated gradient of \"X_1\", accumulated gradient of \"X_2\", accumulated squared gradient of \"X_1\", accumulated squared gradient of \"X_2\"]." + } + ], + "min_input": 3, + "max_input": 2147483647, + "outputs": [ + { + "name": "outputs", + "type": "T3", + "list": true, + "description": "New values of optimized tensors, followed by their respective new accumulated gradients, followed by their respective new accumulated squared gradients. For example, if two tensors \"X_1\" and \"X_2\" are optimized, the outputs list would be [new value of \"X_1\", new value of \"X_2\", new accumulated gradient of \"X_1\", new accumulated gradient of \"X_2\", new accumulated squared gradient of \"X_1\", new accumulated squared gradient of \"X_2\"]." + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "3 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input types to float scalars.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain input types to 64-bit integer scalars.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int64)" + ] + }, + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T3", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "adam", + "code": "# Define operator attributes.\nnorm_coefficient = 0.001\nalpha = 0.95\nbeta = 0.1\nepsilon = 1e-7\n\n# Create operator.\nnode = onnx.helper.make_node('Adam',\n inputs=['R', 'T', 'X', 'G', 'V', 'H'],\n outputs=['X_new', 'V_new', 'H_new'],\n norm_coefficient=norm_coefficient,\n alpha=alpha,\n beta=beta,\n epsilon=epsilon,\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN\n )\n\n# Define operator inputs.\nr = np.array(0.1, dtype=np.float32) # scalar\nt = np.array(0, dtype=np.int64) # scalar\nx = np.array([1.2, 2.8], dtype=np.float32)\ng = np.array([-0.94, -2.5], dtype=np.float32)\nv = np.array([1.7, 3.6], dtype=np.float32)\nh = np.array([0.1, 0.1], dtype=np.float32)\n\n# Compute expected outputs of Adam.\nx_new, v_new, h_new = apply_adam(r, t, x, g, v, h,\n norm_coefficient, 0.0, alpha, beta,\n epsilon)\n\n# Check results.\nexpect(node, inputs=[r, t, x, g, v, h],\n outputs=[x_new, v_new, h_new], name='test_adam',\n opset_imports=[onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)])" + }, + { + "summary": "adam_multiple", + "code": "# Define operator attributes.\nnorm_coefficient = 0.001\nalpha = 0.95\nbeta = 0.85\nepsilon = 1e-2\n\nnode = onnx.helper.make_node('Adam',\n inputs=['R', 'T', 'X1', 'X2',\n 'G1', 'G2', 'V1', 'V2',\n 'H1', 'H2'],\n outputs=['X1_new', 'X2_new',\n 'V1_new', 'V2_new',\n 'H1_new', 'H2_new'],\n norm_coefficient=norm_coefficient,\n alpha=alpha,\n beta=beta,\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN\n )\n\n# Define operator inputs.\nr = np.array(0.1, dtype=np.float32) # scalar\nt = np.array(0, dtype=np.int64) # scalar\n\nx1 = np.array([1.0], dtype=np.float32)\ng1 = np.array([-1.0], dtype=np.float32)\nv1 = np.array([2.0], dtype=np.float32)\nh1 = np.array([0.5], dtype=np.float32)\n\nx2 = np.array([1.0, 2.0], dtype=np.float32)\ng2 = np.array([-1.0, -3.0], dtype=np.float32)\nv2 = np.array([4.0, 1.0], dtype=np.float32)\nh2 = np.array([1.0, 10.0], dtype=np.float32)\n\n# Compute expected outputs of Adam.\nx1_new, v1_new, h1_new = apply_adam(r, t, x1, g1, v1, h1,\n norm_coefficient, 0.0, alpha, beta,\n epsilon)\nx2_new, v2_new, h2_new = apply_adam(r, t, x2, g2, v2, h2,\n norm_coefficient, 0.0, alpha, beta,\n epsilon)\n\n# Check results.\nexpect(node, inputs=[r, t, x1, x2, g1, g2, v1, v2, h1, h2],\n outputs=[x1_new, x2_new, v1_new, v2_new, h1_new, h2_new],\n name='test_adam_multiple',\n opset_imports=[onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)])" + } + ] + }, + { + "name": "Add", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Performs element-wise binary addition (with limited broadcast support).\n\nIf necessary the right-hand-side argument will be broadcasted to match the\nshape of left-hand-side argument. When broadcasting is specified, the second\ntensor can either be of element size 1 (including a scalar tensor and any\ntensor with rank equal to or smaller than the first tensor), or having its\nshape as a contiguous subset of the first tensor's shape. The starting of the\nmutually equal shape is specified by the argument \"axis\", and if it is not set,\nsuffix matching is assumed. 1-dim expansion doesn't work yet.\n\nFor example, the following tensor shapes are supported (with broadcast=1):\n\n shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (5,)\n shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)\n shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1\n shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0\n\nAttribute `broadcast=1` needs to be passed to enable broadcasting.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions. See doc for details." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Pass 1 to enable broadcasting" + }, + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand, should share the type with the second operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same dimensions and type as A" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "add", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add')" + }, + { + "summary": "add_broadcast", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_bcast')" + }, + { + "summary": "add_uint8", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_uint8')" + } + ] + }, + { + "name": "Add", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Performs element-wise binary addition (with limited broadcast support).\n\nIf necessary the right-hand-side argument will be broadcasted to match the\nshape of left-hand-side argument. When broadcasting is specified, the second\ntensor can either be of element size 1 (including a scalar tensor and any\ntensor with rank equal to or smaller than the first tensor), or having its\nshape as a contiguous subset of the first tensor's shape. The starting of the\nmutually equal shape is specified by the argument \"axis\", and if it is not set,\nsuffix matching is assumed. 1-dim expansion doesn't work yet.\n\nFor example, the following tensor shapes are supported (with broadcast=1):\n\n shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (5,)\n shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)\n shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1\n shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0\n\nAttribute `broadcast=1` needs to be passed to enable broadcasting.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions. See doc for details." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Pass 1 to enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand, should share the type with the second operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same dimensions and type as A" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "add", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add')" + }, + { + "summary": "add_broadcast", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_bcast')" + }, + { + "summary": "add_uint8", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_uint8')" + } + ] + }, + { + "name": "Add", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Performs element-wise binary addition (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "add", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add')" + }, + { + "summary": "add_broadcast", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_bcast')" + }, + { + "summary": "add_uint8", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_uint8')" + } + ] + }, + { + "name": "Add", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Performs element-wise binary addition (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "add", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add')" + }, + { + "summary": "add_broadcast", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_bcast')" + }, + { + "summary": "add_uint8", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_uint8')" + } + ] + }, + { + "name": "Add", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Performs element-wise binary addition (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n\n(Opset 14 change): Extend supported types to include uint8, int8, uint16, and int16.\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "add", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add')" + }, + { + "summary": "add_broadcast", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_bcast')" + }, + { + "summary": "add_uint8", + "code": "node = onnx.helper.make_node(\n 'Add',\n inputs=['x', 'y'],\n outputs=['sum'],\n)\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nexpect(node, inputs=[x, y], outputs=[x + y],\n name='test_add_uint8')" + } + ] + }, + { + "name": "And", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `and` logical operation\nelementwise on the input tensors `A` and `B`.\n\nIf broadcasting is enabled, the right-hand-side argument will be broadcasted\nto match the shape of left-hand-side argument. See the doc of `Add` for a\ndetailed description of the broadcasting rules.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Left input tensor for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Right input tensor for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to boolean tensor.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "and", + "code": "node = onnx.helper.make_node(\n 'And',\n inputs=['x', 'y'],\n outputs=['and'],\n)\n\n# 2d\nx = (np.random.randn(3, 4) > 0).astype(bool)\ny = (np.random.randn(3, 4) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and2d')\n\n# 3d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and3d')\n\n# 4d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and4d')" + }, + { + "summary": "and_broadcast", + "code": "node = onnx.helper.make_node(\n 'And',\n inputs=['x', 'y'],\n outputs=['and'],\n)\n\n# 3d vs 1d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(5) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast3v1d')\n\n# 3d vs 2d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(4, 5) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast3v2d')\n\n# 4d vs 2d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(5, 6) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast4v2d')\n\n# 4d vs 3d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(4, 5, 6) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast4v3d')\n\n# 4d vs 4d\nx = (np.random.randn(1, 4, 1, 6) > 0).astype(bool)\ny = (np.random.randn(3, 1, 5, 6) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast4v4d')" + } + ], + "category": "Logic" + }, + { + "name": "And", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `and` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to boolean tensor.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "and", + "code": "node = onnx.helper.make_node(\n 'And',\n inputs=['x', 'y'],\n outputs=['and'],\n)\n\n# 2d\nx = (np.random.randn(3, 4) > 0).astype(bool)\ny = (np.random.randn(3, 4) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and2d')\n\n# 3d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and3d')\n\n# 4d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and4d')" + }, + { + "summary": "and_broadcast", + "code": "node = onnx.helper.make_node(\n 'And',\n inputs=['x', 'y'],\n outputs=['and'],\n)\n\n# 3d vs 1d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(5) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast3v1d')\n\n# 3d vs 2d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(4, 5) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast3v2d')\n\n# 4d vs 2d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(5, 6) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast4v2d')\n\n# 4d vs 3d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(4, 5, 6) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast4v3d')\n\n# 4d vs 4d\nx = (np.random.randn(1, 4, 1, 6) > 0).astype(bool)\ny = (np.random.randn(3, 1, 5, 6) > 0).astype(bool)\nz = np.logical_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_and_bcast4v4d')" + } + ], + "category": "Logic" + }, + { + "name": "ArgMax", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the indices of the max elements of the input tensor's element along the\nprovided axis. The resulting tensor has the same rank as the input if keepdims equals 1.\nIf keepdims equal 0, then the resulted tensor have the reduced dimension pruned.\nThe type of the output tensor is integer.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "The axis in which to compute the arg indices." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "tensor(int64)", + "description": "Reduced output tensor with integer data type." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_random')" + }, + { + "summary": "default_axes_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims,\n select_last_index=True)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_random_select_last_index')" + }, + { + "summary": "keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_random')" + }, + { + "summary": "keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_random_select_last_index')" + }, + { + "summary": "negative_axis_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_random')" + }, + { + "summary": "negative_axis_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_random_select_last_index')" + }, + { + "summary": "no_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [0, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_random')" + }, + { + "summary": "no_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [1, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_random_select_last_index')" + } + ] + }, + { + "name": "ArgMax", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the indices of the max elements of the input tensor's element along the\nprovided axis. The resulting tensor has the same rank as the input if keepdims equals 1.\nIf keepdims equal 0, then the resulting tensor has the reduced dimension pruned.\nThe type of the output tensor is integer.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "The axis in which to compute the arg indices. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "tensor(int64)", + "description": "Reduced output tensor with integer data type." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_random')" + }, + { + "summary": "default_axes_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims,\n select_last_index=True)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_random_select_last_index')" + }, + { + "summary": "keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_random')" + }, + { + "summary": "keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_random_select_last_index')" + }, + { + "summary": "negative_axis_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_random')" + }, + { + "summary": "negative_axis_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_random_select_last_index')" + }, + { + "summary": "no_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [0, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_random')" + }, + { + "summary": "no_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [1, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_random_select_last_index')" + } + ] + }, + { + "name": "ArgMax", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Computes the indices of the max elements of the input tensor's element along the\nprovided axis. The resulting tensor has the same rank as the input if keepdims equals 1.\nIf keepdims equal 0, then the resulting tensor has the reduced dimension pruned.\nIf select_last_index is True (default False), the index of the last occurrence of the max\nis selected if the max appears more than once in the input. Otherwise the index of the\nfirst occurrence is selected.\nThe type of the output tensor is integer.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "The axis in which to compute the arg indices. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + }, + { + "name": "select_last_index", + "type": "int64", + "required": false, + "description": "Whether to select the last index or the first index if the {name} appears in multiple indices, default is False (first index)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "tensor(int64)", + "description": "Reduced output tensor with integer data type." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_random')" + }, + { + "summary": "default_axes_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims,\n select_last_index=True)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_random_select_last_index')" + }, + { + "summary": "keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_random')" + }, + { + "summary": "keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_random_select_last_index')" + }, + { + "summary": "negative_axis_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_random')" + }, + { + "summary": "negative_axis_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_random_select_last_index')" + }, + { + "summary": "no_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [0, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_random')" + }, + { + "summary": "no_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [1, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_random_select_last_index')" + } + ] + }, + { + "name": "ArgMax", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the indices of the max elements of the input tensor's element along the\nprovided axis. The resulting tensor has the same rank as the input if keepdims equals 1.\nIf keepdims equals 0, then the resulting tensor has the reduced dimension pruned.\nIf select_last_index is True (default False), the index of the last occurrence of the max\nis selected if the max appears more than once in the input. Otherwise the index of the\nfirst occurrence is selected.\nThe type of the output tensor is integer.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "The axis in which to compute the arg indices. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + }, + { + "name": "select_last_index", + "type": "int64", + "required": false, + "description": "Whether to select the last index or the first index if the {name} appears in multiple indices, default is False (first index)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "tensor(int64)", + "description": "Reduced output tensor with integer data type." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_random')" + }, + { + "summary": "default_axes_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims,\n select_last_index=True)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_default_axis_random_select_last_index')" + }, + { + "summary": "keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_random')" + }, + { + "summary": "keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_keepdims_random_select_last_index')" + }, + { + "summary": "negative_axis_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_random')" + }, + { + "summary": "negative_axis_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_negative_axis_keepdims_random_select_last_index')" + }, + { + "summary": "no_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# result: [0, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_random')" + }, + { + "summary": "no_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMax',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [1, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmax_no_keepdims_random_select_last_index')" + } + ] + }, + { + "name": "ArgMin", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the indices of the min elements of the input tensor's element along the\nprovided axis. The resulting tensor has the same rank as the input if keepdims equals 1.\nIf keepdims equal 0, then the resulted tensor have the reduced dimension pruned.\nThe type of the output tensor is integer.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "The axis in which to compute the arg indices." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "tensor(int64)", + "description": "Reduced output tensor with integer data type." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims)\n\n# The content of result is : [[0], [0]]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_random')" + }, + { + "summary": "default_axes_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims,\n select_last_index=True)\n\n# result: [[0, 0]]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_random_select_last_index')" + }, + { + "summary": "keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_random')" + }, + { + "summary": "keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_random_select_last_index')" + }, + { + "summary": "negative_axis_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_random')" + }, + { + "summary": "negative_axis_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_random_select_last_index')" + }, + { + "summary": "no_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1, 0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_random')" + }, + { + "summary": "no_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1, 0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_random_select_last_index')" + } + ] + }, + { + "name": "ArgMin", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the indices of the min elements of the input tensor's element along the\nprovided axis. The resulting tensor has the same rank as the input if keepdims equals 1.\nIf keepdims equal 0, then the resulting tensor has the reduced dimension pruned.\nThe type of the output tensor is integer.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "The axis in which to compute the arg indices. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "tensor(int64)", + "description": "Reduced output tensor with integer data type." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims)\n\n# The content of result is : [[0], [0]]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_random')" + }, + { + "summary": "default_axes_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims,\n select_last_index=True)\n\n# result: [[0, 0]]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_random_select_last_index')" + }, + { + "summary": "keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_random')" + }, + { + "summary": "keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_random_select_last_index')" + }, + { + "summary": "negative_axis_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_random')" + }, + { + "summary": "negative_axis_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_random_select_last_index')" + }, + { + "summary": "no_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1, 0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_random')" + }, + { + "summary": "no_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1, 0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_random_select_last_index')" + } + ] + }, + { + "name": "ArgMin", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Computes the indices of the min elements of the input tensor's element along the\nprovided axis. The resulting tensor has the same rank as the input if keepdims equals 1.\nIf keepdims equal 0, then the resulting tensor has the reduced dimension pruned.\nIf select_last_index is True (default False), the index of the last occurrence of the min\nis selected if the min appears more than once in the input. Otherwise the index of the\nfirst occurrence is selected.\nThe type of the output tensor is integer.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "The axis in which to compute the arg indices. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + }, + { + "name": "select_last_index", + "type": "int64", + "required": false, + "description": "Whether to select the last index or the first index if the {name} appears in multiple indices, default is False (first index)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "tensor(int64)", + "description": "Reduced output tensor with integer data type." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims)\n\n# The content of result is : [[0], [0]]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_random')" + }, + { + "summary": "default_axes_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims,\n select_last_index=True)\n\n# result: [[0, 0]]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_random_select_last_index')" + }, + { + "summary": "keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_random')" + }, + { + "summary": "keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_random_select_last_index')" + }, + { + "summary": "negative_axis_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_random')" + }, + { + "summary": "negative_axis_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_random_select_last_index')" + }, + { + "summary": "no_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1, 0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_random')" + }, + { + "summary": "no_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1, 0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_random_select_last_index')" + } + ] + }, + { + "name": "ArgMin", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the indices of the min elements of the input tensor's element along the\nprovided axis. The resulting tensor has the same rank as the input if keepdims equals 1.\nIf keepdims equals 0, then the resulting tensor has the reduced dimension pruned.\nIf select_last_index is True (default False), the index of the last occurrence of the min\nis selected if the min appears more than once in the input. Otherwise the index of the\nfirst occurrence is selected.\nThe type of the output tensor is integer.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "The axis in which to compute the arg indices. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + }, + { + "name": "select_last_index", + "type": "int64", + "required": false, + "description": "Whether to select the last index or the first index if the {name} appears in multiple indices, default is False (first index)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "tensor(int64)", + "description": "Reduced output tensor with integer data type." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims)\n\n# The content of result is : [[0], [0]]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_random')" + }, + { + "summary": "default_axes_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n keepdims=keepdims,\n select_last_index=True)\n\n# result: [[0, 0]]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [1, 3, 4]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_default_axis_random_select_last_index')" + }, + { + "summary": "keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_random')" + }, + { + "summary": "keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 1, 4]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_keepdims_random_select_last_index')" + }, + { + "summary": "negative_axis_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_random')" + }, + { + "summary": "negative_axis_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 3, 1]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_negative_axis_keepdims_random_select_last_index')" + }, + { + "summary": "no_keepdims", + "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims)\n# The content of result is : [[1, 0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_example')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_random')" + }, + { + "summary": "no_keepdims_select_last_index", + "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ArgMin',\n inputs=['data'],\n outputs=['result'],\n axis=axis,\n keepdims=keepdims,\n select_last_index=True)\n# result: [[1, 0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_example_select_last_index')\n\ndata = np.random.uniform(-10, 10, [2, 3, 4]).astype(np.float32)\n# result's shape: [2, 4]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(node, inputs=[data], outputs=[result], name='test_argmin_no_keepdims_random_select_last_index')" + } + ] + }, + { + "name": "ArrayFeatureExtractor", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Select elements of the input tensor based on the indices passed.
\n The indices are applied to the last axes of the tensor.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Data to be selected" + }, + { + "name": "Y", + "type": "tensor(int64)", + "description": "The indices, based on 0 as the first index of any dimension." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Z", + "type": "T", + "description": "Selected output data as an array" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input must be a tensor of a numeric type or string. The output will be of the same tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)", + "tensor(string)" + ] + } + ] + }, + { + "name": "Asin", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Calculates the arcsine (inverse of sine) of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The arcsine of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "asin", + "code": "node = onnx.helper.make_node(\n 'Asin',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-0.5, 0, 0.5]).astype(np.float32)\ny = np.arcsin(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_asin_example')\n\nx = np.random.rand(3, 4, 5).astype(np.float32)\ny = np.arcsin(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_asin')" + } + ] + }, + { + "name": "Asinh", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Calculates the hyperbolic arcsine of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The hyperbolic arcsine values of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "asinh", + "code": "node = onnx.helper.make_node(\n 'Asinh',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.arcsinh(x) # expected output [-0.88137358, 0., 0.88137358]\nexpect(node, inputs=[x], outputs=[y],\n name='test_asinh_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.arcsinh(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_asinh')" + } + ] + }, + { + "name": "Atan", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Calculates the arctangent (inverse of tangent) of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The arctangent of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "atan", + "code": "node = onnx.helper.make_node(\n 'Atan',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.arctan(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_atan_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.arctan(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_atan')" + } + ] + }, + { + "name": "Atanh", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Calculates the hyperbolic arctangent of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The hyperbolic arctangent values of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "atanh", + "code": "node = onnx.helper.make_node(\n 'Atanh',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-0.5, 0, 0.5]).astype(np.float32)\ny = np.arctanh(x) # expected output [-0.54930615, 0., 0.54930615]\nexpect(node, inputs=[x], outputs=[y],\n name='test_atanh_example')\n\nx = np.random.uniform(0.0, 1.0, (3, 4, 5)).astype(np.float32)\ny = np.arctanh(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_atanh')" + } + ] + }, + { + "name": "AveragePool", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "AveragePool consumes an input tensor X and applies average pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n average pooling consisting of computing the average on all values of a\n subset of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - kernel_spatial_shape[i]) / strides_spatial_shape[i] + 1)\n\n * pad_shape[i] is sum of pads along axis i\n ```\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following:\n ```\n VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - kernel_spatial_shape[i] + 1) / strides_spatial_shape[i])\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\n And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:\n ```\n pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + kernel_spatial_shape[i] - input_spatial_shape[i]\n ```\n The output of each pooling window is divided by the number of elements exclude pad.\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from average or max pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes. Floor value of the dimension is used" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "averagepool_1d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2],\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2]\nstrides = [1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_1d_default')" + }, + { + "summary": "averagepool_2d_ceil", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n ceil_mode=True\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [6, 7.5],\n [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_ceil')" + }, + { + "summary": "averagepool_2d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_default')" + }, + { + "summary": "averagepool_2d_pads", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2]\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads')" + }, + { + "summary": "averagepool_2d_pads_count_include_pad", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2],\n count_include_pad=1,\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=0)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG', count_include_pad=1)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads_count_include_pad')" + }, + { + "summary": "averagepool_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 7.5, 8, 8.5, 9],\n [9.5, 10, 10.5, 11, 11.5],\n [12, 12.5, 13, 13.5, 14],\n [14.5, 15, 15.5, 16, 16.5],\n [17, 17.5, 18, 18.5, 19]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads')" + }, + { + "summary": "averagepool_2d_precomputed_pads_count_include_pad", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2],\n count_include_pad=1\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[2.5200, 3.6000, 4.8000, 4.0800, 3.2400],\n [4.5600, 6.4000, 8.4000, 7.0400, 5.5200],\n [7.2000, 10.0000, 13.0000, 10.8000, 8.4000],\n [6.9600, 9.6000, 12.4000, 10.2400, 7.9200],\n [6.1200, 8.4000, 10.8000, 8.8800, 6.8400]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads_count_include_pad')" + }, + { + "summary": "averagepool_2d_precomputed_same_upper", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 3, 3]\npad_shape: [2, 2] -> [1, 1, 1, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[4, 5.5, 7],\n [11.5, 13, 14.5],\n [19, 20.5, 22]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_same_upper')" + }, + { + "summary": "averagepool_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[4, 6],\n [14, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_strides')" + }, + { + "summary": "averagepool_2d_same_lower", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_LOWER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)\npad_bottom = pad_shape[0] // 2\npad_top = pad_shape[0] - pad_bottom\npad_right = pad_shape[1] // 2\npad_left = pad_shape[1] - pad_right\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_lower')" + }, + { + "summary": "averagepool_2d_same_upper", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)\npad_top = pad_shape[0] // 2\npad_bottom = pad_shape[0] - pad_top\npad_left = pad_shape[1] // 2\npad_right = pad_shape[1] - pad_left\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_upper')" + }, + { + "summary": "averagepool_2d_strides", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_strides')" + }, + { + "summary": "averagepool_3d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2, 2],\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_3d_default')" + } + ], + "category": "Pool" + }, + { + "name": "AveragePool", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "AveragePool consumes an input tensor X and applies average pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n average pooling consisting of computing the average on all values of a\n subset of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - kernel_spatial_shape[i]) / strides_spatial_shape[i] + 1)\n\n * pad_shape[i] is sum of pads along axis i\n ```\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following:\n ```\n VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - kernel_spatial_shape[i] + 1) / strides_spatial_shape[i])\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\n And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:\n ```\n pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + kernel_spatial_shape[i] - input_spatial_shape[i]\n ```\n The output of each pooling window is divided by the number of elements (exclude pad when attribute count_include_pad is zero).\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "count_include_pad", + "type": "int64", + "required": false, + "description": "Whether include pad pixels when calculating values for the edges. Default is 0, doesn't count include pad." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from average or max pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes. Floor value of the dimension is used" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "averagepool_1d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2],\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2]\nstrides = [1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_1d_default')" + }, + { + "summary": "averagepool_2d_ceil", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n ceil_mode=True\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [6, 7.5],\n [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_ceil')" + }, + { + "summary": "averagepool_2d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_default')" + }, + { + "summary": "averagepool_2d_pads", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2]\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads')" + }, + { + "summary": "averagepool_2d_pads_count_include_pad", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2],\n count_include_pad=1,\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=0)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG', count_include_pad=1)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads_count_include_pad')" + }, + { + "summary": "averagepool_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 7.5, 8, 8.5, 9],\n [9.5, 10, 10.5, 11, 11.5],\n [12, 12.5, 13, 13.5, 14],\n [14.5, 15, 15.5, 16, 16.5],\n [17, 17.5, 18, 18.5, 19]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads')" + }, + { + "summary": "averagepool_2d_precomputed_pads_count_include_pad", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2],\n count_include_pad=1\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[2.5200, 3.6000, 4.8000, 4.0800, 3.2400],\n [4.5600, 6.4000, 8.4000, 7.0400, 5.5200],\n [7.2000, 10.0000, 13.0000, 10.8000, 8.4000],\n [6.9600, 9.6000, 12.4000, 10.2400, 7.9200],\n [6.1200, 8.4000, 10.8000, 8.8800, 6.8400]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads_count_include_pad')" + }, + { + "summary": "averagepool_2d_precomputed_same_upper", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 3, 3]\npad_shape: [2, 2] -> [1, 1, 1, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[4, 5.5, 7],\n [11.5, 13, 14.5],\n [19, 20.5, 22]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_same_upper')" + }, + { + "summary": "averagepool_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[4, 6],\n [14, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_strides')" + }, + { + "summary": "averagepool_2d_same_lower", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_LOWER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)\npad_bottom = pad_shape[0] // 2\npad_top = pad_shape[0] - pad_bottom\npad_right = pad_shape[1] // 2\npad_left = pad_shape[1] - pad_right\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_lower')" + }, + { + "summary": "averagepool_2d_same_upper", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)\npad_top = pad_shape[0] // 2\npad_bottom = pad_shape[0] - pad_top\npad_left = pad_shape[1] // 2\npad_right = pad_shape[1] - pad_left\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_upper')" + }, + { + "summary": "averagepool_2d_strides", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_strides')" + }, + { + "summary": "averagepool_3d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2, 2],\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_3d_default')" + } + ], + "category": "Pool" + }, + { + "name": "AveragePool", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "AveragePool consumes an input tensor X and applies average pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n average pooling consisting of computing the average on all values of a\n subset of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - kernel_spatial_shape[i]) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - kernel_spatial_shape[i]) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled\n\n ```\n * pad_shape[i] is sum of pads along axis i\n ```\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following:\n ```\n VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - kernel_spatial_shape[i] + 1) / strides_spatial_shape[i])\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\n And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:\n ```\n pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + kernel_spatial_shape[i] - input_spatial_shape[i]\n ```\n The output of each pooling window is divided by the number of elements (exclude pad when attribute count_include_pad is zero).\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "ceil_mode", + "type": "int64", + "required": false, + "description": "Whether to use ceil or floor (default) to compute the output shape." + }, + { + "name": "count_include_pad", + "type": "int64", + "required": false, + "description": "Whether include pad pixels when calculating values for the edges. Default is 0, doesn't count include pad." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from average or max pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes. Floor value of the dimension is used" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "averagepool_1d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2],\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2]\nstrides = [1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_1d_default')" + }, + { + "summary": "averagepool_2d_ceil", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n ceil_mode=True\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [6, 7.5],\n [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_ceil')" + }, + { + "summary": "averagepool_2d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_default')" + }, + { + "summary": "averagepool_2d_pads", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2]\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads')" + }, + { + "summary": "averagepool_2d_pads_count_include_pad", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2],\n count_include_pad=1,\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=0)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG', count_include_pad=1)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads_count_include_pad')" + }, + { + "summary": "averagepool_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 7.5, 8, 8.5, 9],\n [9.5, 10, 10.5, 11, 11.5],\n [12, 12.5, 13, 13.5, 14],\n [14.5, 15, 15.5, 16, 16.5],\n [17, 17.5, 18, 18.5, 19]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads')" + }, + { + "summary": "averagepool_2d_precomputed_pads_count_include_pad", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2],\n count_include_pad=1\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[2.5200, 3.6000, 4.8000, 4.0800, 3.2400],\n [4.5600, 6.4000, 8.4000, 7.0400, 5.5200],\n [7.2000, 10.0000, 13.0000, 10.8000, 8.4000],\n [6.9600, 9.6000, 12.4000, 10.2400, 7.9200],\n [6.1200, 8.4000, 10.8000, 8.8800, 6.8400]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads_count_include_pad')" + }, + { + "summary": "averagepool_2d_precomputed_same_upper", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 3, 3]\npad_shape: [2, 2] -> [1, 1, 1, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[4, 5.5, 7],\n [11.5, 13, 14.5],\n [19, 20.5, 22]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_same_upper')" + }, + { + "summary": "averagepool_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[4, 6],\n [14, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_strides')" + }, + { + "summary": "averagepool_2d_same_lower", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_LOWER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)\npad_bottom = pad_shape[0] // 2\npad_top = pad_shape[0] - pad_bottom\npad_right = pad_shape[1] // 2\npad_left = pad_shape[1] - pad_right\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_lower')" + }, + { + "summary": "averagepool_2d_same_upper", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)\npad_top = pad_shape[0] // 2\npad_bottom = pad_shape[0] - pad_top\npad_left = pad_shape[1] // 2\npad_right = pad_shape[1] - pad_left\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_upper')" + }, + { + "summary": "averagepool_2d_strides", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_strides')" + }, + { + "summary": "averagepool_3d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2, 2],\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_3d_default')" + } + ], + "category": "Pool" + }, + { + "name": "AveragePool", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "AveragePool consumes an input tensor X and applies average pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n average pooling consisting of computing the average on all values of a\n subset of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - kernel_spatial_shape[i]) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - kernel_spatial_shape[i]) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled\n\n ```\n * pad_shape[i] is sum of pads along axis i\n ```\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following:\n ```\n VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - kernel_spatial_shape[i] + 1) / strides_spatial_shape[i])\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\n And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:\n ```\n pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + kernel_spatial_shape[i] - input_spatial_shape[i]\n ```\n The output of each pooling window is divided by the number of elements (exclude pad when attribute count_include_pad is zero).\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = ceil(input_shape[i] / strides[i])` for each axis `i`. The padding is split between the two sides equally or almost equally (depending on whether it is even or odd). In case the padding is an odd number, the extra padding is added at the end for SAME_UPPER and at the beginning for SAME_LOWER." + }, + { + "name": "ceil_mode", + "type": "int64", + "required": false, + "description": "Whether to use ceil or floor (default) to compute the output shape." + }, + { + "name": "count_include_pad", + "type": "int64", + "required": false, + "description": "Whether include pad pixels when calculating values for the edges. Default is 0, doesn't count include pad." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from average or max pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes. Floor value of the dimension is used" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "averagepool_1d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2],\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2]\nstrides = [1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_1d_default')" + }, + { + "summary": "averagepool_2d_ceil", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n ceil_mode=True\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [6, 7.5],\n [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_ceil')" + }, + { + "summary": "averagepool_2d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_default')" + }, + { + "summary": "averagepool_2d_pads", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2]\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads')" + }, + { + "summary": "averagepool_2d_pads_count_include_pad", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2],\n count_include_pad=1,\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=0)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG', count_include_pad=1)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_pads_count_include_pad')" + }, + { + "summary": "averagepool_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 7.5, 8, 8.5, 9],\n [9.5, 10, 10.5, 11, 11.5],\n [12, 12.5, 13, 13.5, 14],\n [14.5, 15, 15.5, 16, 16.5],\n [17, 17.5, 18, 18.5, 19]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads')" + }, + { + "summary": "averagepool_2d_precomputed_pads_count_include_pad", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2],\n count_include_pad=1\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[2.5200, 3.6000, 4.8000, 4.0800, 3.2400],\n [4.5600, 6.4000, 8.4000, 7.0400, 5.5200],\n [7.2000, 10.0000, 13.0000, 10.8000, 8.4000],\n [6.9600, 9.6000, 12.4000, 10.2400, 7.9200],\n [6.1200, 8.4000, 10.8000, 8.8800, 6.8400]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_pads_count_include_pad')" + }, + { + "summary": "averagepool_2d_precomputed_same_upper", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 3, 3]\npad_shape: [2, 2] -> [1, 1, 1, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[4, 5.5, 7],\n [11.5, 13, 14.5],\n [19, 20.5, 22]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_same_upper')" + }, + { + "summary": "averagepool_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[4, 6],\n [14, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_precomputed_strides')" + }, + { + "summary": "averagepool_2d_same_lower", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_LOWER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)\npad_bottom = pad_shape[0] // 2\npad_top = pad_shape[0] - pad_bottom\npad_right = pad_shape[1] // 2\npad_left = pad_shape[1] - pad_right\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_lower')" + }, + { + "summary": "averagepool_2d_same_upper", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)\npad_top = pad_shape[0] // 2\npad_bottom = pad_shape[0] - pad_top\npad_left = pad_shape[1] // 2\npad_right = pad_shape[1] - pad_left\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_same_upper')" + }, + { + "summary": "averagepool_2d_strides", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_2d_strides')" + }, + { + "summary": "averagepool_3d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'AveragePool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2, 2],\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'AVG')\n\nexpect(node, inputs=[x], outputs=[y], name='test_averagepool_3d_default')" + } + ], + "category": "Pool" + }, + { + "name": "BatchNormalization", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Carries out batch normalization as described in the paper\nhttps://arxiv.org/abs/1502.03167. Depending on the mode it is being run,\nthere are multiple cases for the number of outputs, which we list below:\n\nOutput case #1: Y, mean, var, saved_mean, saved_var (training mode)\nOutput case #2: Y (test mode)\n ", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": true, + "description": "legacy optimization attribute." + }, + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999747378752e-06, + "description": "The epsilon value to use to avoid division by zero, default is 1e-5f." + }, + { + "name": "is_test", + "type": "int64", + "required": false, + "description": "If set to nonzero, run spatial batch normalization in test mode, default is 0." + }, + { + "name": "momentum", + "type": "float32", + "required": false, + "default": 0.8999999761581421, + "description": "Factor used in computing the running mean and variance.e.g., running_mean = running_mean * momentum + mean * (1 - momentum), default is 0.9f." + }, + { + "name": "spatial", + "type": "int64", + "required": false, + "default": 1, + "description": "If true, compute the mean and variance across all spatial elements If false, compute the mean and variance across per feature.Default is 1." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input 4-dimensional tensor of shape NCHW." + }, + { + "name": "scale", + "type": "T", + "description": "The scale as a 1-dimensional tensor of size C to be applied to the output." + }, + { + "name": "B", + "type": "T", + "description": "The bias as a 1-dimensional tensor of size C to be applied to the output." + }, + { + "name": "mean", + "type": "T", + "description": "The running mean (training) or the estimated mean (testing) as a 1-dimensional tensor of size C." + }, + { + "name": "var", + "type": "T", + "description": "The running variance (training) or the estimated variance (testing) as a 1-dimensional tensor of size C." + } + ], + "min_input": 5, + "max_input": 5, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "The output 4-dimensional tensor of the same shape as X." + }, + { + "name": "mean", + "type": "T", + "option": "optional", + "description": "The running mean after the BatchNormalization operator. Must be in-place with the input mean. Should not be used for testing." + }, + { + "name": "var", + "type": "T", + "option": "optional", + "description": "The running variance after the BatchNormalization operator. Must be in-place with the input var. Should not be used for testing." + }, + { + "name": "saved_mean", + "type": "T", + "option": "optional", + "description": "Saved mean used during training to speed up gradient computation. Should not be used for testing." + }, + { + "name": "saved_var", + "type": "T", + "option": "optional", + "description": "Saved variance used during training to speed up gradient computation. Should not be used for testing." + } + ], + "min_output": 1, + "max_output": 5, + "outputs_range": "1 - 5", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "batchnormalization", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ny = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_example')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\nepsilon = 1e-2\ny = _batchnorm_test_mode(x, s, bias, mean, var, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n epsilon=epsilon,\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_epsilon')" + }, + { + "summary": "train", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\n# using np.bool(1) while generating test data with \"'bool' object has no attribute 'dtype'\"\n# working around by using np.byte(1).astype(bool)\ntraining_mode = 1\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_example_training_mode')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ntraining_mode = 1\nmomentum = 0.9\nepsilon = 1e-2\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var, momentum,\n epsilon)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n epsilon=epsilon,\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_epsilon_training_mode')" + } + ], + "category": "Normalization" + }, + { + "name": "BatchNormalization", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Carries out batch normalization as described in the paper\nhttps://arxiv.org/abs/1502.03167. Depending on the mode it is being run,\nthere are multiple cases for the number of outputs, which we list below:\n\nOutput case #1: Y, mean, var, saved_mean, saved_var (training mode)\nOutput case #2: Y (test mode)\n", + "attributes": [ + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999747378752e-06, + "description": "The epsilon value to use to avoid division by zero, default is 1e-5f." + }, + { + "name": "is_test", + "type": "int64", + "required": false, + "description": "If set to nonzero, run spatial batch normalization in test mode, default is 0." + }, + { + "name": "momentum", + "type": "float32", + "required": false, + "default": 0.8999999761581421, + "description": "Factor used in computing the running mean and variance.e.g., running_mean = running_mean * momentum + mean * (1 - momentum), default is 0.9f." + }, + { + "name": "spatial", + "type": "int64", + "required": false, + "default": 1, + "description": "If true, compute the mean and variance across all spatial elements If false, compute the mean and variance across per feature.Default is 1." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + }, + { + "name": "scale", + "type": "T", + "description": "The scale as a 1-dimensional tensor of size C to be applied to the output." + }, + { + "name": "B", + "type": "T", + "description": "The bias as a 1-dimensional tensor of size C to be applied to the output." + }, + { + "name": "mean", + "type": "T", + "description": "The running mean (training) or the estimated mean (testing) as a 1-dimensional tensor of size C." + }, + { + "name": "var", + "type": "T", + "description": "The running variance (training) or the estimated variance (testing) as a 1-dimensional tensor of size C." + } + ], + "min_input": 5, + "max_input": 5, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "The output tensor of the same shape as X." + }, + { + "name": "mean", + "type": "T", + "option": "optional", + "description": "The running mean after the BatchNormalization operator. Must be in-place with the input mean. Should not be used for testing." + }, + { + "name": "var", + "type": "T", + "option": "optional", + "description": "The running variance after the BatchNormalization operator. Must be in-place with the input var. Should not be used for testing." + }, + { + "name": "saved_mean", + "type": "T", + "option": "optional", + "description": "Saved mean used during training to speed up gradient computation. Should not be used for testing." + }, + { + "name": "saved_var", + "type": "T", + "option": "optional", + "description": "Saved variance used during training to speed up gradient computation. Should not be used for testing." + } + ], + "min_output": 1, + "max_output": 5, + "outputs_range": "1 - 5", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "batchnormalization", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ny = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_example')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\nepsilon = 1e-2\ny = _batchnorm_test_mode(x, s, bias, mean, var, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n epsilon=epsilon,\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_epsilon')" + }, + { + "summary": "train", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\n# using np.bool(1) while generating test data with \"'bool' object has no attribute 'dtype'\"\n# working around by using np.byte(1).astype(bool)\ntraining_mode = 1\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_example_training_mode')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ntraining_mode = 1\nmomentum = 0.9\nepsilon = 1e-2\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var, momentum,\n epsilon)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n epsilon=epsilon,\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_epsilon_training_mode')" + } + ], + "category": "Normalization" + }, + { + "name": "BatchNormalization", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Carries out batch normalization as described in the paper\n https://arxiv.org/abs/1502.03167. Depending on the mode it is being run,\n there are multiple cases for the number of outputs, which we list below:\n\n Output case #1: Y, mean, var, saved_mean, saved_var (training mode)\n Output case #2: Y (test mode)\n This operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999747378752e-06, + "description": "The epsilon value to use to avoid division by zero." + }, + { + "name": "momentum", + "type": "float32", + "required": false, + "default": 0.8999999761581421, + "description": "Factor used in computing the running mean and variance.e.g., running_mean = running_mean * momentum + mean * (1 - momentum)." + }, + { + "name": "spatial", + "type": "int64", + "required": false, + "default": 1, + "description": "If true, compute the mean and variance across per activation. If false, compute the mean and variance across per feature over each mini-batch." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + }, + { + "name": "scale", + "type": "T", + "description": "If spatial is true, the dimension of scale is (C). If spatial is false, the dimensions of scale are (C x D1 x ... x Dn)" + }, + { + "name": "B", + "type": "T", + "description": "If spatial is true, the dimension of bias is (C). If spatial is false, the dimensions of bias are (C x D1 x ... x Dn)" + }, + { + "name": "mean", + "type": "T", + "description": "If spatial is true, the dimension of the running mean (training) or the estimated mean (testing) is (C). If spatial is false, the dimensions of the running mean (training) or the estimated mean (testing) are (C x D1 x ... x Dn)." + }, + { + "name": "var", + "type": "T", + "description": "If spatial is true, the dimension of the running variance(training) or the estimated variance (testing) is (C). If spatial is false, the dimensions of the running variance(training) or the estimated variance (testing) are (C x D1 x ... x Dn)." + } + ], + "min_input": 5, + "max_input": 5, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "The output tensor of the same shape as X" + }, + { + "name": "mean", + "type": "T", + "option": "optional", + "description": "The running mean after the BatchNormalization operator." + }, + { + "name": "var", + "type": "T", + "option": "optional", + "description": "The running variance after the BatchNormalization operator." + }, + { + "name": "saved_mean", + "type": "T", + "option": "optional", + "description": "Saved mean used during training to speed up gradient computation." + }, + { + "name": "saved_var", + "type": "T", + "option": "optional", + "description": "Saved variance used during training to speed up gradient computation." + } + ], + "min_output": 1, + "max_output": 5, + "outputs_range": "1 - 5", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "batchnormalization", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ny = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_example')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\nepsilon = 1e-2\ny = _batchnorm_test_mode(x, s, bias, mean, var, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n epsilon=epsilon,\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_epsilon')" + }, + { + "summary": "train", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\n# using np.bool(1) while generating test data with \"'bool' object has no attribute 'dtype'\"\n# working around by using np.byte(1).astype(bool)\ntraining_mode = 1\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_example_training_mode')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ntraining_mode = 1\nmomentum = 0.9\nepsilon = 1e-2\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var, momentum,\n epsilon)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n epsilon=epsilon,\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_epsilon_training_mode')" + } + ], + "category": "Normalization" + }, + { + "name": "BatchNormalization", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Carries out batch normalization as described in the paper\nhttps://arxiv.org/abs/1502.03167. Depending on the mode it is being run,\nthere are multiple cases for the number of outputs, which we list below:\n\nOutput case #1: Y, mean, var, saved_mean, saved_var (training mode)\nOutput case #2: Y (test mode)\n\nFor previous (depreciated) non-spatial cases, implementors are suggested\nto flatten the input shape to (N x C*D1*D2 ..*Dn) before a BatchNormalization Op.\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999747378752e-06, + "description": "The epsilon value to use to avoid division by zero." + }, + { + "name": "momentum", + "type": "float32", + "required": false, + "default": 0.8999999761581421, + "description": "Factor used in computing the running mean and variance.e.g., running_mean = running_mean * momentum + mean * (1 - momentum)." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size, C is the number of channels. Statistics are computed for every channel of C over N and D1 to Dn dimensions. For image data, input dimensions become (N x C x H x W). The op also accepts single dimension input of size N in which case C is assumed to be 1" + }, + { + "name": "scale", + "type": "T", + "description": "Scale tensor of shape (C)." + }, + { + "name": "B", + "type": "T", + "description": "Bias tensor of shape (C)." + }, + { + "name": "mean", + "type": "T", + "description": "running (training) or estimated (testing) mean tensor of shape (C)." + }, + { + "name": "var", + "type": "T", + "description": "running (training) or estimated (testing) variance tensor of shape (C)." + } + ], + "min_input": 5, + "max_input": 5, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "The output tensor of the same shape as X" + }, + { + "name": "mean", + "type": "T", + "option": "optional", + "description": "The running mean after the BatchNormalization operator." + }, + { + "name": "var", + "type": "T", + "option": "optional", + "description": "The running variance after the BatchNormalization operator." + }, + { + "name": "saved_mean", + "type": "T", + "option": "optional", + "description": "Saved mean used during training to speed up gradient computation." + }, + { + "name": "saved_var", + "type": "T", + "option": "optional", + "description": "Saved variance used during training to speed up gradient computation." + } + ], + "min_output": 1, + "max_output": 5, + "outputs_range": "1 - 5", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "batchnormalization", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ny = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_example')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\nepsilon = 1e-2\ny = _batchnorm_test_mode(x, s, bias, mean, var, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n epsilon=epsilon,\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_epsilon')" + }, + { + "summary": "train", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\n# using np.bool(1) while generating test data with \"'bool' object has no attribute 'dtype'\"\n# working around by using np.byte(1).astype(bool)\ntraining_mode = 1\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_example_training_mode')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ntraining_mode = 1\nmomentum = 0.9\nepsilon = 1e-2\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var, momentum,\n epsilon)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n epsilon=epsilon,\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_epsilon_training_mode')" + } + ], + "category": "Normalization" + }, + { + "name": "BatchNormalization", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Carries out batch normalization as described in the paper\nhttps://arxiv.org/abs/1502.03167. Depending on the mode it is being run,\nThere are five required inputs 'X', 'scale', 'B', 'input_mean' and\n'input_var'.\nNote that 'input_mean' and 'input_var' are expected to be the estimated\nstatistics in inference mode (training_mode=False, default),\nand the running statistics in training mode (training_mode=True).\nThere are multiple cases for the number of outputs, which we list below:\n\nOutput case #1: Y, running_mean, running_var (training_mode=True)\nOutput case #2: Y (training_mode=False)\n\nWhen training_mode=False, extra outputs are invalid.\nThe outputs are updated as follows when training_mode=True:\n```\nrunning_mean = input_mean * momentum + current_mean * (1 - momentum)\nrunning_var = input_var * momentum + current_var * (1 - momentum)\n\nY = (X - current_mean) / sqrt(current_var + epsilon) * scale + B\n\nwhere:\n\ncurrent_mean = ReduceMean(X, axis=all_except_channel_index)\ncurrent_var = ReduceVar(X, axis=all_except_channel_index)\n\nNotice that ReduceVar refers to the population variance, and it equals to\nsum(sqrd(x_i - x_avg)) / N\nwhere N is the population size (this formula does not use sample size N - 1).\n\n```\n\nWhen training_mode=False:\n```\nY = (X - input_mean) / sqrt(input_var + epsilon) * scale + B\n```\n\nFor previous (depreciated) non-spatial cases, implementors are suggested\nto flatten the input shape to (N x C * D1 * D2 * ... * Dn) before a BatchNormalization Op.\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999747378752e-06, + "description": "The epsilon value to use to avoid division by zero." + }, + { + "name": "momentum", + "type": "float32", + "required": false, + "default": 0.8999999761581421, + "description": "Factor used in computing the running mean and variance.e.g., running_mean = running_mean * momentum + mean * (1 - momentum)." + }, + { + "name": "training_mode", + "type": "int64", + "required": false, + "description": "If set to true, it indicates BatchNormalization is being used for training, and outputs 1, 2, 3, and 4 would be populated." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size, C is the number of channels. Statistics are computed for every channel of C over N and D1 to Dn dimensions. For image data, input dimensions become (N x C x H x W). The op also accepts single dimension input of size N in which case C is assumed to be 1" + }, + { + "name": "scale", + "type": "T", + "description": "Scale tensor of shape (C)." + }, + { + "name": "B", + "type": "T", + "description": "Bias tensor of shape (C)." + }, + { + "name": "input_mean", + "type": "U", + "description": "running (training) or estimated (testing) mean tensor of shape (C)." + }, + { + "name": "input_var", + "type": "U", + "description": "running (training) or estimated (testing) variance tensor of shape (C)." + } + ], + "min_input": 5, + "max_input": 5, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "The output tensor of the same shape as X" + }, + { + "name": "running_mean", + "type": "U", + "option": "optional", + "description": "The running mean after the BatchNormalization operator." + }, + { + "name": "running_var", + "type": "U", + "option": "optional", + "description": "The running variance after the BatchNormalization operator. This op uses the population size (N) for calculating variance, and not the sample size N-1." + } + ], + "min_output": 1, + "max_output": 3, + "outputs_range": "1 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain mean and variance types to float tensors. It allows all float type for U.", + "type_param_str": "U", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "batchnormalization", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ny = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_example')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\nepsilon = 1e-2\ny = _batchnorm_test_mode(x, s, bias, mean, var, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n epsilon=epsilon,\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_epsilon')" + }, + { + "summary": "train", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\n# using np.bool(1) while generating test data with \"'bool' object has no attribute 'dtype'\"\n# working around by using np.byte(1).astype(bool)\ntraining_mode = 1\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_example_training_mode')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ntraining_mode = 1\nmomentum = 0.9\nepsilon = 1e-2\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var, momentum,\n epsilon)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n epsilon=epsilon,\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_epsilon_training_mode')" + } + ], + "category": "Normalization" + }, + { + "name": "BatchNormalization", + "module": "ai.onnx", + "version": 15, + "support_level": "common", + "description": "Carries out batch normalization as described in the paper\nhttps://arxiv.org/abs/1502.03167. Depending on the mode it is being run,\nThere are five required inputs 'X', 'scale', 'B', 'input_mean' and\n'input_var'.\nNote that 'input_mean' and 'input_var' are expected to be the estimated\nstatistics in inference mode (training_mode=False, default),\nand the running statistics in training mode (training_mode=True).\nThere are multiple cases for the number of outputs, which we list below:\n\nOutput case #1: Y, running_mean, running_var (training_mode=True)\nOutput case #2: Y (training_mode=False)\n\nWhen training_mode=False, extra outputs are invalid.\nThe outputs are updated as follows when training_mode=True:\n```\nrunning_mean = input_mean * momentum + current_mean * (1 - momentum)\nrunning_var = input_var * momentum + current_var * (1 - momentum)\n\nY = (X - current_mean) / sqrt(current_var + epsilon) * scale + B\n\nwhere:\n\ncurrent_mean = ReduceMean(X, axis=all_except_channel_index)\ncurrent_var = ReduceVar(X, axis=all_except_channel_index)\n\nNotice that ReduceVar refers to the population variance, and it equals to\nsum(sqrd(x_i - x_avg)) / N\nwhere N is the population size (this formula does not use sample size N - 1).\n\n```\n\nThe computation of ReduceMean and ReduceVar uses float to avoid overflow for float16 inputs.\n\nWhen training_mode=False:\n```\nY = (X - input_mean) / sqrt(input_var + epsilon) * scale + B\n```\n\nFor previous (depreciated) non-spatial cases, implementors are suggested\nto flatten the input shape to (N x C * D1 * D2 * ... * Dn) before a BatchNormalization Op.\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999747378752e-06, + "description": "The epsilon value to use to avoid division by zero." + }, + { + "name": "momentum", + "type": "float32", + "required": false, + "default": 0.8999999761581421, + "description": "Factor used in computing the running mean and variance.e.g., running_mean = running_mean * momentum + mean * (1 - momentum)." + }, + { + "name": "training_mode", + "type": "int64", + "required": false, + "description": "If set to true, it indicates BatchNormalization is being used for training, and outputs 1, 2, 3, and 4 would be populated." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size, C is the number of channels. Statistics are computed for every channel of C over N and D1 to Dn dimensions. For image data, input dimensions become (N x C x H x W). The op also accepts single dimension input of size N in which case C is assumed to be 1" + }, + { + "name": "scale", + "type": "T1", + "description": "Scale tensor of shape (C)." + }, + { + "name": "B", + "type": "T1", + "description": "Bias tensor of shape (C)." + }, + { + "name": "input_mean", + "type": "T2", + "description": "running (training) or estimated (testing) mean tensor of shape (C)." + }, + { + "name": "input_var", + "type": "T2", + "description": "running (training) or estimated (testing) variance tensor of shape (C)." + } + ], + "min_input": 5, + "max_input": 5, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "The output tensor of the same shape as X" + }, + { + "name": "running_mean", + "type": "T2", + "option": "optional", + "description": "The running mean after the BatchNormalization operator." + }, + { + "name": "running_var", + "type": "T2", + "option": "optional", + "description": "The running variance after the BatchNormalization operator. This op uses the population size (N) for calculating variance, and not the sample size N-1." + } + ], + "min_output": 1, + "max_output": 3, + "outputs_range": "1 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain scale and bias types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain mean and variance types to float tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "batchnormalization", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ny = _batchnorm_test_mode(x, s, bias, mean, var).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_example')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\nepsilon = 1e-2\ny = _batchnorm_test_mode(x, s, bias, mean, var, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y'],\n epsilon=epsilon,\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var], outputs=[y],\n name='test_batchnorm_epsilon')" + }, + { + "summary": "train", + "code": "# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\n# using np.bool(1) while generating test data with \"'bool' object has no attribute 'dtype'\"\n# working around by using np.byte(1).astype(bool)\ntraining_mode = 1\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_example_training_mode')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nmean = np.random.randn(3).astype(np.float32)\nvar = np.random.rand(3).astype(np.float32)\ntraining_mode = 1\nmomentum = 0.9\nepsilon = 1e-2\ny, output_mean, output_var = _batchnorm_training_mode(x, s, bias, mean, var, momentum,\n epsilon)\n\nnode = onnx.helper.make_node(\n 'BatchNormalization',\n inputs=['x', 's', 'bias', 'mean', 'var'],\n outputs=['y', 'output_mean', 'output_var'],\n epsilon=epsilon,\n training_mode=training_mode\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name='test_batchnorm_epsilon_training_mode')" + } + ], + "category": "Normalization" + }, + { + "name": "Bernoulli", + "module": "ai.onnx", + "version": 15, + "support_level": "common", + "description": "Draws binary random numbers (0 or 1) from a Bernoulli distribution. The input tensor should be a tensor\ncontaining probabilities p (a value in the range [0,1]) to be used for drawing the binary random number,\nwhere an output of 1 is produced with probability p and an output of 0 is produced with probability (1-p).\n\nThis operator is non-deterministic and may not produce the same values in different\nimplementations (even if a seed is specified).\n", + "attributes": [ + { + "name": "dtype", + "type": "int64", + "required": false, + "description": "The data type for the elements of the output tensor. if not specified, we will use the data type of the input tensor." + }, + { + "name": "seed", + "type": "float32", + "required": false, + "description": "(Optional) Seed to the random generator, if not specified we will auto generate one." + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "All values in input have to be in the range:[0, 1]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "The returned output tensor only has values 0 or 1, same shape as input tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output types to all numeric tensors and bool tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "bernoulli_with_dtype", + "code": "node = onnx.helper.make_node(\n 'Bernoulli',\n inputs=['x'],\n outputs=['y'],\n dtype=onnx.TensorProto.DOUBLE,\n)\n\nx = np.random.uniform(0.0, 1.0, 10).astype(np.float32)\ny = bernoulli_reference_implementation(x, np.float64)\nexpect(node, inputs=[x], outputs=[y], name='test_bernoulli_double')" + }, + { + "summary": "bernoulli_with_seed", + "code": "seed = np.float(0)\nnode = onnx.helper.make_node(\n 'Bernoulli',\n inputs=['x'],\n outputs=['y'],\n seed=seed,\n)\n\nx = np.random.uniform(0.0, 1.0, 10).astype(np.float32)\ny = bernoulli_reference_implementation(x, np.float32)\nexpect(node, inputs=[x], outputs=[y], name='test_bernoulli_seed')" + }, + { + "summary": "bernoulli_without_dtype", + "code": "node = onnx.helper.make_node(\n 'Bernoulli',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.random.uniform(0.0, 1.0, 10).astype(np.float)\ny = bernoulli_reference_implementation(x, np.float)\nexpect(node, inputs=[x], outputs=[y], name='test_bernoulli')" + } + ] + }, + { + "name": "Binarizer", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Maps the values of the input tensor to either 0 or 1, element-wise, based on the outcome of a comparison against a threshold value.\n", + "attributes": [ + { + "name": "threshold", + "type": "float32", + "required": false, + "description": "Values greater than this are mapped to 1, others to 0." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Data to be binarized" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Binarized output data" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input must be a tensor of a numeric type. The output will be of the same tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + } + ] + }, + { + "name": "BitShift", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Bitwise shift operator performs element-wise operation. For each input element, if the\n attribute \"direction\" is \"RIGHT\", this operator moves its binary representation toward\n the right side so that the input value is effectively decreased. If the attribute \"direction\"\n is \"LEFT\", bits of binary representation moves toward the left side, which results the\n increase of its actual value. The input X is the tensor to be shifted and another input\n Y specifies the amounts of shifting. For example, if \"direction\" is \"Right\", X is [1, 4],\n and S is [1, 1], the corresponding output Z would be [0, 2]. If \"direction\" is \"LEFT\" with\n X=[1, 2] and S=[1, 2], the corresponding output Y would be [2, 8].\n\n Because this operator supports Numpy-style broadcasting, X's and Y's shapes are\n not necessarily identical.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "attributes": [ + { + "name": "direction", + "type": "string", + "required": true, + "description": "Direction of moving bits. It can be either \"RIGHT\" (for right shift) or \"LEFT\" (for left shift)." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "First operand, input to be shifted." + }, + { + "name": "Y", + "type": "T", + "description": "Second operand, amounts of shift." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Z", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to integer tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)" + ] + } + ], + "examples": [ + { + "summary": "left_unit16", + "code": "node = onnx.helper.make_node(\n 'BitShift',\n inputs=['x', 'y'],\n outputs=['z'],\n direction=\"LEFT\"\n)\n\nx = np.array([16, 4, 1]).astype(np.uint16)\ny = np.array([1, 2, 3]).astype(np.uint16)\nz = x << y # expected output [32, 16, 8]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_bitshift_left_uint16')" + }, + { + "summary": "left_unit32", + "code": "node = onnx.helper.make_node(\n 'BitShift',\n inputs=['x', 'y'],\n outputs=['z'],\n direction=\"LEFT\"\n)\n\nx = np.array([16, 4, 1]).astype(np.uint32)\ny = np.array([1, 2, 3]).astype(np.uint32)\nz = x << y # expected output [32, 16, 8]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_bitshift_left_uint32')" + }, + { + "summary": "left_unit64", + "code": "node = onnx.helper.make_node(\n 'BitShift',\n inputs=['x', 'y'],\n outputs=['z'],\n direction=\"LEFT\"\n)\n\nx = np.array([16, 4, 1]).astype(np.uint64)\ny = np.array([1, 2, 3]).astype(np.uint64)\nz = x << y # expected output [32, 16, 8]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_bitshift_left_uint64')" + }, + { + "summary": "left_unit8", + "code": "node = onnx.helper.make_node(\n 'BitShift',\n inputs=['x', 'y'],\n outputs=['z'],\n direction=\"LEFT\"\n)\n\nx = np.array([16, 4, 1]).astype(np.uint8)\ny = np.array([1, 2, 3]).astype(np.uint8)\nz = x << y # expected output [32, 16, 8]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_bitshift_left_uint8')" + }, + { + "summary": "right_unit16", + "code": "node = onnx.helper.make_node(\n 'BitShift',\n inputs=['x', 'y'],\n outputs=['z'],\n direction=\"RIGHT\"\n)\n\nx = np.array([16, 4, 1]).astype(np.uint16)\ny = np.array([1, 2, 3]).astype(np.uint16)\nz = x >> y # expected output [8, 1, 0]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_bitshift_right_uint16')" + }, + { + "summary": "right_unit32", + "code": "node = onnx.helper.make_node(\n 'BitShift',\n inputs=['x', 'y'],\n outputs=['z'],\n direction=\"RIGHT\"\n)\n\nx = np.array([16, 4, 1]).astype(np.uint32)\ny = np.array([1, 2, 3]).astype(np.uint32)\nz = x >> y # expected output [8, 1, 0]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_bitshift_right_uint32')" + }, + { + "summary": "right_unit64", + "code": "node = onnx.helper.make_node(\n 'BitShift',\n inputs=['x', 'y'],\n outputs=['z'],\n direction=\"RIGHT\"\n)\n\nx = np.array([16, 4, 1]).astype(np.uint64)\ny = np.array([1, 2, 3]).astype(np.uint64)\nz = x >> y # expected output [8, 1, 0]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_bitshift_right_uint64')" + }, + { + "summary": "right_unit8", + "code": "node = onnx.helper.make_node(\n 'BitShift',\n inputs=['x', 'y'],\n outputs=['z'],\n direction=\"RIGHT\"\n)\n\nx = np.array([16, 4, 1]).astype(np.uint8)\ny = np.array([1, 2, 3]).astype(np.uint8)\nz = x >> y # expected output [8, 1, 0]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_bitshift_right_uint8')" + } + ] + }, + { + "name": "Cast", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "The operator casts the elements of a given input tensor to a data type\nspecified by the 'to' argument and returns an output tensor of the same size in\nthe converted type. The 'to' argument must be one of the data types specified\nin the 'DataType' enum field in the TensorProto message.\nNOTE: Casting to and from strings is not supported yet.\n", + "attributes": [ + { + "name": "to", + "type": "DataType", + "required": true, + "description": "The data type to which the elements of the input tensor are cast. Strictly must be one of the types from DataType enum in TensorProto" + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "Input tensor to be cast." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor with the same shape as input with type specified by the 'to' argument" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types. Casting from strings and complex are not supported.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)" + ] + }, + { + "description": "Constrain output types. Casting to strings and complex are not supported.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "cast", + "code": "shape = (3, 4)\ntest_cases = [\n ('FLOAT', 'FLOAT16'),\n ('FLOAT', 'DOUBLE'),\n ('FLOAT16', 'FLOAT'),\n ('FLOAT16', 'DOUBLE'),\n ('DOUBLE', 'FLOAT'),\n ('DOUBLE', 'FLOAT16'),\n ('FLOAT', 'STRING'),\n ('STRING', 'FLOAT'),\n ('FLOAT', 'BFLOAT16'),\n ('BFLOAT16', 'FLOAT'),\n]\n\nfor from_type, to_type in test_cases:\n input_type_proto = None\n output_type_proto = None\n if 'BFLOAT16' == from_type or 'BFLOAT16' == to_type:\n np_fp32 = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.float32)\n little_endisan = sys.byteorder == 'little'\n np_uint16_view = np_fp32.view(dtype=np.uint16)\n np_bfp16 = np_uint16_view[1::2] if little_endisan else np_uint16_view[0::2]\n if 'BFLOAT16' == to_type:\n assert from_type == 'FLOAT'\n input = np_fp32.reshape([3, 4])\n output = np_bfp16.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), output.shape)\n else:\n assert to_type == 'FLOAT'\n input = np_bfp16.reshape([3, 4])\n #convert bfloat to FLOAT\n np_fp32_zeros = np.zeros((len(np_bfp16) * 2,), dtype=np.uint16)\n if little_endisan:\n np_fp32_zeros[1::2] = np_bfp16\n else:\n np_fp32_zeros[0::2] = np_bfp16\n np_fp32_from_bfloat = np_fp32_zeros.view(dtype=np.float32)\n output = np_fp32_from_bfloat.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), output.shape)\n elif 'STRING' != from_type:\n input = np.random.random_sample(shape).astype(\n TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, from_type)])\n if ('STRING' == to_type):\n # Converting input to str, then give it object dtype for generating script\n ss = []\n for i in input.flatten():\n s = str(i).encode('utf-8')\n su = s.decode('utf-8')\n ss.append(su)\n\n output = np.array(ss).astype(object).reshape([3, 4])\n else:\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n else:\n input = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.dtype(object)).reshape([3, 4])\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n node = onnx.helper.make_node(\n 'Cast',\n inputs=['input'],\n outputs=['output'],\n to=getattr(TensorProto, to_type),\n )\n if input_type_proto and output_type_proto:\n expect(node, inputs=[input], outputs=[output],\n name='test_cast_' + from_type + '_to_' + to_type,\n input_type_protos=[input_type_proto],\n output_type_protos=[output_type_proto])\n else:\n expect(node, inputs=[input], outputs=[output],\n name='test_cast_' + from_type + '_to_' + to_type)" + } + ] + }, + { + "name": "Cast", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "The operator casts the elements of a given input tensor to a data type\nspecified by the 'to' argument and returns an output tensor of the same size in\nthe converted type. The 'to' argument must be one of the data types specified\nin the 'DataType' enum field in the TensorProto message.\nNOTE: Casting to and from strings is not supported yet.\n", + "attributes": [ + { + "name": "to", + "type": "DataType", + "required": true, + "description": "The data type to which the elements of the input tensor are cast. Strictly must be one of the types from DataType enum in TensorProto" + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "Input tensor to be cast." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor with the same shape as input with type specified by the 'to' argument" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types. Casting from strings and complex are not supported.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)" + ] + }, + { + "description": "Constrain output types. Casting to strings and complex are not supported.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "cast", + "code": "shape = (3, 4)\ntest_cases = [\n ('FLOAT', 'FLOAT16'),\n ('FLOAT', 'DOUBLE'),\n ('FLOAT16', 'FLOAT'),\n ('FLOAT16', 'DOUBLE'),\n ('DOUBLE', 'FLOAT'),\n ('DOUBLE', 'FLOAT16'),\n ('FLOAT', 'STRING'),\n ('STRING', 'FLOAT'),\n ('FLOAT', 'BFLOAT16'),\n ('BFLOAT16', 'FLOAT'),\n]\n\nfor from_type, to_type in test_cases:\n input_type_proto = None\n output_type_proto = None\n if 'BFLOAT16' == from_type or 'BFLOAT16' == to_type:\n np_fp32 = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.float32)\n little_endisan = sys.byteorder == 'little'\n np_uint16_view = np_fp32.view(dtype=np.uint16)\n np_bfp16 = np_uint16_view[1::2] if little_endisan else np_uint16_view[0::2]\n if 'BFLOAT16' == to_type:\n assert from_type == 'FLOAT'\n input = np_fp32.reshape([3, 4])\n output = np_bfp16.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), output.shape)\n else:\n assert to_type == 'FLOAT'\n input = np_bfp16.reshape([3, 4])\n #convert bfloat to FLOAT\n np_fp32_zeros = np.zeros((len(np_bfp16) * 2,), dtype=np.uint16)\n if little_endisan:\n np_fp32_zeros[1::2] = np_bfp16\n else:\n np_fp32_zeros[0::2] = np_bfp16\n np_fp32_from_bfloat = np_fp32_zeros.view(dtype=np.float32)\n output = np_fp32_from_bfloat.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), output.shape)\n elif 'STRING' != from_type:\n input = np.random.random_sample(shape).astype(\n TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, from_type)])\n if ('STRING' == to_type):\n # Converting input to str, then give it object dtype for generating script\n ss = []\n for i in input.flatten():\n s = str(i).encode('utf-8')\n su = s.decode('utf-8')\n ss.append(su)\n\n output = np.array(ss).astype(object).reshape([3, 4])\n else:\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n else:\n input = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.dtype(object)).reshape([3, 4])\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n node = onnx.helper.make_node(\n 'Cast',\n inputs=['input'],\n outputs=['output'],\n to=getattr(TensorProto, to_type),\n )\n if input_type_proto and output_type_proto:\n expect(node, inputs=[input], outputs=[output],\n name='test_cast_' + from_type + '_to_' + to_type,\n input_type_protos=[input_type_proto],\n output_type_protos=[output_type_proto])\n else:\n expect(node, inputs=[input], outputs=[output],\n name='test_cast_' + from_type + '_to_' + to_type)" + } + ] + }, + { + "name": "Cast", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "The operator casts the elements of a given input tensor to a data type\nspecified by the 'to' argument and returns an output tensor of the same size in\nthe converted type. The 'to' argument must be one of the data types specified\nin the 'DataType' enum field in the TensorProto message.\n\nCasting from string tensor in plain (e.g., \"3.14\" and \"1000\") and scientific numeric representations\n(e.g., \"1e-5\" and \"1E8\") to float types is supported. For example, converting string \"100.5\" to an integer may\nresult 100. There are some string literals reserved for special floating-point values;\n\"+INF\" (and \"INF\"), \"-INF\", and \"NaN\" are positive infinity, negative infinity, and not-a-number, respectively.\nAny string which can exactly match \"+INF\" in a case-insensitive way would be mapped to positive infinite. Similarly,\nthis case-insensitive rule is applied to \"INF\" and \"NaN\". When casting from numeric tensors\nto string tensors, plain floating-point representation (such as \"314.15926\") would be used.\nConverting non-numerical-literal string such as \"Hello World!\" is an undefined behavior. Cases\nof converting string representing floating-point arithmetic value, such as \"2.718\", to INT is an undefined behavior.\n\nConversion from a numerical type to any numerical type is always allowed.\nUser must be aware of precision loss and value change caused by range difference between two types.\nFor example, a 64-bit float 3.1415926459 may be round to a 32-bit float 3.141592. Similarly, converting\nan integer 36 to Boolean may produce 1 because we truncate bits which can't be stored in the targeted type.\n", + "attributes": [ + { + "name": "to", + "type": "DataType", + "required": true, + "description": "The data type to which the elements of the input tensor are cast. Strictly must be one of the types from DataType enum in TensorProto" + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "Input tensor to be cast." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor with the same shape as input with type specified by the 'to' argument" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types. Casting from complex is not supported.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)", + "tensor(string)" + ] + }, + { + "description": "Constrain output types. Casting to complex is not supported.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)", + "tensor(string)" + ] + } + ], + "examples": [ + { + "summary": "cast", + "code": "shape = (3, 4)\ntest_cases = [\n ('FLOAT', 'FLOAT16'),\n ('FLOAT', 'DOUBLE'),\n ('FLOAT16', 'FLOAT'),\n ('FLOAT16', 'DOUBLE'),\n ('DOUBLE', 'FLOAT'),\n ('DOUBLE', 'FLOAT16'),\n ('FLOAT', 'STRING'),\n ('STRING', 'FLOAT'),\n ('FLOAT', 'BFLOAT16'),\n ('BFLOAT16', 'FLOAT'),\n]\n\nfor from_type, to_type in test_cases:\n input_type_proto = None\n output_type_proto = None\n if 'BFLOAT16' == from_type or 'BFLOAT16' == to_type:\n np_fp32 = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.float32)\n little_endisan = sys.byteorder == 'little'\n np_uint16_view = np_fp32.view(dtype=np.uint16)\n np_bfp16 = np_uint16_view[1::2] if little_endisan else np_uint16_view[0::2]\n if 'BFLOAT16' == to_type:\n assert from_type == 'FLOAT'\n input = np_fp32.reshape([3, 4])\n output = np_bfp16.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), output.shape)\n else:\n assert to_type == 'FLOAT'\n input = np_bfp16.reshape([3, 4])\n #convert bfloat to FLOAT\n np_fp32_zeros = np.zeros((len(np_bfp16) * 2,), dtype=np.uint16)\n if little_endisan:\n np_fp32_zeros[1::2] = np_bfp16\n else:\n np_fp32_zeros[0::2] = np_bfp16\n np_fp32_from_bfloat = np_fp32_zeros.view(dtype=np.float32)\n output = np_fp32_from_bfloat.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), output.shape)\n elif 'STRING' != from_type:\n input = np.random.random_sample(shape).astype(\n TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, from_type)])\n if ('STRING' == to_type):\n # Converting input to str, then give it object dtype for generating script\n ss = []\n for i in input.flatten():\n s = str(i).encode('utf-8')\n su = s.decode('utf-8')\n ss.append(su)\n\n output = np.array(ss).astype(object).reshape([3, 4])\n else:\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n else:\n input = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.dtype(object)).reshape([3, 4])\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n node = onnx.helper.make_node(\n 'Cast',\n inputs=['input'],\n outputs=['output'],\n to=getattr(TensorProto, to_type),\n )\n if input_type_proto and output_type_proto:\n expect(node, inputs=[input], outputs=[output],\n name='test_cast_' + from_type + '_to_' + to_type,\n input_type_protos=[input_type_proto],\n output_type_protos=[output_type_proto])\n else:\n expect(node, inputs=[input], outputs=[output],\n name='test_cast_' + from_type + '_to_' + to_type)" + } + ] + }, + { + "name": "Cast", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "The operator casts the elements of a given input tensor to a data type\nspecified by the 'to' argument and returns an output tensor of the same size in\nthe converted type. The 'to' argument must be one of the data types specified\nin the 'DataType' enum field in the TensorProto message.\n\nCasting from string tensor in plain (e.g., \"3.14\" and \"1000\") and scientific numeric representations\n(e.g., \"1e-5\" and \"1E8\") to float types is supported. For example, converting string \"100.5\" to an integer may\nresult 100. There are some string literals reserved for special floating-point values;\n\"+INF\" (and \"INF\"), \"-INF\", and \"NaN\" are positive infinity, negative infinity, and not-a-number, respectively.\nAny string which can exactly match \"+INF\" in a case-insensitive way would be mapped to positive infinite. Similarly,\nthis case-insensitive rule is applied to \"INF\" and \"NaN\". When casting from numeric tensors\nto string tensors, plain floating-point representation (such as \"314.15926\") would be used.\nConverting non-numerical-literal string such as \"Hello World!\" is an undefined behavior. Cases\nof converting string representing floating-point arithmetic value, such as \"2.718\", to INT is an undefined behavior.\n\nConversion from a numerical type to any numerical type is always allowed.\nUser must be aware of precision loss and value change caused by range difference between two types.\nFor example, a 64-bit float 3.1415926459 may be round to a 32-bit float 3.141592. Similarly, converting\nan integer 36 to Boolean may produce 1 because we truncate bits which can't be stored in the targeted type.\n", + "attributes": [ + { + "name": "to", + "type": "DataType", + "required": true, + "description": "The data type to which the elements of the input tensor are cast. Strictly must be one of the types from DataType enum in TensorProto" + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "Input tensor to be cast." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor with the same shape as input with type specified by the 'to' argument" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types. Casting from complex is not supported.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)", + "tensor(string)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain output types. Casting to complex is not supported.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)", + "tensor(string)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "cast", + "code": "shape = (3, 4)\ntest_cases = [\n ('FLOAT', 'FLOAT16'),\n ('FLOAT', 'DOUBLE'),\n ('FLOAT16', 'FLOAT'),\n ('FLOAT16', 'DOUBLE'),\n ('DOUBLE', 'FLOAT'),\n ('DOUBLE', 'FLOAT16'),\n ('FLOAT', 'STRING'),\n ('STRING', 'FLOAT'),\n ('FLOAT', 'BFLOAT16'),\n ('BFLOAT16', 'FLOAT'),\n]\n\nfor from_type, to_type in test_cases:\n input_type_proto = None\n output_type_proto = None\n if 'BFLOAT16' == from_type or 'BFLOAT16' == to_type:\n np_fp32 = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.float32)\n little_endisan = sys.byteorder == 'little'\n np_uint16_view = np_fp32.view(dtype=np.uint16)\n np_bfp16 = np_uint16_view[1::2] if little_endisan else np_uint16_view[0::2]\n if 'BFLOAT16' == to_type:\n assert from_type == 'FLOAT'\n input = np_fp32.reshape([3, 4])\n output = np_bfp16.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), output.shape)\n else:\n assert to_type == 'FLOAT'\n input = np_bfp16.reshape([3, 4])\n #convert bfloat to FLOAT\n np_fp32_zeros = np.zeros((len(np_bfp16) * 2,), dtype=np.uint16)\n if little_endisan:\n np_fp32_zeros[1::2] = np_bfp16\n else:\n np_fp32_zeros[0::2] = np_bfp16\n np_fp32_from_bfloat = np_fp32_zeros.view(dtype=np.float32)\n output = np_fp32_from_bfloat.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), output.shape)\n elif 'STRING' != from_type:\n input = np.random.random_sample(shape).astype(\n TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, from_type)])\n if ('STRING' == to_type):\n # Converting input to str, then give it object dtype for generating script\n ss = []\n for i in input.flatten():\n s = str(i).encode('utf-8')\n su = s.decode('utf-8')\n ss.append(su)\n\n output = np.array(ss).astype(object).reshape([3, 4])\n else:\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n else:\n input = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.dtype(object)).reshape([3, 4])\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n node = onnx.helper.make_node(\n 'Cast',\n inputs=['input'],\n outputs=['output'],\n to=getattr(TensorProto, to_type),\n )\n if input_type_proto and output_type_proto:\n expect(node, inputs=[input], outputs=[output],\n name='test_cast_' + from_type + '_to_' + to_type,\n input_type_protos=[input_type_proto],\n output_type_protos=[output_type_proto])\n else:\n expect(node, inputs=[input], outputs=[output],\n name='test_cast_' + from_type + '_to_' + to_type)" + } + ] + }, + { + "name": "CastLike", + "module": "ai.onnx", + "version": 15, + "support_level": "common", + "description": "The operator casts the elements of a given input tensor (the first input) to\nthe same data type as the elements of the second input tensor.\nSee documentation of the Cast operator for further details.\n", + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "Input tensor to be cast." + }, + { + "name": "target_type", + "type": "T2", + "description": "The (first) input tensor will be cast to produce a tensor of the same type as this (second input) tensor." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor produced by casting the first input tensor to have the same type as the second input tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types. Casting from complex is not supported.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)", + "tensor(string)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain output types. Casting to complex is not supported.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)", + "tensor(string)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "castlike", + "code": "shape = (3, 4)\ntest_cases = [\n ('FLOAT', 'FLOAT16'),\n ('FLOAT', 'DOUBLE'),\n ('FLOAT16', 'FLOAT'),\n ('FLOAT16', 'DOUBLE'),\n ('DOUBLE', 'FLOAT'),\n ('DOUBLE', 'FLOAT16'),\n ('FLOAT', 'STRING'),\n ('STRING', 'FLOAT'),\n ('FLOAT', 'BFLOAT16'),\n ('BFLOAT16', 'FLOAT'),\n]\n\nfor from_type, to_type in test_cases:\n input_type_proto = None\n output_type_proto = None\n if 'BFLOAT16' == from_type or 'BFLOAT16' == to_type:\n np_fp32 = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.float32)\n little_endisan = sys.byteorder == 'little'\n np_uint16_view = np_fp32.view(dtype=np.uint16)\n np_bfp16 = np_uint16_view[1::2] if little_endisan else np_uint16_view[0::2]\n if 'BFLOAT16' == to_type:\n assert from_type == 'FLOAT'\n input = np_fp32.reshape([3, 4])\n output = np_bfp16.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), output.shape)\n else:\n assert to_type == 'FLOAT'\n input = np_bfp16.reshape([3, 4])\n #convert bfloat to FLOAT\n np_fp32_zeros = np.zeros((len(np_bfp16) * 2,), dtype=np.uint16)\n if little_endisan:\n np_fp32_zeros[1::2] = np_bfp16\n else:\n np_fp32_zeros[0::2] = np_bfp16\n np_fp32_from_bfloat = np_fp32_zeros.view(dtype=np.float32)\n output = np_fp32_from_bfloat.reshape([3, 4])\n input_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.BFLOAT16), input.shape)\n output_type_proto = onnx.helper.make_tensor_type_proto(int(TensorProto.FLOAT), output.shape)\n elif 'STRING' != from_type:\n input = np.random.random_sample(shape).astype(\n TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, from_type)])\n if ('STRING' == to_type):\n # Converting input to str, then give it np.object dtype for generating script\n ss = []\n for i in input.flatten():\n s = str(i).encode('utf-8')\n su = s.decode('utf-8')\n ss.append(su)\n\n output = np.array(ss).astype(np.object).reshape([3, 4])\n else:\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n else:\n input = np.array([u'0.47892547', u'0.48033667', u'0.49968487', u'0.81910545',\n u'0.47031248', u'0.816468', u'0.21087195', u'0.7229038',\n u'NaN', u'INF', u'+INF', u'-INF'], dtype=np.dtype(np.object)).reshape([3, 4])\n output = input.astype(TENSOR_TYPE_TO_NP_TYPE[getattr(TensorProto, to_type)])\n like = output.flatten()[0:1]\n node = onnx.helper.make_node(\n 'CastLike',\n inputs=['input', 'like'],\n outputs=['output'],\n )\n if input_type_proto and output_type_proto:\n expect(node, inputs=[input, like], outputs=[output],\n name='test_castlike_' + from_type + '_to_' + to_type,\n input_type_protos=[input_type_proto, output_type_proto],\n output_type_protos=[output_type_proto])\n else:\n expect(node, inputs=[input, like], outputs=[output],\n name='test_castlike_' + from_type + '_to_' + to_type)" + } + ] + }, + { + "name": "CastMap", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Converts a map to a tensor.
The map key must be an int64 and the values will be ordered\n in ascending order based on this key.
The operator supports dense packing or sparse packing.\n If using sparse packing, the key cannot exceed the max_map-1 value.\n", + "attributes": [ + { + "name": "cast_to", + "type": "string", + "required": false, + "default": "TO_FLOAT", + "description": "A string indicating the desired element type of the output tensor, one of 'TO_FLOAT', 'TO_STRING', 'TO_INT64'." + }, + { + "name": "map_form", + "type": "string", + "required": false, + "default": "DENSE", + "description": "Indicates whether to only output as many values as are in the input (dense), or position the input based on using the key of the map as the index of the output (sparse).
One of 'DENSE', 'SPARSE'." + }, + { + "name": "max_map", + "type": "int64", + "required": false, + "default": 1, + "description": "If the value of map_form is 'SPARSE,' this attribute indicates the total length of the output tensor." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "The input map that is to be cast to a tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "A tensor representing the same data as the input map, ordered by their keys" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input must be an integer map to either string or float.", + "type_param_str": "T1", + "allowed_type_strs": [ + "map(int64, string)", + "map(int64, float)" + ] + }, + { + "description": "The output is a 1-D tensor of string, float, or integer.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(string)", + "tensor(float)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "CategoryMapper", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Converts strings to integers and vice versa.
\n Two sequences of equal length are used to map between integers and strings,\n with strings and integers at the same index detailing the mapping.
\n Each operator converts either integers to strings or strings to integers, depending\n on which default value attribute is provided. Only one default value attribute\n should be defined.
\n If the string default value is set, it will convert integers to strings.\n If the int default value is set, it will convert strings to integers.\n", + "attributes": [ + { + "name": "cats_int64s", + "type": "int64[]", + "required": false, + "description": "The integers of the map. This sequence must be the same length as the 'cats_strings' sequence." + }, + { + "name": "cats_strings", + "type": "string[]", + "required": false, + "description": "The strings of the map. This sequence must be the same length as the 'cats_int64s' sequence" + }, + { + "name": "default_int64", + "type": "int64", + "required": false, + "default": -1, + "description": "An integer to use when an input string value is not found in the map.
One and only one of the 'default_*' attributes must be defined." + }, + { + "name": "default_string", + "type": "string", + "required": false, + "default": "_Unused", + "description": "A string to use when an input integer value is not found in the map.
One and only one of the 'default_*' attributes must be defined." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Input data" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "Output data. If strings are input, the output values are integers, and vice versa." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input must be a tensor of strings or integers, either [N,C] or [C].", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)" + ] + }, + { + "description": "The output is a tensor of strings or integers. Its shape will be the same as the input shape.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "Ceil", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Ceil takes one input data (Tensor) and produces one output data\n(Tensor) where the ceil is, y = ceil(x), is applied to\nthe tensor elementwise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "ceil", + "code": "node = onnx.helper.make_node(\n 'Ceil',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1.5, 1.2]).astype(np.float32)\ny = np.ceil(x) # expected output [-1., 2.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_ceil_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.ceil(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_ceil')" + } + ] + }, + { + "name": "Ceil", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Ceil takes one input data (Tensor) and produces one output data\n(Tensor) where the ceil is, y = ceil(x), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "ceil", + "code": "node = onnx.helper.make_node(\n 'Ceil',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1.5, 1.2]).astype(np.float32)\ny = np.ceil(x) # expected output [-1., 2.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_ceil_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.ceil(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_ceil')" + } + ] + }, + { + "name": "Ceil", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Ceil takes one input data (Tensor) and produces one output data\n(Tensor) where the ceil is, y = ceil(x), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "ceil", + "code": "node = onnx.helper.make_node(\n 'Ceil',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1.5, 1.2]).astype(np.float32)\ny = np.ceil(x) # expected output [-1., 2.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_ceil_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.ceil(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_ceil')" + } + ] + }, + { + "name": "Celu", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Continuously Differentiable Exponential Linear Units:\nPerform the linear unit element-wise on the input tensor X\nusing formula:\n\n```\nmax(0,x) + min(0,alpha*(exp(x/alpha)-1))\n```\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "The Alpha value in Celu formula which control the shape of the unit. The default value is 1.0." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float32 tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)" + ] + } + ], + "examples": [ + { + "summary": "celu", + "code": "alpha = 2.0\nnode = onnx.helper.make_node(\n 'Celu',\n inputs=['X'],\n outputs=['Y'],\n alpha=alpha,\n)\n\ninput_data = np.array([[[[0.8439683], [0.5665144], [0.05836735]],\n [[0.02916367], [0.12964272], [0.5060197]],\n [[0.79538304], [0.9411346], [0.9546573]]],\n [[[0.17730942], [0.46192095], [0.26480448]],\n [[0.6746842], [0.01665257], [0.62473077]],\n [[0.9240844], [0.9722341], [0.11965699]]],\n [[[0.41356155], [0.9129373], [0.59330076]],\n [[0.81929934], [0.7862604], [0.11799799]],\n [[0.69248444], [0.54119414], [0.07513223]]]], dtype=np.float32)\n\n# Calculate expected output data\npositive_input = np.maximum(0, input_data)\nnegative_input = np.minimum(0, alpha * (np.exp(input_data / alpha) - 1))\nexpected_output = positive_input + negative_input\n\nexpect(node, inputs=[input_data], outputs=[expected_output],\n name='test_celu')" + } + ] + }, + { + "name": "Clip", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Clip operator limits the given input within an interval. The interval is\nspecified with arguments 'min' and 'max'. They default to\nnumeric_limits::lowest() and numeric_limits::max() respectively.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + }, + { + "name": "max", + "type": "float32", + "required": false, + "description": "Maximum value, above which element is replaced by max" + }, + { + "name": "min", + "type": "float32", + "required": false, + "description": "Minimum value, under which element is replaced by min" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor whose elements to be clipped" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor with clipped input elements" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "clip", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nx = np.array([-2, 0, 2]).astype(np.float32)\nmin_val = np.float32(-1)\nmax_val = np.float32(1)\ny = np.clip(x, min_val, max_val) # expected output [-1., 0., 1.]\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, max_val)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip')\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nmin_val = np.float32(-5)\nmax_val = np.float32(5)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_inbounds')\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_outbounds')\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_splitbounds')" + }, + { + "summary": "clip_default", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, np.inf)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, -np.inf, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_inbounds')" + }, + { + "summary": "clip_default_int8", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, min_val, np.iinfo(np.int8).max)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_int8_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, np.iinfo(np.int8).min, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_int8_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.int8)\ny = np.array([-1, 0, 1]).astype(np.int8)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_int8_inbounds')" + } + ], + "category": "Activation" + }, + { + "name": "Clip", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Clip operator limits the given input within an interval. The interval is\nspecified with arguments 'min' and 'max'. They default to\nnumeric_limits::lowest() and numeric_limits::max() respectively.\n", + "attributes": [ + { + "name": "max", + "type": "float32", + "required": false, + "default": 3.4028234663852886e+38, + "description": "Maximum value, above which element is replaced by max" + }, + { + "name": "min", + "type": "float32", + "required": false, + "default": -3.4028234663852886e+38, + "description": "Minimum value, under which element is replaced by min" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor whose elements to be clipped" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor with clipped input elements" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "clip", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nx = np.array([-2, 0, 2]).astype(np.float32)\nmin_val = np.float32(-1)\nmax_val = np.float32(1)\ny = np.clip(x, min_val, max_val) # expected output [-1., 0., 1.]\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, max_val)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip')\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nmin_val = np.float32(-5)\nmax_val = np.float32(5)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_inbounds')\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_outbounds')\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_splitbounds')" + }, + { + "summary": "clip_default", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, np.inf)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, -np.inf, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_inbounds')" + }, + { + "summary": "clip_default_int8", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, min_val, np.iinfo(np.int8).max)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_int8_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, np.iinfo(np.int8).min, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_int8_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.int8)\ny = np.array([-1, 0, 1]).astype(np.int8)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_int8_inbounds')" + } + ], + "category": "Activation" + }, + { + "name": "Clip", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Clip operator limits the given input within an interval. The interval is\nspecified by the inputs 'min' and 'max'. They default to\nnumeric_limits::lowest() and numeric_limits::max(), respectively.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor whose elements to be clipped" + }, + { + "name": "min", + "type": "T", + "option": "optional", + "description": "Minimum value, under which element is replaced by min. It must be a scalar(tensor of empty shape)." + }, + { + "name": "max", + "type": "T", + "option": "optional", + "description": "Maximum value, above which element is replaced by max. It must be a scalar(tensor of empty shape)." + } + ], + "min_input": 1, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor with clipped input elements" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "clip", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nx = np.array([-2, 0, 2]).astype(np.float32)\nmin_val = np.float32(-1)\nmax_val = np.float32(1)\ny = np.clip(x, min_val, max_val) # expected output [-1., 0., 1.]\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, max_val)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip')\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nmin_val = np.float32(-5)\nmax_val = np.float32(5)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_inbounds')\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_outbounds')\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_splitbounds')" + }, + { + "summary": "clip_default", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, np.inf)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, -np.inf, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_inbounds')" + }, + { + "summary": "clip_default_int8", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, min_val, np.iinfo(np.int8).max)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_int8_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, np.iinfo(np.int8).min, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_int8_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.int8)\ny = np.array([-1, 0, 1]).astype(np.int8)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_int8_inbounds')" + } + ], + "category": "Activation" + }, + { + "name": "Clip", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Clip operator limits the given input within an interval. The interval is\nspecified by the inputs 'min' and 'max'. They default to\nnumeric_limits::lowest() and numeric_limits::max(), respectively.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor whose elements to be clipped" + }, + { + "name": "min", + "type": "T", + "option": "optional", + "description": "Minimum value, under which element is replaced by min. It must be a scalar(tensor of empty shape)." + }, + { + "name": "max", + "type": "T", + "option": "optional", + "description": "Maximum value, above which element is replaced by max. It must be a scalar(tensor of empty shape)." + } + ], + "min_input": 1, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor with clipped input elements" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "clip", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nx = np.array([-2, 0, 2]).astype(np.float32)\nmin_val = np.float32(-1)\nmax_val = np.float32(1)\ny = np.clip(x, min_val, max_val) # expected output [-1., 0., 1.]\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, max_val)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip')\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nmin_val = np.float32(-5)\nmax_val = np.float32(5)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_inbounds')\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_outbounds')\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_splitbounds')" + }, + { + "summary": "clip_default", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, np.inf)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, -np.inf, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_inbounds')" + }, + { + "summary": "clip_default_int8", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, min_val, np.iinfo(np.int8).max)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_int8_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, np.iinfo(np.int8).min, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_int8_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.int8)\ny = np.array([-1, 0, 1]).astype(np.int8)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_int8_inbounds')" + } + ], + "category": "Activation" + }, + { + "name": "Clip", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Clip operator limits the given input within an interval. The interval is\nspecified by the inputs 'min' and 'max'. They default to\nnumeric_limits::lowest() and numeric_limits::max(), respectively.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor whose elements to be clipped" + }, + { + "name": "min", + "type": "T", + "option": "optional", + "description": "Minimum value, under which element is replaced by min. It must be a scalar(tensor of empty shape)." + }, + { + "name": "max", + "type": "T", + "option": "optional", + "description": "Maximum value, above which element is replaced by max. It must be a scalar(tensor of empty shape)." + } + ], + "min_input": 1, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor with clipped input elements" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "clip", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nx = np.array([-2, 0, 2]).astype(np.float32)\nmin_val = np.float32(-1)\nmax_val = np.float32(1)\ny = np.clip(x, min_val, max_val) # expected output [-1., 0., 1.]\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, max_val)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip')\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min', 'max'],\n outputs=['y'],\n)\n\nmin_val = np.float32(-5)\nmax_val = np.float32(5)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_inbounds')\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_outbounds')\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(node, inputs=[x, min_val, max_val], outputs=[y],\n name='test_clip_splitbounds')" + }, + { + "summary": "clip_default", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, min_val, np.inf)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.float32(0)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, -np.inf, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-1, 0, 1]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_inbounds')" + }, + { + "summary": "clip_default_int8", + "code": "node = onnx.helper.make_node(\n 'Clip',\n inputs=['x', 'min'],\n outputs=['y'],\n)\nmin_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, min_val, np.iinfo(np.int8).max)\nexpect(node, inputs=[x, min_val], outputs=[y],\n name='test_clip_default_int8_min')\n\nno_min = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, 'max'],\n outputs=['y'],\n)\nmax_val = np.int8(0)\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.clip(x, np.iinfo(np.int8).min, max_val)\nexpect(node, inputs=[x, max_val], outputs=[y],\n name='test_clip_default_int8_max')\n\nno_max = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Clip',\n inputs=['x', no_min, no_max],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.int8)\ny = np.array([-1, 0, 1]).astype(np.int8)\nexpect(node, inputs=[x], outputs=[y],\n name='test_clip_default_int8_inbounds')" + } + ], + "category": "Activation" + }, + { + "name": "Compress", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Selects slices from an input tensor along a given axis where condition evaluates to True for each axis index.\n In case axis is not provided, input is flattened before elements are selected.\n Compress behaves like numpy.compress: https://docs.scipy.org/doc/numpy/reference/generated/numpy.compress.html\n ", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "(Optional) Axis along which to take slices. If not specified, input is flattened before elements being selected." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "condition", + "type": "T1", + "description": "Rank 1 tensor of booleans to indicate which slices or data elements to be selected. Its length can be less than the input length alone the axis or the flattened input size if axis is not specified. In such cases data slices or elements exceeding the condition length are discarded." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r if axis is specified. Otherwise output is a Tensor of rank 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain to boolean tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "compress_0", + "code": "node = onnx.helper.make_node(\n 'Compress',\n inputs=['input', 'condition'],\n outputs=['output'],\n axis=0,\n)\ninput = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)\ncondition = np.array([0, 1, 1])\noutput = np.compress(condition, input, axis=0)\n#print(output)\n#[[ 3. 4.]\n# [ 5. 6.]]\n\nexpect(node, inputs=[input, condition.astype(bool)], outputs=[output],\n name='test_compress_0')" + }, + { + "summary": "compress_1", + "code": "node = onnx.helper.make_node(\n 'Compress',\n inputs=['input', 'condition'],\n outputs=['output'],\n axis=1,\n)\ninput = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)\ncondition = np.array([0, 1])\noutput = np.compress(condition, input, axis=1)\n#print(output)\n#[[ 2.]\n# [ 4.]\n# [ 6.]]\n\nexpect(node, inputs=[input, condition.astype(bool)], outputs=[output],\n name='test_compress_1')" + }, + { + "summary": "compress_default_axis", + "code": "node = onnx.helper.make_node(\n 'Compress',\n inputs=['input', 'condition'],\n outputs=['output'],\n)\ninput = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)\ncondition = np.array([0, 1, 0, 0, 1])\noutput = np.compress(condition, input)\n#print(output)\n#[ 2., 5.]\n\nexpect(node, inputs=[input, condition.astype(bool)], outputs=[output],\n name='test_compress_default_axis')" + }, + { + "summary": "compress_negative_axis", + "code": "node = onnx.helper.make_node(\n 'Compress',\n inputs=['input', 'condition'],\n outputs=['output'],\n axis=-1,\n)\ninput = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)\ncondition = np.array([0, 1])\noutput = np.compress(condition, input, axis=-1)\n# print(output)\n#[[ 2.]\n# [ 4.]\n# [ 6.]]\nexpect(node, inputs=[input, condition.astype(bool)], outputs=[output],\n name='test_compress_negative_axis')" + } + ] + }, + { + "name": "Compress", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Selects slices from an input tensor along a given axis where condition evaluates to True for each axis index.\n In case axis is not provided, input is flattened before elements are selected.\n Compress behaves like numpy.compress: https://docs.scipy.org/doc/numpy/reference/generated/numpy.compress.html\n ", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "(Optional) Axis along which to take slices. If not specified, input is flattened before elements being selected. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "condition", + "type": "T1", + "description": "Rank 1 tensor of booleans to indicate which slices or data elements to be selected. Its length can be less than the input length along the axis or the flattened input size if axis is not specified. In such cases data slices or elements exceeding the condition length are discarded." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r if axis is specified. Otherwise output is a Tensor of rank 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain to boolean tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "compress_0", + "code": "node = onnx.helper.make_node(\n 'Compress',\n inputs=['input', 'condition'],\n outputs=['output'],\n axis=0,\n)\ninput = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)\ncondition = np.array([0, 1, 1])\noutput = np.compress(condition, input, axis=0)\n#print(output)\n#[[ 3. 4.]\n# [ 5. 6.]]\n\nexpect(node, inputs=[input, condition.astype(bool)], outputs=[output],\n name='test_compress_0')" + }, + { + "summary": "compress_1", + "code": "node = onnx.helper.make_node(\n 'Compress',\n inputs=['input', 'condition'],\n outputs=['output'],\n axis=1,\n)\ninput = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)\ncondition = np.array([0, 1])\noutput = np.compress(condition, input, axis=1)\n#print(output)\n#[[ 2.]\n# [ 4.]\n# [ 6.]]\n\nexpect(node, inputs=[input, condition.astype(bool)], outputs=[output],\n name='test_compress_1')" + }, + { + "summary": "compress_default_axis", + "code": "node = onnx.helper.make_node(\n 'Compress',\n inputs=['input', 'condition'],\n outputs=['output'],\n)\ninput = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)\ncondition = np.array([0, 1, 0, 0, 1])\noutput = np.compress(condition, input)\n#print(output)\n#[ 2., 5.]\n\nexpect(node, inputs=[input, condition.astype(bool)], outputs=[output],\n name='test_compress_default_axis')" + }, + { + "summary": "compress_negative_axis", + "code": "node = onnx.helper.make_node(\n 'Compress',\n inputs=['input', 'condition'],\n outputs=['output'],\n axis=-1,\n)\ninput = np.array([[1, 2], [3, 4], [5, 6]]).astype(np.float32)\ncondition = np.array([0, 1])\noutput = np.compress(condition, input, axis=-1)\n# print(output)\n#[[ 2.]\n# [ 4.]\n# [ 6.]]\nexpect(node, inputs=[input, condition.astype(bool)], outputs=[output],\n name='test_compress_negative_axis')" + } + ] + }, + { + "name": "Concat", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Concatenate a list of tensors into a single tensor", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to concat on. Default value is 1." + } + ], + "inputs": [ + { + "name": "inputs", + "type": "T", + "list": true, + "description": "List of tensors for concatenation" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "concat_result", + "type": "T", + "description": "Concatenated tensor" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "concat", + "code": "test_cases: Dict[Text, Sequence[Any]] = {\n '1d': ([1, 2],\n [3, 4]),\n '2d': ([[1, 2], [3, 4]],\n [[5, 6], [7, 8]]),\n '3d': ([[[1, 2], [3, 4]], [[5, 6], [7, 8]]],\n [[[9, 10], [11, 12]], [[13, 14], [15, 16]]])\n}\n\nfor test_case, values_ in test_cases.items():\n values = [np.asarray(v, dtype=np.float32) for v in values_]\n for i in range(len(values[0].shape)):\n in_args = ['value' + str(k) for k in range(len(values))]\n node = onnx.helper.make_node(\n 'Concat',\n inputs=[s for s in in_args],\n outputs=['output'],\n axis=i\n )\n output = np.concatenate(values, i)\n expect(node, inputs=[v for v in values], outputs=[output],\n name='test_concat_' + test_case + '_axis_' + str(i))\n\n for i in range(-len(values[0].shape), 0):\n in_args = ['value' + str(k) for k in range(len(values))]\n node = onnx.helper.make_node(\n 'Concat',\n inputs=[s for s in in_args],\n outputs=['output'],\n axis=i\n )\n output = np.concatenate(values, i)\n expect(node, inputs=[v for v in values], outputs=[output],\n name='test_concat_' + test_case + '_axis_negative_' + str(abs(i)))" + } + ], + "category": "Tensor" + }, + { + "name": "Concat", + "module": "ai.onnx", + "version": 4, + "support_level": "common", + "description": "Concatenate a list of tensors into a single tensor", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": true, + "description": "Which axis to concat on" + } + ], + "inputs": [ + { + "name": "inputs", + "type": "T", + "list": true, + "description": "List of tensors for concatenation" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "concat_result", + "type": "T", + "description": "Concatenated tensor" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "concat", + "code": "test_cases: Dict[Text, Sequence[Any]] = {\n '1d': ([1, 2],\n [3, 4]),\n '2d': ([[1, 2], [3, 4]],\n [[5, 6], [7, 8]]),\n '3d': ([[[1, 2], [3, 4]], [[5, 6], [7, 8]]],\n [[[9, 10], [11, 12]], [[13, 14], [15, 16]]])\n}\n\nfor test_case, values_ in test_cases.items():\n values = [np.asarray(v, dtype=np.float32) for v in values_]\n for i in range(len(values[0].shape)):\n in_args = ['value' + str(k) for k in range(len(values))]\n node = onnx.helper.make_node(\n 'Concat',\n inputs=[s for s in in_args],\n outputs=['output'],\n axis=i\n )\n output = np.concatenate(values, i)\n expect(node, inputs=[v for v in values], outputs=[output],\n name='test_concat_' + test_case + '_axis_' + str(i))\n\n for i in range(-len(values[0].shape), 0):\n in_args = ['value' + str(k) for k in range(len(values))]\n node = onnx.helper.make_node(\n 'Concat',\n inputs=[s for s in in_args],\n outputs=['output'],\n axis=i\n )\n output = np.concatenate(values, i)\n expect(node, inputs=[v for v in values], outputs=[output],\n name='test_concat_' + test_case + '_axis_negative_' + str(abs(i)))" + } + ], + "category": "Tensor" + }, + { + "name": "Concat", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Concatenate a list of tensors into a single tensor. All input tensors must have the same shape, except for the dimension size of the axis to concatenate on.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": true, + "description": "Which axis to concat on. A negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(inputs).." + } + ], + "inputs": [ + { + "name": "inputs", + "type": "T", + "list": true, + "description": "List of tensors for concatenation" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "concat_result", + "type": "T", + "description": "Concatenated tensor" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "concat", + "code": "test_cases: Dict[Text, Sequence[Any]] = {\n '1d': ([1, 2],\n [3, 4]),\n '2d': ([[1, 2], [3, 4]],\n [[5, 6], [7, 8]]),\n '3d': ([[[1, 2], [3, 4]], [[5, 6], [7, 8]]],\n [[[9, 10], [11, 12]], [[13, 14], [15, 16]]])\n}\n\nfor test_case, values_ in test_cases.items():\n values = [np.asarray(v, dtype=np.float32) for v in values_]\n for i in range(len(values[0].shape)):\n in_args = ['value' + str(k) for k in range(len(values))]\n node = onnx.helper.make_node(\n 'Concat',\n inputs=[s for s in in_args],\n outputs=['output'],\n axis=i\n )\n output = np.concatenate(values, i)\n expect(node, inputs=[v for v in values], outputs=[output],\n name='test_concat_' + test_case + '_axis_' + str(i))\n\n for i in range(-len(values[0].shape), 0):\n in_args = ['value' + str(k) for k in range(len(values))]\n node = onnx.helper.make_node(\n 'Concat',\n inputs=[s for s in in_args],\n outputs=['output'],\n axis=i\n )\n output = np.concatenate(values, i)\n expect(node, inputs=[v for v in values], outputs=[output],\n name='test_concat_' + test_case + '_axis_negative_' + str(abs(i)))" + } + ], + "category": "Tensor" + }, + { + "name": "Concat", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Concatenate a list of tensors into a single tensor. All input tensors must have the same shape, except for the dimension size of the axis to concatenate on.", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": true, + "description": "Which axis to concat on. A negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(inputs).." + } + ], + "inputs": [ + { + "name": "inputs", + "type": "T", + "list": true, + "description": "List of tensors for concatenation" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "concat_result", + "type": "T", + "description": "Concatenated tensor" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "concat", + "code": "test_cases: Dict[Text, Sequence[Any]] = {\n '1d': ([1, 2],\n [3, 4]),\n '2d': ([[1, 2], [3, 4]],\n [[5, 6], [7, 8]]),\n '3d': ([[[1, 2], [3, 4]], [[5, 6], [7, 8]]],\n [[[9, 10], [11, 12]], [[13, 14], [15, 16]]])\n}\n\nfor test_case, values_ in test_cases.items():\n values = [np.asarray(v, dtype=np.float32) for v in values_]\n for i in range(len(values[0].shape)):\n in_args = ['value' + str(k) for k in range(len(values))]\n node = onnx.helper.make_node(\n 'Concat',\n inputs=[s for s in in_args],\n outputs=['output'],\n axis=i\n )\n output = np.concatenate(values, i)\n expect(node, inputs=[v for v in values], outputs=[output],\n name='test_concat_' + test_case + '_axis_' + str(i))\n\n for i in range(-len(values[0].shape), 0):\n in_args = ['value' + str(k) for k in range(len(values))]\n node = onnx.helper.make_node(\n 'Concat',\n inputs=[s for s in in_args],\n outputs=['output'],\n axis=i\n )\n output = np.concatenate(values, i)\n expect(node, inputs=[v for v in values], outputs=[output],\n name='test_concat_' + test_case + '_axis_negative_' + str(abs(i)))" + } + ], + "category": "Tensor" + }, + { + "name": "ConcatFromSequence", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Concatenate a sequence of tensors into a single tensor.\nAll input tensors must have the same shape, except for the dimension size of the axis to concatenate on.\nBy default 'new_axis' is 0, the behavior is similar to numpy.concatenate.\nWhen 'new_axis' is 1, the behavior is similar to numpy.stack.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": true, + "description": "Which axis to concat on. Accepted range in `[-r, r - 1]`, where `r` is the rank of input tensors. When `new_axis` is 1, accepted range is `[-r - 1, r]`. " + }, + { + "name": "new_axis", + "type": "int64", + "required": false, + "description": "Insert and concatenate on a new axis or not, default 0 means do not insert new axis." + } + ], + "inputs": [ + { + "name": "input_sequence", + "type": "S", + "description": "Sequence of tensors for concatenation" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "concat_result", + "type": "T", + "description": "Concatenated tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to any tensor type.", + "type_param_str": "S", + "allowed_type_strs": [ + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + }, + { + "description": "Constrain output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ] + }, + { + "name": "Constant", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "A constant tensor.", + "attributes": [ + { + "name": "value", + "type": "tensor", + "required": true, + "description": "The value for the elements of the output tensor." + } + ], + "min_input": 0, + "max_input": 0, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor containing the same value of the provided tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "constant", + "code": "values = np.random.randn(5, 5).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['values'],\n value=onnx.helper.make_tensor(\n name='const_tensor',\n data_type=onnx.TensorProto.FLOAT,\n dims=values.shape,\n vals=values.flatten().astype(float),\n ),\n)\n\nexpect(node, inputs=[], outputs=[values],\n name='test_constant')" + } + ], + "category": "Constant" + }, + { + "name": "Constant", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "A constant tensor.", + "attributes": [ + { + "name": "value", + "type": "tensor", + "required": true, + "description": "The value for the elements of the output tensor." + } + ], + "min_input": 0, + "max_input": 0, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor containing the same value of the provided tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "constant", + "code": "values = np.random.randn(5, 5).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['values'],\n value=onnx.helper.make_tensor(\n name='const_tensor',\n data_type=onnx.TensorProto.FLOAT,\n dims=values.shape,\n vals=values.flatten().astype(float),\n ),\n)\n\nexpect(node, inputs=[], outputs=[values],\n name='test_constant')" + } + ], + "category": "Constant" + }, + { + "name": "Constant", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "A constant tensor. Exactly one of the two attributes, either value or sparse_value,\nmust be specified.\n", + "attributes": [ + { + "name": "sparse_value", + "required": false, + "description": "The value for the elements of the output tensor in sparse format." + }, + { + "name": "value", + "type": "tensor", + "required": false, + "description": "The value for the elements of the output tensor." + } + ], + "min_input": 0, + "max_input": 0, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor containing the same value of the provided tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "constant", + "code": "values = np.random.randn(5, 5).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['values'],\n value=onnx.helper.make_tensor(\n name='const_tensor',\n data_type=onnx.TensorProto.FLOAT,\n dims=values.shape,\n vals=values.flatten().astype(float),\n ),\n)\n\nexpect(node, inputs=[], outputs=[values],\n name='test_constant')" + } + ], + "category": "Constant" + }, + { + "name": "Constant", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "This operator produces a constant tensor. Exactly one of the provided attributes, either value, sparse_value,\nor value_* must be specified.\n", + "attributes": [ + { + "name": "sparse_value", + "required": false, + "description": "The value for the elements of the output tensor in sparse format." + }, + { + "name": "value", + "type": "tensor", + "required": false, + "description": "The value for the elements of the output tensor." + }, + { + "name": "value_float", + "type": "float32", + "required": false, + "description": "The value for the sole element for the scalar, float32, output tensor." + }, + { + "name": "value_floats", + "type": "float32[]", + "required": false, + "description": "The values for the elements for the 1D, float32, output tensor." + }, + { + "name": "value_int", + "type": "int64", + "required": false, + "description": "The value for the sole element for the scalar, int64, output tensor." + }, + { + "name": "value_ints", + "type": "int64[]", + "required": false, + "description": "The values for the elements for the 1D, int64, output tensor." + }, + { + "name": "value_string", + "type": "string", + "required": false, + "description": "The value for the sole element for the scalar, UTF-8 string, output tensor." + }, + { + "name": "value_strings", + "type": "string[]", + "required": false, + "description": "The values for the elements for the 1D, UTF-8 string, output tensor." + } + ], + "min_input": 0, + "max_input": 0, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor containing the same value of the provided tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "constant", + "code": "values = np.random.randn(5, 5).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['values'],\n value=onnx.helper.make_tensor(\n name='const_tensor',\n data_type=onnx.TensorProto.FLOAT,\n dims=values.shape,\n vals=values.flatten().astype(float),\n ),\n)\n\nexpect(node, inputs=[], outputs=[values],\n name='test_constant')" + } + ], + "category": "Constant" + }, + { + "name": "Constant", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "This operator produces a constant tensor. Exactly one of the provided attributes, either value, sparse_value,\nor value_* must be specified.\n", + "attributes": [ + { + "name": "sparse_value", + "required": false, + "description": "The value for the elements of the output tensor in sparse format." + }, + { + "name": "value", + "type": "tensor", + "required": false, + "description": "The value for the elements of the output tensor." + }, + { + "name": "value_float", + "type": "float32", + "required": false, + "description": "The value for the sole element for the scalar, float32, output tensor." + }, + { + "name": "value_floats", + "type": "float32[]", + "required": false, + "description": "The values for the elements for the 1D, float32, output tensor." + }, + { + "name": "value_int", + "type": "int64", + "required": false, + "description": "The value for the sole element for the scalar, int64, output tensor." + }, + { + "name": "value_ints", + "type": "int64[]", + "required": false, + "description": "The values for the elements for the 1D, int64, output tensor." + }, + { + "name": "value_string", + "type": "string", + "required": false, + "description": "The value for the sole element for the scalar, UTF-8 string, output tensor." + }, + { + "name": "value_strings", + "type": "string[]", + "required": false, + "description": "The values for the elements for the 1D, UTF-8 string, output tensor." + } + ], + "min_input": 0, + "max_input": 0, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor containing the same value of the provided tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "constant", + "code": "values = np.random.randn(5, 5).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['values'],\n value=onnx.helper.make_tensor(\n name='const_tensor',\n data_type=onnx.TensorProto.FLOAT,\n dims=values.shape,\n vals=values.flatten().astype(float),\n ),\n)\n\nexpect(node, inputs=[], outputs=[values],\n name='test_constant')" + } + ], + "category": "Constant" + }, + { + "name": "ConstantOfShape", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Generate a tensor with given value and shape.\n", + "attributes": [ + { + "name": "value", + "type": "tensor", + "required": false, + "description": "(Optional) The value of the output elements.Should be a one-element tensor. If not specified, it defaults to a tensor of value 0 and datatype float32" + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "1D tensor. The shape of the expected output tensor. If empty tensor is given, the output would be a scalar. All values must be >= 0." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor of shape specified by 'input'.If attribute 'value' is specified, the value and datatype of the output tensor is taken from 'value'.If attribute 'value' is not specified, the value in the output defaults to 0, and the datatype defaults to float32." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int64)" + ] + }, + { + "description": "Constrain output types to be numerics.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "float_ones", + "code": "x = np.array([4, 3, 2]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\"value\", onnx.TensorProto.FLOAT,\n [1], [1])\nnode = onnx.helper.make_node(\n 'ConstantOfShape',\n inputs=['x'],\n outputs=['y'],\n value=tensor_value,\n)\n\ny = np.ones(x, dtype=np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_constantofshape_float_ones')" + }, + { + "summary": "int32_shape_zero", + "code": "x = np.array([0, ]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\"value\", onnx.TensorProto.INT32,\n [1], [0])\nnode = onnx.helper.make_node(\n 'ConstantOfShape',\n inputs=['x'],\n outputs=['y'],\n value=tensor_value,\n)\ny = np.zeros(x, dtype=np.int32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_constantofshape_int_shape_zero')" + }, + { + "summary": "int32_zeros", + "code": "x = np.array([10, 6]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\"value\", onnx.TensorProto.INT32,\n [1], [0])\nnode = onnx.helper.make_node(\n 'ConstantOfShape',\n inputs=['x'],\n outputs=['y'],\n value=tensor_value,\n)\ny = np.zeros(x, dtype=np.int32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_constantofshape_int_zeros')" + } + ] + }, + { + "name": "Conv", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "The convolution operator consumes an input tensor and a filter, and\ncomputes the output.", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "dilations", + "type": "int64[]", + "required": false, + "description": "dilation value along each spatial axis of the filter." + }, + { + "name": "group", + "type": "int64", + "required": false, + "default": 1, + "description": "number of groups input channels and output channels are divided into." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": false, + "description": "The shape of the convolution kernel. If not present, should be inferred from input W." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from previous layer; has size (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and width. Note that this is for the 2D image. Otherwise the size is (N x C x D1 x D2 ... x Dn). Optionally, if dimension denotation is in effect, the operation expects input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor that will be used in the convolutions; has size (M x C/group x kH x kW), where C is the number of channels, and kH and kW are the height and width of the kernel, and M is the number of feature maps. For more than 2 dimensions, the kernel shape will be (M x C/group x k1 x k2 x ... x kn), where (k1 x k2 x ... kn) is the dimension of the kernel. Optionally, if dimension denotation is in effect, the operation expects the weight tensor to arrive with the dimension denotation of [FILTER_OUT_CHANNEL, FILTER_IN_CHANNEL, FILTER_SPATIAL, FILTER_SPATIAL ...]. X.shape[1] == (W.shape[1] * group) == C (assuming zero based indices for the shape array). Or in other words FILTER_IN_CHANNEL should be equal to DATA_CHANNEL. " + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "Optional 1D bias to be added to the convolution, has size of M." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor that contains the result of the convolution. The output dimensions are functions of the kernel size, stride size, and pad lengths." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "conv", + "code": "\nx = np.array([[[[0., 1., 2., 3., 4.], # (1, 1, 5, 5) input tensor\n [5., 6., 7., 8., 9.],\n [10., 11., 12., 13., 14.],\n [15., 16., 17., 18., 19.],\n [20., 21., 22., 23., 24.]]]]).astype(np.float32)\nW = np.array([[[[1., 1., 1.], # (1, 1, 3, 3) tensor for convolution weights\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\n# Convolution with padding\nnode_with_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n # Default values for other attributes: strides=[1, 1], dilations=[1, 1], groups=1\n pads=[1, 1, 1, 1],\n)\ny_with_padding = np.array([[[[12., 21., 27., 33., 24.], # (1, 1, 5, 5) output tensor\n [33., 54., 63., 72., 51.],\n [63., 99., 108., 117., 81.],\n [93., 144., 153., 162., 111.],\n [72., 111., 117., 123., 84.]]]]).astype(np.float32)\nexpect(node_with_padding, inputs=[x, W], outputs=[y_with_padding],\n name='test_basic_conv_with_padding')\n\n# Convolution without padding\nnode_without_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n # Default values for other attributes: strides=[1, 1], dilations=[1, 1], groups=1\n pads=[0, 0, 0, 0],\n)\ny_without_padding = np.array([[[[54., 63., 72.], # (1, 1, 3, 3) output tensor\n [99., 108., 117.],\n [144., 153., 162.]]]]).astype(np.float32)\nexpect(node_without_padding, inputs=[x, W], outputs=[y_without_padding],\n name='test_basic_conv_without_padding')" + }, + { + "summary": "conv_with_autopad_same", + "code": "\nx = np.array([[[[0., 1., 2., 3., 4.], # (1, 1, 5, 5) input tensor\n [5., 6., 7., 8., 9.],\n [10., 11., 12., 13., 14.],\n [15., 16., 17., 18., 19.],\n [20., 21., 22., 23., 24.]]]]).astype(np.float32)\nW = np.array([[[[1., 1., 1.], # (1, 1, 3, 3) tensor for convolution weights\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\n# Convolution with auto_pad='SAME_LOWER' and strides=2\nnode = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n auto_pad='SAME_LOWER',\n kernel_shape=[3, 3],\n strides=[2, 2],\n)\ny = np.array([[[[12., 27., 24.],\n [63., 108., 81.],\n [72., 117., 84.]]]]).astype(np.float32)\nexpect(node, inputs=[x, W], outputs=[y],\n name='test_conv_with_autopad_same')" + }, + { + "summary": "conv_with_strides", + "code": "\nx = np.array([[[[0., 1., 2., 3., 4.], # (1, 1, 7, 5) input tensor\n [5., 6., 7., 8., 9.],\n [10., 11., 12., 13., 14.],\n [15., 16., 17., 18., 19.],\n [20., 21., 22., 23., 24.],\n [25., 26., 27., 28., 29.],\n [30., 31., 32., 33., 34.]]]]).astype(np.float32)\nW = np.array([[[[1., 1., 1.], # (1, 1, 3, 3) tensor for convolution weights\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\n# Convolution with strides=2 and padding\nnode_with_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[1, 1, 1, 1],\n strides=[2, 2], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_padding = np.array([[[[12., 27., 24.], # (1, 1, 4, 3) output tensor\n [63., 108., 81.],\n [123., 198., 141.],\n [112., 177., 124.]]]]).astype(np.float32)\nexpect(node_with_padding, inputs=[x, W], outputs=[y_with_padding],\n name='test_conv_with_strides_padding')\n\n# Convolution with strides=2 and no padding\nnode_without_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[0, 0, 0, 0],\n strides=[2, 2], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_without_padding = np.array([[[[54., 72.], # (1, 1, 3, 2) output tensor\n [144., 162.],\n [234., 252.]]]]).astype(np.float32)\nexpect(node_without_padding, inputs=[x, W], outputs=[y_without_padding],\n name='test_conv_with_strides_no_padding')\n\n# Convolution with strides=2 and padding only along one dimension (the H dimension in NxCxHxW tensor)\nnode_with_asymmetric_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[1, 0, 1, 0],\n strides=[2, 2], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_asymmetric_padding = np.array([[[[21., 33.], # (1, 1, 4, 2) output tensor\n [99., 117.],\n [189., 207.],\n [171., 183.]]]]).astype(np.float32)\nexpect(node_with_asymmetric_padding, inputs=[x, W], outputs=[y_with_asymmetric_padding],\n name='test_conv_with_strides_and_asymmetric_padding')" + } + ], + "category": "Layer" + }, + { + "name": "Conv", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "The convolution operator consumes an input tensor and a filter, and\ncomputes the output.", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = ceil(input_shape[i] / strides[i])` for each axis `i`. The padding is split between the two sides equally or almost equally (depending on whether it is even or odd). In case the padding is an odd number, the extra padding is added at the end for SAME_UPPER and at the beginning for SAME_LOWER." + }, + { + "name": "dilations", + "type": "int64[]", + "required": false, + "description": "dilation value along each spatial axis of the filter. If not present, the dilation defaults is 1 along each spatial axis." + }, + { + "name": "group", + "type": "int64", + "required": false, + "default": 1, + "description": "number of groups input channels and output channels are divided into." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": false, + "description": "The shape of the convolution kernel. If not present, should be inferred from input W." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis. If not present, the stride defaults is 1 along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from previous layer; has size (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and width. Note that this is for the 2D image. Otherwise the size is (N x C x D1 x D2 ... x Dn). Optionally, if dimension denotation is in effect, the operation expects input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor that will be used in the convolutions; has size (M x C/group x kH x kW), where C is the number of channels, and kH and kW are the height and width of the kernel, and M is the number of feature maps. For more than 2 dimensions, the kernel shape will be (M x C/group x k1 x k2 x ... x kn), where (k1 x k2 x ... kn) is the dimension of the kernel. Optionally, if dimension denotation is in effect, the operation expects the weight tensor to arrive with the dimension denotation of [FILTER_OUT_CHANNEL, FILTER_IN_CHANNEL, FILTER_SPATIAL, FILTER_SPATIAL ...]. Assuming zero based indices for the shape array, X.shape[1] == (W.shape[1] * group) == C and W.shape[0] mod G == 0. Or in other words FILTER_IN_CHANNEL multiplied by the number of groups should be equal to DATA_CHANNEL and the number of feature maps M should be a multiple of the number of groups G." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "Optional 1D bias to be added to the convolution, has size of M." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor that contains the result of the convolution. The output dimensions are functions of the kernel size, stride size, and pad lengths." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "conv", + "code": "\nx = np.array([[[[0., 1., 2., 3., 4.], # (1, 1, 5, 5) input tensor\n [5., 6., 7., 8., 9.],\n [10., 11., 12., 13., 14.],\n [15., 16., 17., 18., 19.],\n [20., 21., 22., 23., 24.]]]]).astype(np.float32)\nW = np.array([[[[1., 1., 1.], # (1, 1, 3, 3) tensor for convolution weights\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\n# Convolution with padding\nnode_with_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n # Default values for other attributes: strides=[1, 1], dilations=[1, 1], groups=1\n pads=[1, 1, 1, 1],\n)\ny_with_padding = np.array([[[[12., 21., 27., 33., 24.], # (1, 1, 5, 5) output tensor\n [33., 54., 63., 72., 51.],\n [63., 99., 108., 117., 81.],\n [93., 144., 153., 162., 111.],\n [72., 111., 117., 123., 84.]]]]).astype(np.float32)\nexpect(node_with_padding, inputs=[x, W], outputs=[y_with_padding],\n name='test_basic_conv_with_padding')\n\n# Convolution without padding\nnode_without_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n # Default values for other attributes: strides=[1, 1], dilations=[1, 1], groups=1\n pads=[0, 0, 0, 0],\n)\ny_without_padding = np.array([[[[54., 63., 72.], # (1, 1, 3, 3) output tensor\n [99., 108., 117.],\n [144., 153., 162.]]]]).astype(np.float32)\nexpect(node_without_padding, inputs=[x, W], outputs=[y_without_padding],\n name='test_basic_conv_without_padding')" + }, + { + "summary": "conv_with_autopad_same", + "code": "\nx = np.array([[[[0., 1., 2., 3., 4.], # (1, 1, 5, 5) input tensor\n [5., 6., 7., 8., 9.],\n [10., 11., 12., 13., 14.],\n [15., 16., 17., 18., 19.],\n [20., 21., 22., 23., 24.]]]]).astype(np.float32)\nW = np.array([[[[1., 1., 1.], # (1, 1, 3, 3) tensor for convolution weights\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\n# Convolution with auto_pad='SAME_LOWER' and strides=2\nnode = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n auto_pad='SAME_LOWER',\n kernel_shape=[3, 3],\n strides=[2, 2],\n)\ny = np.array([[[[12., 27., 24.],\n [63., 108., 81.],\n [72., 117., 84.]]]]).astype(np.float32)\nexpect(node, inputs=[x, W], outputs=[y],\n name='test_conv_with_autopad_same')" + }, + { + "summary": "conv_with_strides", + "code": "\nx = np.array([[[[0., 1., 2., 3., 4.], # (1, 1, 7, 5) input tensor\n [5., 6., 7., 8., 9.],\n [10., 11., 12., 13., 14.],\n [15., 16., 17., 18., 19.],\n [20., 21., 22., 23., 24.],\n [25., 26., 27., 28., 29.],\n [30., 31., 32., 33., 34.]]]]).astype(np.float32)\nW = np.array([[[[1., 1., 1.], # (1, 1, 3, 3) tensor for convolution weights\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\n# Convolution with strides=2 and padding\nnode_with_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[1, 1, 1, 1],\n strides=[2, 2], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_padding = np.array([[[[12., 27., 24.], # (1, 1, 4, 3) output tensor\n [63., 108., 81.],\n [123., 198., 141.],\n [112., 177., 124.]]]]).astype(np.float32)\nexpect(node_with_padding, inputs=[x, W], outputs=[y_with_padding],\n name='test_conv_with_strides_padding')\n\n# Convolution with strides=2 and no padding\nnode_without_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[0, 0, 0, 0],\n strides=[2, 2], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_without_padding = np.array([[[[54., 72.], # (1, 1, 3, 2) output tensor\n [144., 162.],\n [234., 252.]]]]).astype(np.float32)\nexpect(node_without_padding, inputs=[x, W], outputs=[y_without_padding],\n name='test_conv_with_strides_no_padding')\n\n# Convolution with strides=2 and padding only along one dimension (the H dimension in NxCxHxW tensor)\nnode_with_asymmetric_padding = onnx.helper.make_node(\n 'Conv',\n inputs=['x', 'W'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[1, 0, 1, 0],\n strides=[2, 2], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_asymmetric_padding = np.array([[[[21., 33.], # (1, 1, 4, 2) output tensor\n [99., 117.],\n [189., 207.],\n [171., 183.]]]]).astype(np.float32)\nexpect(node_with_asymmetric_padding, inputs=[x, W], outputs=[y_with_asymmetric_padding],\n name='test_conv_with_strides_and_asymmetric_padding')" + } + ], + "category": "Layer" + }, + { + "name": "ConvInteger", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "The integer convolution operator consumes an input tensor, its zero-point, a filter, and its zero-point,\nand computes the output. The production MUST never overflow. The accumulation may overflow if and only if in 32 bits.\n", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = ceil(input_shape[i] / strides[i])` for each axis `i`. The padding is split between the two sides equally or almost equally (depending on whether it is even or odd). In case the padding is an odd number, the extra padding is added at the end for SAME_UPPER and at the beginning for SAME_LOWER." + }, + { + "name": "dilations", + "type": "int64[]", + "required": false, + "description": "dilation value along each spatial axis of the filter. If not present, the dilation defaults to 1 along each axis." + }, + { + "name": "group", + "type": "int64", + "required": false, + "default": 1, + "description": "number of groups input channels and output channels are divided into. default is 1." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": false, + "description": "The shape of the convolution kernel. If not present, should be inferred from input 'w'." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0.The value represent the number of pixels added to the beginning and end part of the corresponding axis.`pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number ofpixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`.This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaultsto 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis. If not present, the stride defaults to 1 along each axis." + } + ], + "inputs": [ + { + "name": "x", + "type": "T1", + "description": "Input data tensor from previous layer; has size (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and width. Note that this is for the 2D image. Otherwise the size is (N x C x D1 x D2 ... x Dn). Optionally, if dimension denotation is in effect, the operation expects input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + }, + { + "name": "w", + "type": "T2", + "description": "The weight tensor that will be used in the convolutions; has size (M x C/group x kH x kW), where C is the number of channels, and kH and kW are the height and width of the kernel, and M is the number of feature maps. For more than 2 dimensions, the kernel shape will be (M x C/group x k1 x k2 x ... x kn), where (k1 x k2 x ... kn) is the dimension of the kernel. Optionally, if dimension denotation is in effect, the operation expects the weight tensor to arrive with the dimension denotation of [FILTER_OUT_CHANNEL, FILTER_IN_CHANNEL, FILTER_SPATIAL, FILTER_SPATIAL ...]. X.shape[1] == (W.shape[1] * group) == C (assuming zero based indices for the shape array). Or in other words FILTER_IN_CHANNEL should be equal to DATA_CHANNEL. " + }, + { + "name": "x_zero_point", + "type": "T1", + "option": "optional", + "description": "Zero point tensor for input 'x'. It's optional and default value is 0. It's a scalar, which means a per-tensor/layer quantization." + }, + { + "name": "w_zero_point", + "type": "T2", + "option": "optional", + "description": "Zero point tensor for input 'w'. It's optional and default value is 0. It could be a scalar or a 1-D tensor, which means a per-tensor/layer or per output channel quantization. If it's a 1-D tensor, its number of elements should be equal to the number of output channels (M)" + } + ], + "min_input": 2, + "max_input": 4, + "outputs": [ + { + "name": "y", + "type": "T3", + "description": "Output data tensor that contains the result of the convolution. The output dimensions are functions of the kernel size, stride size, and pad lengths." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 4", + "type_constraints": [ + { + "description": "Constrain input x and its zero point data type to 8-bit integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain input w and its zero point data type to 8-bit integer tensor.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain output y data type to 32-bit integer tensor.", + "type_param_str": "T3", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "with_padding", + "code": "\nx = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10]).astype(np.uint8).reshape((1, 1, 3, 3))\nx_zero_point = np.uint8(1)\nw = np.array([1, 1, 1, 1]).astype(np.uint8).reshape((1, 1, 2, 2))\n\ny = np.array([1, 3, 5, 3, 5, 12, 16, 9, 11, 24, 28, 15, 7, 15, 17, 9]).astype(np.int32).reshape((1, 1, 4, 4))\n\n# ConvInteger with padding\nconvinteger_node_with_padding = onnx.helper.make_node('ConvInteger',\n inputs=['x', 'w', 'x_zero_point'],\n outputs=['y'],\n pads=[1, 1, 1, 1],)\n\nexpect(convinteger_node_with_padding, inputs=[x, w, x_zero_point], outputs=[y],\n name='test_convinteger_with_padding')" + }, + { + "summary": "without_padding", + "code": "\nx = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10]).astype(np.uint8).reshape((1, 1, 3, 3))\nx_zero_point = np.uint8(1)\nw = np.array([1, 1, 1, 1]).astype(np.uint8).reshape((1, 1, 2, 2))\n\ny = np.array([12, 16, 24, 28]).astype(np.int32).reshape(1, 1, 2, 2)\n\n# ConvInteger without padding\nconvinteger_node = onnx.helper.make_node('ConvInteger',\n inputs=['x', 'w', 'x_zero_point'],\n outputs=['y'])\n\nexpect(convinteger_node, inputs=[x, w, x_zero_point], outputs=[y],\n name='test_convinteger_without_padding')" + } + ], + "category": "Layer" + }, + { + "name": "ConvTranspose", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "The convolution transpose operator consumes an input tensor and a filter,\nand computes the output.\n\nIf the pads parameter is provided the shape of the output is calculated via the following equation:\n\n output_shape[i] = stride[i] * (input_size[i] - 1) + output_padding[i] + ((kernel_shape[i] - 1) * dilations[i] + 1) - pads[start_i] - pads[end_i]\n\noutput_shape can also be explicitly specified in which case pads values are auto generated using these equations:\n\n total_padding[i] = stride[i] * (input_size[i] - 1) + output_padding[i] + ((kernel_shape[i] - 1) * dilations[i] + 1) - output_shape[i]\n If (auto_pads != SAME_UPPER): pads[start_i] = total_padding[i]/2; pads[end_i] = total_padding[i] - (total_padding[i]/2)\n Else: pads[start_i] = total_padding[i] - (total_padding[i]/2); pads[end_i] = (total_padding[i]/2).\n\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "dilations", + "type": "int64[]", + "required": false, + "description": "dilation value along each spatial axis of the filter." + }, + { + "name": "group", + "type": "int64", + "required": false, + "default": 1, + "description": "number of groups input channels and output channels are divided into." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": false, + "description": "The shape of the convolution kernel. If not present, should be inferred from input W." + }, + { + "name": "output_padding", + "type": "int64[]", + "required": false, + "description": "The zero-padding added to one side of the output. This is also called adjs/adjustment in some frameworks." + }, + { + "name": "output_shape", + "type": "int64[]", + "required": false, + "description": "The shape of the output can be explicitly set which will cause pads values to be auto generated. If output_shape is specified pads values are ignored. See doc for details for equations to generate pads" + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from previous layer; has size (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and width. Note that this is for the 2D image. Otherwise the size is (N x C x D1 x D2 ... x Dn)" + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor that will be used in the convolutions; has size (C x M/group x kH x kW), where C is the number of channels, and kH and kW are the height and width of the kernel, and M is the number of feature maps. For more than 2 dimensions, the weight shape will be (C x M/group x k1 x k2 x ... x kn), where (k1 x k2 x ... x kn) is the dimension of the kernel. The number of channels in the output should be equal to W.shape[1] * group (assuming zero based indices of the shape array)" + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "Optional 1D bias to be added to the convolution, has size of M." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor that contains the result of the convolution. The output dimensions are functions of the kernel size, stride size, pad lengths and group count. The number of channels in the output should be equal to W.shape[1] * group (assuming zero based indices of the shape array)" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "convtranspose", + "code": "x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)\n [3., 4., 5.],\n [6., 7., 8.]]]]).astype(np.float32)\n\nW = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array([[[[0., 1., 3., 3., 2.], # (1, 2, 5, 5)\n [3., 8., 15., 12., 7.],\n [9., 21., 36., 27., 15.],\n [9., 20., 33., 24., 13.],\n [6., 13., 21., 15., 8.]],\n\n [[0., 1., 3., 3., 2.],\n [3., 8., 15., 12., 7.],\n [9., 21., 36., 27., 15.],\n [9., 20., 33., 24., 13.],\n [6., 13., 21., 15., 8.]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose')" + }, + { + "summary": "convtranspose_1d", + "code": "x = np.array([[[0., 1., 2.]]]).astype(np.float32) # (1, 1, 3)\n\nW = np.array([[[1., 1., 1.], # (1, 2, 3)\n [1., 1., 1.]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array([[[0., 1., 3., 3., 2.], # (1, 2, 5)\n [0., 1., 3., 3., 2.]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_1d')" + }, + { + "summary": "convtranspose_3d", + "code": "x = np.array([[[[[0., 1., 2., 3., 4.], # (1, 1, 3, 4, 5)\n [5., 6., 7., 8., 9.],\n [10., 11., 12., 13., 14.],\n [15., 16., 17., 18., 19.]],\n [[20., 21., 22., 23., 24.],\n [25., 26., 27., 28., 29.],\n [30., 31., 32., 33., 34.],\n [35., 36., 37., 38., 39.]],\n [[40., 41., 42., 43., 44.],\n [45., 46., 47., 48., 49.],\n [50., 51., 52., 53., 54.],\n [55., 56., 57., 58., 59.]]]]]).astype(np.float32)\n\nW = np.array([[[[[1., 1., 1.], # (1, 2, 3, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]],\n [[[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array([[[[[0., 1., 3., 6., 9., 7., 4.], # (1, 2, 5, 6, 7)\n [5., 12., 21., 27., 33., 24., 13.],\n [15., 33., 54., 63., 72., 51., 27.],\n [30., 63., 99., 108., 117., 81., 42.],\n [25., 52., 81., 87., 93., 64., 33.],\n [15., 31., 48., 51., 54., 37., 19.]],\n\n [[20., 42., 66., 72., 78., 54., 28.],\n [50., 104., 162., 174., 186., 128., 66.],\n [90., 186., 288., 306., 324., 222., 114.],\n [120., 246., 378., 396., 414., 282., 144.],\n [90., 184., 282., 294., 306., 208., 106.],\n [50., 102., 156., 162., 168., 114., 58.]],\n\n [[60., 123., 189., 198., 207., 141., 72.],\n [135., 276., 423., 441., 459., 312., 159.],\n [225., 459., 702., 729., 756., 513., 261.],\n [270., 549., 837., 864., 891., 603., 306.],\n [195., 396., 603., 621., 639., 432., 219.],\n [105., 213., 324., 333., 342., 231., 117.]],\n\n [[60., 122., 186., 192., 198., 134., 68.],\n [130., 264., 402., 414., 426., 288., 146.],\n [210., 426., 648., 666., 684., 462., 234.],\n [240., 486., 738., 756., 774., 522., 264.],\n [170., 344., 522., 534., 546., 368., 186.],\n [90., 182., 276., 282., 288., 194., 98.]],\n\n [[40., 81., 123., 126., 129., 87., 44.],\n [85., 172., 261., 267., 273., 184., 93.],\n [135., 273., 414., 423., 432., 291., 147.],\n [150., 303., 459., 468., 477., 321., 162.],\n [105., 212., 321., 327., 333., 224., 113.],\n [55., 111., 168., 171., 174., 117., 59.]]],\n\n [[[0., 1., 3., 6., 9., 7., 4.],\n [5., 12., 21., 27., 33., 24., 13.],\n [15., 33., 54., 63., 72., 51., 27.],\n [30., 63., 99., 108., 117., 81., 42.],\n [25., 52., 81., 87., 93., 64., 33.],\n [15., 31., 48., 51., 54., 37., 19.]],\n\n [[20., 42., 66., 72., 78., 54., 28.],\n [50., 104., 162., 174., 186., 128., 66.],\n [90., 186., 288., 306., 324., 222., 114.],\n [120., 246., 378., 396., 414., 282., 144.],\n [90., 184., 282., 294., 306., 208., 106.],\n [50., 102., 156., 162., 168., 114., 58.]],\n\n [[60., 123., 189., 198., 207., 141., 72.],\n [135., 276., 423., 441., 459., 312., 159.],\n [225., 459., 702., 729., 756., 513., 261.],\n [270., 549., 837., 864., 891., 603., 306.],\n [195., 396., 603., 621., 639., 432., 219.],\n [105., 213., 324., 333., 342., 231., 117.]],\n\n [[60., 122., 186., 192., 198., 134., 68.],\n [130., 264., 402., 414., 426., 288., 146.],\n [210., 426., 648., 666., 684., 462., 234.],\n [240., 486., 738., 756., 774., 522., 264.],\n [170., 344., 522., 534., 546., 368., 186.],\n [90., 182., 276., 282., 288., 194., 98.]],\n\n [[40., 81., 123., 126., 129., 87., 44.],\n [85., 172., 261., 267., 273., 184., 93.],\n [135., 273., 414., 423., 432., 291., 147.],\n [150., 303., 459., 468., 477., 321., 162.],\n [105., 212., 321., 327., 333., 224., 113.],\n [55., 111., 168., 171., 174., 117., 59.]]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_3d')" + }, + { + "summary": "convtranspose_attributes", + "code": "x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)\n [3., 4., 5.],\n [6., 7., 8.]]]]).astype(np.float32)\n\nW = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\ny = np.array([[[[0., 0., 1., 1., 3., 2., 2., 0.], # (1, 2, 10, 8)\n [0., 0., 1., 1., 3., 2., 2., 0.],\n [0., 0., 1., 1., 3., 2., 2., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [0., 0., 0., 0., 0., 0., 0., 0.]],\n\n [[0., 0., 1., 1., 3., 2., 2., 0.],\n [0., 0., 1., 1., 3., 2., 2., 0.],\n [0., 0., 1., 1., 3., 2., 2., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [0., 0., 0., 0., 0., 0., 0., 0.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"],\n strides=[3, 2],\n output_shape=[10, 8])\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_output_shape')\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"],\n strides=[3, 2],\n output_padding=[1, 1])\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_pad')\n\nnode = onnx.helper.make_node(\n 'ConvTranspose', ['X', 'W'], ['Y'],\n name='test',\n strides=[3, 2],\n output_shape=[10, 8],\n kernel_shape=[3, 3],\n output_padding=[1, 1]\n)\nexpect(node, inputs=[x, W], outputs=[y],\n name='test_convtranspose_kernel_shape')" + }, + { + "summary": "convtranspose_autopad_same", + "code": "x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)\n [3., 4., 5.],\n [6., 7., 8.]]]]).astype(np.float32)\n\nW = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], auto_pad=\"SAME_UPPER\", strides=[2, 2])\n\ny = np.array([[[[0., 0., 1., 1., 3., 2.],\n [0., 0., 1., 1., 3., 2.],\n [3., 3., 8., 5., 12., 7.],\n [3., 3., 7., 4., 9., 5.],\n [9., 9., 20., 11., 24., 13.],\n [6., 6., 13., 7., 15., 8.]],\n\n [[0., 0., 1., 1., 3., 2.],\n [0., 0., 1., 1., 3., 2.],\n [3., 3., 8., 5., 12., 7.],\n [3., 3., 7., 4., 9., 5.],\n [9., 9., 20., 11., 24., 13.],\n [6., 6., 13., 7., 15., 8.]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_autopad_same')" + }, + { + "summary": "convtranspose_dilations", + "code": "x = np.array([[[[3., 8., 1.], # (1, 1, 3, 3)\n [9., 5., 7.],\n [3., 2., 6.]]]]).astype(np.float32)\nW = np.array([[[[7., 2.], # (1, 1, 2, 2)\n [1., 9.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], dilations=[2, 2])\n\ny = np.array([[[[21., 56., 13., 16., 2.], # [1, 1, 5, 5]\n [63., 35., 67., 10., 14.],\n [24., 22., 76., 76., 21.],\n [9., 5., 88., 45., 63.],\n [3., 2., 33., 18., 54.]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_dilations')" + }, + { + "summary": "convtranspose_pads", + "code": "x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)\n [3., 4., 5.],\n [6., 7., 8.]]]]).astype(np.float32)\n\nW = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"],\n strides=[3, 2],\n pads=[1, 2, 1, 2])\n\ny = np.array([[[[1., 1., 3.], # (1, 2, 7, 3)\n [1., 1., 3.],\n [7., 4., 9.],\n [7., 4., 9.],\n [7., 4., 9.],\n [13., 7., 15.],\n [13., 7., 15.]],\n\n [[1., 1., 3.],\n [1., 1., 3.],\n [7., 4., 9.],\n [7., 4., 9.],\n [7., 4., 9.],\n [13., 7., 15.],\n [13., 7., 15.]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_pads')" + } + ], + "category": "Layer" + }, + { + "name": "ConvTranspose", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "The convolution transpose operator consumes an input tensor and a filter,\nand computes the output.\n\nIf the pads parameter is provided the shape of the output is calculated via the following equation:\n\n output_shape[i] = stride[i] * (input_size[i] - 1) + output_padding[i] + ((kernel_shape[i] - 1) * dilations[i] + 1) - pads[start_i] - pads[end_i]\n\noutput_shape can also be explicitly specified in which case pads values are auto generated using these equations:\n\n total_padding[i] = stride[i] * (input_size[i] - 1) + output_padding[i] + ((kernel_shape[i] - 1) * dilations[i] + 1) - output_shape[i]\n If (auto_pads == SAME_UPPER): pads[start_i] = total_padding[i]/2; pads[end_i] = total_padding[i] - (total_padding[i]/2)\n Else: pads[start_i] = total_padding[i] - (total_padding[i]/2); pads[end_i] = (total_padding[i]/2).\n\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = input_shape[i] * strides[i]` for each axis `i`. The padding is split between the two sides equally or almost equally (depending on whether it is even or odd). In case the padding is an odd number, the extra padding is added at the end for SAME_UPPER and at the beginning for SAME_LOWER." + }, + { + "name": "dilations", + "type": "int64[]", + "required": false, + "description": "dilation value along each spatial axis of the filter. If not present, the dilation defaults to 1 along each spatial axis." + }, + { + "name": "group", + "type": "int64", + "required": false, + "default": 1, + "description": "number of groups input channels and output channels are divided into." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": false, + "description": "The shape of the convolution kernel. If not present, should be inferred from input W." + }, + { + "name": "output_padding", + "type": "int64[]", + "required": false, + "description": "Additional elements added to the side with higher coordinate indices in the output. Each padding value in \"output_padding\" must be less than the corresponding stride/dilation dimension. By default, this attribute is a zero vector. Note that this attribute doesn't directly affect the computed output values. It only controls the selection of the computed values, so changing this attribute only adds or removes output elements. If \"output_shape\" is explicitly provided, \"output_padding\" does not contribute additional size to \"output_shape\" but participates in the computation of the needed padding amount. This is also called adjs or adjustment in some frameworks." + }, + { + "name": "output_shape", + "type": "int64[]", + "required": false, + "description": "The shape of the output can be explicitly set which will cause pads values to be auto generated. If output_shape is specified pads values are ignored. See doc for details for equations to generate pads" + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from previous layer; has size (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and width. Note that this is for the 2D image. Otherwise the size is (N x C x D1 x D2 ... x Dn)" + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor that will be used in the convolutions; has size (C x M/group x kH x kW), where C is the number of channels, and kH and kW are the height and width of the kernel, and M is the number of feature maps. For more than 2 dimensions, the weight shape will be (C x M/group x k1 x k2 x ... x kn), where (k1 x k2 x ... x kn) is the dimension of the kernel. The number of channels in the output should be equal to W.shape[1] * group (assuming zero based indices of the shape array)" + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "Optional 1D bias to be added to the convolution, has size of M." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor that contains the result of the convolution. The output dimensions are functions of the kernel size, stride size, pad lengths and group count. The number of channels in the output should be equal to W.shape[1] * group (assuming zero based indices of the shape array)" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "convtranspose", + "code": "x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)\n [3., 4., 5.],\n [6., 7., 8.]]]]).astype(np.float32)\n\nW = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array([[[[0., 1., 3., 3., 2.], # (1, 2, 5, 5)\n [3., 8., 15., 12., 7.],\n [9., 21., 36., 27., 15.],\n [9., 20., 33., 24., 13.],\n [6., 13., 21., 15., 8.]],\n\n [[0., 1., 3., 3., 2.],\n [3., 8., 15., 12., 7.],\n [9., 21., 36., 27., 15.],\n [9., 20., 33., 24., 13.],\n [6., 13., 21., 15., 8.]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose')" + }, + { + "summary": "convtranspose_1d", + "code": "x = np.array([[[0., 1., 2.]]]).astype(np.float32) # (1, 1, 3)\n\nW = np.array([[[1., 1., 1.], # (1, 2, 3)\n [1., 1., 1.]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array([[[0., 1., 3., 3., 2.], # (1, 2, 5)\n [0., 1., 3., 3., 2.]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_1d')" + }, + { + "summary": "convtranspose_3d", + "code": "x = np.array([[[[[0., 1., 2., 3., 4.], # (1, 1, 3, 4, 5)\n [5., 6., 7., 8., 9.],\n [10., 11., 12., 13., 14.],\n [15., 16., 17., 18., 19.]],\n [[20., 21., 22., 23., 24.],\n [25., 26., 27., 28., 29.],\n [30., 31., 32., 33., 34.],\n [35., 36., 37., 38., 39.]],\n [[40., 41., 42., 43., 44.],\n [45., 46., 47., 48., 49.],\n [50., 51., 52., 53., 54.],\n [55., 56., 57., 58., 59.]]]]]).astype(np.float32)\n\nW = np.array([[[[[1., 1., 1.], # (1, 2, 3, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]],\n [[[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array([[[[[0., 1., 3., 6., 9., 7., 4.], # (1, 2, 5, 6, 7)\n [5., 12., 21., 27., 33., 24., 13.],\n [15., 33., 54., 63., 72., 51., 27.],\n [30., 63., 99., 108., 117., 81., 42.],\n [25., 52., 81., 87., 93., 64., 33.],\n [15., 31., 48., 51., 54., 37., 19.]],\n\n [[20., 42., 66., 72., 78., 54., 28.],\n [50., 104., 162., 174., 186., 128., 66.],\n [90., 186., 288., 306., 324., 222., 114.],\n [120., 246., 378., 396., 414., 282., 144.],\n [90., 184., 282., 294., 306., 208., 106.],\n [50., 102., 156., 162., 168., 114., 58.]],\n\n [[60., 123., 189., 198., 207., 141., 72.],\n [135., 276., 423., 441., 459., 312., 159.],\n [225., 459., 702., 729., 756., 513., 261.],\n [270., 549., 837., 864., 891., 603., 306.],\n [195., 396., 603., 621., 639., 432., 219.],\n [105., 213., 324., 333., 342., 231., 117.]],\n\n [[60., 122., 186., 192., 198., 134., 68.],\n [130., 264., 402., 414., 426., 288., 146.],\n [210., 426., 648., 666., 684., 462., 234.],\n [240., 486., 738., 756., 774., 522., 264.],\n [170., 344., 522., 534., 546., 368., 186.],\n [90., 182., 276., 282., 288., 194., 98.]],\n\n [[40., 81., 123., 126., 129., 87., 44.],\n [85., 172., 261., 267., 273., 184., 93.],\n [135., 273., 414., 423., 432., 291., 147.],\n [150., 303., 459., 468., 477., 321., 162.],\n [105., 212., 321., 327., 333., 224., 113.],\n [55., 111., 168., 171., 174., 117., 59.]]],\n\n [[[0., 1., 3., 6., 9., 7., 4.],\n [5., 12., 21., 27., 33., 24., 13.],\n [15., 33., 54., 63., 72., 51., 27.],\n [30., 63., 99., 108., 117., 81., 42.],\n [25., 52., 81., 87., 93., 64., 33.],\n [15., 31., 48., 51., 54., 37., 19.]],\n\n [[20., 42., 66., 72., 78., 54., 28.],\n [50., 104., 162., 174., 186., 128., 66.],\n [90., 186., 288., 306., 324., 222., 114.],\n [120., 246., 378., 396., 414., 282., 144.],\n [90., 184., 282., 294., 306., 208., 106.],\n [50., 102., 156., 162., 168., 114., 58.]],\n\n [[60., 123., 189., 198., 207., 141., 72.],\n [135., 276., 423., 441., 459., 312., 159.],\n [225., 459., 702., 729., 756., 513., 261.],\n [270., 549., 837., 864., 891., 603., 306.],\n [195., 396., 603., 621., 639., 432., 219.],\n [105., 213., 324., 333., 342., 231., 117.]],\n\n [[60., 122., 186., 192., 198., 134., 68.],\n [130., 264., 402., 414., 426., 288., 146.],\n [210., 426., 648., 666., 684., 462., 234.],\n [240., 486., 738., 756., 774., 522., 264.],\n [170., 344., 522., 534., 546., 368., 186.],\n [90., 182., 276., 282., 288., 194., 98.]],\n\n [[40., 81., 123., 126., 129., 87., 44.],\n [85., 172., 261., 267., 273., 184., 93.],\n [135., 273., 414., 423., 432., 291., 147.],\n [150., 303., 459., 468., 477., 321., 162.],\n [105., 212., 321., 327., 333., 224., 113.],\n [55., 111., 168., 171., 174., 117., 59.]]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_3d')" + }, + { + "summary": "convtranspose_attributes", + "code": "x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)\n [3., 4., 5.],\n [6., 7., 8.]]]]).astype(np.float32)\n\nW = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\ny = np.array([[[[0., 0., 1., 1., 3., 2., 2., 0.], # (1, 2, 10, 8)\n [0., 0., 1., 1., 3., 2., 2., 0.],\n [0., 0., 1., 1., 3., 2., 2., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [0., 0., 0., 0., 0., 0., 0., 0.]],\n\n [[0., 0., 1., 1., 3., 2., 2., 0.],\n [0., 0., 1., 1., 3., 2., 2., 0.],\n [0., 0., 1., 1., 3., 2., 2., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [3., 3., 7., 4., 9., 5., 5., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [6., 6., 13., 7., 15., 8., 8., 0.],\n [0., 0., 0., 0., 0., 0., 0., 0.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"],\n strides=[3, 2],\n output_shape=[10, 8])\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_output_shape')\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"],\n strides=[3, 2],\n output_padding=[1, 1])\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_pad')\n\nnode = onnx.helper.make_node(\n 'ConvTranspose', ['X', 'W'], ['Y'],\n name='test',\n strides=[3, 2],\n output_shape=[10, 8],\n kernel_shape=[3, 3],\n output_padding=[1, 1]\n)\nexpect(node, inputs=[x, W], outputs=[y],\n name='test_convtranspose_kernel_shape')" + }, + { + "summary": "convtranspose_autopad_same", + "code": "x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)\n [3., 4., 5.],\n [6., 7., 8.]]]]).astype(np.float32)\n\nW = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], auto_pad=\"SAME_UPPER\", strides=[2, 2])\n\ny = np.array([[[[0., 0., 1., 1., 3., 2.],\n [0., 0., 1., 1., 3., 2.],\n [3., 3., 8., 5., 12., 7.],\n [3., 3., 7., 4., 9., 5.],\n [9., 9., 20., 11., 24., 13.],\n [6., 6., 13., 7., 15., 8.]],\n\n [[0., 0., 1., 1., 3., 2.],\n [0., 0., 1., 1., 3., 2.],\n [3., 3., 8., 5., 12., 7.],\n [3., 3., 7., 4., 9., 5.],\n [9., 9., 20., 11., 24., 13.],\n [6., 6., 13., 7., 15., 8.]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_autopad_same')" + }, + { + "summary": "convtranspose_dilations", + "code": "x = np.array([[[[3., 8., 1.], # (1, 1, 3, 3)\n [9., 5., 7.],\n [3., 2., 6.]]]]).astype(np.float32)\nW = np.array([[[[7., 2.], # (1, 1, 2, 2)\n [1., 9.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], dilations=[2, 2])\n\ny = np.array([[[[21., 56., 13., 16., 2.], # [1, 1, 5, 5]\n [63., 35., 67., 10., 14.],\n [24., 22., 76., 76., 21.],\n [9., 5., 88., 45., 63.],\n [3., 2., 33., 18., 54.]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_dilations')" + }, + { + "summary": "convtranspose_pads", + "code": "x = np.array([[[[0., 1., 2.], # (1, 1, 3, 3)\n [3., 4., 5.],\n [6., 7., 8.]]]]).astype(np.float32)\n\nW = np.array([[[[1., 1., 1.], # (1, 2, 3, 3)\n [1., 1., 1.],\n [1., 1., 1.]],\n [[1., 1., 1.],\n [1., 1., 1.],\n [1., 1., 1.]]]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"],\n strides=[3, 2],\n pads=[1, 2, 1, 2])\n\ny = np.array([[[[1., 1., 3.], # (1, 2, 7, 3)\n [1., 1., 3.],\n [7., 4., 9.],\n [7., 4., 9.],\n [7., 4., 9.],\n [13., 7., 15.],\n [13., 7., 15.]],\n\n [[1., 1., 3.],\n [1., 1., 3.],\n [7., 4., 9.],\n [7., 4., 9.],\n [7., 4., 9.],\n [13., 7., 15.],\n [13., 7., 15.]]]]).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name='test_convtranspose_pads')" + } + ], + "category": "Layer" + }, + { + "name": "Cos", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Calculates the cosine of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The cosine of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "cos", + "code": "node = onnx.helper.make_node(\n 'Cos',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.cos(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_cos_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.cos(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_cos')" + } + ] + }, + { + "name": "Cosh", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Calculates the hyperbolic cosine of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The hyperbolic cosine values of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "cosh", + "code": "node = onnx.helper.make_node(\n 'Cosh',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.cosh(x) # expected output [1.54308069, 1., 1.54308069]\nexpect(node, inputs=[x], outputs=[y],\n name='test_cosh_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.cosh(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_cosh')" + } + ] + }, + { + "name": "CumSum", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Performs cumulative sum of the input elements along the given axis.\nBy default, it will do the sum inclusively meaning the first element is copied as is.\nThrough an `exclusive` attribute, this behavior can change to exclude the first element.\nIt can also perform summation in the opposite direction of the axis. For that, set `reverse` attribute to 1.\n\nExample:\n```\ninput_x = [1, 2, 3]\naxis=0\noutput = [1, 3, 6]\nexclusive=1\noutput = [0, 1, 3]\nexclusive=0\nreverse=1\noutput = [6, 5, 3]\nexclusive=1\nreverse=1\noutput = [5, 3, 0]\n```\n ", + "attributes": [ + { + "name": "exclusive", + "type": "int64", + "required": false, + "description": "If set to 1 will return exclusive sum in which the top element is not included. In other terms, if set to 1, the j-th output element would be the sum of the first (j-1) elements. Otherwise, it would be the sum of the first j elements." + }, + { + "name": "reverse", + "type": "int64", + "required": false, + "description": "If set to 1 will perform the sums in reverse direction." + } + ], + "inputs": [ + { + "name": "x", + "type": "T", + "description": "An input tensor that is to be processed." + }, + { + "name": "axis", + "type": "T2", + "description": "A 0-D tensor. Must be in the range [-rank(x), rank(x)-1]. Negative value means counting dimensions from the back." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "y", + "type": "T", + "description": "Output tensor of the same type as 'x' with cumulative sums of the x's elements" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input can be of any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "axis tensor can be int32 or int64 only", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "cumsum_1d", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y']\n)\nx = np.array([1., 2., 3., 4., 5.]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([1., 3., 6., 10., 15.]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_1d')" + }, + { + "summary": "cumsum_1d_exclusive", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n exclusive=1\n)\nx = np.array([1., 2., 3., 4., 5.]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([0., 1., 3., 6., 10.]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_1d_exclusive')" + }, + { + "summary": "cumsum_1d_reverse", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n reverse=1\n)\nx = np.array([1., 2., 3., 4., 5.]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([15., 14., 12., 9., 5.]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_1d_reverse')" + }, + { + "summary": "cumsum_1d_reverse_exclusive", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n reverse=1,\n exclusive=1\n)\nx = np.array([1., 2., 3., 4., 5.]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([14., 12., 9., 5., 0.]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_1d_reverse_exclusive')" + }, + { + "summary": "cumsum_2d_axis_0", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n)\nx = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float64).reshape((2, 3))\naxis = np.int32(0)\ny = np.array([1., 2., 3., 5., 7., 9.]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_2d_axis_0')" + }, + { + "summary": "cumsum_2d_axis_1", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n)\nx = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float64).reshape((2, 3))\naxis = np.int32(1)\ny = np.array([1., 3., 6., 4., 9., 15.]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_2d_axis_1')" + }, + { + "summary": "cumsum_2d_negative_axis", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n)\nx = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float64).reshape((2, 3))\naxis = np.int32(-1)\ny = np.array([1., 3., 6., 4., 9., 15.]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_2d_negative_axis')" + } + ] + }, + { + "name": "CumSum", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Performs cumulative sum of the input elements along the given axis.\nBy default, it will do the sum inclusively meaning the first element is copied as is.\nThrough an `exclusive` attribute, this behavior can change to exclude the first element.\nIt can also perform summation in the opposite direction of the axis. For that, set `reverse` attribute to 1.\n\nExample:\n```\ninput_x = [1, 2, 3]\naxis=0\noutput = [1, 3, 6]\nexclusive=1\noutput = [0, 1, 3]\nexclusive=0\nreverse=1\noutput = [6, 5, 3]\nexclusive=1\nreverse=1\noutput = [5, 3, 0]\n```\n ", + "attributes": [ + { + "name": "exclusive", + "type": "int64", + "required": false, + "description": "If set to 1 will return exclusive sum in which the top element is not included. In other terms, if set to 1, the j-th output element would be the sum of the first (j-1) elements. Otherwise, it would be the sum of the first j elements." + }, + { + "name": "reverse", + "type": "int64", + "required": false, + "description": "If set to 1 will perform the sums in reverse direction." + } + ], + "inputs": [ + { + "name": "x", + "type": "T", + "description": "An input tensor that is to be processed." + }, + { + "name": "axis", + "type": "T2", + "description": "A 0-D tensor. Must be in the range [-rank(x), rank(x)-1]. Negative value means counting dimensions from the back." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "y", + "type": "T", + "description": "Output tensor of the same type as 'x' with cumulative sums of the x's elements" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "axis tensor can be int32 or int64 only", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "cumsum_1d", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y']\n)\nx = np.array([1., 2., 3., 4., 5.]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([1., 3., 6., 10., 15.]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_1d')" + }, + { + "summary": "cumsum_1d_exclusive", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n exclusive=1\n)\nx = np.array([1., 2., 3., 4., 5.]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([0., 1., 3., 6., 10.]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_1d_exclusive')" + }, + { + "summary": "cumsum_1d_reverse", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n reverse=1\n)\nx = np.array([1., 2., 3., 4., 5.]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([15., 14., 12., 9., 5.]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_1d_reverse')" + }, + { + "summary": "cumsum_1d_reverse_exclusive", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n reverse=1,\n exclusive=1\n)\nx = np.array([1., 2., 3., 4., 5.]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([14., 12., 9., 5., 0.]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_1d_reverse_exclusive')" + }, + { + "summary": "cumsum_2d_axis_0", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n)\nx = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float64).reshape((2, 3))\naxis = np.int32(0)\ny = np.array([1., 2., 3., 5., 7., 9.]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_2d_axis_0')" + }, + { + "summary": "cumsum_2d_axis_1", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n)\nx = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float64).reshape((2, 3))\naxis = np.int32(1)\ny = np.array([1., 3., 6., 4., 9., 15.]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_2d_axis_1')" + }, + { + "summary": "cumsum_2d_negative_axis", + "code": "node = onnx.helper.make_node(\n 'CumSum',\n inputs=['x', 'axis'],\n outputs=['y'],\n)\nx = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float64).reshape((2, 3))\naxis = np.int32(-1)\ny = np.array([1., 3., 6., 4., 9., 15.]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y],\n name='test_cumsum_2d_negative_axis')" + } + ] + }, + { + "name": "DepthToSpace", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "DepthToSpace rearranges (permutes) data from depth into blocks of spatial data.\nThis is the reverse transformation of SpaceToDepth. More specifically, this op outputs a copy of\nthe input tensor where values from the depth dimension are moved in spatial blocks to the height\nand width dimensions.\n", + "attributes": [ + { + "name": "blocksize", + "type": "int64", + "required": true, + "description": "Blocks of [blocksize, blocksize] are moved." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of [N,C,H,W], where N is the batch axis, C is the channel or depth, H is the height and W is the width." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of [N, C/(blocksize * blocksize), H * blocksize, W * blocksize]." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "crd_mode_example", + "code": "node = onnx.helper.make_node(\n 'DepthToSpace',\n inputs=['x'],\n outputs=['y'],\n blocksize=2,\n mode='CRD'\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array([[[[0., 1., 2.],\n [3., 4., 5.]],\n [[9., 10., 11.],\n [12., 13., 14.]],\n [[18., 19., 20.],\n [21., 22., 23.]],\n [[27., 28., 29.],\n [30., 31., 32.]],\n [[36., 37., 38.],\n [39., 40., 41.]],\n [[45., 46., 47.],\n [48., 49., 50.]],\n [[54., 55., 56.],\n [57., 58., 59.]],\n [[63., 64., 65.],\n [66., 67., 68.]]]]).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array([[[[0., 9., 1., 10., 2., 11.],\n [18., 27., 19., 28., 20., 29.],\n [3., 12., 4., 13., 5., 14.],\n [21., 30., 22., 31., 23., 32.]],\n [[36., 45., 37., 46., 38., 47.],\n [54., 63., 55., 64., 56., 65.],\n [39., 48., 40., 49., 41., 50.],\n [57., 66., 58., 67., 59., 68.]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_depthtospace_crd_mode_example')" + }, + { + "summary": "default_mode_example", + "code": "node = onnx.helper.make_node(\n 'DepthToSpace',\n inputs=['x'],\n outputs=['y'],\n blocksize=2,\n mode='DCR'\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array([[[[0., 1., 2.],\n [3., 4., 5.]],\n [[9., 10., 11.],\n [12., 13., 14.]],\n [[18., 19., 20.],\n [21., 22., 23.]],\n [[27., 28., 29.],\n [30., 31., 32.]],\n [[36., 37., 38.],\n [39., 40., 41.]],\n [[45., 46., 47.],\n [48., 49., 50.]],\n [[54., 55., 56.],\n [57., 58., 59.]],\n [[63., 64., 65.],\n [66., 67., 68.]]]]).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array([[[[0., 18., 1., 19., 2., 20.],\n [36., 54., 37., 55., 38., 56.],\n [3., 21., 4., 22., 5., 23.],\n [39., 57., 40., 58., 41., 59.]],\n [[9., 27., 10., 28., 11., 29.],\n [45., 63., 46., 64., 47., 65.],\n [12., 30., 13., 31., 14., 32.],\n [48., 66., 49., 67., 50., 68.]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_depthtospace_example')" + } + ] + }, + { + "name": "DepthToSpace", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "DepthToSpace rearranges (permutes) data from depth into blocks of spatial data.\nThis is the reverse transformation of SpaceToDepth. More specifically, this op outputs a copy of\nthe input tensor where values from the depth dimension are moved in spatial blocks to the height\nand width dimensions. By default, `mode` = `DCR`.\nIn the DCR mode, elements along the depth dimension from the input tensor are rearranged in the\nfollowing order: depth, column, and then row. The output y is computed from the input x as below:\n\nb, c, h, w = x.shape\n\ntmp = np.reshape(x, [b, blocksize, blocksize, c // (blocksize**2), h, w])\n\ntmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2])\n\ny = np.reshape(tmp, [b, c // (blocksize**2), h * blocksize, w * blocksize])\n\n\nIn the CRD mode, elements along the depth dimension from the input tensor are rearranged in the\nfollowing order: column, row, and the depth. The output y is computed from the input x as below:\n\nb, c, h, w = x.shape\n\ntmp = np.reshape(x, [b, c // (blocksize ** 2), blocksize, blocksize, h, w])\n\ntmp = np.transpose(tmp, [0, 1, 4, 2, 5, 3])\n\ny = np.reshape(tmp, [b, c // (blocksize ** 2), h * blocksize, w * blocksize])\n\n", + "attributes": [ + { + "name": "blocksize", + "type": "int64", + "required": true, + "description": "Blocks of [blocksize, blocksize] are moved." + }, + { + "name": "mode", + "type": "string", + "required": false, + "default": "DCR", + "description": "DCR (default) for depth-column-row order re-arrangement. Use CRD for column-row-depth order." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of [N,C,H,W], where N is the batch axis, C is the channel or depth, H is the height and W is the width." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of [N, C/(blocksize * blocksize), H * blocksize, W * blocksize]." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "crd_mode_example", + "code": "node = onnx.helper.make_node(\n 'DepthToSpace',\n inputs=['x'],\n outputs=['y'],\n blocksize=2,\n mode='CRD'\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array([[[[0., 1., 2.],\n [3., 4., 5.]],\n [[9., 10., 11.],\n [12., 13., 14.]],\n [[18., 19., 20.],\n [21., 22., 23.]],\n [[27., 28., 29.],\n [30., 31., 32.]],\n [[36., 37., 38.],\n [39., 40., 41.]],\n [[45., 46., 47.],\n [48., 49., 50.]],\n [[54., 55., 56.],\n [57., 58., 59.]],\n [[63., 64., 65.],\n [66., 67., 68.]]]]).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array([[[[0., 9., 1., 10., 2., 11.],\n [18., 27., 19., 28., 20., 29.],\n [3., 12., 4., 13., 5., 14.],\n [21., 30., 22., 31., 23., 32.]],\n [[36., 45., 37., 46., 38., 47.],\n [54., 63., 55., 64., 56., 65.],\n [39., 48., 40., 49., 41., 50.],\n [57., 66., 58., 67., 59., 68.]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_depthtospace_crd_mode_example')" + }, + { + "summary": "default_mode_example", + "code": "node = onnx.helper.make_node(\n 'DepthToSpace',\n inputs=['x'],\n outputs=['y'],\n blocksize=2,\n mode='DCR'\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array([[[[0., 1., 2.],\n [3., 4., 5.]],\n [[9., 10., 11.],\n [12., 13., 14.]],\n [[18., 19., 20.],\n [21., 22., 23.]],\n [[27., 28., 29.],\n [30., 31., 32.]],\n [[36., 37., 38.],\n [39., 40., 41.]],\n [[45., 46., 47.],\n [48., 49., 50.]],\n [[54., 55., 56.],\n [57., 58., 59.]],\n [[63., 64., 65.],\n [66., 67., 68.]]]]).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array([[[[0., 18., 1., 19., 2., 20.],\n [36., 54., 37., 55., 38., 56.],\n [3., 21., 4., 22., 5., 23.],\n [39., 57., 40., 58., 41., 59.]],\n [[9., 27., 10., 28., 11., 29.],\n [45., 63., 46., 64., 47., 65.],\n [12., 30., 13., 31., 14., 32.],\n [48., 66., 49., 67., 50., 68.]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_depthtospace_example')" + } + ] + }, + { + "name": "DepthToSpace", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "DepthToSpace rearranges (permutes) data from depth into blocks of spatial data.\nThis is the reverse transformation of SpaceToDepth. More specifically, this op outputs a copy of\nthe input tensor where values from the depth dimension are moved in spatial blocks to the height\nand width dimensions. By default, `mode` = `DCR`.\nIn the DCR mode, elements along the depth dimension from the input tensor are rearranged in the\nfollowing order: depth, column, and then row. The output y is computed from the input x as below:\n\nb, c, h, w = x.shape\n\ntmp = np.reshape(x, [b, blocksize, blocksize, c // (blocksize**2), h, w])\n\ntmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2])\n\ny = np.reshape(tmp, [b, c // (blocksize**2), h * blocksize, w * blocksize])\n\n\nIn the CRD mode, elements along the depth dimension from the input tensor are rearranged in the\nfollowing order: column, row, and the depth. The output y is computed from the input x as below:\n\nb, c, h, w = x.shape\n\ntmp = np.reshape(x, [b, c // (blocksize ** 2), blocksize, blocksize, h, w])\n\ntmp = np.transpose(tmp, [0, 1, 4, 2, 5, 3])\n\ny = np.reshape(tmp, [b, c // (blocksize ** 2), h * blocksize, w * blocksize])\n\n", + "attributes": [ + { + "name": "blocksize", + "type": "int64", + "required": true, + "description": "Blocks of [blocksize, blocksize] are moved." + }, + { + "name": "mode", + "type": "string", + "required": false, + "default": "DCR", + "description": "DCR (default) for depth-column-row order re-arrangement. Use CRD for column-row-depth order." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of [N,C,H,W], where N is the batch axis, C is the channel or depth, H is the height and W is the width." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of [N, C/(blocksize * blocksize), H * blocksize, W * blocksize]." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "crd_mode_example", + "code": "node = onnx.helper.make_node(\n 'DepthToSpace',\n inputs=['x'],\n outputs=['y'],\n blocksize=2,\n mode='CRD'\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array([[[[0., 1., 2.],\n [3., 4., 5.]],\n [[9., 10., 11.],\n [12., 13., 14.]],\n [[18., 19., 20.],\n [21., 22., 23.]],\n [[27., 28., 29.],\n [30., 31., 32.]],\n [[36., 37., 38.],\n [39., 40., 41.]],\n [[45., 46., 47.],\n [48., 49., 50.]],\n [[54., 55., 56.],\n [57., 58., 59.]],\n [[63., 64., 65.],\n [66., 67., 68.]]]]).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array([[[[0., 9., 1., 10., 2., 11.],\n [18., 27., 19., 28., 20., 29.],\n [3., 12., 4., 13., 5., 14.],\n [21., 30., 22., 31., 23., 32.]],\n [[36., 45., 37., 46., 38., 47.],\n [54., 63., 55., 64., 56., 65.],\n [39., 48., 40., 49., 41., 50.],\n [57., 66., 58., 67., 59., 68.]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_depthtospace_crd_mode_example')" + }, + { + "summary": "default_mode_example", + "code": "node = onnx.helper.make_node(\n 'DepthToSpace',\n inputs=['x'],\n outputs=['y'],\n blocksize=2,\n mode='DCR'\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array([[[[0., 1., 2.],\n [3., 4., 5.]],\n [[9., 10., 11.],\n [12., 13., 14.]],\n [[18., 19., 20.],\n [21., 22., 23.]],\n [[27., 28., 29.],\n [30., 31., 32.]],\n [[36., 37., 38.],\n [39., 40., 41.]],\n [[45., 46., 47.],\n [48., 49., 50.]],\n [[54., 55., 56.],\n [57., 58., 59.]],\n [[63., 64., 65.],\n [66., 67., 68.]]]]).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array([[[[0., 18., 1., 19., 2., 20.],\n [36., 54., 37., 55., 38., 56.],\n [3., 21., 4., 22., 5., 23.],\n [39., 57., 40., 58., 41., 59.]],\n [[9., 27., 10., 28., 11., 29.],\n [45., 63., 46., 64., 47., 65.],\n [12., 30., 13., 31., 14., 32.],\n [48., 66., 49., 67., 50., 68.]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_depthtospace_example')" + } + ] + }, + { + "name": "DequantizeLinear", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "The linear dequantization operator. It consumes a quantized tensor, a scale, a zero point to compute the full precision tensor.\nThe dequantization formula is y = (x - x_zero_point) * x_scale. 'x_scale' and 'x_zero_point' are both scalars.\n'x_zero_point' and 'x' must have same type. 'x' and 'y' must have same shape. In the case of dequantizing int32,\nthere's no zero point (zero point is supposed to be 0).\n", + "inputs": [ + { + "name": "x", + "type": "T", + "description": "N-D quantized input tensor to be de-quantized." + }, + { + "name": "x_scale", + "type": "tensor(float)", + "description": "Scale for input 'x'. It's a scalar, which means a per-tensor/layer quantization." + }, + { + "name": "x_zero_point", + "type": "T", + "option": "optional", + "description": "Zero point for input 'x'. It's a scalar, which means a per-tensor/layer quantization. It's optional. 0 is the default value when it's not specified." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "y", + "type": "tensor(float)", + "description": "N-D full precision output tensor. It has same shape as input 'x'." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain 'x_zero_point' and 'x' to 8-bit/32-bit integer tensor.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)", + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "axis", + "code": "node = onnx.helper.make_node('DequantizeLinear',\n inputs=['x', 'x_scale', 'x_zero_point'],\n outputs=['y'],)\n\n# 1-D tensor zero point and scale of size equal to axis 1 of the input tensor\nx = np.array([[[[3, 89],\n [34, 200],\n [74, 59]],\n\n [[5, 24],\n [24, 87],\n [32, 13]],\n\n [[245, 99],\n [4, 142],\n [121, 102]], ], ], dtype=np.uint8)\nx_scale = np.array([2, 4, 5], dtype=np.float32)\nx_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (x.astype(np.float32) - x_zero_point.reshape(1, 3, 1, 1).astype(np.float32)) * x_scale.reshape(1, 3, 1, 1)\n\nexpect(node, inputs=[x, x_scale, x_zero_point], outputs=[y],\n name='test_dequantizelinear_axis')" + }, + { + "summary": "dequantizelinear", + "code": "node = onnx.helper.make_node('DequantizeLinear',\n inputs=['x', 'x_scale', 'x_zero_point'],\n outputs=['y'],)\n\n# scalar zero point and scale\nx = np.array([0, 3, 128, 255]).astype(np.uint8)\nx_scale = np.float32(2)\nx_zero_point = np.uint8(128)\ny = np.array([-256, -250, 0, 254], dtype=np.float32)\n\nexpect(node, inputs=[x, x_scale, x_zero_point], outputs=[y],\n name='test_dequantizelinear')" + } + ] + }, + { + "name": "DequantizeLinear", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "The linear dequantization operator. It consumes a quantized tensor, a scale, and a zero point to compute the full precision tensor.\nThe dequantization formula is y = (x - x_zero_point) * x_scale. 'x_scale' and 'x_zero_point' must have same shape, and can be either a scalar\nfor per-tensor / per layer quantization, or a 1-D tensor for per-axis quantization.\n'x_zero_point' and 'x' must have same type. 'x' and 'y' must have same shape. In the case of dequantizing int32,\nthere's no zero point (zero point is supposed to be 0).\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Ignored for per-tensor quantization. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + } + ], + "inputs": [ + { + "name": "x", + "type": "T", + "description": "N-D quantized input tensor to be de-quantized." + }, + { + "name": "x_scale", + "type": "tensor(float)", + "description": "Scale for input 'x'. It can be a scalar, which means a per-tensor/layer dequantization, or a 1-D tensor for per-axis dequantization." + }, + { + "name": "x_zero_point", + "type": "T", + "option": "optional", + "description": "Zero point for input 'x'. Shape must match x_scale. It's optional. Zero point is 0 when it's not specified." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "y", + "type": "tensor(float)", + "description": "N-D full precision output tensor. It has same shape as input 'x'." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain 'x_zero_point' and 'x' to 8-bit/32-bit integer tensor.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)", + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "axis", + "code": "node = onnx.helper.make_node('DequantizeLinear',\n inputs=['x', 'x_scale', 'x_zero_point'],\n outputs=['y'],)\n\n# 1-D tensor zero point and scale of size equal to axis 1 of the input tensor\nx = np.array([[[[3, 89],\n [34, 200],\n [74, 59]],\n\n [[5, 24],\n [24, 87],\n [32, 13]],\n\n [[245, 99],\n [4, 142],\n [121, 102]], ], ], dtype=np.uint8)\nx_scale = np.array([2, 4, 5], dtype=np.float32)\nx_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (x.astype(np.float32) - x_zero_point.reshape(1, 3, 1, 1).astype(np.float32)) * x_scale.reshape(1, 3, 1, 1)\n\nexpect(node, inputs=[x, x_scale, x_zero_point], outputs=[y],\n name='test_dequantizelinear_axis')" + }, + { + "summary": "dequantizelinear", + "code": "node = onnx.helper.make_node('DequantizeLinear',\n inputs=['x', 'x_scale', 'x_zero_point'],\n outputs=['y'],)\n\n# scalar zero point and scale\nx = np.array([0, 3, 128, 255]).astype(np.uint8)\nx_scale = np.float32(2)\nx_zero_point = np.uint8(128)\ny = np.array([-256, -250, 0, 254], dtype=np.float32)\n\nexpect(node, inputs=[x, x_scale, x_zero_point], outputs=[y],\n name='test_dequantizelinear')" + } + ] + }, + { + "name": "Det", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Det calculates determinant of a square matrix or batches of square matrices.\nDet takes one input tensor of shape `[*, M, M]`, where `*` is zero or more batch dimensions,\nand the inner-most 2 dimensions form square matrices.\nThe output is a tensor of shape `[*]`, containing the determinants of all input submatrices.\ne.g., When the input is 2-D, the output is a scalar(shape is empty: `[]`).\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to floating-point tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "2d", + "code": "node = onnx.helper.make_node(\n 'Det',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.arange(4).reshape(2, 2).astype(np.float32)\ny = np.linalg.det(x) # expect -2\nexpect(node, inputs=[x], outputs=[y],\n name='test_det_2d')" + }, + { + "summary": "nd", + "code": "node = onnx.helper.make_node(\n 'Det',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]]).astype(np.float32)\ny = np.linalg.det(x) # expect array([-2., -3., -8.])\nexpect(node, inputs=[x], outputs=[y],\n name='test_det_nd')" + } + ] + }, + { + "name": "DictVectorizer", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Uses an index mapping to convert a dictionary to an array.
\n Given a dictionary, each key is looked up in the vocabulary attribute corresponding to\n the key type. The index into the vocabulary array at which the key is found is then\n used to index the output 1-D tensor 'Y' and insert into it the value found in the dictionary 'X'.
\n The key type of the input map must correspond to the element type of the defined vocabulary attribute.\n Therefore, the output array will be equal in length to the index mapping vector parameter.\n All keys in the input dictionary must be present in the index mapping vector.\n For each item in the input dictionary, insert its value in the output array.\n Any keys not present in the input dictionary, will be zero in the output array.
\n For example: if the ``string_vocabulary`` parameter is set to ``[\"a\", \"c\", \"b\", \"z\"]``,\n then an input of ``{\"a\": 4, \"c\": 8}`` will produce an output of ``[4, 8, 0, 0]``.\n ", + "attributes": [ + { + "name": "int64_vocabulary", + "type": "int64[]", + "required": false, + "description": "An integer vocabulary array.
One and only one of the vocabularies must be defined." + }, + { + "name": "string_vocabulary", + "type": "string[]", + "required": false, + "description": "A string vocabulary array.
One and only one of the vocabularies must be defined." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "A dictionary." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "A 1-D tensor holding values from the input dictionary." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input must be a map from strings or integers to either strings or a numeric type. The key and value types cannot be the same.", + "type_param_str": "T1", + "allowed_type_strs": [ + "map(string, int64)", + "map(int64, string)", + "map(int64, float)", + "map(int64, double)", + "map(string, float)", + "map(string, double)" + ] + }, + { + "description": "The output will be a tensor of the value type of the input map. It's shape will be [1,C], where C is the length of the input dictionary.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int64)", + "tensor(float)", + "tensor(double)", + "tensor(string)" + ] + } + ] + }, + { + "name": "Div", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Performs element-wise binary division (with limited broadcast support).\n\nIf necessary the right-hand-side argument will be broadcasted to match the\nshape of left-hand-side argument. When broadcasting is specified, the second\ntensor can either be of element size 1 (including a scalar tensor and any\ntensor with rank equal to or smaller than the first tensor), or having its\nshape as a contiguous subset of the first tensor's shape. The starting of the\nmutually equal shape is specified by the argument \"axis\", and if it is not set,\nsuffix matching is assumed. 1-dim expansion doesn't work yet.\n\nFor example, the following tensor shapes are supported (with broadcast=1):\n\n shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (5,)\n shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)\n shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1\n shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0\n\nAttribute `broadcast=1` needs to be passed to enable broadcasting.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions. See doc for details." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Pass 1 to enable broadcasting" + }, + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand, should share the type with the second operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same dimensions and type as A" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "div", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([3, 4]).astype(np.float32)\ny = np.array([1, 2]).astype(np.float32)\nz = x / y # expected output [3., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(3, 4, 5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div')\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_uint8')" + }, + { + "summary": "div_broadcast", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_bcast')" + } + ] + }, + { + "name": "Div", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Performs element-wise binary division (with limited broadcast support).\n\nIf necessary the right-hand-side argument will be broadcasted to match the\nshape of left-hand-side argument. When broadcasting is specified, the second\ntensor can either be of element size 1 (including a scalar tensor and any\ntensor with rank equal to or smaller than the first tensor), or having its\nshape as a contiguous subset of the first tensor's shape. The starting of the\nmutually equal shape is specified by the argument \"axis\", and if it is not set,\nsuffix matching is assumed. 1-dim expansion doesn't work yet.\n\nFor example, the following tensor shapes are supported (with broadcast=1):\n\n shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (5,)\n shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)\n shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1\n shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0\n\nAttribute `broadcast=1` needs to be passed to enable broadcasting.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions. See doc for details." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Pass 1 to enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand, should share the type with the second operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same dimensions and type as A" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "div", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([3, 4]).astype(np.float32)\ny = np.array([1, 2]).astype(np.float32)\nz = x / y # expected output [3., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(3, 4, 5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div')\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_uint8')" + }, + { + "summary": "div_broadcast", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_bcast')" + } + ] + }, + { + "name": "Div", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Performs element-wise binary division (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "div", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([3, 4]).astype(np.float32)\ny = np.array([1, 2]).astype(np.float32)\nz = x / y # expected output [3., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(3, 4, 5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div')\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_uint8')" + }, + { + "summary": "div_broadcast", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_bcast')" + } + ] + }, + { + "name": "Div", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Performs element-wise binary division (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "div", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([3, 4]).astype(np.float32)\ny = np.array([1, 2]).astype(np.float32)\nz = x / y # expected output [3., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(3, 4, 5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div')\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_uint8')" + }, + { + "summary": "div_broadcast", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_bcast')" + } + ] + }, + { + "name": "Div", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Performs element-wise binary division (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n\n(Opset 14 change): Extend supported types to include uint8, int8, uint16, and int16.\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "div", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([3, 4]).astype(np.float32)\ny = np.array([1, 2]).astype(np.float32)\nz = x / y # expected output [3., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(3, 4, 5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div')\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_uint8')" + }, + { + "summary": "div_broadcast", + "code": "node = onnx.helper.make_node(\n 'Div',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.rand(5).astype(np.float32) + 1.0\nz = x / y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_div_bcast')" + } + ] + }, + { + "name": "Dropout", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Dropout takes one input data (Tensor) and produces two Tensor outputs,\noutput (Tensor) and mask (Tensor). Depending on whether it is in\ntest mode or not, the output Y will either be a random dropout, or a simple\ncopy of the input. Note that our implementation of Dropout does scaling in\nthe training phase, so during testing nothing needs to be done.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + }, + { + "name": "is_test", + "type": "int64", + "required": false, + "description": "(int, default 0) if nonzero, run dropout in test mode where the output is simply Y = X." + }, + { + "name": "ratio", + "type": "float32", + "required": false, + "default": 0.5, + "description": "(float, default 0.5) the ratio of random dropout" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "The input data as Tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output." + }, + { + "name": "mask", + "type": "T", + "option": "optional", + "description": "The output mask. If is_test is nonzero, this output is not filled." + } + ], + "min_output": 1, + "max_output": 2, + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x)\nexpect(node, inputs=[x], outputs=[y], name='test_dropout_default')" + }, + { + "summary": "default_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, return_mask=True)\nexpect(node, inputs=[x], outputs=[y, z], name='test_dropout_default_mask')" + }, + { + "summary": "default_mask_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, r, return_mask=True)\nexpect(node, inputs=[x, r], outputs=[y, z], name='test_dropout_default_mask_ratio')" + }, + { + "summary": "default_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_default_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "default_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x, r)\nexpect(node, inputs=[x, r], outputs=[y], name='test_dropout_default_ratio')" + }, + { + "summary": "random_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n ratio=.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_random_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "training", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout')" + }, + { + "summary": "training_default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_default')" + }, + { + "summary": "training_default_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_default_mask')" + }, + { + "summary": "training_default_zero_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_zero_ratio')" + }, + { + "summary": "training_default_zero_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_zero_ratio_mask')" + }, + { + "summary": "training_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_mask')" + } + ], + "category": "Dropout" + }, + { + "name": "Dropout", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Dropout takes one input data (Tensor) and produces two Tensor outputs,\noutput (Tensor) and mask (Tensor). Depending on whether it is in\ntest mode or not, the output Y will either be a random dropout, or a simple\ncopy of the input. Note that our implementation of Dropout does scaling in\nthe training phase, so during testing nothing needs to be done.\n", + "attributes": [ + { + "name": "is_test", + "type": "int64", + "required": false, + "description": "(int, default 0) if nonzero, run dropout in test mode where the output is simply Y = X." + }, + { + "name": "ratio", + "type": "float32", + "required": false, + "default": 0.5, + "description": "(float, default 0.5) the ratio of random dropout" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "The input data as Tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output." + }, + { + "name": "mask", + "type": "T", + "option": "optional", + "description": "The output mask. If is_test is nonzero, this output is not filled." + } + ], + "min_output": 1, + "max_output": 2, + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x)\nexpect(node, inputs=[x], outputs=[y], name='test_dropout_default')" + }, + { + "summary": "default_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, return_mask=True)\nexpect(node, inputs=[x], outputs=[y, z], name='test_dropout_default_mask')" + }, + { + "summary": "default_mask_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, r, return_mask=True)\nexpect(node, inputs=[x, r], outputs=[y, z], name='test_dropout_default_mask_ratio')" + }, + { + "summary": "default_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_default_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "default_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x, r)\nexpect(node, inputs=[x, r], outputs=[y], name='test_dropout_default_ratio')" + }, + { + "summary": "random_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n ratio=.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_random_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "training", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout')" + }, + { + "summary": "training_default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_default')" + }, + { + "summary": "training_default_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_default_mask')" + }, + { + "summary": "training_default_zero_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_zero_ratio')" + }, + { + "summary": "training_default_zero_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_zero_ratio_mask')" + }, + { + "summary": "training_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_mask')" + } + ], + "category": "Dropout" + }, + { + "name": "Dropout", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Dropout takes one input data (Tensor) and produces two Tensor outputs,\noutput (Tensor) and mask (Tensor). Depending on whether it is in\ntest mode or not, the output Y will either be a random dropout, or a simple\ncopy of the input. Note that our implementation of Dropout does scaling in\nthe training phase, so during testing nothing needs to be done.\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "ratio", + "type": "float32", + "required": false, + "default": 0.5, + "description": "The ratio of random dropout" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "The input data as Tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output." + }, + { + "name": "mask", + "type": "T", + "option": "optional", + "description": "The output mask." + } + ], + "min_output": 1, + "max_output": 2, + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x)\nexpect(node, inputs=[x], outputs=[y], name='test_dropout_default')" + }, + { + "summary": "default_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, return_mask=True)\nexpect(node, inputs=[x], outputs=[y, z], name='test_dropout_default_mask')" + }, + { + "summary": "default_mask_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, r, return_mask=True)\nexpect(node, inputs=[x, r], outputs=[y, z], name='test_dropout_default_mask_ratio')" + }, + { + "summary": "default_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_default_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "default_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x, r)\nexpect(node, inputs=[x, r], outputs=[y], name='test_dropout_default_ratio')" + }, + { + "summary": "random_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n ratio=.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_random_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "training", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout')" + }, + { + "summary": "training_default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_default')" + }, + { + "summary": "training_default_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_default_mask')" + }, + { + "summary": "training_default_zero_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_zero_ratio')" + }, + { + "summary": "training_default_zero_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_zero_ratio_mask')" + }, + { + "summary": "training_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_mask')" + } + ], + "category": "Dropout" + }, + { + "name": "Dropout", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Dropout takes one input floating tensor and produces two tensor outputs,\noutput (floating tensor) and mask (`Tensor`). Depending on whether it is\nin test mode or not, the output Y will either be a random dropout, or a simple\ncopy of the input. Note that our implementation of Dropout does scaling in\nthe training phase, so during testing nothing needs to be done.\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "ratio", + "type": "float32", + "required": false, + "default": 0.5, + "description": "The ratio of random dropout" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "The input data as Tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output." + }, + { + "name": "mask", + "type": "T1", + "option": "optional", + "description": "The output mask." + } + ], + "min_output": 1, + "max_output": 2, + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output mask types to boolean tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x)\nexpect(node, inputs=[x], outputs=[y], name='test_dropout_default')" + }, + { + "summary": "default_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, return_mask=True)\nexpect(node, inputs=[x], outputs=[y, z], name='test_dropout_default_mask')" + }, + { + "summary": "default_mask_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, r, return_mask=True)\nexpect(node, inputs=[x, r], outputs=[y, z], name='test_dropout_default_mask_ratio')" + }, + { + "summary": "default_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_default_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "default_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x, r)\nexpect(node, inputs=[x, r], outputs=[y], name='test_dropout_default_ratio')" + }, + { + "summary": "random_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n ratio=.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_random_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "training", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout')" + }, + { + "summary": "training_default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_default')" + }, + { + "summary": "training_default_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_default_mask')" + }, + { + "summary": "training_default_zero_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_zero_ratio')" + }, + { + "summary": "training_default_zero_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_zero_ratio_mask')" + }, + { + "summary": "training_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_mask')" + } + ], + "category": "Dropout" + }, + { + "name": "Dropout", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Dropout takes an input floating-point tensor, an optional input ratio (floating-point scalar) and an optional input training_mode (boolean scalar). It produces two tensor outputs,\noutput (floating-point tensor) and mask (optional `Tensor`). If `training_mode` is true then the output Y will be a random dropout;\nNote that this Dropout scales the masked input data by the following equation, so to convert the trained model into inference mode,\nthe user can simply not pass `training_mode` input or set it to false.\n```\noutput = scale * data * mask,\n```\nwhere\n```\nscale = 1. / (1. - ratio).\n```\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "seed", + "type": "int64", + "required": false, + "description": "(Optional) Seed to the random generator, if not specified we will auto generate one." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "The input data as Tensor." + }, + { + "name": "ratio", + "type": "T1", + "option": "optional", + "description": "The ratio of random dropout, with value in [0, 1). If this input was not set, or if it was set to 0, the output would be a simple copy of the input. If it's non-zero, output will be a random dropout of the scaled input, which is typically the case during training. It is an optional value, if not specified it will default to 0.5." + }, + { + "name": "training_mode", + "type": "T2", + "option": "optional", + "description": "If set to true then it indicates dropout is being used for training. It is an optional value hence unless specified explicitly, it is false. If it is false, ratio is ignored and the operation mimics inference mode where nothing will be dropped from the input data and if mask is requested as output it will contain all ones." + } + ], + "min_input": 1, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output." + }, + { + "name": "mask", + "type": "T2", + "option": "optional", + "description": "The output mask." + } + ], + "min_output": 1, + "max_output": 2, + "inputs_range": "1 - 3", + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain input 'ratio' types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output 'mask' types to boolean tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x)\nexpect(node, inputs=[x], outputs=[y], name='test_dropout_default')" + }, + { + "summary": "default_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, return_mask=True)\nexpect(node, inputs=[x], outputs=[y, z], name='test_dropout_default_mask')" + }, + { + "summary": "default_mask_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, r, return_mask=True)\nexpect(node, inputs=[x, r], outputs=[y, z], name='test_dropout_default_mask_ratio')" + }, + { + "summary": "default_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_default_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "default_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x, r)\nexpect(node, inputs=[x, r], outputs=[y], name='test_dropout_default_ratio')" + }, + { + "summary": "random_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n ratio=.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_random_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "training", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout')" + }, + { + "summary": "training_default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_default')" + }, + { + "summary": "training_default_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_default_mask')" + }, + { + "summary": "training_default_zero_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_zero_ratio')" + }, + { + "summary": "training_default_zero_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_zero_ratio_mask')" + }, + { + "summary": "training_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_mask')" + } + ], + "category": "Dropout" + }, + { + "name": "Dropout", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Dropout takes an input floating-point tensor, an optional input ratio (floating-point scalar) and an optional input training_mode (boolean scalar). It produces two tensor outputs,\noutput (floating-point tensor) and mask (optional `Tensor`). If `training_mode` is true then the output Y will be a random dropout;\nNote that this Dropout scales the masked input data by the following equation, so to convert the trained model into inference mode,\nthe user can simply not pass `training_mode` input or set it to false.\n```\noutput = scale * data * mask,\n```\nwhere\n```\nscale = 1. / (1. - ratio).\n```\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "seed", + "type": "int64", + "required": false, + "description": "(Optional) Seed to the random generator, if not specified we will auto generate one." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "The input data as Tensor." + }, + { + "name": "ratio", + "type": "T1", + "option": "optional", + "description": "The ratio of random dropout, with value in [0, 1). If this input was not set, or if it was set to 0, the output would be a simple copy of the input. If it's non-zero, output will be a random dropout of the scaled input, which is typically the case during training. It is an optional value, if not specified it will default to 0.5." + }, + { + "name": "training_mode", + "type": "T2", + "option": "optional", + "description": "If set to true then it indicates dropout is being used for training. It is an optional value hence unless specified explicitly, it is false. If it is false, ratio is ignored and the operation mimics inference mode where nothing will be dropped from the input data and if mask is requested as output it will contain all ones." + } + ], + "min_input": 1, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output." + }, + { + "name": "mask", + "type": "T2", + "option": "optional", + "description": "The output mask." + } + ], + "min_output": 1, + "max_output": 2, + "inputs_range": "1 - 3", + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain input 'ratio' types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output 'mask' types to boolean tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x)\nexpect(node, inputs=[x], outputs=[y], name='test_dropout_default')" + }, + { + "summary": "default_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, return_mask=True)\nexpect(node, inputs=[x], outputs=[y, z], name='test_dropout_default_mask')" + }, + { + "summary": "default_mask_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny, z = dropout(x, r, return_mask=True)\nexpect(node, inputs=[x, r], outputs=[y, z], name='test_dropout_default_mask_ratio')" + }, + { + "summary": "default_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_default_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "default_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r'],\n outputs=['y'],\n seed=seed\n)\n\nr = np.float32(0.1)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = dropout(x, r)\nexpect(node, inputs=[x, r], outputs=[y], name='test_dropout_default_ratio')" + }, + { + "summary": "random_old", + "code": "node = onnx.helper.make_node(\n 'Dropout',\n inputs=['x'],\n outputs=['y'],\n ratio=.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(node, inputs=[x], outputs=[y],\n name='test_dropout_random_old', opset_imports=[helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "training", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout')" + }, + { + "summary": "training_default", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_default')" + }, + { + "summary": "training_default_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.5)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_default_mask')" + }, + { + "summary": "training_default_zero_ratio", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny = dropout(x, r, training_mode=t)\nexpect(node, inputs=[x, r, t], outputs=[y], name='test_training_dropout_zero_ratio')" + }, + { + "summary": "training_default_zero_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.0)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_zero_ratio_mask')" + }, + { + "summary": "training_ratio_mask", + "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n 'Dropout',\n inputs=['x', 'r', 't'],\n outputs=['y', 'z'],\n seed=seed\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nr = np.float32(0.75)\nt = np.bool_(True)\ny, z = dropout(x, r, training_mode=t, return_mask=True)\nexpect(node, inputs=[x, r, t], outputs=[y, z], name='test_training_dropout_mask')" + } + ], + "category": "Dropout" + }, + { + "name": "DynamicQuantizeLinear", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "A Function to fuse calculation for Scale, Zero Point and FP32->8Bit convertion of FP32 Input data.\nOutputs Scale, ZeroPoint and Quantized Input for a given FP32 Input.\nScale is calculated as:\n```\n y_scale = (max(x) - min(x))/(qmax - qmin)\n * where qmax and qmin are max and min values for quantization range .i.e [0, 255] in case of uint8\n * data range is adjusted to include 0.\n```\nZero point is calculated as:\n```\nintermediate_zero_point = qmin - min(x)/y_scale\ny_zero_point = cast(round(saturate(itermediate_zero_point)))\n* where qmax and qmin are max and min values for quantization range .i.e [0, 255] in case of uint8\n* for saturation, it saturates to [0, 255] if it's uint8, or [-127, 127] if it's int8. Right now only uint8 is supported.\n* rounding to nearest ties to even.\n```\nData quantization formula is:\n```\ny = saturate (round (x / y_scale) + y_zero_point)\n* for saturation, it saturates to [0, 255] if it's uint8, or [-127, 127] if it's int8. Right now only uint8 is supported.\n* rounding to nearest ties to even.\n```\n", + "inputs": [ + { + "name": "x", + "type": "T1", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "y", + "type": "T2", + "description": "Quantized output tensor" + }, + { + "name": "y_scale", + "type": "tensor(float)", + "description": "Output scale. It's a scalar, which means a per-tensor/layer quantization." + }, + { + "name": "y_zero_point", + "type": "T2", + "description": "Output zero point. It's a scalar, which means a per-tensor/layer quantization." + } + ], + "min_output": 3, + "max_output": 3, + "type_constraints": [ + { + "description": "Constrain 'x' to float tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)" + ] + }, + { + "description": "Constrain 'y_zero_point' and 'y' to 8-bit unsigned integer tensor.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(uint8)" + ] + } + ], + "examples": [ + { + "summary": "dynamicquantizelinear", + "code": "node = onnx.helper.make_node('DynamicQuantizeLinear',\n inputs=['x'],\n outputs=['y', 'y_scale', 'y_zero_point'],\n)\n\n# expected scale 0.0196078438 and zero point 153\nX = np.array([0, 2, -3, -2.5, 1.34, 0.5]).astype(np.float32)\nx_min = np.minimum(0, np.min(X))\nx_max = np.maximum(0, np.max(X))\nY_Scale = np.float32((x_max - x_min) / (255 - 0)) # uint8 -> [0, 255]\nY_ZeroPoint = np.clip(round((0 - x_min) / Y_Scale), 0, 255).astype(np.uint8)\nY = np.clip(np.round(X / Y_Scale) + Y_ZeroPoint, 0, 255).astype(np.uint8)\n\nexpect(node, inputs=[X], outputs=[Y, Y_Scale, Y_ZeroPoint],\n name='test_dynamicquantizelinear')\n\n# expected scale 0.0156862754 and zero point 255\nX = np.array([-1.0, -2.1, -1.3, -2.5, -3.34, -4.0]).astype(np.float32)\nx_min = np.minimum(0, np.min(X))\nx_max = np.maximum(0, np.max(X))\nY_Scale = np.float32((x_max - x_min) / (255 - 0)) # uint8 -> [0, 255]\nY_ZeroPoint = np.clip(round((0 - x_min) / Y_Scale), 0, 255).astype(np.uint8)\nY = np.clip(np.round(X / Y_Scale) + Y_ZeroPoint, 0, 255).astype(np.uint8)\n\nexpect(node, inputs=[X], outputs=[Y, Y_Scale, Y_ZeroPoint],\n name='test_dynamicquantizelinear_max_adjusted')\n\nX = np.array([1, 2.1, 1.3, 2.5,\n 3.34, 4.0, 1.5, 2.6,\n 3.9, 4.0, 3.0, 2.345]).astype(np.float32).reshape((3, 4))\n\n# expected scale 0.0156862754 and zero point 0\nx_min = np.minimum(0, np.min(X))\nx_max = np.maximum(0, np.max(X))\nY_Scale = np.float32((x_max - x_min) / (255 - 0)) # uint8 -> [0, 255]\nY_ZeroPoint = np.clip(round((0 - x_min) / Y_Scale), 0, 255).astype(np.uint8)\nY = np.clip(np.round(X / Y_Scale) + Y_ZeroPoint, 0, 255).astype(np.uint8)\n\nexpect(node, inputs=[X], outputs=[Y, Y_Scale, Y_ZeroPoint],\n name='test_dynamicquantizelinear_min_adjusted')" + } + ] + }, + { + "name": "Einsum", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "An einsum of the form ```term1, term2 -> output-term``` produces an output tensor using the following equation\n\n```output[output-term] = reduce-sum( input1[term1] * input2[term] )```\n\nwhere the reduce-sum performs a summation over all the indices occurring in the input terms (term1, term2)\nthat do not occur in the output-term.\n\nThe Einsum operator evaluates algebraic tensor operations on a sequence of tensors, using the Einstein summation\nconvention. The equation string contains a comma-separated sequence of lower case letters. Each term corresponds to\nan operand tensor, and the characters within the terms correspond to operands dimensions.\n\nThis sequence may be followed by \"->\" to separate the left and right hand side of the equation.\nIf the equation contains \"->\" followed by the right-hand side, the explicit (not classical) form of the Einstein\nsummation is performed, and the right-hand side indices indicate output tensor dimensions. In other cases,\noutput indices are (implicitly) set to the alphabetically sorted sequence of indices appearing exactly once in the\nequation.\n\nWhen a dimension character is repeated in the left-hand side, it represents summation along the dimension.\n\nThe equation may contain ellipsis (\"...\") to enable broadcasting. Ellipsis must indicate a fixed number of dimensions.\nSpecifically, every occurrence of ellipsis in the equation must represent the same number of dimensions.\nThe right-hand side may contain exactly one ellipsis. In implicit mode, the ellipsis dimensions are set to the\nbeginning of the output. The equation string may contain space (U+0020) character.\n", + "attributes": [ + { + "name": "equation", + "type": "string", + "required": true, + "description": "Einsum expression string." + } + ], + "inputs": [ + { + "name": "Inputs", + "type": "T", + "list": true, + "description": "Operands" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "Output", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to all numerical tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "einsum_batch_diagonal", + "code": "Eqn = '...ii ->...i'\nnode = onnx.helper.make_node(\n 'Einsum',\n inputs=['x'],\n outputs=['y'],\n equation=Eqn\n)\n\nX = np.random.randn(3, 5, 5)\nZ = einsum_reference_implementation(Eqn, (X,))\n\nexpect(node, inputs=[X], outputs=[Z], name='test_einsum_batch_diagonal')" + }, + { + "summary": "einsum_batch_matmul", + "code": "Eqn = 'bij, bjk -> bik'\nnode = onnx.helper.make_node(\n 'Einsum',\n inputs=['x', 'y'],\n outputs=['z'],\n equation=Eqn\n)\n\nX = np.random.randn(5, 2, 3)\nY = np.random.randn(5, 3, 4)\nZ = einsum_reference_implementation(Eqn, (X, Y))\n\nexpect(node, inputs=[X, Y], outputs=[Z], name='test_einsum_batch_matmul')" + }, + { + "summary": "einsum_inner_prod", + "code": "Eqn = 'i,i'\nnode = onnx.helper.make_node(\n 'Einsum',\n inputs=['x', 'y'],\n outputs=['z'],\n equation=Eqn\n)\n\nX = np.random.randn(5)\nY = np.random.randn(5)\nZ = einsum_reference_implementation(Eqn, (X, Y))\n\nexpect(node, inputs=[X, Y], outputs=[Z], name='test_einsum_inner_prod')" + }, + { + "summary": "einsum_sum", + "code": "Eqn = 'ij->i'\nnode = onnx.helper.make_node(\n 'Einsum',\n inputs=['x'],\n outputs=['y'],\n equation=Eqn\n)\n\nX = np.random.randn(3, 4)\nZ = einsum_reference_implementation(Eqn, (X,))\n\nexpect(node, inputs=[X], outputs=[Z], name='test_einsum_sum')" + }, + { + "summary": "einsum_transpose", + "code": "Eqn = 'ij->ji'\nnode = onnx.helper.make_node(\n 'Einsum',\n inputs=['x'],\n outputs=['y'],\n equation=Eqn\n)\n\nX = np.random.randn(3, 4)\nY = einsum_reference_implementation(Eqn, (X,))\n\nexpect(node, inputs=[X], outputs=[Y], name='test_einsum_transpose')" + } + ] + }, + { + "name": "Elu", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Elu takes one input data (Tensor) and produces one output data\n(Tensor) where the function `f(x) = alpha * (exp(x) - 1.) for x <\n0`, `f(x) = x for x >= 0`., is applied to the tensor elementwise.\n\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Coefficient of ELU default to 1.0." + }, + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "1D input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "1D input tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "elu", + "code": "node = onnx.helper.make_node(\n 'Elu',\n inputs=['x'],\n outputs=['y'],\n alpha=2.0\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-1.2642411, 0., 1.]\ny = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0\nexpect(node, inputs=[x], outputs=[y],\n name='test_elu_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0\nexpect(node, inputs=[x], outputs=[y],\n name='test_elu')" + }, + { + "summary": "elu_default", + "code": "default_alpha = 1.0\nnode = onnx.helper.make_node(\n 'Elu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * default_alpha\nexpect(node, inputs=[x], outputs=[y],\n name='test_elu_default')" + } + ], + "category": "Activation" + }, + { + "name": "Elu", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Elu takes one input data (Tensor) and produces one output data\n(Tensor) where the function `f(x) = alpha * (exp(x) - 1.) for x <\n0`, `f(x) = x for x >= 0`., is applied to the tensor elementwise.\n\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Coefficient of ELU." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "1D input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "1D output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "elu", + "code": "node = onnx.helper.make_node(\n 'Elu',\n inputs=['x'],\n outputs=['y'],\n alpha=2.0\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-1.2642411, 0., 1.]\ny = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0\nexpect(node, inputs=[x], outputs=[y],\n name='test_elu_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0\nexpect(node, inputs=[x], outputs=[y],\n name='test_elu')" + }, + { + "summary": "elu_default", + "code": "default_alpha = 1.0\nnode = onnx.helper.make_node(\n 'Elu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + (np.exp(np.clip(x, -np.inf, 0)) - 1) * default_alpha\nexpect(node, inputs=[x], outputs=[y],\n name='test_elu_default')" + } + ], + "category": "Activation" + }, + { + "name": "Equal", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `equal` logical operation\nelementwise on the input tensors `A` and `B`.\n\nIf broadcasting is enabled, the right-hand-side argument will be broadcasted\nto match the shape of left-hand-side argument. See the doc of `Add` for a\ndetailed description of the broadcasting rules.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Left input tensor for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Right input tensor for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to integral tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)", + "tensor(int32)", + "tensor(int64)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "equal", + "code": "node = onnx.helper.make_node(\n 'Equal',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_equal')" + }, + { + "summary": "equal_broadcast", + "code": "node = onnx.helper.make_node(\n 'Equal',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\ny = (np.random.randn(5) * 10).astype(np.int32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Equal", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `equal` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to integral tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)", + "tensor(int32)", + "tensor(int64)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "equal", + "code": "node = onnx.helper.make_node(\n 'Equal',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_equal')" + }, + { + "summary": "equal_broadcast", + "code": "node = onnx.helper.make_node(\n 'Equal',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\ny = (np.random.randn(5) * 10).astype(np.int32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Equal", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `equal` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "equal", + "code": "node = onnx.helper.make_node(\n 'Equal',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_equal')" + }, + { + "summary": "equal_broadcast", + "code": "node = onnx.helper.make_node(\n 'Equal',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\ny = (np.random.randn(5) * 10).astype(np.int32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Equal", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `equal` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "equal", + "code": "node = onnx.helper.make_node(\n 'Equal',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_equal')" + }, + { + "summary": "equal_broadcast", + "code": "node = onnx.helper.make_node(\n 'Equal',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int32)\ny = (np.random.randn(5) * 10).astype(np.int32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Erf", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Computes the error function of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The error function of the input tensor computed element-wise. It has the same shape and type of the input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "erf", + "code": "node = onnx.helper.make_node(\n 'Erf',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\ny = np.vectorize(math.erf)(x).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_erf')" + } + ] + }, + { + "name": "Erf", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the error function of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The error function of the input tensor computed element-wise. It has the same shape and type of the input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "erf", + "code": "node = onnx.helper.make_node(\n 'Erf',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\ny = np.vectorize(math.erf)(x).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_erf')" + } + ] + }, + { + "name": "Exp", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Calculates the exponential of the given input tensor, element-wise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The exponential of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "exp", + "code": "node = onnx.helper.make_node(\n 'Exp',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.exp(x) # expected output [0.36787945, 1., 2.71828175]\nexpect(node, inputs=[x], outputs=[y],\n name='test_exp_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.exp(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_exp')" + } + ] + }, + { + "name": "Exp", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Calculates the exponential of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The exponential of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "exp", + "code": "node = onnx.helper.make_node(\n 'Exp',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.exp(x) # expected output [0.36787945, 1., 2.71828175]\nexpect(node, inputs=[x], outputs=[y],\n name='test_exp_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.exp(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_exp')" + } + ] + }, + { + "name": "Exp", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Calculates the exponential of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The exponential of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "exp", + "code": "node = onnx.helper.make_node(\n 'Exp',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.exp(x) # expected output [0.36787945, 1., 2.71828175]\nexpect(node, inputs=[x], outputs=[y],\n name='test_exp_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.exp(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_exp')" + } + ] + }, + { + "name": "Expand", + "module": "ai.onnx", + "version": 8, + "support_level": "common", + "description": "Broadcast the input tensor following the given shape and the broadcast rule.\nThe broadcast rule is similar to numpy.array(input) * numpy.ones(shape):\nDimensions are right alignment;\nTwo corresponding dimensions must have the same value, or one of them is equal to 1.\nAlso, this operator is similar to numpy.broadcast_to(input, shape),\nbut the major difference is numpy.broadcast_to() does not allow shape to be smaller than input.size().\nIt is possible that the output.shape is not equal to shape, when some dimensions in shape is equal to 1,\nor the shape.ndim < input.shape.ndim.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + }, + { + "name": "shape", + "type": "tensor(int64)", + "description": "A 1-D tensor indicates the shape you want to expand to, following the broadcast rule" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "dim_changed", + "code": "node = onnx.helper.make_node(\n 'Expand',\n inputs=['data', 'new_shape'],\n outputs=['expanded'],\n)\nshape = [3, 1]\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[1.], [2.], [3.]]\nnew_shape = [2, 1, 6]\nexpanded = data * np.ones(new_shape, dtype=np.float32)\n#print(expanded)\n#[[[1., 1., 1., 1., 1., 1.],\n# [2., 2., 2., 2., 2., 2.],\n# [3., 3., 3., 3., 3., 3.]],\n#\n# [[1., 1., 1., 1., 1., 1.],\n# [2., 2., 2., 2., 2., 2.],\n# [3., 3., 3., 3., 3., 3.]]]\nnew_shape = np.array(new_shape, dtype=np.int64)\nexpect(node, inputs=[data, new_shape], outputs=[expanded],\n name='test_expand_dim_changed')" + }, + { + "summary": "dim_unchanged", + "code": "node = onnx.helper.make_node(\n 'Expand',\n inputs=['data', 'new_shape'],\n outputs=['expanded'],\n)\nshape = [3, 1]\nnew_shape = [3, 4]\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[1.], [2.], [3.]]\nexpanded = np.tile(data, 4)\n#print(expanded)\n#[[1., 1., 1., 1.],\n# [2., 2., 2., 2.],\n# [3., 3., 3., 3.]]\nnew_shape = np.array(new_shape, dtype=np.int64)\nexpect(node, inputs=[data, new_shape], outputs=[expanded],\n name='test_expand_dim_unchanged')" + } + ] + }, + { + "name": "Expand", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Broadcast the input tensor following the given shape and the broadcast rule.\nThe broadcast rule is similar to numpy.array(input) * numpy.ones(shape):\nDimensions are right alignment;\nTwo corresponding dimensions must have the same value, or one of them is equal to 1.\nAlso, this operator is similar to numpy.broadcast_to(input, shape),\nbut the major difference is numpy.broadcast_to() does not allow shape to be smaller than input.size().\nIt is possible that the output.shape is not equal to shape, when some dimensions in shape is equal to 1,\nor the shape.ndim < input.shape.ndim.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + }, + { + "name": "shape", + "type": "tensor(int64)", + "description": "A 1-D tensor indicates the shape you want to expand to, following the broadcast rule" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "dim_changed", + "code": "node = onnx.helper.make_node(\n 'Expand',\n inputs=['data', 'new_shape'],\n outputs=['expanded'],\n)\nshape = [3, 1]\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[1.], [2.], [3.]]\nnew_shape = [2, 1, 6]\nexpanded = data * np.ones(new_shape, dtype=np.float32)\n#print(expanded)\n#[[[1., 1., 1., 1., 1., 1.],\n# [2., 2., 2., 2., 2., 2.],\n# [3., 3., 3., 3., 3., 3.]],\n#\n# [[1., 1., 1., 1., 1., 1.],\n# [2., 2., 2., 2., 2., 2.],\n# [3., 3., 3., 3., 3., 3.]]]\nnew_shape = np.array(new_shape, dtype=np.int64)\nexpect(node, inputs=[data, new_shape], outputs=[expanded],\n name='test_expand_dim_changed')" + }, + { + "summary": "dim_unchanged", + "code": "node = onnx.helper.make_node(\n 'Expand',\n inputs=['data', 'new_shape'],\n outputs=['expanded'],\n)\nshape = [3, 1]\nnew_shape = [3, 4]\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[1.], [2.], [3.]]\nexpanded = np.tile(data, 4)\n#print(expanded)\n#[[1., 1., 1., 1.],\n# [2., 2., 2., 2.],\n# [3., 3., 3., 3.]]\nnew_shape = np.array(new_shape, dtype=np.int64)\nexpect(node, inputs=[data, new_shape], outputs=[expanded],\n name='test_expand_dim_unchanged')" + } + ] + }, + { + "name": "EyeLike", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Generate a 2D tensor (matrix) with ones on the diagonal and zeros everywhere else. Only 2D\ntensors are supported, i.e. input T1 must be of rank 2. The shape of the output tensor is the\nsame as the input tensor. The data type can be specified by the 'dtype' argument. If\n'dtype' is not specified, then the type of input tensor is used. By default, the main diagonal\nis populated with ones, but attribute 'k' can be used to populate upper or lower diagonals.\nThe 'dtype' argument must be one of the data types specified in the 'DataType' enum field in the\nTensorProto message and be valid as an output type.\n", + "attributes": [ + { + "name": "dtype", + "type": "DataType", + "required": false, + "description": "(Optional) The data type for the elements of the output tensor. If not specified,the data type of the input tensor T1 is used. If input tensor T1 is also notspecified, then type defaults to 'float'." + }, + { + "name": "k", + "type": "int64", + "required": false, + "description": "(Optional) Index of the diagonal to be populated with ones. Default is 0. If T2 is the output, this op sets T2[i, i+k] = 1. k = 0 populates the main diagonal, k > 0 populates an upper diagonal, and k < 0 populates a lower diagonal." + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "2D input tensor to copy shape, and optionally, type information from." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor, same shape as input tensor T1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types. Strings and complex are not supported.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)" + ] + }, + { + "description": "Constrain output types. Strings and complex are not supported.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "populate_off_main_diagonal", + "code": "shape = (4, 5)\noff_diagonal_offset = 1\nnode = onnx.helper.make_node(\n 'EyeLike',\n inputs=['x'],\n outputs=['y'],\n k=off_diagonal_offset,\n dtype=onnx.TensorProto.FLOAT,\n)\n\nx = np.random.randint(0, 100, size=shape, dtype=np.int32)\ny = np.eye(shape[0], shape[1], k=off_diagonal_offset, dtype=np.float32)\nexpect(node, inputs=[x], outputs=[y], name='test_eyelike_populate_off_main_diagonal')" + }, + { + "summary": "with_dtype", + "code": "shape = (3, 4)\nnode = onnx.helper.make_node(\n 'EyeLike',\n inputs=['x'],\n outputs=['y'],\n dtype=onnx.TensorProto.DOUBLE,\n)\n\nx = np.random.randint(0, 100, size=shape, dtype=np.int32)\ny = np.eye(shape[0], shape[1], dtype=np.float64)\nexpect(node, inputs=[x], outputs=[y], name='test_eyelike_with_dtype')" + }, + { + "summary": "without_dtype", + "code": "shape = (4, 4)\nnode = onnx.helper.make_node(\n 'EyeLike',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.random.randint(0, 100, size=shape, dtype=np.int32)\ny = np.eye(shape[0], shape[1], dtype=np.int32)\nexpect(node, inputs=[x], outputs=[y], name='test_eyelike_without_dtype')" + } + ] + }, + { + "name": "FeatureVectorizer", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Concatenates input tensors into one continuous output.
\n All input shapes are 2-D and are concatenated along the second dimention. 1-D tensors are treated as [1,C].\n Inputs are copied to the output maintaining the order of the input arguments.
\n All inputs must be integers or floats, while the output will be all floating point values.\n", + "attributes": [ + { + "name": "inputdimensions", + "type": "int64[]", + "required": false, + "description": "The size of each input in the input list" + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "list": true, + "description": "An ordered collection of tensors, all with the same element type." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "Y", + "type": "tensor(float)", + "description": "The output array, elements ordered as the inputs." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "The input type must be a tensor of a numeric type.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)", + "tensor(float)", + "tensor(double)" + ] + } + ] + }, + { + "name": "Flatten", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Flattens the input tensor into a 2D matrix. If input tensor has shape\n(d_0, d_1, ... d_n) then the output will have shape\n(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [0, R], where R is the rank of the input tensor. When axis = 0, the shape of the output tensor is (1, (d_0 X d_1 ... d_n), where the shape of the input tensor is (d_0, d_1, ... d_n). " + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "A tensor of rank >= axis." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "flatten", + "code": "shape = (2, 3, 4, 5)\na = np.random.random_sample(shape).astype(np.float32)\n\nfor i in range(len(shape)):\n node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'],\n axis=i,\n )\n\n new_shape = (1, -1) if i == 0 else (np.prod(shape[0:i]).astype(int), -1)\n b = np.reshape(a, new_shape)\n expect(node, inputs=[a], outputs=[b],\n name='test_flatten_axis' + str(i))" + }, + { + "summary": "flatten_negative_axis", + "code": "shape = (2, 3, 4, 5)\na = np.random.random_sample(shape).astype(np.float32)\n\nfor i in range(-len(shape), 0):\n node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'],\n axis=i,\n )\n\n new_shape = (np.prod(shape[0:i]).astype(int), -1)\n b = np.reshape(a, new_shape)\n expect(node, inputs=[a], outputs=[b],\n name='test_flatten_negative_axis' + str(abs(i)))" + }, + { + "summary": "flatten_with_default_axis", + "code": "node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'], # Default value for axis: axis=1\n)\n\nshape = (5, 4, 3, 2)\na = np.random.random_sample(shape).astype(np.float32)\nnew_shape = (5, 24)\nb = np.reshape(a, new_shape)\nexpect(node, inputs=[a], outputs=[b],\n name='test_flatten_default_axis')" + } + ], + "category": "Shape" + }, + { + "name": "Flatten", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Flattens the input tensor into a 2D matrix. If input tensor has shape\n(d_0, d_1, ... d_n) then the output will have shape\n(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [0, R], where R is the rank of the input tensor. When axis = 0, the shape of the output tensor is (1, (d_0 X d_1 ... d_n), where the shape of the input tensor is (d_0, d_1, ... d_n). " + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "A tensor of rank >= axis." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "flatten", + "code": "shape = (2, 3, 4, 5)\na = np.random.random_sample(shape).astype(np.float32)\n\nfor i in range(len(shape)):\n node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'],\n axis=i,\n )\n\n new_shape = (1, -1) if i == 0 else (np.prod(shape[0:i]).astype(int), -1)\n b = np.reshape(a, new_shape)\n expect(node, inputs=[a], outputs=[b],\n name='test_flatten_axis' + str(i))" + }, + { + "summary": "flatten_negative_axis", + "code": "shape = (2, 3, 4, 5)\na = np.random.random_sample(shape).astype(np.float32)\n\nfor i in range(-len(shape), 0):\n node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'],\n axis=i,\n )\n\n new_shape = (np.prod(shape[0:i]).astype(int), -1)\n b = np.reshape(a, new_shape)\n expect(node, inputs=[a], outputs=[b],\n name='test_flatten_negative_axis' + str(abs(i)))" + }, + { + "summary": "flatten_with_default_axis", + "code": "node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'], # Default value for axis: axis=1\n)\n\nshape = (5, 4, 3, 2)\na = np.random.random_sample(shape).astype(np.float32)\nnew_shape = (5, 24)\nb = np.reshape(a, new_shape)\nexpect(node, inputs=[a], outputs=[b],\n name='test_flatten_default_axis')" + } + ], + "category": "Shape" + }, + { + "name": "Flatten", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Flattens the input tensor into a 2D matrix. If input tensor has shape\n(d_0, d_1, ... d_n) then the output will have shape\n(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [-r, r], where r is the rank of the input tensor. Negative value means counting dimensions from the back. When axis = 0, the shape of the output tensor is (1, (d_0 X d_1 ... d_n), where the shape of the input tensor is (d_0, d_1, ... d_n). " + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "A tensor of rank >= axis." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "flatten", + "code": "shape = (2, 3, 4, 5)\na = np.random.random_sample(shape).astype(np.float32)\n\nfor i in range(len(shape)):\n node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'],\n axis=i,\n )\n\n new_shape = (1, -1) if i == 0 else (np.prod(shape[0:i]).astype(int), -1)\n b = np.reshape(a, new_shape)\n expect(node, inputs=[a], outputs=[b],\n name='test_flatten_axis' + str(i))" + }, + { + "summary": "flatten_negative_axis", + "code": "shape = (2, 3, 4, 5)\na = np.random.random_sample(shape).astype(np.float32)\n\nfor i in range(-len(shape), 0):\n node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'],\n axis=i,\n )\n\n new_shape = (np.prod(shape[0:i]).astype(int), -1)\n b = np.reshape(a, new_shape)\n expect(node, inputs=[a], outputs=[b],\n name='test_flatten_negative_axis' + str(abs(i)))" + }, + { + "summary": "flatten_with_default_axis", + "code": "node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'], # Default value for axis: axis=1\n)\n\nshape = (5, 4, 3, 2)\na = np.random.random_sample(shape).astype(np.float32)\nnew_shape = (5, 24)\nb = np.reshape(a, new_shape)\nexpect(node, inputs=[a], outputs=[b],\n name='test_flatten_default_axis')" + } + ], + "category": "Shape" + }, + { + "name": "Flatten", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Flattens the input tensor into a 2D matrix. If input tensor has shape\n(d_0, d_1, ... d_n) then the output will have shape\n(d_0 X d_1 ... d_(axis-1), d_axis X d_(axis+1) ... X dn).\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Indicate up to which input dimensions (exclusive) should be flattened to the outer dimension of the output. The value for axis must be in the range [-r, r], where r is the rank of the input tensor. Negative value means counting dimensions from the back. When axis = 0, the shape of the output tensor is (1, (d_0 X d_1 ... d_n), where the shape of the input tensor is (d_0, d_1, ... d_n). " + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "A tensor of rank >= axis." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "A 2D tensor with the contents of the input tensor, with input dimensions up to axis flattened to the outer dimension of the output and remaining input dimensions flattened into the inner dimension of the output." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "flatten", + "code": "shape = (2, 3, 4, 5)\na = np.random.random_sample(shape).astype(np.float32)\n\nfor i in range(len(shape)):\n node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'],\n axis=i,\n )\n\n new_shape = (1, -1) if i == 0 else (np.prod(shape[0:i]).astype(int), -1)\n b = np.reshape(a, new_shape)\n expect(node, inputs=[a], outputs=[b],\n name='test_flatten_axis' + str(i))" + }, + { + "summary": "flatten_negative_axis", + "code": "shape = (2, 3, 4, 5)\na = np.random.random_sample(shape).astype(np.float32)\n\nfor i in range(-len(shape), 0):\n node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'],\n axis=i,\n )\n\n new_shape = (np.prod(shape[0:i]).astype(int), -1)\n b = np.reshape(a, new_shape)\n expect(node, inputs=[a], outputs=[b],\n name='test_flatten_negative_axis' + str(abs(i)))" + }, + { + "summary": "flatten_with_default_axis", + "code": "node = onnx.helper.make_node(\n 'Flatten',\n inputs=['a'],\n outputs=['b'], # Default value for axis: axis=1\n)\n\nshape = (5, 4, 3, 2)\na = np.random.random_sample(shape).astype(np.float32)\nnew_shape = (5, 24)\nb = np.reshape(a, new_shape)\nexpect(node, inputs=[a], outputs=[b],\n name='test_flatten_default_axis')" + } + ], + "category": "Shape" + }, + { + "name": "Floor", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Floor takes one input data (Tensor) and produces one output data\n(Tensor) where the floor is, y = floor(x), is applied to\nthe tensor elementwise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "floor", + "code": "node = onnx.helper.make_node(\n 'Floor',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1.5, 1.2, 2]).astype(np.float32)\ny = np.floor(x) # expected output [-2., 1., 2.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_floor_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.floor(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_floor')" + } + ] + }, + { + "name": "Floor", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Floor takes one input data (Tensor) and produces one output data\n(Tensor) where the floor is, y = floor(x), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "floor", + "code": "node = onnx.helper.make_node(\n 'Floor',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1.5, 1.2, 2]).astype(np.float32)\ny = np.floor(x) # expected output [-2., 1., 2.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_floor_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.floor(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_floor')" + } + ] + }, + { + "name": "Floor", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Floor takes one input data (Tensor) and produces one output data\n(Tensor) where the floor is, y = floor(x), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "floor", + "code": "node = onnx.helper.make_node(\n 'Floor',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1.5, 1.2, 2]).astype(np.float32)\ny = np.floor(x) # expected output [-2., 1., 2.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_floor_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.floor(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_floor')" + } + ] + }, + { + "name": "GRU", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes an one-layer GRU. This operator is usually supported via some custom\nimplementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`z` - update gate\n\n`r` - reset gate\n\n`h` - hidden gate\n\n`t` - time step (t-1 means previous time step)\n\n`W[zrh]` - W parameter weight matrix for update, reset, and hidden gates\n\n`R[zrh]` - R recurrence weight matrix for update, reset, and hidden gates\n\n`Wb[zrh]` - W bias vectors for update, reset, and hidden gates\n\n`Rb[zrh]` - R bias vectors for update, reset, and hidden gates\n\n`WB[zrh]` - W parameter weight matrix for backward update, reset, and hidden gates\n\n`RB[zrh]` - R recurrence weight matrix for backward update, reset, and hidden gates\n\n`WBb[zrh]` - W bias vectors for backward update, reset, and hidden gates\n\n`RBb[zrh]` - R bias vectors for backward update, reset, and hidden gates\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Sigmoid, g=Tanh):\n\n - zt = f(Xt*(Wz^T) + Ht-1*Rz + Wbz + Rbz)\n\n - rt = f(Xt*(Wr^T) + Ht-1*Rr + Wbr + Rbr)\n\n - ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*Rh + Rbh + Wbh) # default, when linear_before_reset = 0\n\n - ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*Rh + Rbh) + Wbh) # when linear_before_reset != 0\n\n - Ht = (1 - zt) (.) ht + zt (.) Ht-1\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "A list of 2 (or 4 if bidirectional) activation functions for update, reset, and hidden gates. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "foward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + }, + { + "name": "output_sequence", + "type": "int64", + "required": false, + "description": "The sequence output for the hidden is optional if 0. Default 0." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for the gates. Concatenation of `W[zrh]` and `WB[zrh]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 3*hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `R[zrh]` and `RB[zrh]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 3*hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for the gates. Concatenation of `[Wb[zrh], Rb[zrh]]` and `[WBb[zrh], RBb[zrh]]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 6*hidden_size]`. Optional: If not specified - assumed to be 0" + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_input": 3, + "max_input": 6, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. It is optional if `output_sequence` is 0." + }, + { + "name": "Y_h", + "type": "T", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 2, + "max_output": 2, + "inputs_range": "3 - 6", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 6\nnumber_of_gates = 3\nweight_scale = 0.2\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\ngru = GRU_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = gru.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_gru_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 5\nweight_scale = 0.1\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\ngru = GRU_Helper(X=input, W=W, R=R)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_gru_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 3\nweight_scale = 0.1\ncustom_bias = 0.1\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRU_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_with_initial_bias')" + }, + { + "summary": "seq_length", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],\n [[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = np.random.randn(1, number_of_gates * hidden_size, input_size).astype(np.float32)\nR = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(np.float32)\n\n# Adding custom bias\nW_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)\nR_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRU_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_seq_length')" + } + ], + "category": "Layer" + }, + { + "name": "GRU", + "module": "ai.onnx", + "version": 3, + "support_level": "common", + "description": "Computes an one-layer GRU. This operator is usually supported via some custom\nimplementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`z` - update gate\n\n`r` - reset gate\n\n`h` - hidden gate\n\n`t` - time step (t-1 means previous time step)\n\n`W[zrh]` - W parameter weight matrix for update, reset, and hidden gates\n\n`R[zrh]` - R recurrence weight matrix for update, reset, and hidden gates\n\n`Wb[zrh]` - W bias vectors for update, reset, and hidden gates\n\n`Rb[zrh]` - R bias vectors for update, reset, and hidden gates\n\n`WB[zrh]` - W parameter weight matrix for backward update, reset, and hidden gates\n\n`RB[zrh]` - R recurrence weight matrix for backward update, reset, and hidden gates\n\n`WBb[zrh]` - W bias vectors for backward update, reset, and hidden gates\n\n`RBb[zrh]` - R bias vectors for backward update, reset, and hidden gates\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Sigmoid, g=Tanh):\n\n - zt = f(Xt*(Wz^T) + Ht-1*Rz + Wbz + Rbz)\n\n - rt = f(Xt*(Wr^T) + Ht-1*Rr + Wbr + Rbr)\n\n - ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*Rh + Rbh + Wbh) # default, when linear_before_reset = 0\n\n - ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*Rh + Rbh) + Wbh) # when linear_before_reset != 0\n\n - Ht = (1 - zt) (.) ht + zt (.) Ht-1\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "A list of 2 (or 4 if bidirectional) activation functions for update, reset, and hidden gates. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "forward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + }, + { + "name": "linear_before_reset", + "type": "int64", + "required": false, + "description": "When computing the output of the hidden gate, apply the linear transformation before multiplying by the output of the reset gate." + }, + { + "name": "output_sequence", + "type": "int64", + "required": false, + "description": "The sequence output for the hidden is optional if 0. Default 0." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for the gates. Concatenation of `W[zrh]` and `WB[zrh]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 3*hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `R[zrh]` and `RB[zrh]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 3*hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for the gates. Concatenation of `[Wb[zrh], Rb[zrh]]` and `[WBb[zrh], RBb[zrh]]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 6*hidden_size]`. Optional: If not specified - assumed to be 0" + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_input": 3, + "max_input": 6, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. It is optional if `output_sequence` is 0." + }, + { + "name": "Y_h", + "type": "T", + "option": "optional", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 0, + "max_output": 2, + "inputs_range": "3 - 6", + "outputs_range": "0 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 6\nnumber_of_gates = 3\nweight_scale = 0.2\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\ngru = GRU_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = gru.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_gru_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 5\nweight_scale = 0.1\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\ngru = GRU_Helper(X=input, W=W, R=R)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_gru_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 3\nweight_scale = 0.1\ncustom_bias = 0.1\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRU_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_with_initial_bias')" + }, + { + "summary": "seq_length", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],\n [[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = np.random.randn(1, number_of_gates * hidden_size, input_size).astype(np.float32)\nR = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(np.float32)\n\n# Adding custom bias\nW_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)\nR_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRU_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_seq_length')" + } + ], + "category": "Layer" + }, + { + "name": "GRU", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Computes an one-layer GRU. This operator is usually supported via some custom\nimplementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`z` - update gate\n\n`r` - reset gate\n\n`h` - hidden gate\n\n`t` - time step (t-1 means previous time step)\n\n`W[zrh]` - W parameter weight matrix for update, reset, and hidden gates\n\n`R[zrh]` - R recurrence weight matrix for update, reset, and hidden gates\n\n`Wb[zrh]` - W bias vectors for update, reset, and hidden gates\n\n`Rb[zrh]` - R bias vectors for update, reset, and hidden gates\n\n`WB[zrh]` - W parameter weight matrix for backward update, reset, and hidden gates\n\n`RB[zrh]` - R recurrence weight matrix for backward update, reset, and hidden gates\n\n`WBb[zrh]` - W bias vectors for backward update, reset, and hidden gates\n\n`RBb[zrh]` - R bias vectors for backward update, reset, and hidden gates\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Sigmoid, g=Tanh):\n\n - zt = f(Xt*(Wz^T) + Ht-1*(Rz^T) + Wbz + Rbz)\n\n - rt = f(Xt*(Wr^T) + Ht-1*(Rr^T) + Wbr + Rbr)\n\n - ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*(Rh^T) + Rbh + Wbh) # default, when linear_before_reset = 0\n\n - ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*(Rh^T) + Rbh)) + Wbh) # when linear_before_reset != 0\n\n - Ht = (1 - zt) (.) ht + zt (.) Ht-1\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "A list of 2 (or 4 if bidirectional) activation functions for update, reset, and hidden gates. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "forward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + }, + { + "name": "linear_before_reset", + "type": "int64", + "required": false, + "description": "When computing the output of the hidden gate, apply the linear transformation before multiplying by the output of the reset gate." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for the gates. Concatenation of `W[zrh]` and `WB[zrh]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 3*hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `R[zrh]` and `RB[zrh]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 3*hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for the gates. Concatenation of `[Wb[zrh], Rb[zrh]]` and `[WBb[zrh], RBb[zrh]]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 6*hidden_size]`. Optional: If not specified - assumed to be 0" + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_input": 3, + "max_input": 6, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. " + }, + { + "name": "Y_h", + "type": "T", + "option": "optional", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 0, + "max_output": 2, + "inputs_range": "3 - 6", + "outputs_range": "0 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 6\nnumber_of_gates = 3\nweight_scale = 0.2\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\ngru = GRU_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = gru.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_gru_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 5\nweight_scale = 0.1\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\ngru = GRU_Helper(X=input, W=W, R=R)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_gru_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 3\nweight_scale = 0.1\ncustom_bias = 0.1\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRU_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_with_initial_bias')" + }, + { + "summary": "seq_length", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],\n [[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = np.random.randn(1, number_of_gates * hidden_size, input_size).astype(np.float32)\nR = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(np.float32)\n\n# Adding custom bias\nW_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)\nR_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRU_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_seq_length')" + } + ], + "category": "Layer" + }, + { + "name": "GRU", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Computes an one-layer GRU. This operator is usually supported via some custom\nimplementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`z` - update gate\n\n`r` - reset gate\n\n`h` - hidden gate\n\n`t` - time step (t-1 means previous time step)\n\n`W[zrh]` - W parameter weight matrix for update, reset, and hidden gates\n\n`R[zrh]` - R recurrence weight matrix for update, reset, and hidden gates\n\n`Wb[zrh]` - W bias vectors for update, reset, and hidden gates\n\n`Rb[zrh]` - R bias vectors for update, reset, and hidden gates\n\n`WB[zrh]` - W parameter weight matrix for backward update, reset, and hidden gates\n\n`RB[zrh]` - R recurrence weight matrix for backward update, reset, and hidden gates\n\n`WBb[zrh]` - W bias vectors for backward update, reset, and hidden gates\n\n`RBb[zrh]` - R bias vectors for backward update, reset, and hidden gates\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Sigmoid, g=Tanh):\n\n - zt = f(Xt*(Wz^T) + Ht-1*(Rz^T) + Wbz + Rbz)\n\n - rt = f(Xt*(Wr^T) + Ht-1*(Rr^T) + Wbr + Rbr)\n\n - ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*(Rh^T) + Rbh + Wbh) # default, when linear_before_reset = 0\n\n - ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*(Rh^T) + Rbh)) + Wbh) # when linear_before_reset != 0\n\n - Ht = (1 - zt) (.) ht + zt (.) Ht-1\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "A list of 2 (or 4 if bidirectional) activation functions for update, reset, and hidden gates. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "forward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + }, + { + "name": "layout", + "type": "int64", + "required": false, + "description": "The shape format of inputs X, initial_h and outputs Y, Y_h. If 0, the following shapes are expected: X.shape = [seq_length, batch_size, input_size], Y.shape = [seq_length, num_directions, batch_size, hidden_size], initial_h.shape = Y_h.shape = [num_directions, batch_size, hidden_size]. If 1, the following shapes are expected: X.shape = [batch_size, seq_length, input_size], Y.shape = [batch_size, seq_length, num_directions, hidden_size], initial_h.shape = Y_h.shape = [batch_size, num_directions, hidden_size]." + }, + { + "name": "linear_before_reset", + "type": "int64", + "required": false, + "description": "When computing the output of the hidden gate, apply the linear transformation before multiplying by the output of the reset gate." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for the gates. Concatenation of `W[zrh]` and `WB[zrh]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 3*hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `R[zrh]` and `RB[zrh]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 3*hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for the gates. Concatenation of `[Wb[zrh], Rb[zrh]]` and `[WBb[zrh], RBb[zrh]]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 6*hidden_size]`. Optional: If not specified - assumed to be 0" + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_input": 3, + "max_input": 6, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. " + }, + { + "name": "Y_h", + "type": "T", + "option": "optional", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 0, + "max_output": 2, + "inputs_range": "3 - 6", + "outputs_range": "0 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 6\nnumber_of_gates = 3\nweight_scale = 0.2\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\ngru = GRU_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = gru.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_gru_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 5\nweight_scale = 0.1\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\ngru = GRU_Helper(X=input, W=W, R=R)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_gru_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 3\nweight_scale = 0.1\ncustom_bias = 0.1\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRU_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_with_initial_bias')" + }, + { + "summary": "seq_length", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],\n [[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\nnumber_of_gates = 3\n\nnode = onnx.helper.make_node(\n 'GRU',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = np.random.randn(1, number_of_gates * hidden_size, input_size).astype(np.float32)\nR = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(np.float32)\n\n# Adding custom bias\nW_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)\nR_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRU_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_seq_length')" + } + ], + "category": "Layer" + }, + { + "name": "Gather", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Given `data` tensor of rank r >= 1, and `indices` tensor of rank q, gather\nentries of the axis dimension of `data` (by default outer-most one as axis=0) indexed by `indices`, and concatenates\nthem in an output tensor of rank q + (r - 1).\nExample 1:\n```\n data = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n indices = [\n [0, 1],\n [1, 2],\n ]\n output = [\n [\n [1.0, 1.2],\n [2.3, 3.4],\n ],\n [\n [2.3, 3.4],\n [4.5, 5.7],\n ],\n ]\n```\nExample 2:\n```\n data = [\n [1.0, 1.2, 1.9],\n [2.3, 3.4, 3.9],\n [4.5, 5.7, 5.9],\n ]\n indices = [\n [0, 2],\n ]\n axis = 1,\n output = [\n [\n [1.0, 1.9],\n [2.3, 3.9],\n [4.5, 5.9],\n ],\n ]\n```\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to gather on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1]" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, of any rank q. All index values are expected to be within bounds. It is an error if any of the index values are out of bounds." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank q + (r - 1)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "gather_0", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=0,\n)\ndata = np.random.randn(5, 4, 3, 2).astype(np.float32)\nindices = np.array([0, 1, 3])\ny = np.take(data, indices, axis=0)\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_0')" + }, + { + "summary": "gather_1", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=1,\n)\ndata = np.random.randn(5, 4, 3, 2).astype(np.float32)\nindices = np.array([0, 1, 3])\ny = np.take(data, indices, axis=1)\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_1')" + }, + { + "summary": "gather_2d_indices", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=1,\n)\ndata = np.random.randn(3, 3).astype(np.float32)\nindices = np.array([[0, 2]])\ny = np.take(data, indices, axis=1)\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_2d_indices')" + }, + { + "summary": "gather_negative_indices", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=0,\n)\ndata = np.arange(10).astype(np.float32)\nindices = np.array([0, -9, -10])\ny = np.take(data, indices, axis=0)\n\n# print(y)\n# [0. 1. 0.]\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_negative_indices')" + } + ], + "category": "Transform" + }, + { + "name": "Gather", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Given `data` tensor of rank r >= 1, and `indices` tensor of rank q, gather\nentries of the axis dimension of `data` (by default outer-most one as axis=0) indexed by `indices`, and concatenates\nthem in an output tensor of rank q + (r - 1).\n\naxis = 0 :\n\nLet\nk = indices[i_{0}, ..., i_{q-1}]\nThen\noutput[i_{0}, ..., i_{q-1}, j_{0}, ..., j_{r-2}] = input[k , j_{0}, ..., j_{r-2}]\n\n```\n data = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n indices = [\n [0, 1],\n [1, 2],\n ]\n output = [\n [\n [1.0, 1.2],\n [2.3, 3.4],\n ],\n [\n [2.3, 3.4],\n [4.5, 5.7],\n ],\n ]\n```\naxis = 1 :\n\nLet\nk = indices[i_{0}, ..., i_{q-1}]\nThen\noutput[i_{0}, ..., i_{q-1}, j_{0}, ..., j_{r-2}] = input[j_{0}, k, j_{1}, ..., j_{r-2}]\n\n```\n data = [\n [1.0, 1.2, 1.9],\n [2.3, 3.4, 3.9],\n [4.5, 5.7, 5.9],\n ]\n indices = [\n [0, 2],\n ]\n axis = 1,\n output = [\n [\n [1.0, 1.9],\n [2.3, 3.9],\n [4.5, 5.9],\n ],\n ]\n```\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to gather on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, of any rank q. All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank q + (r - 1)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "gather_0", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=0,\n)\ndata = np.random.randn(5, 4, 3, 2).astype(np.float32)\nindices = np.array([0, 1, 3])\ny = np.take(data, indices, axis=0)\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_0')" + }, + { + "summary": "gather_1", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=1,\n)\ndata = np.random.randn(5, 4, 3, 2).astype(np.float32)\nindices = np.array([0, 1, 3])\ny = np.take(data, indices, axis=1)\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_1')" + }, + { + "summary": "gather_2d_indices", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=1,\n)\ndata = np.random.randn(3, 3).astype(np.float32)\nindices = np.array([[0, 2]])\ny = np.take(data, indices, axis=1)\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_2d_indices')" + }, + { + "summary": "gather_negative_indices", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=0,\n)\ndata = np.arange(10).astype(np.float32)\nindices = np.array([0, -9, -10])\ny = np.take(data, indices, axis=0)\n\n# print(y)\n# [0. 1. 0.]\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_negative_indices')" + } + ], + "category": "Transform" + }, + { + "name": "Gather", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Given `data` tensor of rank r >= 1, and `indices` tensor of rank q, gather\nentries of the axis dimension of `data` (by default outer-most one as axis=0) indexed by `indices`, and concatenates\nthem in an output tensor of rank q + (r - 1).\n\naxis = 0 :\n\nLet\nk = indices[i_{0}, ..., i_{q-1}]\nThen\noutput[i_{0}, ..., i_{q-1}, j_{0}, ..., j_{r-2}] = input[k , j_{0}, ..., j_{r-2}]\n\n```\n data = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n indices = [\n [0, 1],\n [1, 2],\n ]\n output = [\n [\n [1.0, 1.2],\n [2.3, 3.4],\n ],\n [\n [2.3, 3.4],\n [4.5, 5.7],\n ],\n ]\n```\naxis = 1 :\n\nLet\nk = indices[i_{0}, ..., i_{q-1}]\nThen\noutput[j_{0}, i_{0}, ..., i_{q-1}, j_{1}, ..., j_{r-2}] = input[j_{0}, k, j_{1}, ..., j_{r-2}]\n\n```\n data = [\n [1.0, 1.2, 1.9],\n [2.3, 3.4, 3.9],\n [4.5, 5.7, 5.9],\n ]\n indices = [\n [0, 2],\n ]\n axis = 1,\n output = [\n [[1.0, 1.9]],\n [[2.3, 3.9]],\n [[4.5, 5.9]],\n ]\n```\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to gather on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, of any rank q. All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank q + (r - 1)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "gather_0", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=0,\n)\ndata = np.random.randn(5, 4, 3, 2).astype(np.float32)\nindices = np.array([0, 1, 3])\ny = np.take(data, indices, axis=0)\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_0')" + }, + { + "summary": "gather_1", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=1,\n)\ndata = np.random.randn(5, 4, 3, 2).astype(np.float32)\nindices = np.array([0, 1, 3])\ny = np.take(data, indices, axis=1)\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_1')" + }, + { + "summary": "gather_2d_indices", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=1,\n)\ndata = np.random.randn(3, 3).astype(np.float32)\nindices = np.array([[0, 2]])\ny = np.take(data, indices, axis=1)\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_2d_indices')" + }, + { + "summary": "gather_negative_indices", + "code": "node = onnx.helper.make_node(\n 'Gather',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=0,\n)\ndata = np.arange(10).astype(np.float32)\nindices = np.array([0, -9, -10])\ny = np.take(data, indices, axis=0)\n\n# print(y)\n# [0. 1. 0.]\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_negative_indices')" + } + ], + "category": "Transform" + }, + { + "name": "GatherElements", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "GatherElements takes two inputs `data` and `indices` of the same rank r >= 1\nand an optional attribute `axis` that identifies an axis of `data`\n(by default, the outer-most axis, that is axis 0). It is an indexing operation\nthat produces its output by indexing into the input data tensor at index\npositions determined by elements of the `indices` tensor.\nIts output shape is the same as the shape of `indices` and consists of one value\n(gathered from the `data`) for each element in `indices`.\n\nFor instance, in the 3-D case (r = 3), the output produced is determined\nby the following equations:\n```\n out[i][j][k] = input[index[i][j][k]][j][k] if axis = 0,\n out[i][j][k] = input[i][index[i][j][k]][k] if axis = 1,\n out[i][j][k] = input[i][j][index[i][j][k]] if axis = 2,\n```\n\nThis operator is also the inverse of ScatterElements. It is similar to Torch's gather operation.\n\nExample 1:\n```\n data = [\n [1, 2],\n [3, 4],\n ]\n indices = [\n [0, 0],\n [1, 0],\n ]\n axis = 1\n output = [\n [\n [1, 1],\n [4, 3],\n ],\n ]\n```\nExample 2:\n```\n data = [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9],\n ]\n indices = [\n [1, 2, 0],\n [2, 0, 0],\n ]\n axis = 0\n output = [\n [\n [4, 8, 3],\n [7, 2, 3],\n ],\n ]\n```\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to gather on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, with the same rank r as the input. All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of the same shape as indices." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "gather_elements_0", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'GatherElements',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1, 2],\n [3, 4]], dtype=np.float32)\nindices = np.array([[0, 0],\n [1, 0]], dtype=np.int32)\n\ny = gather_elements(data, indices, axis)\n# print(y) produces\n# [[1, 1],\n# [4, 3]]\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_elements_0')" + }, + { + "summary": "gather_elements_1", + "code": "axis = 0\nnode = onnx.helper.make_node(\n 'GatherElements',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]], dtype=np.float32)\nindices = np.array([[1, 2, 0],\n [2, 0, 0]], dtype=np.int32)\n\ny = gather_elements(data, indices, axis)\n# print(y) produces\n# [[4, 8, 3],\n# [7, 2, 3]]\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_elements_1')" + }, + { + "summary": "gather_elements_negative_indices", + "code": "axis = 0\nnode = onnx.helper.make_node(\n 'GatherElements',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]], dtype=np.float32)\nindices = np.array([[-1, -2, 0],\n [-2, 0, 0]], dtype=np.int32)\n\ny = gather_elements(data, indices, axis)\n# print(y) produces\n# [[7, 5, 3],\n# [4, 2, 3]]\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_elements_negative_indices')" + } + ] + }, + { + "name": "GatherElements", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "GatherElements takes two inputs `data` and `indices` of the same rank r >= 1\nand an optional attribute `axis` that identifies an axis of `data`\n(by default, the outer-most axis, that is axis 0). It is an indexing operation\nthat produces its output by indexing into the input data tensor at index\npositions determined by elements of the `indices` tensor.\nIts output shape is the same as the shape of `indices` and consists of one value\n(gathered from the `data`) for each element in `indices`.\n\nFor instance, in the 3-D case (r = 3), the output produced is determined\nby the following equations:\n```\n out[i][j][k] = input[index[i][j][k]][j][k] if axis = 0,\n out[i][j][k] = input[i][index[i][j][k]][k] if axis = 1,\n out[i][j][k] = input[i][j][index[i][j][k]] if axis = 2,\n```\n\nThis operator is also the inverse of ScatterElements. It is similar to Torch's gather operation.\n\nExample 1:\n```\n data = [\n [1, 2],\n [3, 4],\n ]\n indices = [\n [0, 0],\n [1, 0],\n ]\n axis = 1\n output = [\n [1, 1],\n [4, 3],\n ]\n```\nExample 2:\n```\n data = [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9],\n ]\n indices = [\n [1, 2, 0],\n [2, 0, 0],\n ]\n axis = 0\n output = [\n [4, 8, 3],\n [7, 2, 3],\n ]\n```\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to gather on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, with the same rank r as the input. All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of the same shape as indices." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "gather_elements_0", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'GatherElements',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1, 2],\n [3, 4]], dtype=np.float32)\nindices = np.array([[0, 0],\n [1, 0]], dtype=np.int32)\n\ny = gather_elements(data, indices, axis)\n# print(y) produces\n# [[1, 1],\n# [4, 3]]\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_elements_0')" + }, + { + "summary": "gather_elements_1", + "code": "axis = 0\nnode = onnx.helper.make_node(\n 'GatherElements',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]], dtype=np.float32)\nindices = np.array([[1, 2, 0],\n [2, 0, 0]], dtype=np.int32)\n\ny = gather_elements(data, indices, axis)\n# print(y) produces\n# [[4, 8, 3],\n# [7, 2, 3]]\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_elements_1')" + }, + { + "summary": "gather_elements_negative_indices", + "code": "axis = 0\nnode = onnx.helper.make_node(\n 'GatherElements',\n inputs=['data', 'indices'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1, 2, 3],\n [4, 5, 6],\n [7, 8, 9]], dtype=np.float32)\nindices = np.array([[-1, -2, 0],\n [-2, 0, 0]], dtype=np.int32)\n\ny = gather_elements(data, indices, axis)\n# print(y) produces\n# [[7, 5, 3],\n# [4, 2, 3]]\n\nexpect(node, inputs=[data, indices.astype(np.int64)], outputs=[y],\n name='test_gather_elements_negative_indices')" + } + ] + }, + { + "name": "GatherND", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Given `data` tensor of rank `r` >= 1, and `indices` tensor of rank `q` >= 1, this operator gathers\nslices of `data` into an output tensor of rank `q + r - indices_shape[-1] - 1`.\n\n`indices` is an q-dimensional integer tensor, best thought of as a `(q-1)`-dimensional tensor of index-tuples into `data`,\nwhere each element defines a slice of `data`\n\nSome salient points about the inputs' rank and shape:\n\n1) r >= 1 and q >= 1 are to be honored. There is no dependency condition to be met between ranks `r` and `q`\n\n2) The `indices_shape[-1]` should have a value between 1 (inclusive) and rank `r` (inclusive)\n\n3) All values in `indices` are expected to be within bounds [-s, s-1] along axis of size `s` (i.e.) `-data_shape[i] <= indices[...,i] <= data_shape[i] - 1`.\n It is an error if any of the index values are out of bounds.\n\nThe output is computed as follows:\n\nThe output tensor is obtained by mapping each index-tuple in the `indices` tensor to the corresponding slice of the input `data`.\n\n1) If `indices_shape[-1] > r` => error condition\n\n2) If `indices_shape[-1] == r`, since the rank of `indices` is `q`, `indices` can be thought of as a `(q-1)`-dimensional tensor\n containing 1-D tensors of dimension `r`. Let us think of each such `r` ranked tensor as `indices_slice`.\n Each *scalar value* corresponding to `data[indices_slice]` is filled into the corresponding location of the `(q-1)`-dimensional tensor\n to form the `output` tensor (Example 1 below)\n\n3) If `indices_shape[-1] < r`, since the rank of `indices` is `q`, `indices` can be thought of as a `(q-1)`-dimensional tensor\n containing 1-D tensors of dimension `< r`. Let us think of each such tensors as `indices_slice`.\n Each *tensor slice* corresponding to `data[indices_slice , :]` is filled into the corresponding location of the `(q-1)`-dimensional tensor\n to form the `output` tensor (Examples 2, 3, and 4 below)\n\nThis operator is the inverse of `ScatterND`.\n\n`Example 1`\n\n data = [[0,1],[2,3]] # data_shape = [2, 2]\n\n indices = [[0,0],[1,1]] # indices_shape = [2, 2]\n\n output = [0,3] # output_shape = [2]\n\n`Example 2`\n\n data = [[0,1],[2,3]] # data_shape = [2, 2]\n\n indices = [[1],[0]] # indices_shape = [2, 1]\n\n output = [[2,3],[0,1]] # output_shape = [2, 2]\n\n`Example 3`\n\n data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\n\n indices = [[0,1],[1,0]] # indices_shape = [2, 2]\n\n output = [[2,3],[4,5]] # output_shape = [2, 2]\n\n`Example 4`\n\n data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\n\n indices = [[[0,1]],[[1,0]]] # indices_shape = [2, 1, 2]\n\n output = [[[2,3]],[[4,5]]] # output_shape = [2, 1, 2]\n\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "tensor(int64)", + "description": "Tensor of rank q >= 1. All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank q + r - indices_shape[-1] - 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "float32", + "code": "node = onnx.helper.make_node(\n 'GatherND',\n inputs=['data', 'indices'],\n outputs=['output'],\n)\n\ndata = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype=np.float32)\nindices = np.array([[[0, 1]], [[1, 0]]], dtype=np.int64)\noutput = gather_nd_impl(data, indices, 0)\nexpected_output = np.array([[[2, 3]], [[4, 5]]], dtype=np.float32)\nassert (np.array_equal(output, expected_output))\nexpect(node, inputs=[data, indices], outputs=[output],\n name='test_gathernd_example_float32')" + }, + { + "summary": "int32", + "code": "node = onnx.helper.make_node(\n 'GatherND',\n inputs=['data', 'indices'],\n outputs=['output'],\n)\n\ndata = np.array([[0, 1], [2, 3]], dtype=np.int32)\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\noutput = gather_nd_impl(data, indices, 0)\nexpected_output = np.array([0, 3], dtype=np.int32)\nassert (np.array_equal(output, expected_output))\nexpect(node, inputs=[data, indices], outputs=[output],\n name='test_gathernd_example_int32')" + }, + { + "summary": "int32_batchdim_1", + "code": "node = onnx.helper.make_node(\n 'GatherND',\n inputs=['data', 'indices'],\n outputs=['output'],\n batch_dims=1,\n)\n\ndata = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype=np.int32)\nindices = np.array([[1], [0]], dtype=np.int64)\noutput = gather_nd_impl(data, indices, 1)\nexpected_output = np.array([[2, 3], [4, 5]], dtype=np.int32)\nassert (np.array_equal(output, expected_output))\nexpect(node, inputs=[data, indices], outputs=[output],\n name='test_gathernd_example_int32_batch_dim1')" + } + ] + }, + { + "name": "GatherND", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Given `data` tensor of rank `r` >= 1, `indices` tensor of rank `q` >= 1, and `batch_dims` integer `b`, this operator gathers\nslices of `data` into an output tensor of rank `q + r - indices_shape[-1] - 1 - b`.\n\n`indices` is an q-dimensional integer tensor, best thought of as a `(q-1)`-dimensional tensor of index-tuples into `data`,\nwhere each element defines a slice of `data`\n\n`batch_dims` (denoted as `b`) is an integer indicating the number of batch dimensions, i.e the leading `b` number of dimensions of\n`data` tensor and `indices` are representing the batches, and the gather starts from the `b+1` dimension.\n\nSome salient points about the inputs' rank and shape:\n\n1) r >= 1 and q >= 1 are to be honored. There is no dependency condition to be met between ranks `r` and `q`\n\n2) The first `b` dimensions of the shape of `indices` tensor and `data` tensor must be equal.\n\n3) b < min(q, r) is to be honored.\n\n4) The `indices_shape[-1]` should have a value between 1 (inclusive) and rank `r-b` (inclusive)\n\n5) All values in `indices` are expected to be within bounds [-s, s-1] along axis of size `s` (i.e.) `-data_shape[i] <= indices[...,i] <= data_shape[i] - 1`.\n It is an error if any of the index values are out of bounds.\n\nThe output is computed as follows:\n\nThe output tensor is obtained by mapping each index-tuple in the `indices` tensor to the corresponding slice of the input `data`.\n\n1) If `indices_shape[-1] > r-b` => error condition\n\n2) If `indices_shape[-1] == r-b`, since the rank of `indices` is `q`, `indices` can be thought of as `N` `(q-b-1)`-dimensional tensors\n containing 1-D tensors of dimension `r-b`, where `N` is an integer equals to the product of 1 and all the elements in the batch dimensions\n of the indices_shape. Let us think of each such `r-b` ranked tensor as `indices_slice`. Each *scalar value* corresponding to `data[0:b-1,indices_slice]`\n is filled into the corresponding location of the `(q-b-1)`-dimensional tensor to form the `output` tensor (Example 1 below)\n\n3) If `indices_shape[-1] < r-b`, since the rank of `indices` is `q`, `indices` can be thought of as `N` `(q-b-1)`-dimensional tensor\n containing 1-D tensors of dimension `< r-b`. Let us think of each such tensors as `indices_slice`. Each *tensor slice* corresponding\n to `data[0:b-1, indices_slice , :]` is filled into the corresponding location of the `(q-b-1)`-dimensional tensor\n to form the `output` tensor (Examples 2, 3, 4 and 5 below)\n\nThis operator is the inverse of `ScatterND`.\n\n`Example 1`\n\n batch_dims = 0\n\n data = [[0,1],[2,3]] # data_shape = [2, 2]\n\n indices = [[0,0],[1,1]] # indices_shape = [2, 2]\n\n output = [0,3] # output_shape = [2]\n\n`Example 2`\n\n batch_dims = 0\n\n data = [[0,1],[2,3]] # data_shape = [2, 2]\n\n indices = [[1],[0]] # indices_shape = [2, 1]\n\n output = [[2,3],[0,1]] # output_shape = [2, 2]\n\n`Example 3`\n\n batch_dims = 0\n\n data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\n\n indices = [[0,1],[1,0]] # indices_shape = [2, 2]\n\n output = [[2,3],[4,5]] # output_shape = [2, 2]\n\n`Example 4`\n\n batch_dims = 0\n\n data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\n\n indices = [[[0,1]],[[1,0]]] # indices_shape = [2, 1, 2]\n\n output = [[[2,3]],[[4,5]]] # output_shape = [2, 1, 2]\n\n`Example 5`\n\n batch_dims = 1\n\n data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\n\n indices = [[1],[0]] # indices_shape = [2, 1]\n\n output = [[2,3],[4,5]] # output_shape = [2, 2]\n\n\n", + "attributes": [ + { + "name": "batch_dims", + "type": "int64", + "required": false, + "description": "The number of batch dimensions. The gather of indexing starts from dimension of data[batch_dims:]" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "tensor(int64)", + "description": "Tensor of rank q >= 1. All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank q + r - indices_shape[-1] - 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "float32", + "code": "node = onnx.helper.make_node(\n 'GatherND',\n inputs=['data', 'indices'],\n outputs=['output'],\n)\n\ndata = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype=np.float32)\nindices = np.array([[[0, 1]], [[1, 0]]], dtype=np.int64)\noutput = gather_nd_impl(data, indices, 0)\nexpected_output = np.array([[[2, 3]], [[4, 5]]], dtype=np.float32)\nassert (np.array_equal(output, expected_output))\nexpect(node, inputs=[data, indices], outputs=[output],\n name='test_gathernd_example_float32')" + }, + { + "summary": "int32", + "code": "node = onnx.helper.make_node(\n 'GatherND',\n inputs=['data', 'indices'],\n outputs=['output'],\n)\n\ndata = np.array([[0, 1], [2, 3]], dtype=np.int32)\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\noutput = gather_nd_impl(data, indices, 0)\nexpected_output = np.array([0, 3], dtype=np.int32)\nassert (np.array_equal(output, expected_output))\nexpect(node, inputs=[data, indices], outputs=[output],\n name='test_gathernd_example_int32')" + }, + { + "summary": "int32_batchdim_1", + "code": "node = onnx.helper.make_node(\n 'GatherND',\n inputs=['data', 'indices'],\n outputs=['output'],\n batch_dims=1,\n)\n\ndata = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype=np.int32)\nindices = np.array([[1], [0]], dtype=np.int64)\noutput = gather_nd_impl(data, indices, 1)\nexpected_output = np.array([[2, 3], [4, 5]], dtype=np.int32)\nassert (np.array_equal(output, expected_output))\nexpect(node, inputs=[data, indices], outputs=[output],\n name='test_gathernd_example_int32_batch_dim1')" + } + ] + }, + { + "name": "GatherND", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Given `data` tensor of rank `r` >= 1, `indices` tensor of rank `q` >= 1, and `batch_dims` integer `b`, this operator gathers\nslices of `data` into an output tensor of rank `q + r - indices_shape[-1] - 1 - b`.\n\n`indices` is an q-dimensional integer tensor, best thought of as a `(q-1)`-dimensional tensor of index-tuples into `data`,\nwhere each element defines a slice of `data`\n\n`batch_dims` (denoted as `b`) is an integer indicating the number of batch dimensions, i.e the leading `b` number of dimensions of\n`data` tensor and `indices` are representing the batches, and the gather starts from the `b+1` dimension.\n\nSome salient points about the inputs' rank and shape:\n\n1) r >= 1 and q >= 1 are to be honored. There is no dependency condition to be met between ranks `r` and `q`\n\n2) The first `b` dimensions of the shape of `indices` tensor and `data` tensor must be equal.\n\n3) b < min(q, r) is to be honored.\n\n4) The `indices_shape[-1]` should have a value between 1 (inclusive) and rank `r-b` (inclusive)\n\n5) All values in `indices` are expected to be within bounds [-s, s-1] along axis of size `s` (i.e.) `-data_shape[i] <= indices[...,i] <= data_shape[i] - 1`.\n It is an error if any of the index values are out of bounds.\n\nThe output is computed as follows:\n\nThe output tensor is obtained by mapping each index-tuple in the `indices` tensor to the corresponding slice of the input `data`.\n\n1) If `indices_shape[-1] > r-b` => error condition\n\n2) If `indices_shape[-1] == r-b`, since the rank of `indices` is `q`, `indices` can be thought of as `N` `(q-b-1)`-dimensional tensors\n containing 1-D tensors of dimension `r-b`, where `N` is an integer equals to the product of 1 and all the elements in the batch dimensions\n of the indices_shape. Let us think of each such `r-b` ranked tensor as `indices_slice`. Each *scalar value* corresponding to `data[0:b-1,indices_slice]`\n is filled into the corresponding location of the `(q-b-1)`-dimensional tensor to form the `output` tensor (Example 1 below)\n\n3) If `indices_shape[-1] < r-b`, since the rank of `indices` is `q`, `indices` can be thought of as `N` `(q-b-1)`-dimensional tensor\n containing 1-D tensors of dimension `< r-b`. Let us think of each such tensors as `indices_slice`. Each *tensor slice* corresponding\n to `data[0:b-1, indices_slice , :]` is filled into the corresponding location of the `(q-b-1)`-dimensional tensor\n to form the `output` tensor (Examples 2, 3, 4 and 5 below)\n\nThis operator is the inverse of `ScatterND`.\n\n`Example 1`\n\n batch_dims = 0\n\n data = [[0,1],[2,3]] # data_shape = [2, 2]\n\n indices = [[0,0],[1,1]] # indices_shape = [2, 2]\n\n output = [0,3] # output_shape = [2]\n\n`Example 2`\n\n batch_dims = 0\n\n data = [[0,1],[2,3]] # data_shape = [2, 2]\n\n indices = [[1],[0]] # indices_shape = [2, 1]\n\n output = [[2,3],[0,1]] # output_shape = [2, 2]\n\n`Example 3`\n\n batch_dims = 0\n\n data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\n\n indices = [[0,1],[1,0]] # indices_shape = [2, 2]\n\n output = [[2,3],[4,5]] # output_shape = [2, 2]\n\n`Example 4`\n\n batch_dims = 0\n\n data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\n\n indices = [[[0,1]],[[1,0]]] # indices_shape = [2, 1, 2]\n\n output = [[[2,3]],[[4,5]]] # output_shape = [2, 1, 2]\n\n`Example 5`\n\n batch_dims = 1\n\n data = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\n\n indices = [[1],[0]] # indices_shape = [2, 1]\n\n output = [[2,3],[4,5]] # output_shape = [2, 2]\n\n\n", + "attributes": [ + { + "name": "batch_dims", + "type": "int64", + "required": false, + "description": "The number of batch dimensions. The gather of indexing starts from dimension of data[batch_dims:]" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "tensor(int64)", + "description": "Tensor of rank q >= 1. All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank q + r - indices_shape[-1] - 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "float32", + "code": "node = onnx.helper.make_node(\n 'GatherND',\n inputs=['data', 'indices'],\n outputs=['output'],\n)\n\ndata = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype=np.float32)\nindices = np.array([[[0, 1]], [[1, 0]]], dtype=np.int64)\noutput = gather_nd_impl(data, indices, 0)\nexpected_output = np.array([[[2, 3]], [[4, 5]]], dtype=np.float32)\nassert (np.array_equal(output, expected_output))\nexpect(node, inputs=[data, indices], outputs=[output],\n name='test_gathernd_example_float32')" + }, + { + "summary": "int32", + "code": "node = onnx.helper.make_node(\n 'GatherND',\n inputs=['data', 'indices'],\n outputs=['output'],\n)\n\ndata = np.array([[0, 1], [2, 3]], dtype=np.int32)\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\noutput = gather_nd_impl(data, indices, 0)\nexpected_output = np.array([0, 3], dtype=np.int32)\nassert (np.array_equal(output, expected_output))\nexpect(node, inputs=[data, indices], outputs=[output],\n name='test_gathernd_example_int32')" + }, + { + "summary": "int32_batchdim_1", + "code": "node = onnx.helper.make_node(\n 'GatherND',\n inputs=['data', 'indices'],\n outputs=['output'],\n batch_dims=1,\n)\n\ndata = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]], dtype=np.int32)\nindices = np.array([[1], [0]], dtype=np.int64)\noutput = gather_nd_impl(data, indices, 1)\nexpected_output = np.array([[2, 3], [4, 5]], dtype=np.int32)\nassert (np.array_equal(output, expected_output))\nexpect(node, inputs=[data, indices], outputs=[output],\n name='test_gathernd_example_int32_batch_dim1')" + } + ] + }, + { + "name": "Gemm", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "General Matrix multiplication:\nhttps://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3\nCompute Y = alpha * A * B + beta * C, where input tensor A has\ndimension (M X K), input tensor B has dimension (K X N), input tensor C and\noutput tensor Y have dimension (M X N).\nIf attribute broadcast is non-zero, input tensor C will be broadcasted to match\nthe dimension requirement. A will be transposed before doing the computation\nif attribute transA is non-zero, same for B and transB.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for the product of input tensors A * B, the default value is 1.0." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for input tensor C, the default value is 1.0." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Whether C should be broadcasted" + }, + { + "name": "transA", + "type": "int64", + "required": false, + "description": "Whether A should be transposed" + }, + { + "name": "transB", + "type": "int64", + "required": false, + "description": "Whether B should be transposed" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Input tensor A" + }, + { + "name": "B", + "type": "T", + "description": "Input tensor B" + }, + { + "name": "C", + "type": "T", + "description": "Input tensor C, can be inplace." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "all_attributes", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.25,\n beta=0.35,\n transA=1,\n transB=1\n)\na = np.random.ranf([4, 3]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.random.ranf([1, 5]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_all_attributes')" + }, + { + "summary": "alpha", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.5\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, alpha=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_alpha')" + }, + { + "summary": "beta", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n beta=0.5\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, beta=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_beta')" + }, + { + "summary": "default_matrix_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.random.ranf([3, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_matrix_bias')" + }, + { + "summary": "default_no_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b'],\n outputs=['y']\n)\na = np.random.ranf([2, 10]).astype(np.float32)\nb = np.random.ranf([10, 3]).astype(np.float32)\ny = gemm_reference_implementation(a, b)\nexpect(node, inputs=[a, b], outputs=[y],\n name='test_gemm_default_no_bias')" + }, + { + "summary": "default_scalar_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 3]).astype(np.float32)\nb = np.random.ranf([3, 4]).astype(np.float32)\nc = np.array(3.14).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_scalar_bias')" + }, + { + "summary": "default_single_elem_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 7]).astype(np.float32)\nb = np.random.ranf([7, 3]).astype(np.float32)\nc = np.random.ranf([1]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_single_elem_vector_bias')" + }, + { + "summary": "default_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_vector_bias')" + }, + { + "summary": "default_zero_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_zero_bias')" + }, + { + "summary": "transposeA", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transA=1\n)\na = np.random.ranf([6, 3]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeA')" + }, + { + "summary": "transposeB", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transB=1\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([4, 6]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transB=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeB')" + } + ], + "category": "Layer" + }, + { + "name": "Gemm", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "General Matrix multiplication:\nhttps://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3\nCompute Y = alpha * A * B + beta * C, where input tensor A has\ndimension (M X K), input tensor B has dimension (K X N), input tensor C and\noutput tensor Y have dimension (M X N).\nIf attribute broadcast is non-zero, input tensor C will be broadcasted to match\nthe dimension requirement. A will be transposed before doing the computation\nif attribute transA is non-zero, same for B and transB.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for the product of input tensors A * B, the default value is 1.0." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for input tensor C, the default value is 1.0." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Whether C should be broadcasted" + }, + { + "name": "transA", + "type": "int64", + "required": false, + "description": "Whether A should be transposed" + }, + { + "name": "transB", + "type": "int64", + "required": false, + "description": "Whether B should be transposed" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Input tensor A" + }, + { + "name": "B", + "type": "T", + "description": "Input tensor B" + }, + { + "name": "C", + "type": "T", + "description": "Input tensor C" + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "all_attributes", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.25,\n beta=0.35,\n transA=1,\n transB=1\n)\na = np.random.ranf([4, 3]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.random.ranf([1, 5]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_all_attributes')" + }, + { + "summary": "alpha", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.5\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, alpha=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_alpha')" + }, + { + "summary": "beta", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n beta=0.5\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, beta=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_beta')" + }, + { + "summary": "default_matrix_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.random.ranf([3, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_matrix_bias')" + }, + { + "summary": "default_no_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b'],\n outputs=['y']\n)\na = np.random.ranf([2, 10]).astype(np.float32)\nb = np.random.ranf([10, 3]).astype(np.float32)\ny = gemm_reference_implementation(a, b)\nexpect(node, inputs=[a, b], outputs=[y],\n name='test_gemm_default_no_bias')" + }, + { + "summary": "default_scalar_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 3]).astype(np.float32)\nb = np.random.ranf([3, 4]).astype(np.float32)\nc = np.array(3.14).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_scalar_bias')" + }, + { + "summary": "default_single_elem_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 7]).astype(np.float32)\nb = np.random.ranf([7, 3]).astype(np.float32)\nc = np.random.ranf([1]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_single_elem_vector_bias')" + }, + { + "summary": "default_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_vector_bias')" + }, + { + "summary": "default_zero_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_zero_bias')" + }, + { + "summary": "transposeA", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transA=1\n)\na = np.random.ranf([6, 3]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeA')" + }, + { + "summary": "transposeB", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transB=1\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([4, 6]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transB=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeB')" + } + ], + "category": "Layer" + }, + { + "name": "Gemm", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "General Matrix multiplication:\nhttps://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3\n\nA' = transpose(A) if transA else A\n\nB' = transpose(B) if transB else B\n\nCompute Y = alpha * A' * B' + beta * C, where input tensor A has shape (M, K) or (K, M),\ninput tensor B has shape (K, N) or (N, K), input tensor C is broadcastable to shape (M, N),\nand output tensor Y has shape (M, N). A will be transposed before doing the\ncomputation if attribute transA is non-zero, same for B and transB.\nThis operator supports **unidirectional broadcasting** (tensor C should be unidirectional broadcastable to tensor A * B); for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for the product of input tensors A * B." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for input tensor C." + }, + { + "name": "transA", + "type": "int64", + "required": false, + "description": "Whether A should be transposed" + }, + { + "name": "transB", + "type": "int64", + "required": false, + "description": "Whether B should be transposed" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Input tensor A. The shape of A should be (M, K) if transA is 0, or (K, M) if transA is non-zero." + }, + { + "name": "B", + "type": "T", + "description": "Input tensor B. The shape of B should be (K, N) if transB is 0, or (N, K) if transB is non-zero." + }, + { + "name": "C", + "type": "T", + "description": "Input tensor C. The shape of C should be unidirectional broadcastable to (M, N)." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor of shape (M, N)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "all_attributes", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.25,\n beta=0.35,\n transA=1,\n transB=1\n)\na = np.random.ranf([4, 3]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.random.ranf([1, 5]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_all_attributes')" + }, + { + "summary": "alpha", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.5\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, alpha=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_alpha')" + }, + { + "summary": "beta", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n beta=0.5\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, beta=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_beta')" + }, + { + "summary": "default_matrix_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.random.ranf([3, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_matrix_bias')" + }, + { + "summary": "default_no_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b'],\n outputs=['y']\n)\na = np.random.ranf([2, 10]).astype(np.float32)\nb = np.random.ranf([10, 3]).astype(np.float32)\ny = gemm_reference_implementation(a, b)\nexpect(node, inputs=[a, b], outputs=[y],\n name='test_gemm_default_no_bias')" + }, + { + "summary": "default_scalar_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 3]).astype(np.float32)\nb = np.random.ranf([3, 4]).astype(np.float32)\nc = np.array(3.14).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_scalar_bias')" + }, + { + "summary": "default_single_elem_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 7]).astype(np.float32)\nb = np.random.ranf([7, 3]).astype(np.float32)\nc = np.random.ranf([1]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_single_elem_vector_bias')" + }, + { + "summary": "default_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_vector_bias')" + }, + { + "summary": "default_zero_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_zero_bias')" + }, + { + "summary": "transposeA", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transA=1\n)\na = np.random.ranf([6, 3]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeA')" + }, + { + "summary": "transposeB", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transB=1\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([4, 6]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transB=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeB')" + } + ], + "category": "Layer" + }, + { + "name": "Gemm", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "General Matrix multiplication:\nhttps://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3\n\nA' = transpose(A) if transA else A\n\nB' = transpose(B) if transB else B\n\nCompute Y = alpha * A' * B' + beta * C, where input tensor A has shape (M, K) or (K, M),\ninput tensor B has shape (K, N) or (N, K), input tensor C is broadcastable to shape (M, N),\nand output tensor Y has shape (M, N). A will be transposed before doing the\ncomputation if attribute transA is non-zero, same for B and transB.\nThis operator supports **unidirectional broadcasting** (tensor C should be unidirectional broadcastable to tensor A * B); for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for the product of input tensors A * B." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for input tensor C." + }, + { + "name": "transA", + "type": "int64", + "required": false, + "description": "Whether A should be transposed" + }, + { + "name": "transB", + "type": "int64", + "required": false, + "description": "Whether B should be transposed" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Input tensor A. The shape of A should be (M, K) if transA is 0, or (K, M) if transA is non-zero." + }, + { + "name": "B", + "type": "T", + "description": "Input tensor B. The shape of B should be (K, N) if transB is 0, or (N, K) if transB is non-zero." + }, + { + "name": "C", + "type": "T", + "description": "Input tensor C. The shape of C should be unidirectional broadcastable to (M, N)." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor of shape (M, N)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "all_attributes", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.25,\n beta=0.35,\n transA=1,\n transB=1\n)\na = np.random.ranf([4, 3]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.random.ranf([1, 5]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_all_attributes')" + }, + { + "summary": "alpha", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.5\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, alpha=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_alpha')" + }, + { + "summary": "beta", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n beta=0.5\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, beta=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_beta')" + }, + { + "summary": "default_matrix_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.random.ranf([3, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_matrix_bias')" + }, + { + "summary": "default_no_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b'],\n outputs=['y']\n)\na = np.random.ranf([2, 10]).astype(np.float32)\nb = np.random.ranf([10, 3]).astype(np.float32)\ny = gemm_reference_implementation(a, b)\nexpect(node, inputs=[a, b], outputs=[y],\n name='test_gemm_default_no_bias')" + }, + { + "summary": "default_scalar_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 3]).astype(np.float32)\nb = np.random.ranf([3, 4]).astype(np.float32)\nc = np.array(3.14).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_scalar_bias')" + }, + { + "summary": "default_single_elem_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 7]).astype(np.float32)\nb = np.random.ranf([7, 3]).astype(np.float32)\nc = np.random.ranf([1]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_single_elem_vector_bias')" + }, + { + "summary": "default_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_vector_bias')" + }, + { + "summary": "default_zero_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_zero_bias')" + }, + { + "summary": "transposeA", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transA=1\n)\na = np.random.ranf([6, 3]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeA')" + }, + { + "summary": "transposeB", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transB=1\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([4, 6]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transB=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeB')" + } + ], + "category": "Layer" + }, + { + "name": "Gemm", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "General Matrix multiplication:\nhttps://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3\n\nA' = transpose(A) if transA else A\n\nB' = transpose(B) if transB else B\n\nCompute Y = alpha * A' * B' + beta * C, where input tensor A has shape (M, K) or (K, M),\ninput tensor B has shape (K, N) or (N, K), input tensor C is broadcastable to shape (M, N),\nand output tensor Y has shape (M, N). A will be transposed before doing the\ncomputation if attribute transA is non-zero, same for B and transB.\nThis operator supports **unidirectional broadcasting** (tensor C should be unidirectional broadcastable to tensor A * B); for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for the product of input tensors A * B." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for input tensor C." + }, + { + "name": "transA", + "type": "int64", + "required": false, + "description": "Whether A should be transposed" + }, + { + "name": "transB", + "type": "int64", + "required": false, + "description": "Whether B should be transposed" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Input tensor A. The shape of A should be (M, K) if transA is 0, or (K, M) if transA is non-zero." + }, + { + "name": "B", + "type": "T", + "description": "Input tensor B. The shape of B should be (K, N) if transB is 0, or (N, K) if transB is non-zero." + }, + { + "name": "C", + "type": "T", + "option": "optional", + "description": "Optional input tensor C. If not specified, the computation is done as if C is a scalar 0. The shape of C should be unidirectional broadcastable to (M, N)." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor of shape (M, N)." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "all_attributes", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.25,\n beta=0.35,\n transA=1,\n transB=1\n)\na = np.random.ranf([4, 3]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.random.ranf([1, 5]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_all_attributes')" + }, + { + "summary": "alpha", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.5\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, alpha=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_alpha')" + }, + { + "summary": "beta", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n beta=0.5\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, beta=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_beta')" + }, + { + "summary": "default_matrix_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.random.ranf([3, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_matrix_bias')" + }, + { + "summary": "default_no_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b'],\n outputs=['y']\n)\na = np.random.ranf([2, 10]).astype(np.float32)\nb = np.random.ranf([10, 3]).astype(np.float32)\ny = gemm_reference_implementation(a, b)\nexpect(node, inputs=[a, b], outputs=[y],\n name='test_gemm_default_no_bias')" + }, + { + "summary": "default_scalar_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 3]).astype(np.float32)\nb = np.random.ranf([3, 4]).astype(np.float32)\nc = np.array(3.14).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_scalar_bias')" + }, + { + "summary": "default_single_elem_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 7]).astype(np.float32)\nb = np.random.ranf([7, 3]).astype(np.float32)\nc = np.random.ranf([1]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_single_elem_vector_bias')" + }, + { + "summary": "default_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_vector_bias')" + }, + { + "summary": "default_zero_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_zero_bias')" + }, + { + "summary": "transposeA", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transA=1\n)\na = np.random.ranf([6, 3]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeA')" + }, + { + "summary": "transposeB", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transB=1\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([4, 6]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transB=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeB')" + } + ], + "category": "Layer" + }, + { + "name": "Gemm", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "General Matrix multiplication:\nhttps://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3\n\nA' = transpose(A) if transA else A\n\nB' = transpose(B) if transB else B\n\nCompute Y = alpha * A' * B' + beta * C, where input tensor A has shape (M, K) or (K, M),\ninput tensor B has shape (K, N) or (N, K), input tensor C is broadcastable to shape (M, N),\nand output tensor Y has shape (M, N). A will be transposed before doing the\ncomputation if attribute transA is non-zero, same for B and transB.\nThis operator supports **unidirectional broadcasting** (tensor C should be unidirectional broadcastable to tensor A * B); for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for the product of input tensors A * B." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Scalar multiplier for input tensor C." + }, + { + "name": "transA", + "type": "int64", + "required": false, + "description": "Whether A should be transposed" + }, + { + "name": "transB", + "type": "int64", + "required": false, + "description": "Whether B should be transposed" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Input tensor A. The shape of A should be (M, K) if transA is 0, or (K, M) if transA is non-zero." + }, + { + "name": "B", + "type": "T", + "description": "Input tensor B. The shape of B should be (K, N) if transB is 0, or (N, K) if transB is non-zero." + }, + { + "name": "C", + "type": "T", + "option": "optional", + "description": "Optional input tensor C. If not specified, the computation is done as if C is a scalar 0. The shape of C should be unidirectional broadcastable to (M, N)." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor of shape (M, N)." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "all_attributes", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.25,\n beta=0.35,\n transA=1,\n transB=1\n)\na = np.random.ranf([4, 3]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.random.ranf([1, 5]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_all_attributes')" + }, + { + "summary": "alpha", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n alpha=0.5\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, alpha=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_alpha')" + }, + { + "summary": "beta", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n beta=0.5\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, beta=0.5)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_beta')" + }, + { + "summary": "default_matrix_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.random.ranf([3, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_matrix_bias')" + }, + { + "summary": "default_no_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b'],\n outputs=['y']\n)\na = np.random.ranf([2, 10]).astype(np.float32)\nb = np.random.ranf([10, 3]).astype(np.float32)\ny = gemm_reference_implementation(a, b)\nexpect(node, inputs=[a, b], outputs=[y],\n name='test_gemm_default_no_bias')" + }, + { + "summary": "default_scalar_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 3]).astype(np.float32)\nb = np.random.ranf([3, 4]).astype(np.float32)\nc = np.array(3.14).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_scalar_bias')" + }, + { + "summary": "default_single_elem_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 7]).astype(np.float32)\nb = np.random.ranf([7, 3]).astype(np.float32)\nc = np.random.ranf([1]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_single_elem_vector_bias')" + }, + { + "summary": "default_vector_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([2, 7]).astype(np.float32)\nb = np.random.ranf([7, 4]).astype(np.float32)\nc = np.random.ranf([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_vector_bias')" + }, + { + "summary": "default_zero_bias", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y']\n)\na = np.random.ranf([3, 5]).astype(np.float32)\nb = np.random.ranf([5, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_default_zero_bias')" + }, + { + "summary": "transposeA", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transA=1\n)\na = np.random.ranf([6, 3]).astype(np.float32)\nb = np.random.ranf([6, 4]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transA=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeA')" + }, + { + "summary": "transposeB", + "code": "node = onnx.helper.make_node(\n 'Gemm',\n inputs=['a', 'b', 'c'],\n outputs=['y'],\n transB=1\n)\na = np.random.ranf([3, 6]).astype(np.float32)\nb = np.random.ranf([4, 6]).astype(np.float32)\nc = np.zeros([1, 4]).astype(np.float32)\ny = gemm_reference_implementation(a, b, c, transB=1)\nexpect(node, inputs=[a, b, c], outputs=[y],\n name='test_gemm_transposeB')" + } + ], + "category": "Layer" + }, + { + "name": "GlobalAveragePool", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "GlobalAveragePool consumes an input tensor X and applies average pooling across\n the values in the same channel. This is equivalent to AveragePool with kernel size\n equal to the spatial dimension of input tensor.", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from pooling across the input tensor. The output tensor has the same rank as the input. The first two dimensions of output shape are the same as the input (N x C), while the other dimensions are all 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "globalaveragepool", + "code": "node = onnx.helper.make_node(\n 'GlobalAveragePool',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 5, 5).astype(np.float32)\ny = np.mean(x, axis=tuple(range(2, np.ndim(x))), keepdims=True)\nexpect(node, inputs=[x], outputs=[y], name='test_globalaveragepool')" + }, + { + "summary": "globalaveragepool_precomputed", + "code": "\nnode = onnx.helper.make_node(\n 'GlobalAveragePool',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.array([[[\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9],\n]]]).astype(np.float32)\ny = np.array([[[[5]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name='test_globalaveragepool_precomputed')" + } + ], + "category": "Pool" + }, + { + "name": "GlobalLpPool", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "GlobalLpPool consumes an input tensor X and applies lp pool pooling across the\n the values in the same channel. This is equivalent to LpPool with kernel size\n equal to the spatial dimension of input tensor.", + "attributes": [ + { + "name": "p", + "type": "float32", + "required": false, + "default": 2.0, + "description": "p value of the Lp norm used to pool over the input data, default is 2.0." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimension are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from pooling across the input tensor. Dimensions will be N x C x 1 x 1" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "category": "Pool" + }, + { + "name": "GlobalLpPool", + "module": "ai.onnx", + "version": 2, + "support_level": "common", + "description": "GlobalLpPool consumes an input tensor X and applies lp pool pooling across\n the values in the same channel. This is equivalent to LpPool with kernel size\n equal to the spatial dimension of input tensor.", + "attributes": [ + { + "name": "p", + "type": "int64", + "required": false, + "default": 2, + "description": "p value of the Lp norm used to pool over the input data." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from pooling across the input tensor. The output tensor has the same rank as the input. The first two dimensions of output shape are the same as the input (N x C), while the other dimensions are all 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "category": "Pool" + }, + { + "name": "GlobalMaxPool", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "GlobalMaxPool consumes an input tensor X and applies max pooling across\n the values in the same channel. This is equivalent to MaxPool with kernel size\n equal to the spatial dimension of input tensor.", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from pooling across the input tensor. The output tensor has the same rank as the input. The first two dimensions of output shape are the same as the input (N x C), while the other dimensions are all 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "globalmaxpool", + "code": "\nnode = onnx.helper.make_node(\n 'GlobalMaxPool',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 5, 5).astype(np.float32)\ny = np.max(x, axis=tuple(range(2, np.ndim(x))), keepdims=True)\nexpect(node, inputs=[x], outputs=[y], name='test_globalmaxpool')" + }, + { + "summary": "globalmaxpool_precomputed", + "code": "\nnode = onnx.helper.make_node(\n 'GlobalMaxPool',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.array([[[\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9],\n]]]).astype(np.float32)\ny = np.array([[[[9]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name='test_globalmaxpool_precomputed')" + } + ], + "category": "Pool" + }, + { + "name": "Gradient", + "module": "ai.onnx.preview.training", + "version": 1, + "support_level": "common", + "description": "Gradient operator computes the partial derivatives of a specific tensor w.r.t.\nsome other tensors. This operator is widely used in gradient-based training\nalgorithms. To illustrate its use, let's consider a computation graph,\n\n```\nX -----.\n |\n v\nW --> Conv --> H --> Gemm --> Y\n ^\n |\n Z\n```\n\n, where W and Z are trainable tensors. Note that operators' attributes are\nomitted for the sake of simplicity. Let dY/dW (dY/dZ) be the gradient of\nY with respect to W (Z). The user can compute gradient by inserting Gradient\noperator to form another graph shown below.\n\n```\nW --> Conv --> H --> Gemm --> Y\n| ^ ^\n| | |\n| X Z\n| | |\n| | .----------'\n| | | (W/Z/X is the 1st/2nd/3rd input of Gradient as shown in\n| | | \"xs\" followed by \"zs\")\n| v v\n'---> Gradient(xs=[\"W\", \"Z\"], zs=[\"X\"], y=\"Y\")\n | |\n | '-----------------------------------> dY/dW (1st output of Gradient)\n |\n '---------------------------------------> dY/dZ (2nd output of Gradient)\n```\n\nBy definition, the tensor \"y\" is a function of independent variables in \"xs\"\nand \"zs\". Since we only compute the gradient of \"y\" w.r.t. the differentiable\nvariables in \"xs\", this Gradient only outputs dY/dW and dY/dZ. Note that \"H\"\ncannot appear in \"xs\" and \"zs\". The reason is that \"H\" can be determined by\ntensors \"W\" and \"X\" and therefore \"H\" is not an independent variable.\n\nAll outputs are optional. If needed, for example, user can assign an empty\nstring to the 1st output name of that Gradient to skip the generation of dY/dW.\nNote that the concept of optional outputs can also be found in ONNX's RNN, GRU,\nand LSTM.\n\nGradient operator can compute derivative against intermediate tensors. For\nexample, the gradient of Y with respect to H can be done via\n\n```\nW --> Conv --> H --> Gemm --> Y\n ^ | ^\n | | |\n X | Z\n .-------' |\n | .----------'\n | | (H/Z is the 1st/2nd input of Gradient as shown in \"xs\")\n v v\n Gradient(xs=[\"H\", \"Z\"], y=\"Y\")\n | |\n | '-----------------------------------> dY/dH (1st output of Gradient)\n |\n '---------------------------------------> dY/dZ (2nd output of Gradient)\n```\n\nIt is possible to represent high-order differentiation using Gradient operators.\nFor example, given the following linear model:\n\n```\nW --> Gemm --> Y --> Loss --> O\n ^ ^\n | |\n X L\n```\n\nTo compute the 2nd order derivative of O with respect to W (denoted by\nd^2O/dW^2), one can do\n\n```\nW --> Gemm --> Y --> Loss --> O\n| ^ ^\n| | |\n| X .------------L\n| | | |\n| | | v\n+------+-+> Gradient(xs=[\"X\", \"W\"], zs=[\"L\"], y=\"O\") ---> dO/dX (1st output of Gradient)\n| | | |\n| | | '---> dO/dW (2nd output of Gradient)\n| v v\n'---> Gradient(xs=[\"X\", \"W\"], zs=[\"L\"], y=\"dO/dW\") ---> d(dO/dW)dX (1st output of\n | Gradient)\n |\n |\n '---> d^2O/dW^2 (2nd output of Gradient)\n```\n\nThe tensors named in attributes \"xs\", \"zs\", and \"y\" define the differentiated\ncomputation graph, and the inputs to Gradient node define the values at\nwhich the gradient is computed. We can feed different tensors to the identified\ngraph. For example, one can compute the gradient of Y with respect to H at\na specific value of H, H_1, by providing that value as an input to the Gradient\nnode.\n\n```\nW --> Conv --> H --> Gemm --> Y\n ^ ^\n | |\n X Z\n\n Z_1 (2nd input of Gradient)\n |\n v\nH_1 --> Gradient(xs=[\"H\", \"Z\"], y=\"Y\") ---> dY/dH when H = H_1 and Y = Y_1.\n |\n '------------------------------> dY/dZ (2nd output of Gradient)\n```\n\nWhen the inputs of Gradient are the tensors named in \"xs\" and \"zs\", the\ncomputation can be optimized. More specifically, intermediate variables in\nforward pass can be reused if the gradient is computed via reverse-mode\nauto-differentiation.\n\n", + "attributes": [ + { + "name": "xs", + "type": "string[]", + "required": true, + "description": "Input tensor names of the differentiated sub-graph. It contains only the necessary differentiated inputs of a (sub-)graph. Variables (usually called intermediate variables) that can be generated from inputs cannot be included in this attribute." + }, + { + "name": "y", + "type": "string", + "required": true, + "description": "The targeted tensor. It can be viewed as the output of the differentiated function. The attribute \"xs\" and attribute \"zs\" are the minimal independent variable set that determines the value of \"y\"." + }, + { + "name": "zs", + "type": "string[]", + "required": false, + "description": "Input tensor names of the differentiated sub-graph. It contains only the necessary non-differentiated inputs of a (sub-)graph. Variables (usually called intermediate variables) that can be generated from inputs cannot be included in this attribute." + } + ], + "inputs": [ + { + "name": "Inputs", + "type": "T1", + "list": true, + "description": "The values fed into graph identified by the attributes. The i-th input is the value of the i-th tensor specified in the concatenated list of the attribute \"xs\" and the attribute \"zs\". For example, if xs=[\"A\", \"B\"] and zs=[\"C\"], the first input is used as the value of symbol \"A\" and the 3rd input is substituted for all the occurrences of \"C\"." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "Outputs", + "type": "T2", + "list": true, + "description": "The gradient of the tensor specified by the attribute \"y\" with respect to each of tensors specified in the attribute \"xs\". The i-th output is the gradient of \"y\" with respect to the i-th tensor specified in the attribute \"xs\"." + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "1 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Allow outputs to be any kind of tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Allow inputs to be any kind of floating-point tensor.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "gradient_scalar_add", + "code": "add_node = onnx.helper.make_node('Add',\n ['a', 'b'], ['c'], name='my_add')\ngradient_node = onnx.helper.make_node(\n 'Gradient', ['a', 'b'],\n ['dc_da', 'dc_db'], name='my_gradient',\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN,\n xs=['a', 'b'], y='c')\n\na = np.array(1.0).astype(np.float32)\nb = np.array(2.0).astype(np.float32)\nc = a + b\n# dc / da = d(a+b) / da = 1\ndc_da = np.array(1).astype(np.float32)\n# db / db = d(a+b) / db = 1\ndc_db = np.array(1).astype(np.float32)\n\ngraph = onnx.helper.make_graph(\n nodes=[add_node, gradient_node],\n name='GradientOfAdd',\n inputs=[\n onnx.helper.make_tensor_value_info('a', onnx.TensorProto.FLOAT,\n []),\n onnx.helper.make_tensor_value_info('b', onnx.TensorProto.FLOAT,\n [])],\n outputs=[\n onnx.helper.make_tensor_value_info('c', onnx.TensorProto.FLOAT,\n []),\n onnx.helper.make_tensor_value_info('dc_da',\n onnx.TensorProto.FLOAT, []),\n onnx.helper.make_tensor_value_info('dc_db',\n onnx.TensorProto.FLOAT, [])])\nopsets = [\n onnx.helper.make_operatorsetid(ONNX_DOMAIN, 12),\n onnx.helper.make_operatorsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)]\nmodel = onnx.helper.make_model(\n graph,\n producer_name='backend-test',\n opset_imports=opsets)\nexpect(model, inputs=[a, b], outputs=[c, dc_da, dc_db],\n name='test_gradient_of_add')" + }, + { + "summary": "gradient_scalar_add_and_mul", + "code": "add_node = onnx.helper.make_node('Add',\n ['a', 'b'], ['c'], name='my_add')\nmul_node = onnx.helper.make_node('Mul',\n ['c', 'a'], ['d'], name='my_mul')\ngradient_node = onnx.helper.make_node(\n 'Gradient', ['a', 'b'],\n ['dd_da', 'dd_db'], name='my_gradient',\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN,\n xs=['a', 'b'], y='d')\n\na = np.array(1.0).astype(np.float32)\nb = np.array(2.0).astype(np.float32)\nc = a + b\n# d = a * c = a * (a + b)\nd = a * c\n# dd / da = d(a*a+a*b) / da = 2 * a + b\ndd_da = (2 * a + b).astype(np.float32)\n# dd / db = d(a*a+a*b) / db = a\ndd_db = a\n\ngraph = onnx.helper.make_graph(\n nodes=[add_node, mul_node, gradient_node],\n name='GradientOfTwoOperators',\n inputs=[\n onnx.helper.make_tensor_value_info('a', onnx.TensorProto.FLOAT,\n []),\n onnx.helper.make_tensor_value_info('b', onnx.TensorProto.FLOAT,\n [])],\n outputs=[\n onnx.helper.make_tensor_value_info('d', onnx.TensorProto.FLOAT,\n []),\n onnx.helper.make_tensor_value_info('dd_da',\n onnx.TensorProto.FLOAT, []),\n onnx.helper.make_tensor_value_info('dd_db',\n onnx.TensorProto.FLOAT, [])])\n\nopsets = [\n onnx.helper.make_operatorsetid(ONNX_DOMAIN, 12),\n onnx.helper.make_operatorsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)]\nmodel = onnx.helper.make_model(graph,\n producer_name='backend-test',\n opset_imports=opsets)\nexpect(model, inputs=[a, b], outputs=[d, dd_da, dd_db],\n name='test_gradient_of_add_and_mul')" + } + ] + }, + { + "name": "Greater", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `greater` logical operation\nelementwise on the input tensors `A` and `B`.\n\nIf broadcasting is enabled, the right-hand-side argument will be broadcasted\nto match the shape of left-hand-side argument. See the doc of `Add` for a\ndetailed description of the broadcasting rules.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Left input tensor for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Right input tensor for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "greater", + "code": "node = onnx.helper.make_node(\n 'Greater',\n inputs=['x', 'y'],\n outputs=['greater'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater')" + }, + { + "summary": "greater", + "code": "node = onnx.helper.make_node(\n 'GreaterOrEqual',\n inputs=['x', 'y'],\n outputs=['greater_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_equal')" + }, + { + "summary": "greater_broadcast", + "code": "node = onnx.helper.make_node(\n 'Greater',\n inputs=['x', 'y'],\n outputs=['greater'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_bcast')" + }, + { + "summary": "greater_broadcast", + "code": "node = onnx.helper.make_node(\n 'GreaterOrEqual',\n inputs=['x', 'y'],\n outputs=['greater_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Greater", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `greater` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "greater", + "code": "node = onnx.helper.make_node(\n 'Greater',\n inputs=['x', 'y'],\n outputs=['greater'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater')" + }, + { + "summary": "greater", + "code": "node = onnx.helper.make_node(\n 'GreaterOrEqual',\n inputs=['x', 'y'],\n outputs=['greater_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_equal')" + }, + { + "summary": "greater_broadcast", + "code": "node = onnx.helper.make_node(\n 'Greater',\n inputs=['x', 'y'],\n outputs=['greater'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_bcast')" + }, + { + "summary": "greater_broadcast", + "code": "node = onnx.helper.make_node(\n 'GreaterOrEqual',\n inputs=['x', 'y'],\n outputs=['greater_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Greater", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `greater` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "greater", + "code": "node = onnx.helper.make_node(\n 'Greater',\n inputs=['x', 'y'],\n outputs=['greater'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater')" + }, + { + "summary": "greater", + "code": "node = onnx.helper.make_node(\n 'GreaterOrEqual',\n inputs=['x', 'y'],\n outputs=['greater_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_equal')" + }, + { + "summary": "greater_broadcast", + "code": "node = onnx.helper.make_node(\n 'Greater',\n inputs=['x', 'y'],\n outputs=['greater'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_bcast')" + }, + { + "summary": "greater_broadcast", + "code": "node = onnx.helper.make_node(\n 'GreaterOrEqual',\n inputs=['x', 'y'],\n outputs=['greater_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Greater", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `greater` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "greater", + "code": "node = onnx.helper.make_node(\n 'Greater',\n inputs=['x', 'y'],\n outputs=['greater'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater')" + }, + { + "summary": "greater", + "code": "node = onnx.helper.make_node(\n 'GreaterOrEqual',\n inputs=['x', 'y'],\n outputs=['greater_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_equal')" + }, + { + "summary": "greater_broadcast", + "code": "node = onnx.helper.make_node(\n 'Greater',\n inputs=['x', 'y'],\n outputs=['greater'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_bcast')" + }, + { + "summary": "greater_broadcast", + "code": "node = onnx.helper.make_node(\n 'GreaterOrEqual',\n inputs=['x', 'y'],\n outputs=['greater_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_greater_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "GreaterOrEqual", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `greater_equal` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ] + }, + { + "name": "GreaterOrEqual", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `greater_equal` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ] + }, + { + "name": "GridSample", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "Given an `input` and a flow-field `grid`, computes the `output` using `input` values and pixel locations from `grid`.\nCurrently, only spatial (4-D) inputs are supported. For `input` with shape (N, C, H, W) and `grid` with shape (N, H_out, W_out, 2),\nthe `output` will have shape (N, C, H_out, W_out).\nFor each output location `output[N, C, H_out, W_out]`, the size-2 vector `grid[N, H_out, W_out]` specifies `input` pixel locations `x` and `y`,\nwhich are used to interpolate the output value `output[N, C, H_out, W_out]`.\n\nThe GridSample operator is often used in doing grid generator and sampler in the [Spatial Transformer Networks](https://arxiv.org/abs/1506.02025).\nSee also in [torch.nn.functional.grid_sample](https://pytorch.org/docs/master/generated/torch.nn.functional.grid_sample.html#torch-nn-functional-grid-sample).\n", + "attributes": [ + { + "name": "align_corners", + "type": "int64", + "required": false, + "description": "If align_corners=1, the extrema (-1 and 1) are considered as referring to the center points of the input's corner pixels. If align_corners=0, they are instead considered as referring to the corner points of the input's corner pixels, making the sampling more resolution agnostic." + }, + { + "name": "mode", + "type": "string", + "required": false, + "default": "bilinear", + "description": "Three interpolation modes: bilinear (default), nearest and bicubic." + }, + { + "name": "padding_mode", + "type": "string", + "required": false, + "default": "zeros", + "description": "Support padding modes for outside grid values: `zeros`(default), `border`, `reflection`. zeros: use 0 for out-of-bound grid locations, border: use border values for out-of-bound grid locations, reflection: use values at locations reflected by the border for out-of-bound grid locations. If index 0 represents the margin pixel, the reflected value at index -1 will be the same as the value at index 1. For location far away from the border, it will keep being reflected until becoming in bound. If pixel location x = -3.5 reflects by border -1 and becomes x' = 1.5, then reflects by border 1 and becomes x'' = 0.5." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "4-D tensor of shape (N, C, H, W), where N is the batch size, C is the numbers of channels, H and W are the height and width of the input data." + }, + { + "name": "grid", + "type": "T1", + "description": "Input offset, 4-D tensor of shape (N, H_out, W_out, 2), where H_out and W_out are the height and width of grid and output, Grid specifies the sampling pixel locations normalized by the input spatial dimensions. Therefore, it should have most values in the range of [-1, 1]. If grid has values outside the range of [-1, 1], the corresponding outputs will be handled as defined by padding_mode." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "4-D tensor of shape (N, C, H_out, W_out)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all tensor types.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain output types to float tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "gridsample", + "code": "node = onnx.helper.make_node(\n 'GridSample',\n inputs=['X', 'Grid'],\n outputs=['Y'],\n mode='bilinear',\n padding_mode='zeros',\n align_corners=0,\n)\n# X shape, [N, C, H, W] - [1, 1, 4, 4]\nX = np.array(\n [\n [\n [\n [0., 1., 2., 3.],\n [4., 5., 6., 7.],\n [8., 9., 10., 11.],\n [12., 13., 14., 15.]\n ]\n ]\n ],\n dtype=np.float32,\n)\n# Grid shape, [N, H_out, W_out, 2] - [1, 6, 6, 2]\nGrid = np.array(\n [\n [\n [\n [-1.0000, -1.0000],\n [-0.6000, -1.0000],\n [-0.2000, -1.0000],\n [0.2000, -1.0000],\n [0.6000, -1.0000],\n [1.0000, -1.0000]\n ],\n [\n [-1.0000, -0.6000],\n [-0.6000, -0.6000],\n [-0.2000, -0.6000],\n [0.2000, -0.6000],\n [0.6000, -0.6000],\n [1.0000, -0.6000]\n ],\n [\n [-1.0000, -0.2000],\n [-0.6000, -0.2000],\n [-0.2000, -0.2000],\n [0.2000, -0.2000],\n [0.6000, -0.2000],\n [1.0000, -0.2000]\n ],\n [\n [-1.0000, 0.2000],\n [-0.6000, 0.2000],\n [-0.2000, 0.2000],\n [0.2000, 0.2000],\n [0.6000, 0.2000],\n [1.0000, 0.2000]\n ],\n [\n [-1.0000, 0.6000],\n [-0.6000, 0.6000],\n [-0.2000, 0.6000],\n [0.2000, 0.6000],\n [0.6000, 0.6000],\n [1.0000, 0.6000]\n ],\n [\n [-1.0000, 1.0000],\n [-0.6000, 1.0000],\n [-0.2000, 1.0000],\n [0.2000, 1.0000],\n [0.6000, 1.0000],\n [1.0000, 1.0000]\n ]\n ]\n ],\n dtype=np.float32,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 6, 6]\nY = np.array(\n [\n [\n [\n [0.0000, 0.1500, 0.5500, 0.9500, 1.3500, 0.7500],\n [0.6000, 1.5000, 2.3000, 3.1000, 3.9000, 2.1000],\n [2.2000, 4.7000, 5.5000, 6.3000, 7.1000, 3.7000],\n [3.8000, 7.9000, 8.7000, 9.5000, 10.3000, 5.3000],\n [5.4000, 11.1000, 11.9000, 12.7000, 13.5000, 6.9000],\n [3.0000, 6.1500, 6.5500, 6.9500, 7.3500, 3.7500]\n ]\n ]\n ],\n dtype=np.float32,\n)\nexpect(node, inputs=[X, Grid], outputs=[Y],\n name='test_gridsample')" + }, + { + "summary": "gridsample_mode_aligncorners", + "code": "# X shape, [N, C, H, W] - [1, 1, 3, 2]\nX = np.array(\n [\n [\n [\n [0., 1.],\n [2., 3.],\n [4., 5.]\n ]\n ]\n ],\n dtype=np.float32,\n)\n# Grid shape, [N, H_out, W_out, 2] - [1, 2, 4, 2]\nGrid = np.array(\n [\n [\n [\n [-1.0000, -1.0000],\n [-0.5000, -0.5000],\n [-0.2000, -0.2000],\n [0.0000, 0.0000]\n ],\n\n [\n [0.0000, 0.0000],\n [-0.2000, -0.2000],\n [0.5000, 0.5000],\n [1.0000, 1.0000]\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# setting mode = 'bilinear', default align_corners = 0\nnode = onnx.helper.make_node(\n 'GridSample',\n inputs=['X', 'Grid'],\n outputs=['Y'],\n mode='bilinear',\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [\n [\n [\n [0.0000, 0.5000, 1.7000, 2.5000],\n [2.5000, 1.7000, 4.5000, 1.2500]\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, Grid], outputs=[Y_bilinear],\n name='test_gridsample_bilinear')\n\n# setting mode = 'bilinear', align_corners = 1\nnode = onnx.helper.make_node(\n 'GridSample',\n inputs=['X', 'Grid'],\n outputs=['Y'],\n mode='bilinear',\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_align_corners = np.array(\n [\n [\n [\n [0.0000, 1.2500, 2.0000, 2.5000],\n [2.5000, 2.0000, 3.7500, 5.0000]\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, Grid], outputs=[Y_align_corners],\n name='test_gridsample_aligncorners_true')\n\n# setting mode = 'nearest'\nnode = onnx.helper.make_node(\n 'GridSample',\n inputs=['X', 'Grid'],\n outputs=['Y'],\n mode='nearest',\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [\n [\n [\n [0., 0., 2., 2.],\n [2., 2., 5., 0.]\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, Grid], outputs=[Y_nearest],\n name='test_gridsample_nearest')\n\n# setting mode = 'bicubic'\nnode = onnx.helper.make_node(\n 'GridSample',\n inputs=['X', 'Grid'],\n outputs=['Y'],\n mode='bicubic',\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [\n [\n [\n [-0.1406, 0.3828, 1.7556, 2.9688],\n [2.9688, 1.7556, 5.1445, 1.3906]\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, Grid], outputs=[Y_bicubic],\n name='test_gridsample_bicubic')" + }, + { + "summary": "gridsample_paddingmode", + "code": "# X shape, [N, C, H, W] - [1, 1, 3, 2]\nX = np.array(\n [\n [\n [\n [0., 1.],\n [2., 3.],\n [4., 5.]\n ]\n ]\n ],\n dtype=np.float32,\n)\n# Grid shape, [N, H_out, W_out, 2] - [1, 2, 4, 2]\nGrid = np.array(\n [\n [\n [\n [-10.0000, -10.0000],\n [-5.0000, -5.0000],\n [-0.2000, -0.2000],\n [10.0000, 10.0000]\n ],\n\n [\n [10.0000, 10.0000],\n [-0.2000, -0.2000],\n [5.0000, 5.0000],\n [10.0000, 10.0000]\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# setting padding_mode = 'zeros'\nnode = onnx.helper.make_node(\n 'GridSample',\n inputs=['X', 'Grid'],\n outputs=['Y'],\n padding_mode='zeros',\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_zeros = np.array(\n [\n [\n [\n [0.0000, 0.0000, 1.7000, 0.0000],\n [0.0000, 1.7000, 0.0000, 0.0000]\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, Grid], outputs=[Y_zeros],\n name='test_gridsample_zeros_padding')\n\n# setting padding_mode = 'border'\nnode = onnx.helper.make_node(\n 'GridSample',\n inputs=['X', 'Grid'],\n outputs=['Y'],\n padding_mode='border',\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_border = np.array(\n [\n [\n [\n [0.0000, 0.0000, 1.7000, 5.0000],\n [5.0000, 1.7000, 5.0000, 5.0000]\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, Grid], outputs=[Y_border],\n name='test_gridsample_border_padding')\n\n# setting padding_mode = 'reflection'\nnode = onnx.helper.make_node(\n 'GridSample',\n inputs=['X', 'Grid'],\n outputs=['Y'],\n padding_mode='reflection',\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_reflection = np.array(\n [\n [\n [\n [2.5000, 0.0000, 1.7000, 2.5000],\n [2.5000, 1.7000, 5.0000, 2.5000]\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, Grid], outputs=[Y_reflection],\n name='test_gridsample_reflection_padding')" + } + ] + }, + { + "name": "HardSigmoid", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "HardSigmoid takes one input data (Tensor) and produces one output data\n(Tensor) where the HardSigmoid function, y = max(0, min(1, alpha * x + beta)),\nis applied to the tensor elementwise.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 0.20000000298023224, + "description": "Value of alpha default to 0.2" + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 0.5, + "description": "Value of beta default to 0.5" + }, + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "hardsigmoid", + "code": "node = onnx.helper.make_node(\n 'HardSigmoid',\n inputs=['x'],\n outputs=['y'],\n alpha=0.5,\n beta=0.6\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.clip(x * 0.5 + 0.6, 0, 1) # expected output [0.1, 0.6, 1.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardsigmoid_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x * 0.5 + 0.6, 0, 1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardsigmoid')" + }, + { + "summary": "hardsigmoid_default", + "code": "default_alpha = 0.2\ndefault_beta = 0.5\nnode = onnx.helper.make_node(\n 'HardSigmoid',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x * default_alpha + default_beta, 0, 1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardsigmoid_default')" + } + ], + "category": "Activation" + }, + { + "name": "HardSigmoid", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "HardSigmoid takes one input data (Tensor) and produces one output data\n(Tensor) where the HardSigmoid function, y = max(0, min(1, alpha * x + beta)),\nis applied to the tensor elementwise.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 0.20000000298023224, + "description": "Value of alpha." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 0.5, + "description": "Value of beta." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "hardsigmoid", + "code": "node = onnx.helper.make_node(\n 'HardSigmoid',\n inputs=['x'],\n outputs=['y'],\n alpha=0.5,\n beta=0.6\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.clip(x * 0.5 + 0.6, 0, 1) # expected output [0.1, 0.6, 1.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardsigmoid_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x * 0.5 + 0.6, 0, 1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardsigmoid')" + }, + { + "summary": "hardsigmoid_default", + "code": "default_alpha = 0.2\ndefault_beta = 0.5\nnode = onnx.helper.make_node(\n 'HardSigmoid',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x * default_alpha + default_beta, 0, 1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardsigmoid_default')" + } + ], + "category": "Activation" + }, + { + "name": "HardSwish", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "HardSwish takes one input data (Tensor) and produces one output data (Tensor) where\nthe HardSwish function, y = x * max(0, min(1, alpha * x + beta)) = x * HardSigmoid(x),\nwhere alpha = 1/6 and beta = 0.5, is applied to the tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "hardswish", + "code": "node = onnx.helper.make_node(\n 'HardSwish',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = hardswish(x)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardswish')" + } + ] + }, + { + "name": "Hardmax", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "The operator computes the hardmax (1 for the first maximum value, and 0 for all others) values for each layer in the batch\n of the given input. The input is a 2-D tensor (Tensor) of size\n(batch_size x input_feature_dimensions). The output tensor has the same shape\nand contains the hardmax values of the corresponding input.\n\nInput does not need to explicitly be a 2D vector; rather, it will be\ncoerced into one. For an arbitrary n-dimensional tensor\ninput \\in [a_0, a_1, ..., a_{k-1}, a_k, ..., a_{n-1}] and k is\nthe axis provided, then input will be coerced into a 2-dimensional tensor with\ndimensions [a_0 * ... * a_{k-1}, a_k * ... * a_{n-1}]. For the default\ncase where axis=1, this means the input tensor will be coerced into a 2D tensor\nof dimensions [a_0, a_1 * ... * a_{n-1}], where a_0 is often the batch size.\nIn this situation, we must have a_0 = N and a_1 * ... * a_{n-1} = D.\nEach of these dimensions must be matched correctly, or else the operator\nwill throw errors.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Describes the axis of the inputs when coerced to 2D; defaults to one because the 0th axis most likely describes the batch_size" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input tensor that's coerced into a 2D matrix of size (NxD) as described above." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output values with the same shape as input tensor (the original size without coercion)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "hardmax", + "code": "node = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([[3, 0, 1, 2], [2, 5, 1, 0], [0, 1, 3, 2],\n [0, 1, 2, 3]]).astype(np.float32)\n# expect result:\n# [[1. 0. 0. 0.]\n# [0. 1. 0. 0.]\n# [0. 0. 1. 0.]\n# [0. 0. 0. 1.]]\ny = hardmax(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_example')\n\n# For multiple occurrences of the maximal values, the first occurrence is selected for one-hot output\nx = np.array([[3, 3, 3, 1]]).astype(np.float32)\n# expect result:\n# [[1, 0, 0, 0]]\ny = hardmax(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_one_hot')" + }, + { + "summary": "hardmax_axis", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=0,\n)\ny = hardmax(x, axis=0)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_axis_0')\n\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=1,\n)\ny = hardmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_axis_1')\n\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=2,\n)\ny = hardmax(x, axis=2)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_axis_2')\n\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=-1,\n)\ny = hardmax(x, axis=-1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_negative_axis')\n\n# default axis is -1\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_default_axis')" + } + ] + }, + { + "name": "Hardmax", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "The operator computes the hardmax (1 for the first maximum value, and 0 for all others) values for each layer in the batch\n of the given input.\n\nThe input does not need to explicitly be a 2D vector; rather, it will be\ncoerced into one. For an arbitrary n-dimensional tensor\ninput \\in [a_0, a_1, ..., a_{k-1}, a_k, ..., a_{n-1}] and k is\nthe axis provided, then input will be coerced into a 2-dimensional tensor with\ndimensions [a_0 * ... * a_{k-1}, a_k * ... * a_{n-1}]. For the default\ncase where axis=1, this means the input tensor will be coerced into a 2D tensor\nof dimensions [a_0, a_1 * ... * a_{n-1}], where a_0 is often the batch size.\nIn this situation, we must have a_0 = N and a_1 * ... * a_{n-1} = D.\nEach of these dimensions must be matched correctly, or else the operator\nwill throw errors. The output tensor has the same shape\nand contains the hardmax values of the corresponding input.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Describes the axis of the inputs when coerced to 2D; defaults to one because the 0th axis most likely describes the batch_size. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input tensor that's coerced into a 2D matrix of size (NxD) as described above." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output values with the same shape as input tensor (the original size without coercion)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "hardmax", + "code": "node = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([[3, 0, 1, 2], [2, 5, 1, 0], [0, 1, 3, 2],\n [0, 1, 2, 3]]).astype(np.float32)\n# expect result:\n# [[1. 0. 0. 0.]\n# [0. 1. 0. 0.]\n# [0. 0. 1. 0.]\n# [0. 0. 0. 1.]]\ny = hardmax(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_example')\n\n# For multiple occurrences of the maximal values, the first occurrence is selected for one-hot output\nx = np.array([[3, 3, 3, 1]]).astype(np.float32)\n# expect result:\n# [[1, 0, 0, 0]]\ny = hardmax(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_one_hot')" + }, + { + "summary": "hardmax_axis", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=0,\n)\ny = hardmax(x, axis=0)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_axis_0')\n\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=1,\n)\ny = hardmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_axis_1')\n\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=2,\n)\ny = hardmax(x, axis=2)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_axis_2')\n\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=-1,\n)\ny = hardmax(x, axis=-1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_negative_axis')\n\n# default axis is -1\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_default_axis')" + } + ] + }, + { + "name": "Hardmax", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "The operator computes the hardmax values for the given input:\n\n Hardmax(element in input, axis) = 1 if the element is the first maximum value along the specified axis, 0 otherwise\n\nThe \"axis\" attribute indicates the dimension along which Hardmax\nwill be performed. The output tensor has the same shape\nand contains the Hardmax values of the corresponding input.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": -1, + "description": "\nDescribes the dimension Hardmax will be performed on.\nNegative value means counting dimensions\nfrom the back. Accepted range is [-r, r-1] where r = rank(input).\n" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input tensor of rank >= axis." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output values with the same shape as the input tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "hardmax", + "code": "node = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([[3, 0, 1, 2], [2, 5, 1, 0], [0, 1, 3, 2],\n [0, 1, 2, 3]]).astype(np.float32)\n# expect result:\n# [[1. 0. 0. 0.]\n# [0. 1. 0. 0.]\n# [0. 0. 1. 0.]\n# [0. 0. 0. 1.]]\ny = hardmax(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_example')\n\n# For multiple occurrences of the maximal values, the first occurrence is selected for one-hot output\nx = np.array([[3, 3, 3, 1]]).astype(np.float32)\n# expect result:\n# [[1, 0, 0, 0]]\ny = hardmax(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_one_hot')" + }, + { + "summary": "hardmax_axis", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=0,\n)\ny = hardmax(x, axis=0)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_axis_0')\n\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=1,\n)\ny = hardmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_axis_1')\n\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=2,\n)\ny = hardmax(x, axis=2)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_axis_2')\n\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n axis=-1,\n)\ny = hardmax(x, axis=-1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_negative_axis')\n\n# default axis is -1\nnode = onnx.helper.make_node(\n 'Hardmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_hardmax_default_axis')" + } + ] + }, + { + "name": "Identity", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Identity operator", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor to copy input into." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "identity", + "code": "node = onnx.helper.make_node(\n 'Identity',\n inputs=['x'],\n outputs=['y'],\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nexpect(node, inputs=[data], outputs=[data],\n name='test_identity')" + }, + { + "summary": "identity_opt", + "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\nopt_in_tp = onnx.helper.make_optional_type_proto(seq_in_tp)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['opt_in'],\n outputs=['opt_out']\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(identity_node, inputs=[x], outputs=[x], name='test_identity_opt',\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[opt_in_tp],\n output_type_protos=[opt_in_tp])" + }, + { + "summary": "sequence", + "code": "node = onnx.helper.make_node(\n 'Identity',\n inputs=['x'],\n outputs=['y'],\n)\n\ndata = [\n np.array([[[\n [1, 2],\n [3, 4],\n ]]], dtype=np.float32),\n np.array([[[\n [2, 3],\n [1, 5],\n ]]], dtype=np.float32)]\n\nexpect(node, inputs=[data], outputs=[data], name='test_identity_sequence')" + } + ] + }, + { + "name": "Identity", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Identity operator", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor to copy input into." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "identity", + "code": "node = onnx.helper.make_node(\n 'Identity',\n inputs=['x'],\n outputs=['y'],\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nexpect(node, inputs=[data], outputs=[data],\n name='test_identity')" + }, + { + "summary": "identity_opt", + "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\nopt_in_tp = onnx.helper.make_optional_type_proto(seq_in_tp)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['opt_in'],\n outputs=['opt_out']\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(identity_node, inputs=[x], outputs=[x], name='test_identity_opt',\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[opt_in_tp],\n output_type_protos=[opt_in_tp])" + }, + { + "summary": "sequence", + "code": "node = onnx.helper.make_node(\n 'Identity',\n inputs=['x'],\n outputs=['y'],\n)\n\ndata = [\n np.array([[[\n [1, 2],\n [3, 4],\n ]]], dtype=np.float32),\n np.array([[[\n [2, 3],\n [1, 5],\n ]]], dtype=np.float32)]\n\nexpect(node, inputs=[data], outputs=[data], name='test_identity_sequence')" + } + ] + }, + { + "name": "Identity", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Identity operator", + "inputs": [ + { + "name": "input", + "type": "V", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "V", + "description": "Tensor to copy input into." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor and sequence types.", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)", + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + } + ], + "examples": [ + { + "summary": "identity", + "code": "node = onnx.helper.make_node(\n 'Identity',\n inputs=['x'],\n outputs=['y'],\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nexpect(node, inputs=[data], outputs=[data],\n name='test_identity')" + }, + { + "summary": "identity_opt", + "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\nopt_in_tp = onnx.helper.make_optional_type_proto(seq_in_tp)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['opt_in'],\n outputs=['opt_out']\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(identity_node, inputs=[x], outputs=[x], name='test_identity_opt',\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[opt_in_tp],\n output_type_protos=[opt_in_tp])" + }, + { + "summary": "sequence", + "code": "node = onnx.helper.make_node(\n 'Identity',\n inputs=['x'],\n outputs=['y'],\n)\n\ndata = [\n np.array([[[\n [1, 2],\n [3, 4],\n ]]], dtype=np.float32),\n np.array([[[\n [2, 3],\n [1, 5],\n ]]], dtype=np.float32)]\n\nexpect(node, inputs=[data], outputs=[data], name='test_identity_sequence')" + } + ] + }, + { + "name": "Identity", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "Identity operator", + "inputs": [ + { + "name": "input", + "type": "V", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "V", + "description": "Tensor to copy input into." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor, sequence, and optional types.", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)", + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))", + "optional(seq(tensor(uint8)))", + "optional(seq(tensor(uint16)))", + "optional(seq(tensor(uint32)))", + "optional(seq(tensor(uint64)))", + "optional(seq(tensor(int8)))", + "optional(seq(tensor(int16)))", + "optional(seq(tensor(int32)))", + "optional(seq(tensor(int64)))", + "optional(seq(tensor(float16)))", + "optional(seq(tensor(float)))", + "optional(seq(tensor(double)))", + "optional(seq(tensor(string)))", + "optional(seq(tensor(bool)))", + "optional(seq(tensor(complex64)))", + "optional(seq(tensor(complex128)))", + "optional(tensor(uint8))", + "optional(tensor(uint16))", + "optional(tensor(uint32))", + "optional(tensor(uint64))", + "optional(tensor(int8))", + "optional(tensor(int16))", + "optional(tensor(int32))", + "optional(tensor(int64))", + "optional(tensor(float16))", + "optional(tensor(float))", + "optional(tensor(double))", + "optional(tensor(string))", + "optional(tensor(bool))", + "optional(tensor(complex64))", + "optional(tensor(complex128))" + ] + } + ], + "examples": [ + { + "summary": "identity", + "code": "node = onnx.helper.make_node(\n 'Identity',\n inputs=['x'],\n outputs=['y'],\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nexpect(node, inputs=[data], outputs=[data],\n name='test_identity')" + }, + { + "summary": "identity_opt", + "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\nopt_in_tp = onnx.helper.make_optional_type_proto(seq_in_tp)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['opt_in'],\n outputs=['opt_out']\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(identity_node, inputs=[x], outputs=[x], name='test_identity_opt',\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[opt_in_tp],\n output_type_protos=[opt_in_tp])" + }, + { + "summary": "sequence", + "code": "node = onnx.helper.make_node(\n 'Identity',\n inputs=['x'],\n outputs=['y'],\n)\n\ndata = [\n np.array([[[\n [1, 2],\n [3, 4],\n ]]], dtype=np.float32),\n np.array([[[\n [2, 3],\n [1, 5],\n ]]], dtype=np.float32)]\n\nexpect(node, inputs=[data], outputs=[data], name='test_identity_sequence')" + } + ] + }, + { + "name": "If", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "If conditional", + "attributes": [ + { + "name": "else_branch", + "type": "graph", + "required": true, + "description": "Graph to run if condition is false. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the then_branch." + }, + { + "name": "then_branch", + "type": "graph", + "required": true, + "description": "Graph to run if condition is true. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the else_branch." + } + ], + "inputs": [ + { + "name": "cond", + "type": "B", + "description": "Condition for the if" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "outputs", + "type": "V", + "list": true, + "description": "Values that are live-out to the enclosing scope. The return values in the `then_branch` and `else_branch` must be of the same shape and same data type." + } + ], + "min_output": 1, + "max_output": 2147483647, + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Only bool", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "if", + "code": "# Given a bool scalar input cond.\n# return constant tensor x if cond is True, otherwise return constant tensor y.\n\nthen_out = onnx.helper.make_tensor_value_info('then_out', onnx.TensorProto.FLOAT, [5])\nelse_out = onnx.helper.make_tensor_value_info('else_out', onnx.TensorProto.FLOAT, [5])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\ny = np.array([5, 4, 3, 2, 1]).astype(np.float32)\n\nthen_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['then_out'],\n value=onnx.numpy_helper.from_array(x)\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['else_out'],\n value=onnx.numpy_helper.from_array(y)\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['res'],\n then_branch=then_body,\n else_branch=else_body\n)\n\ncond = np.array(1).astype(bool)\nres = x if cond else y\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if',\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "if_optional", + "code": "# Given a bool scalar input cond, return an empty optional sequence of\n# tensor if True, return an optional sequence with value x\n# (the input optional sequence) otherwise.\n\nten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nthen_out_seq_tp = onnx.helper.make_sequence_type_proto(then_out_tensor_tp)\nthen_out_opt_tp = onnx.helper.make_optional_type_proto(then_out_seq_tp)\nthen_out = onnx.helper.make_value_info('optional_empty', then_out_opt_tp)\n\nelse_out_tensor_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nelse_out_seq_tp = onnx.helper.make_sequence_type_proto(else_out_tensor_tp)\nelse_out_opt_tp = onnx.helper.make_optional_type_proto(else_out_seq_tp)\nelse_out = onnx.helper.make_value_info('else_opt', else_out_opt_tp)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\ncond = np.array(0).astype(bool)\nres = compute_if_outputs(x, cond)\n\nopt_empty_in = onnx.helper.make_node(\n 'Optional',\n inputs=[],\n outputs=['optional_empty'],\n type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph(\n [opt_empty_in],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.numpy_helper.from_array(x[0])\n)\n\nelse_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['x'],\n outputs=['else_seq']\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n 'Optional',\n inputs=['else_seq'],\n outputs=['else_opt']\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node, else_optional_seq_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['sequence'],\n then_branch=then_body,\n else_branch=else_body\n)\n\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if_opt',\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)])" + }, + { + "summary": "if_seq", + "code": "# Given a bool scalar input cond.\n# return constant sequence x if cond is True, otherwise return constant sequence y.\n\nthen_out = onnx.helper.make_tensor_sequence_value_info('then_out', onnx.TensorProto.FLOAT, shape=[5])\nelse_out = onnx.helper.make_tensor_sequence_value_info('else_out', onnx.TensorProto.FLOAT, shape=[5])\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\ny = [np.array([5, 4, 3, 2, 1]).astype(np.float32)]\n\nthen_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.numpy_helper.from_array(x[0])\n)\n\nthen_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['x'],\n outputs=['then_out']\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['y'],\n value=onnx.numpy_helper.from_array(y[0])\n)\n\nelse_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['y'],\n outputs=['else_out']\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['res'],\n then_branch=then_body,\n else_branch=else_body\n)\n\ncond = np.array(1).astype(bool)\nres = x if cond else y\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if_seq',\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)])" + } + ] + }, + { + "name": "If", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "If conditional", + "attributes": [ + { + "name": "else_branch", + "type": "graph", + "required": true, + "description": "Graph to run if condition is false. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the then_branch." + }, + { + "name": "then_branch", + "type": "graph", + "required": true, + "description": "Graph to run if condition is true. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the else_branch." + } + ], + "inputs": [ + { + "name": "cond", + "type": "B", + "description": "Condition for the if" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "outputs", + "type": "V", + "list": true, + "description": "Values that are live-out to the enclosing scope. The return values in the `then_branch` and `else_branch` must be of the same data type. The `then_branch` and `else_branch` may produce tensors with the same element type and different shapes. If corresponding outputs from the then-branch and the else-branch have static shapes S1 and S2, then the shape of the corresponding output variable of the if-node (if present) must be compatible with both S1 and S2 as it represents the union of both possible shapes.For example, if in a model file, the the first output of `then_branch` is typed float tensor with shape [2] and the first output of `else_branch` is another float tensor with shape [3], If's first output should have (a) no shape set, or (b) a shape of rank 1 with neither `dim_value` nor `dim_param` set, or (c) a shape of rank 1 with a unique `dim_param`. In contrast, the first output cannot have the shape [2] since [2] and [3] are not compatible." + } + ], + "min_output": 1, + "max_output": 2147483647, + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Only bool", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "if", + "code": "# Given a bool scalar input cond.\n# return constant tensor x if cond is True, otherwise return constant tensor y.\n\nthen_out = onnx.helper.make_tensor_value_info('then_out', onnx.TensorProto.FLOAT, [5])\nelse_out = onnx.helper.make_tensor_value_info('else_out', onnx.TensorProto.FLOAT, [5])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\ny = np.array([5, 4, 3, 2, 1]).astype(np.float32)\n\nthen_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['then_out'],\n value=onnx.numpy_helper.from_array(x)\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['else_out'],\n value=onnx.numpy_helper.from_array(y)\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['res'],\n then_branch=then_body,\n else_branch=else_body\n)\n\ncond = np.array(1).astype(bool)\nres = x if cond else y\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if',\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "if_optional", + "code": "# Given a bool scalar input cond, return an empty optional sequence of\n# tensor if True, return an optional sequence with value x\n# (the input optional sequence) otherwise.\n\nten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nthen_out_seq_tp = onnx.helper.make_sequence_type_proto(then_out_tensor_tp)\nthen_out_opt_tp = onnx.helper.make_optional_type_proto(then_out_seq_tp)\nthen_out = onnx.helper.make_value_info('optional_empty', then_out_opt_tp)\n\nelse_out_tensor_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nelse_out_seq_tp = onnx.helper.make_sequence_type_proto(else_out_tensor_tp)\nelse_out_opt_tp = onnx.helper.make_optional_type_proto(else_out_seq_tp)\nelse_out = onnx.helper.make_value_info('else_opt', else_out_opt_tp)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\ncond = np.array(0).astype(bool)\nres = compute_if_outputs(x, cond)\n\nopt_empty_in = onnx.helper.make_node(\n 'Optional',\n inputs=[],\n outputs=['optional_empty'],\n type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph(\n [opt_empty_in],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.numpy_helper.from_array(x[0])\n)\n\nelse_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['x'],\n outputs=['else_seq']\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n 'Optional',\n inputs=['else_seq'],\n outputs=['else_opt']\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node, else_optional_seq_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['sequence'],\n then_branch=then_body,\n else_branch=else_body\n)\n\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if_opt',\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)])" + }, + { + "summary": "if_seq", + "code": "# Given a bool scalar input cond.\n# return constant sequence x if cond is True, otherwise return constant sequence y.\n\nthen_out = onnx.helper.make_tensor_sequence_value_info('then_out', onnx.TensorProto.FLOAT, shape=[5])\nelse_out = onnx.helper.make_tensor_sequence_value_info('else_out', onnx.TensorProto.FLOAT, shape=[5])\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\ny = [np.array([5, 4, 3, 2, 1]).astype(np.float32)]\n\nthen_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.numpy_helper.from_array(x[0])\n)\n\nthen_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['x'],\n outputs=['then_out']\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['y'],\n value=onnx.numpy_helper.from_array(y[0])\n)\n\nelse_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['y'],\n outputs=['else_out']\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['res'],\n then_branch=then_body,\n else_branch=else_body\n)\n\ncond = np.array(1).astype(bool)\nres = x if cond else y\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if_seq',\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)])" + } + ] + }, + { + "name": "If", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "If conditional", + "attributes": [ + { + "name": "else_branch", + "type": "graph", + "required": true, + "description": "Graph to run if condition is false. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the then_branch." + }, + { + "name": "then_branch", + "type": "graph", + "required": true, + "description": "Graph to run if condition is true. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the else_branch." + } + ], + "inputs": [ + { + "name": "cond", + "type": "B", + "description": "Condition for the if" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "outputs", + "type": "V", + "list": true, + "description": "Values that are live-out to the enclosing scope. The return values in the `then_branch` and `else_branch` must be of the same data type. The `then_branch` and `else_branch` may produce tensors with the same element type and different shapes. If corresponding outputs from the then-branch and the else-branch have static shapes S1 and S2, then the shape of the corresponding output variable of the if-node (if present) must be compatible with both S1 and S2 as it represents the union of both possible shapes.For example, if in a model file, the the first output of `then_branch` is typed float tensor with shape [2] and the first output of `else_branch` is another float tensor with shape [3], If's first output should have (a) no shape set, or (b) a shape of rank 1 with neither `dim_value` nor `dim_param` set, or (c) a shape of rank 1 with a unique `dim_param`. In contrast, the first output cannot have the shape [2] since [2] and [3] are not compatible." + } + ], + "min_output": 1, + "max_output": 2147483647, + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor and Sequence types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)", + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + }, + { + "description": "Only bool", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "if", + "code": "# Given a bool scalar input cond.\n# return constant tensor x if cond is True, otherwise return constant tensor y.\n\nthen_out = onnx.helper.make_tensor_value_info('then_out', onnx.TensorProto.FLOAT, [5])\nelse_out = onnx.helper.make_tensor_value_info('else_out', onnx.TensorProto.FLOAT, [5])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\ny = np.array([5, 4, 3, 2, 1]).astype(np.float32)\n\nthen_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['then_out'],\n value=onnx.numpy_helper.from_array(x)\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['else_out'],\n value=onnx.numpy_helper.from_array(y)\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['res'],\n then_branch=then_body,\n else_branch=else_body\n)\n\ncond = np.array(1).astype(bool)\nres = x if cond else y\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if',\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "if_optional", + "code": "# Given a bool scalar input cond, return an empty optional sequence of\n# tensor if True, return an optional sequence with value x\n# (the input optional sequence) otherwise.\n\nten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nthen_out_seq_tp = onnx.helper.make_sequence_type_proto(then_out_tensor_tp)\nthen_out_opt_tp = onnx.helper.make_optional_type_proto(then_out_seq_tp)\nthen_out = onnx.helper.make_value_info('optional_empty', then_out_opt_tp)\n\nelse_out_tensor_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nelse_out_seq_tp = onnx.helper.make_sequence_type_proto(else_out_tensor_tp)\nelse_out_opt_tp = onnx.helper.make_optional_type_proto(else_out_seq_tp)\nelse_out = onnx.helper.make_value_info('else_opt', else_out_opt_tp)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\ncond = np.array(0).astype(bool)\nres = compute_if_outputs(x, cond)\n\nopt_empty_in = onnx.helper.make_node(\n 'Optional',\n inputs=[],\n outputs=['optional_empty'],\n type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph(\n [opt_empty_in],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.numpy_helper.from_array(x[0])\n)\n\nelse_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['x'],\n outputs=['else_seq']\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n 'Optional',\n inputs=['else_seq'],\n outputs=['else_opt']\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node, else_optional_seq_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['sequence'],\n then_branch=then_body,\n else_branch=else_body\n)\n\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if_opt',\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)])" + }, + { + "summary": "if_seq", + "code": "# Given a bool scalar input cond.\n# return constant sequence x if cond is True, otherwise return constant sequence y.\n\nthen_out = onnx.helper.make_tensor_sequence_value_info('then_out', onnx.TensorProto.FLOAT, shape=[5])\nelse_out = onnx.helper.make_tensor_sequence_value_info('else_out', onnx.TensorProto.FLOAT, shape=[5])\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\ny = [np.array([5, 4, 3, 2, 1]).astype(np.float32)]\n\nthen_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.numpy_helper.from_array(x[0])\n)\n\nthen_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['x'],\n outputs=['then_out']\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['y'],\n value=onnx.numpy_helper.from_array(y[0])\n)\n\nelse_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['y'],\n outputs=['else_out']\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['res'],\n then_branch=then_body,\n else_branch=else_body\n)\n\ncond = np.array(1).astype(bool)\nres = x if cond else y\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if_seq',\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)])" + } + ] + }, + { + "name": "If", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "If conditional", + "attributes": [ + { + "name": "else_branch", + "type": "graph", + "required": true, + "description": "Graph to run if condition is false. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the then_branch." + }, + { + "name": "then_branch", + "type": "graph", + "required": true, + "description": "Graph to run if condition is true. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the else_branch." + } + ], + "inputs": [ + { + "name": "cond", + "type": "B", + "description": "Condition for the if" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "outputs", + "type": "V", + "list": true, + "description": "Values that are live-out to the enclosing scope. The return values in the `then_branch` and `else_branch` must be of the same data type. The `then_branch` and `else_branch` may produce tensors with the same element type and different shapes. If corresponding outputs from the then-branch and the else-branch have static shapes S1 and S2, then the shape of the corresponding output variable of the if-node (if present) must be compatible with both S1 and S2 as it represents the union of both possible shapes.For example, if in a model file, the the first output of `then_branch` is typed float tensor with shape [2] and the first output of `else_branch` is another float tensor with shape [3], If's first output should have (a) no shape set, or (b) a shape of rank 1 with neither `dim_value` nor `dim_param` set, or (c) a shape of rank 1 with a unique `dim_param`. In contrast, the first output cannot have the shape [2] since [2] and [3] are not compatible." + } + ], + "min_output": 1, + "max_output": 2147483647, + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor, Sequence(Tensor), Optional(Tensor), and Optional(Sequence(Tensor)) types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)", + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(bfloat16))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))", + "optional(seq(tensor(uint8)))", + "optional(seq(tensor(uint16)))", + "optional(seq(tensor(uint32)))", + "optional(seq(tensor(uint64)))", + "optional(seq(tensor(int8)))", + "optional(seq(tensor(int16)))", + "optional(seq(tensor(int32)))", + "optional(seq(tensor(int64)))", + "optional(seq(tensor(bfloat16)))", + "optional(seq(tensor(float16)))", + "optional(seq(tensor(float)))", + "optional(seq(tensor(double)))", + "optional(seq(tensor(string)))", + "optional(seq(tensor(bool)))", + "optional(seq(tensor(complex64)))", + "optional(seq(tensor(complex128)))", + "optional(tensor(uint8))", + "optional(tensor(uint16))", + "optional(tensor(uint32))", + "optional(tensor(uint64))", + "optional(tensor(int8))", + "optional(tensor(int16))", + "optional(tensor(int32))", + "optional(tensor(int64))", + "optional(tensor(bfloat16))", + "optional(tensor(float16))", + "optional(tensor(float))", + "optional(tensor(double))", + "optional(tensor(string))", + "optional(tensor(bool))", + "optional(tensor(complex64))", + "optional(tensor(complex128))" + ] + }, + { + "description": "Only bool", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "if", + "code": "# Given a bool scalar input cond.\n# return constant tensor x if cond is True, otherwise return constant tensor y.\n\nthen_out = onnx.helper.make_tensor_value_info('then_out', onnx.TensorProto.FLOAT, [5])\nelse_out = onnx.helper.make_tensor_value_info('else_out', onnx.TensorProto.FLOAT, [5])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\ny = np.array([5, 4, 3, 2, 1]).astype(np.float32)\n\nthen_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['then_out'],\n value=onnx.numpy_helper.from_array(x)\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['else_out'],\n value=onnx.numpy_helper.from_array(y)\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['res'],\n then_branch=then_body,\n else_branch=else_body\n)\n\ncond = np.array(1).astype(bool)\nres = x if cond else y\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if',\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "if_optional", + "code": "# Given a bool scalar input cond, return an empty optional sequence of\n# tensor if True, return an optional sequence with value x\n# (the input optional sequence) otherwise.\n\nten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nthen_out_seq_tp = onnx.helper.make_sequence_type_proto(then_out_tensor_tp)\nthen_out_opt_tp = onnx.helper.make_optional_type_proto(then_out_seq_tp)\nthen_out = onnx.helper.make_value_info('optional_empty', then_out_opt_tp)\n\nelse_out_tensor_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, shape=[5])\nelse_out_seq_tp = onnx.helper.make_sequence_type_proto(else_out_tensor_tp)\nelse_out_opt_tp = onnx.helper.make_optional_type_proto(else_out_seq_tp)\nelse_out = onnx.helper.make_value_info('else_opt', else_out_opt_tp)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\ncond = np.array(0).astype(bool)\nres = compute_if_outputs(x, cond)\n\nopt_empty_in = onnx.helper.make_node(\n 'Optional',\n inputs=[],\n outputs=['optional_empty'],\n type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph(\n [opt_empty_in],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.numpy_helper.from_array(x[0])\n)\n\nelse_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['x'],\n outputs=['else_seq']\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n 'Optional',\n inputs=['else_seq'],\n outputs=['else_opt']\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node, else_optional_seq_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['sequence'],\n then_branch=then_body,\n else_branch=else_body\n)\n\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if_opt',\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)])" + }, + { + "summary": "if_seq", + "code": "# Given a bool scalar input cond.\n# return constant sequence x if cond is True, otherwise return constant sequence y.\n\nthen_out = onnx.helper.make_tensor_sequence_value_info('then_out', onnx.TensorProto.FLOAT, shape=[5])\nelse_out = onnx.helper.make_tensor_sequence_value_info('else_out', onnx.TensorProto.FLOAT, shape=[5])\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\ny = [np.array([5, 4, 3, 2, 1]).astype(np.float32)]\n\nthen_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.numpy_helper.from_array(x[0])\n)\n\nthen_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['x'],\n outputs=['then_out']\n)\n\nelse_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['y'],\n value=onnx.numpy_helper.from_array(y[0])\n)\n\nelse_seq_node = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['y'],\n outputs=['else_out']\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node],\n 'then_body',\n [],\n [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node],\n 'else_body',\n [],\n [else_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['cond'],\n outputs=['res'],\n then_branch=then_body,\n else_branch=else_body\n)\n\ncond = np.array(1).astype(bool)\nres = x if cond else y\nexpect(if_node, inputs=[cond], outputs=[res], name='test_if_seq',\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)])" + } + ] + }, + { + "name": "Imputer", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Replaces inputs that equal one value with another, leaving all other elements alone.
\n This operator is typically used to replace missing values in situations where they have a canonical\n representation, such as -1, 0, NaN, or some extreme value.
\n One and only one of imputed_value_floats or imputed_value_int64s should be defined -- floats if the input tensor\n holds floats, integers if the input tensor holds integers. The imputed values must all fit within the\n width of the tensor element type. One and only one of the replaced_value_float or replaced_value_int64 should be defined,\n which one depends on whether floats or integers are being processed.
\n The imputed_value attribute length can be 1 element, or it can have one element per input feature.
In other words, if the input tensor has the shape [*,F], then the length of the attribute array may be 1 or F. If it is 1, then it is broadcast along the last dimension and applied to each feature.\n", + "attributes": [ + { + "name": "imputed_value_floats", + "type": "float32[]", + "required": false, + "description": "Value(s) to change to" + }, + { + "name": "imputed_value_int64s", + "type": "int64[]", + "required": false, + "description": "Value(s) to change to." + }, + { + "name": "replaced_value_float", + "type": "float32", + "required": false, + "description": "A value that needs replacing." + }, + { + "name": "replaced_value_int64", + "type": "int64", + "required": false, + "description": "A value that needs replacing." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Data to be processed." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Imputed output data" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input type must be a tensor of a numeric type, either [N,C] or [C]. The output type will be of the same tensor type and shape.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + } + ] + }, + { + "name": "InstanceNormalization", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Carries out instance normalization as described in the paper\nhttps://arxiv.org/abs/1607.08022.\n\ny = scale * (x - mean) / sqrt(variance + epsilon) + B,\nwhere mean and variance are computed per instance per channel.\n\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + }, + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999747378752e-06, + "description": "The epsilon value to use to avoid division by zero, default is 1e-5f." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input 4-dimensional tensor of shape NCHW." + }, + { + "name": "scale", + "type": "T", + "description": "The input 1-dimensional scale tensor of size C." + }, + { + "name": "B", + "type": "T", + "description": "The input 1-dimensional bias tensor of size C." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output 4-dimensional tensor of the same shape as input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "instancenormalization", + "code": "def _instancenorm_test_mode(x, s, bias, epsilon=1e-5): # type: ignore\n dims_x = len(x.shape)\n axis = tuple(range(2, dims_x))\n mean = np.mean(x, axis=axis, keepdims=True)\n var = np.var(x, axis=axis, keepdims=True)\n dim_ones = (1,) * (dims_x - 2)\n s = s.reshape(-1, *dim_ones)\n bias = bias.reshape(-1, *dim_ones)\n return s * (x - mean) / np.sqrt(var + epsilon) + bias\n\n# input size: (1, 2, 1, 3)\nx = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)\ns = np.array([1.0, 1.5]).astype(np.float32)\nbias = np.array([0, 1]).astype(np.float32)\ny = _instancenorm_test_mode(x, s, bias).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'InstanceNormalization',\n inputs=['x', 's', 'bias'],\n outputs=['y'],\n)\n\n# output size: (1, 2, 1, 3)\nexpect(node, inputs=[x, s, bias], outputs=[y],\n name='test_instancenorm_example')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nepsilon = 1e-2\ny = _instancenorm_test_mode(x, s, bias, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'InstanceNormalization',\n inputs=['x', 's', 'bias'],\n outputs=['y'],\n epsilon=epsilon,\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias], outputs=[y],\n name='test_instancenorm_epsilon')" + } + ], + "category": "Normalization" + }, + { + "name": "InstanceNormalization", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Carries out instance normalization as described in the paper\nhttps://arxiv.org/abs/1607.08022.\n\ny = scale * (x - mean) / sqrt(variance + epsilon) + B,\nwhere mean and variance are computed per instance per channel.\n\n", + "attributes": [ + { + "name": "epsilon", + "type": "float32", + "required": false, + "default": 9.999999747378752e-06, + "description": "The epsilon value to use to avoid division by zero." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + }, + { + "name": "scale", + "type": "T", + "description": "The input 1-dimensional scale tensor of size C." + }, + { + "name": "B", + "type": "T", + "description": "The input 1-dimensional bias tensor of size C." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output tensor of the same shape as input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "instancenormalization", + "code": "def _instancenorm_test_mode(x, s, bias, epsilon=1e-5): # type: ignore\n dims_x = len(x.shape)\n axis = tuple(range(2, dims_x))\n mean = np.mean(x, axis=axis, keepdims=True)\n var = np.var(x, axis=axis, keepdims=True)\n dim_ones = (1,) * (dims_x - 2)\n s = s.reshape(-1, *dim_ones)\n bias = bias.reshape(-1, *dim_ones)\n return s * (x - mean) / np.sqrt(var + epsilon) + bias\n\n# input size: (1, 2, 1, 3)\nx = np.array([[[[-1, 0, 1]], [[2, 3, 4]]]]).astype(np.float32)\ns = np.array([1.0, 1.5]).astype(np.float32)\nbias = np.array([0, 1]).astype(np.float32)\ny = _instancenorm_test_mode(x, s, bias).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'InstanceNormalization',\n inputs=['x', 's', 'bias'],\n outputs=['y'],\n)\n\n# output size: (1, 2, 1, 3)\nexpect(node, inputs=[x, s, bias], outputs=[y],\n name='test_instancenorm_example')\n\n# input size: (2, 3, 4, 5)\nx = np.random.randn(2, 3, 4, 5).astype(np.float32)\ns = np.random.randn(3).astype(np.float32)\nbias = np.random.randn(3).astype(np.float32)\nepsilon = 1e-2\ny = _instancenorm_test_mode(x, s, bias, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'InstanceNormalization',\n inputs=['x', 's', 'bias'],\n outputs=['y'],\n epsilon=epsilon,\n)\n\n# output size: (2, 3, 4, 5)\nexpect(node, inputs=[x, s, bias], outputs=[y],\n name='test_instancenorm_epsilon')" + } + ], + "category": "Normalization" + }, + { + "name": "IsInf", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Map infinity to true and other values to false.", + "attributes": [ + { + "name": "detect_negative", + "type": "int64", + "required": false, + "default": 1, + "description": "(Optional) Whether map negative infinity to true. Default to 1 so that negative infinity induces true. Set this attribute to 0 if negative infinity should be mapped to false." + }, + { + "name": "detect_positive", + "type": "int64", + "required": false, + "default": 1, + "description": "(Optional) Whether map positive infinity to true. Default to 1 so that positive infinity induces true. Set this attribute to 0 if positive infinity should be mapped to false." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "input" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "output" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output types to boolean tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "infinity", + "code": "node = onnx.helper.make_node('IsInf',\n inputs=['x'],\n outputs=['y'],\n )\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, np.NINF, np.inf],\n dtype=np.float32)\ny = np.isinf(x)\nexpect(node, inputs=[x], outputs=[y], name='test_isinf')" + }, + { + "summary": "negative_infinity_only", + "code": "node = onnx.helper.make_node('IsInf',\n inputs=['x'],\n outputs=['y'],\n detect_positive=0\n )\n\nx = np.array([-1.7, np.nan, np.inf, -3.6, np.NINF, np.inf],\n dtype=np.float32)\ny = np.isneginf(x)\nexpect(node, inputs=[x], outputs=[y], name='test_isinf_negative')" + }, + { + "summary": "positive_infinity_only", + "code": "node = onnx.helper.make_node('IsInf',\n inputs=['x'],\n outputs=['y'],\n detect_negative=0\n )\n\nx = np.array([-1.7, np.nan, np.inf, 3.6, np.NINF, np.inf],\n dtype=np.float32)\ny = np.isposinf(x)\nexpect(node, inputs=[x], outputs=[y], name='test_isinf_positive')" + } + ] + }, + { + "name": "IsNaN", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Returns which elements of the input are NaN.", + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "input" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "output" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output types to boolean tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "isnan", + "code": "node = onnx.helper.make_node(\n 'IsNaN',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([3.0, np.nan, 4.0, np.nan], dtype=np.float32)\ny = np.isnan(x)\nexpect(node, inputs=[x], outputs=[y], name='test_isnan')" + } + ] + }, + { + "name": "IsNaN", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Returns which elements of the input are NaN.", + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "input" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "output" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain output types to boolean tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "isnan", + "code": "node = onnx.helper.make_node(\n 'IsNaN',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([3.0, np.nan, 4.0, np.nan], dtype=np.float32)\ny = np.isnan(x)\nexpect(node, inputs=[x], outputs=[y], name='test_isnan')" + } + ] + }, + { + "name": "LRN", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Local Response Normalization proposed in the [AlexNet paper](https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf).\nIt normalizes over local input regions.\nThe local region is defined across the channels. For an element X[n, c, d1, ..., dk] in a tensor\nof shape (N x C x D1 x D2, ..., Dk), its region is\n{X[n, i, d1, ..., dk] | max(0, c - floor((size - 1) / 2)) <= i <= min(C - 1, c + ceil((size - 1) / 2))}.\n\nsquare_sum[n, c, d1, ..., dk] = sum(X[n, i, d1, ..., dk] ^ 2),\nwhere max(0, c - floor((size - 1) / 2)) <= i <= min(C - 1, c + ceil((size - 1) / 2)).\n\nY[n, c, d1, ..., dk] = X[n, c, d1, ..., dk] / (bias + alpha / size * square_sum[n, c, d1, ..., dk] ) ^ beta\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 9.999999747378752e-05, + "description": "Scaling parameter." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 0.75, + "description": "The exponent." + }, + { + "name": "bias", + "type": "float32", + "required": false, + "default": 1.0, + "description": "" + }, + { + "name": "size", + "type": "int64", + "required": true, + "description": "The number of channels to sum over" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor, which has the shape and type as input tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default", + "code": "alpha = 0.0001\nbeta = 0.75\nbias = 1.0\nnsize = 3\nnode = onnx.helper.make_node(\n 'LRN',\n inputs=['x'],\n outputs=['y'],\n size=3\n)\nx = np.random.randn(5, 5, 5, 5).astype(np.float32)\nsquare_sum = np.zeros((5, 5, 5, 5)).astype(np.float32)\nfor n, c, h, w in np.ndindex(x.shape):\n square_sum[n, c, h, w] = sum(x[n,\n max(0, c - int(math.floor((nsize - 1) / 2))):min(5, c + int(math.ceil((nsize - 1) / 2)) + 1),\n h,\n w] ** 2)\ny = x / ((bias + (alpha / nsize) * square_sum) ** beta)\nexpect(node, inputs=[x], outputs=[y],\n name='test_lrn_default')" + }, + { + "summary": "lrn", + "code": "alpha = 0.0002\nbeta = 0.5\nbias = 2.0\nnsize = 3\nnode = onnx.helper.make_node(\n 'LRN',\n inputs=['x'],\n outputs=['y'],\n alpha=alpha,\n beta=beta,\n bias=bias,\n size=nsize\n)\nx = np.random.randn(5, 5, 5, 5).astype(np.float32)\nsquare_sum = np.zeros((5, 5, 5, 5)).astype(np.float32)\nfor n, c, h, w in np.ndindex(x.shape):\n square_sum[n, c, h, w] = sum(x[n,\n max(0, c - int(math.floor((nsize - 1) / 2))):min(5, c + int(math.ceil((nsize - 1) / 2)) + 1),\n h,\n w] ** 2)\ny = x / ((bias + (alpha / nsize) * square_sum) ** beta)\nexpect(node, inputs=[x], outputs=[y],\n name='test_lrn')" + } + ], + "category": "Normalization" + }, + { + "name": "LRN", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Local Response Normalization proposed in the [AlexNet paper](https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf).\nIt normalizes over local input regions.\nThe local region is defined across the channels. For an element X[n, c, d1, ..., dk] in a tensor\nof shape (N x C x D1 x D2, ..., Dk), its region is\n{X[n, i, d1, ..., dk] | max(0, c - floor((size - 1) / 2)) <= i <= min(C - 1, c + ceil((size - 1) / 2))}.\n\nsquare_sum[n, c, d1, ..., dk] = sum(X[n, i, d1, ..., dk] ^ 2),\nwhere max(0, c - floor((size - 1) / 2)) <= i <= min(C - 1, c + ceil((size - 1) / 2)).\n\nY[n, c, d1, ..., dk] = X[n, c, d1, ..., dk] / (bias + alpha / size * square_sum[n, c, d1, ..., dk] ) ^ beta\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 9.999999747378752e-05, + "description": "Scaling parameter." + }, + { + "name": "beta", + "type": "float32", + "required": false, + "default": 0.75, + "description": "The exponent." + }, + { + "name": "bias", + "type": "float32", + "required": false, + "default": 1.0, + "description": "" + }, + { + "name": "size", + "type": "int64", + "required": true, + "description": "The number of channels to sum over" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor, which has the shape and type as input tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default", + "code": "alpha = 0.0001\nbeta = 0.75\nbias = 1.0\nnsize = 3\nnode = onnx.helper.make_node(\n 'LRN',\n inputs=['x'],\n outputs=['y'],\n size=3\n)\nx = np.random.randn(5, 5, 5, 5).astype(np.float32)\nsquare_sum = np.zeros((5, 5, 5, 5)).astype(np.float32)\nfor n, c, h, w in np.ndindex(x.shape):\n square_sum[n, c, h, w] = sum(x[n,\n max(0, c - int(math.floor((nsize - 1) / 2))):min(5, c + int(math.ceil((nsize - 1) / 2)) + 1),\n h,\n w] ** 2)\ny = x / ((bias + (alpha / nsize) * square_sum) ** beta)\nexpect(node, inputs=[x], outputs=[y],\n name='test_lrn_default')" + }, + { + "summary": "lrn", + "code": "alpha = 0.0002\nbeta = 0.5\nbias = 2.0\nnsize = 3\nnode = onnx.helper.make_node(\n 'LRN',\n inputs=['x'],\n outputs=['y'],\n alpha=alpha,\n beta=beta,\n bias=bias,\n size=nsize\n)\nx = np.random.randn(5, 5, 5, 5).astype(np.float32)\nsquare_sum = np.zeros((5, 5, 5, 5)).astype(np.float32)\nfor n, c, h, w in np.ndindex(x.shape):\n square_sum[n, c, h, w] = sum(x[n,\n max(0, c - int(math.floor((nsize - 1) / 2))):min(5, c + int(math.ceil((nsize - 1) / 2)) + 1),\n h,\n w] ** 2)\ny = x / ((bias + (alpha / nsize) * square_sum) ** beta)\nexpect(node, inputs=[x], outputs=[y],\n name='test_lrn')" + } + ], + "category": "Normalization" + }, + { + "name": "LSTM", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes an one-layer LSTM. This operator is usually supported via some\ncustom implementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`i` - input gate\n\n`o` - output gate\n\n`f` - forget gate\n\n`c` - cell gate\n\n`t` - time step (t-1 means previous time step)\n\n`W[iofc]` - W parameter weight matrix for input, output, forget, and cell gates\n\n`R[iofc]` - R recurrence weight matrix for input, output, forget, and cell gates\n\n`Wb[iofc]` - W bias vectors for input, output, forget, and cell gates\n\n`Rb[iofc]` - R bias vectors for input, output, forget, and cell gates\n\n`P[iof]` - P peephole weight vector for input, output, and forget gates\n\n`WB[iofc]` - W parameter weight matrix for backward input, output, forget, and cell gates\n\n`RB[iofc]` - R recurrence weight matrix for backward input, output, forget, and cell gates\n\n`WBb[iofc]` - W bias vectors for backward input, output, forget, and cell gates\n\n`RBb[iofc]` - R bias vectors for backward input, output, forget, and cell gates\n\n`PB[iof]` - P peephole weight vector for backward input, output, and forget gates\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Sigmoid, g=Tanh, h=Tanh):\n\n - it = f(Xt*(Wi^T) + Ht-1*Ri + Pi (.) Ct-1 + Wbi + Rbi)\n\n - ft = f(Xt*(Wf^T) + Ht-1*Rf + Pf (.) Ct-1 + Wbf + Rbf)\n\n - ct = g(Xt*(Wc^T) + Ht-1*Rc + Wbc + Rbc)\n\n - Ct = ft (.) Ct-1 + it (.) ct\n\n - ot = f(Xt*(Wo^T) + Ht-1*Ro + Po (.) Ct + Wbo + Rbo)\n\n - Ht = ot (.) h(Ct)\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "A list of 3 (or 6 if bidirectional) activation functions for input, output, forget, cell, and hidden. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "forward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + }, + { + "name": "input_forget", + "type": "int64", + "required": false, + "description": "Couple the input and forget gates if 1, default 0." + }, + { + "name": "output_sequence", + "type": "int64", + "required": false, + "description": "The sequence output for the hidden is optional if 0. Default 0." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for the gates. Concatenation of `W[iofc]` and `WB[iofc]` (if bidirectional) along dimension 0. The tensor has shape `[num_directions, 4*hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `R[iofc]` and `RB[iofc]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 4*hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for input gate. Concatenation of `[Wb[iofc], Rb[iofc]]`, and `[WBb[iofc], RBb[iofc]]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 8*hidden_size]`. Optional: If not specified - assumed to be 0." + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + }, + { + "name": "initial_c", + "type": "T", + "option": "optional", + "description": "Optional initial value of the cell. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + }, + { + "name": "P", + "type": "T", + "option": "optional", + "description": "The weight tensor for peepholes. Concatenation of `P[iof]` and `PB[iof]` (if bidirectional) along dimension 0. It has shape `[num_directions, 3*hidde_size]`. Optional: If not specified - assumed to be 0." + } + ], + "min_input": 3, + "max_input": 8, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. It is optional if `output_sequence` is 0." + }, + { + "name": "Y_h", + "type": "T", + "option": "optional", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + }, + { + "name": "Y_c", + "type": "T", + "option": "optional", + "description": "The last output value of the cell. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 0, + "max_output": 3, + "inputs_range": "3 - 8", + "outputs_range": "0 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 7\nweight_scale = 0.3\nnumber_of_gates = 4\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\nlstm = LSTM_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_lstm_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 3\nweight_scale = 0.1\nnumber_of_gates = 4\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\nlstm = LSTM_Helper(X=input, W=W, R=R)\n_, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_lstm_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 4\nweight_scale = 0.1\ncustom_bias = 0.1\nnumber_of_gates = 4\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), 1)\n\nlstm = LSTM_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_lstm_with_initial_bias')" + }, + { + "summary": "peepholes", + "code": "input = np.array([[[1., 2., 3., 4.], [5., 6., 7., 8.]]]).astype(np.float32)\n\ninput_size = 4\nhidden_size = 3\nweight_scale = 0.1\nnumber_of_gates = 4\nnumber_of_peepholes = 3\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R', 'B', 'sequence_lens', 'initial_h', 'initial_c', 'P'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\n# Initializing Inputs\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\nB = np.zeros((1, 2 * number_of_gates * hidden_size)).astype(np.float32)\nseq_lens = np.repeat(input.shape[0], input.shape[1]).astype(np.int32)\ninit_h = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)\ninit_c = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)\nP = weight_scale * np.ones((1, number_of_peepholes * hidden_size)).astype(np.float32)\n\nlstm = LSTM_Helper(X=input, W=W, R=R, B=B, P=P, initial_c=init_c, initial_h=init_h)\n_, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R, B, seq_lens, init_h, init_c, P], outputs=[Y_h.astype(np.float32)],\n name='test_lstm_with_peepholes')" + } + ], + "category": "Layer" + }, + { + "name": "LSTM", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Computes an one-layer LSTM. This operator is usually supported via some\ncustom implementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`i` - input gate\n\n`o` - output gate\n\n`f` - forget gate\n\n`c` - cell gate\n\n`t` - time step (t-1 means previous time step)\n\n`W[iofc]` - W parameter weight matrix for input, output, forget, and cell gates\n\n`R[iofc]` - R recurrence weight matrix for input, output, forget, and cell gates\n\n`Wb[iofc]` - W bias vectors for input, output, forget, and cell gates\n\n`Rb[iofc]` - R bias vectors for input, output, forget, and cell gates\n\n`P[iof]` - P peephole weight vector for input, output, and forget gates\n\n`WB[iofc]` - W parameter weight matrix for backward input, output, forget, and cell gates\n\n`RB[iofc]` - R recurrence weight matrix for backward input, output, forget, and cell gates\n\n`WBb[iofc]` - W bias vectors for backward input, output, forget, and cell gates\n\n`RBb[iofc]` - R bias vectors for backward input, output, forget, and cell gates\n\n`PB[iof]` - P peephole weight vector for backward input, output, and forget gates\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Sigmoid, g=Tanh, h=Tanh):\n\n - it = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Pi (.) Ct-1 + Wbi + Rbi)\n\n - ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Pf (.) Ct-1 + Wbf + Rbf)\n\n - ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)\n\n - Ct = ft (.) Ct-1 + it (.) ct\n\n - ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Po (.) Ct + Wbo + Rbo)\n\n - Ht = ot (.) h(Ct)\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "A list of 3 (or 6 if bidirectional) activation functions for input, output, forget, cell, and hidden. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "forward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + }, + { + "name": "input_forget", + "type": "int64", + "required": false, + "description": "Couple the input and forget gates if 1." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for the gates. Concatenation of `W[iofc]` and `WB[iofc]` (if bidirectional) along dimension 0. The tensor has shape `[num_directions, 4*hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `R[iofc]` and `RB[iofc]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 4*hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for input gate. Concatenation of `[Wb[iofc], Rb[iofc]]`, and `[WBb[iofc], RBb[iofc]]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 8*hidden_size]`. Optional: If not specified - assumed to be 0." + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + }, + { + "name": "initial_c", + "type": "T", + "option": "optional", + "description": "Optional initial value of the cell. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + }, + { + "name": "P", + "type": "T", + "option": "optional", + "description": "The weight tensor for peepholes. Concatenation of `P[iof]` and `PB[iof]` (if bidirectional) along dimension 0. It has shape `[num_directions, 3*hidde_size]`. Optional: If not specified - assumed to be 0." + } + ], + "min_input": 3, + "max_input": 8, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. " + }, + { + "name": "Y_h", + "type": "T", + "option": "optional", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + }, + { + "name": "Y_c", + "type": "T", + "option": "optional", + "description": "The last output value of the cell. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 0, + "max_output": 3, + "inputs_range": "3 - 8", + "outputs_range": "0 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 7\nweight_scale = 0.3\nnumber_of_gates = 4\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\nlstm = LSTM_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_lstm_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 3\nweight_scale = 0.1\nnumber_of_gates = 4\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\nlstm = LSTM_Helper(X=input, W=W, R=R)\n_, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_lstm_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 4\nweight_scale = 0.1\ncustom_bias = 0.1\nnumber_of_gates = 4\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), 1)\n\nlstm = LSTM_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_lstm_with_initial_bias')" + }, + { + "summary": "peepholes", + "code": "input = np.array([[[1., 2., 3., 4.], [5., 6., 7., 8.]]]).astype(np.float32)\n\ninput_size = 4\nhidden_size = 3\nweight_scale = 0.1\nnumber_of_gates = 4\nnumber_of_peepholes = 3\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R', 'B', 'sequence_lens', 'initial_h', 'initial_c', 'P'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\n# Initializing Inputs\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\nB = np.zeros((1, 2 * number_of_gates * hidden_size)).astype(np.float32)\nseq_lens = np.repeat(input.shape[0], input.shape[1]).astype(np.int32)\ninit_h = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)\ninit_c = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)\nP = weight_scale * np.ones((1, number_of_peepholes * hidden_size)).astype(np.float32)\n\nlstm = LSTM_Helper(X=input, W=W, R=R, B=B, P=P, initial_c=init_c, initial_h=init_h)\n_, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R, B, seq_lens, init_h, init_c, P], outputs=[Y_h.astype(np.float32)],\n name='test_lstm_with_peepholes')" + } + ], + "category": "Layer" + }, + { + "name": "LSTM", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Computes an one-layer LSTM. This operator is usually supported via some\ncustom implementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`i` - input gate\n\n`o` - output gate\n\n`f` - forget gate\n\n`c` - cell gate\n\n`t` - time step (t-1 means previous time step)\n\n`W[iofc]` - W parameter weight matrix for input, output, forget, and cell gates\n\n`R[iofc]` - R recurrence weight matrix for input, output, forget, and cell gates\n\n`Wb[iofc]` - W bias vectors for input, output, forget, and cell gates\n\n`Rb[iofc]` - R bias vectors for input, output, forget, and cell gates\n\n`P[iof]` - P peephole weight vector for input, output, and forget gates\n\n`WB[iofc]` - W parameter weight matrix for backward input, output, forget, and cell gates\n\n`RB[iofc]` - R recurrence weight matrix for backward input, output, forget, and cell gates\n\n`WBb[iofc]` - W bias vectors for backward input, output, forget, and cell gates\n\n`RBb[iofc]` - R bias vectors for backward input, output, forget, and cell gates\n\n`PB[iof]` - P peephole weight vector for backward input, output, and forget gates\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Sigmoid, g=Tanh, h=Tanh):\n\n - it = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Pi (.) Ct-1 + Wbi + Rbi)\n\n - ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Pf (.) Ct-1 + Wbf + Rbf)\n\n - ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)\n\n - Ct = ft (.) Ct-1 + it (.) ct\n\n - ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Po (.) Ct + Wbo + Rbo)\n\n - Ht = ot (.) h(Ct)\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "A list of 3 (or 6 if bidirectional) activation functions for input, output, forget, cell, and hidden. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "forward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + }, + { + "name": "input_forget", + "type": "int64", + "required": false, + "description": "Couple the input and forget gates if 1." + }, + { + "name": "layout", + "type": "int64", + "required": false, + "description": "The shape format of inputs X, initial_h, initial_c and outputs Y, Y_h, Y_c. If 0, the following shapes are expected: X.shape = [seq_length, batch_size, input_size], Y.shape = [seq_length, num_directions, batch_size, hidden_size], initial_h.shape = Y_h.shape = initial_c.shape = Y_c.shape = [num_directions, batch_size, hidden_size]. If 1, the following shapes are expected: X.shape = [batch_size, seq_length, input_size], Y.shape = [batch_size, seq_length, num_directions, hidden_size], initial_h.shape = Y_h.shape = initial_c.shape = Y_c.shape = [batch_size, num_directions, hidden_size]." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for the gates. Concatenation of `W[iofc]` and `WB[iofc]` (if bidirectional) along dimension 0. The tensor has shape `[num_directions, 4*hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `R[iofc]` and `RB[iofc]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 4*hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for input gate. Concatenation of `[Wb[iofc], Rb[iofc]]`, and `[WBb[iofc], RBb[iofc]]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, 8*hidden_size]`. Optional: If not specified - assumed to be 0." + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + }, + { + "name": "initial_c", + "type": "T", + "option": "optional", + "description": "Optional initial value of the cell. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + }, + { + "name": "P", + "type": "T", + "option": "optional", + "description": "The weight tensor for peepholes. Concatenation of `P[iof]` and `PB[iof]` (if bidirectional) along dimension 0. It has shape `[num_directions, 3*hidde_size]`. Optional: If not specified - assumed to be 0." + } + ], + "min_input": 3, + "max_input": 8, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. " + }, + { + "name": "Y_h", + "type": "T", + "option": "optional", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + }, + { + "name": "Y_c", + "type": "T", + "option": "optional", + "description": "The last output value of the cell. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 0, + "max_output": 3, + "inputs_range": "3 - 8", + "outputs_range": "0 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 7\nweight_scale = 0.3\nnumber_of_gates = 4\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\nlstm = LSTM_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_lstm_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 3\nweight_scale = 0.1\nnumber_of_gates = 4\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\nlstm = LSTM_Helper(X=input, W=W, R=R)\n_, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_lstm_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 4\nweight_scale = 0.1\ncustom_bias = 0.1\nnumber_of_gates = 4\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), 1)\n\nlstm = LSTM_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_lstm_with_initial_bias')" + }, + { + "summary": "peepholes", + "code": "input = np.array([[[1., 2., 3., 4.], [5., 6., 7., 8.]]]).astype(np.float32)\n\ninput_size = 4\nhidden_size = 3\nweight_scale = 0.1\nnumber_of_gates = 4\nnumber_of_peepholes = 3\n\nnode = onnx.helper.make_node(\n 'LSTM',\n inputs=['X', 'W', 'R', 'B', 'sequence_lens', 'initial_h', 'initial_c', 'P'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\n# Initializing Inputs\nW = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)\nB = np.zeros((1, 2 * number_of_gates * hidden_size)).astype(np.float32)\nseq_lens = np.repeat(input.shape[0], input.shape[1]).astype(np.int32)\ninit_h = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)\ninit_c = np.zeros((1, input.shape[1], hidden_size)).astype(np.float32)\nP = weight_scale * np.ones((1, number_of_peepholes * hidden_size)).astype(np.float32)\n\nlstm = LSTM_Helper(X=input, W=W, R=R, B=B, P=P, initial_c=init_c, initial_h=init_h)\n_, Y_h = lstm.step()\nexpect(node, inputs=[input, W, R, B, seq_lens, init_h, init_c, P], outputs=[Y_h.astype(np.float32)],\n name='test_lstm_with_peepholes')" + } + ], + "category": "Layer" + }, + { + "name": "LabelEncoder", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Converts strings to integers and vice versa.
\n If the string default value is set, it will convert integers to strings.\n If the int default value is set, it will convert strings to integers.
\n Each operator converts either integers to strings or strings to integers, depending\n on which default value attribute is provided. Only one default value attribute\n should be defined.
\n When converting from integers to strings, the string is fetched from the\n 'classes_strings' list, by simple indexing.
\n When converting from strings to integers, the string is looked up in the list\n and the index at which it is found is used as the converted value.\n", + "attributes": [ + { + "name": "classes_strings", + "type": "string[]", + "required": false, + "description": "A list of labels." + }, + { + "name": "default_int64", + "type": "int64", + "required": false, + "default": -1, + "description": "An integer to use when an input string value is not found in the map.
One and only one of the 'default_*' attributes must be defined." + }, + { + "name": "default_string", + "type": "string", + "required": false, + "default": "_Unused", + "description": "A string to use when an input integer value is not found in the map.
One and only one of the 'default_*' attributes must be defined." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Input data." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "Output data. If strings are input, the output values are integers, and vice versa." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input type must be a tensor of integers or strings, of any shape.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)" + ] + }, + { + "description": "The output type will be a tensor of strings or integers, and will have the same shape as the input.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "LabelEncoder", + "module": "ai.onnx.ml", + "version": 2, + "support_level": "common", + "description": "Maps each element in the input tensor to another value.
\n The mapping is determined by the two parallel attributes, 'keys_*' and\n 'values_*' attribute. The i-th value in the specified 'keys_*' attribute\n would be mapped to the i-th value in the specified 'values_*' attribute. It\n implies that input's element type and the element type of the specified\n 'keys_*' should be identical while the output type is identical to the\n specified 'values_*' attribute. If an input element can not be found in the\n specified 'keys_*' attribute, the 'default_*' that matches the specified\n 'values_*' attribute may be used as its output value.
\n Let's consider an example which maps a string tensor to an integer tensor.\n Assume and 'keys_strings' is [\"Amy\", \"Sally\"], 'values_int64s' is [5, 6],\n and 'default_int64' is '-1'. The input [\"Dori\", \"Amy\", \"Amy\", \"Sally\",\n \"Sally\"] would be mapped to [-1, 5, 5, 6, 6].
\n Since this operator is an one-to-one mapping, its input and output shapes\n are the same. Notice that only one of 'keys_*'/'values_*' can be set.
\n For key look-up, bit-wise comparison is used so even a float NaN can be\n mapped to a value in 'values_*' attribute.
\n", + "attributes": [ + { + "name": "default_float", + "type": "float32", + "required": false, + "description": "A float." + }, + { + "name": "default_int64", + "type": "int64", + "required": false, + "default": -1, + "description": "An integer." + }, + { + "name": "default_string", + "type": "string", + "required": false, + "default": "_Unused", + "description": "A string." + }, + { + "name": "keys_floats", + "type": "float32[]", + "required": false, + "description": "A list of floats." + }, + { + "name": "keys_int64s", + "type": "int64[]", + "required": false, + "description": "A list of ints." + }, + { + "name": "keys_strings", + "type": "string[]", + "required": false, + "description": "A list of strings. One and only one of 'keys_*'s should be set." + }, + { + "name": "values_floats", + "type": "float32[]", + "required": false, + "description": "A list of floats." + }, + { + "name": "values_int64s", + "type": "int64[]", + "required": false, + "description": "A list of ints." + }, + { + "name": "values_strings", + "type": "string[]", + "required": false, + "description": "A list of strings. One and only one of 'value_*'s should be set." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Input data. It can be either tensor or scalar." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "Output data." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input type is a tensor of any shape.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)", + "tensor(float)" + ] + }, + { + "description": "Output type is determined by the specified 'values_*' attribute.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)", + "tensor(float)" + ] + } + ] + }, + { + "name": "LeakyRelu", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "LeakyRelu takes input data (Tensor) and an argument alpha, and produces one\noutput data (Tensor) where the function `f(x) = alpha * x for x < 0`,\n`f(x) = x for x >= 0`, is applied to the data tensor elementwise.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 0.009999999776482582, + "description": "Coefficient of leakage default to 0.01." + }, + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "leakyrelu", + "code": "node = onnx.helper.make_node(\n 'LeakyRelu',\n inputs=['x'],\n outputs=['y'],\n alpha=0.1\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-0.1, 0., 1.]\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * 0.1\nexpect(node, inputs=[x], outputs=[y],\n name='test_leakyrelu_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * 0.1\nexpect(node, inputs=[x], outputs=[y],\n name='test_leakyrelu')" + }, + { + "summary": "leakyrelu_default", + "code": "default_alpha = 0.01\nnode = onnx.helper.make_node(\n 'LeakyRelu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * default_alpha\nexpect(node, inputs=[x], outputs=[y],\n name='test_leakyrelu_default')" + } + ], + "category": "Activation" + }, + { + "name": "LeakyRelu", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "LeakyRelu takes input data (Tensor) and an argument alpha, and produces one\noutput data (Tensor) where the function `f(x) = alpha * x for x < 0`,\n`f(x) = x for x >= 0`, is applied to the data tensor elementwise.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 0.009999999776482582, + "description": "Coefficient of leakage." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "leakyrelu", + "code": "node = onnx.helper.make_node(\n 'LeakyRelu',\n inputs=['x'],\n outputs=['y'],\n alpha=0.1\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-0.1, 0., 1.]\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * 0.1\nexpect(node, inputs=[x], outputs=[y],\n name='test_leakyrelu_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * 0.1\nexpect(node, inputs=[x], outputs=[y],\n name='test_leakyrelu')" + }, + { + "summary": "leakyrelu_default", + "code": "default_alpha = 0.01\nnode = onnx.helper.make_node(\n 'LeakyRelu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * default_alpha\nexpect(node, inputs=[x], outputs=[y],\n name='test_leakyrelu_default')" + } + ], + "category": "Activation" + }, + { + "name": "LeakyRelu", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "LeakyRelu takes input data (Tensor) and an argument alpha, and produces one\noutput data (Tensor) where the function `f(x) = alpha * x for x < 0`,\n`f(x) = x for x >= 0`, is applied to the data tensor elementwise.\n\n**History**\n- Version 16 adds bfloat16 to the types allowed.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 0.009999999776482582, + "description": "Coefficient of leakage." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "leakyrelu", + "code": "node = onnx.helper.make_node(\n 'LeakyRelu',\n inputs=['x'],\n outputs=['y'],\n alpha=0.1\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-0.1, 0., 1.]\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * 0.1\nexpect(node, inputs=[x], outputs=[y],\n name='test_leakyrelu_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * 0.1\nexpect(node, inputs=[x], outputs=[y],\n name='test_leakyrelu')" + }, + { + "summary": "leakyrelu_default", + "code": "default_alpha = 0.01\nnode = onnx.helper.make_node(\n 'LeakyRelu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * default_alpha\nexpect(node, inputs=[x], outputs=[y],\n name='test_leakyrelu_default')" + } + ], + "category": "Activation" + }, + { + "name": "Less", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `less` logical operation\nelementwise on the input tensors `A` and `B`.\n\nIf broadcasting is enabled, the right-hand-side argument will be broadcasted\nto match the shape of left-hand-side argument. See the doc of `Add` for a\ndetailed description of the broadcasting rules.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Left input tensor for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Right input tensor for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "less", + "code": "node = onnx.helper.make_node(\n 'Less',\n inputs=['x', 'y'],\n outputs=['less'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less')" + }, + { + "summary": "less", + "code": "node = onnx.helper.make_node(\n 'LessOrEqual',\n inputs=['x', 'y'],\n outputs=['less_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_equal')" + }, + { + "summary": "less_broadcast", + "code": "node = onnx.helper.make_node(\n 'Less',\n inputs=['x', 'y'],\n outputs=['less'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_bcast')" + }, + { + "summary": "less_broadcast", + "code": "node = onnx.helper.make_node(\n 'LessOrEqual',\n inputs=['x', 'y'],\n outputs=['less_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Less", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `less` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "less", + "code": "node = onnx.helper.make_node(\n 'Less',\n inputs=['x', 'y'],\n outputs=['less'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less')" + }, + { + "summary": "less", + "code": "node = onnx.helper.make_node(\n 'LessOrEqual',\n inputs=['x', 'y'],\n outputs=['less_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_equal')" + }, + { + "summary": "less_broadcast", + "code": "node = onnx.helper.make_node(\n 'Less',\n inputs=['x', 'y'],\n outputs=['less'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_bcast')" + }, + { + "summary": "less_broadcast", + "code": "node = onnx.helper.make_node(\n 'LessOrEqual',\n inputs=['x', 'y'],\n outputs=['less_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Less", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `less` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "less", + "code": "node = onnx.helper.make_node(\n 'Less',\n inputs=['x', 'y'],\n outputs=['less'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less')" + }, + { + "summary": "less", + "code": "node = onnx.helper.make_node(\n 'LessOrEqual',\n inputs=['x', 'y'],\n outputs=['less_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_equal')" + }, + { + "summary": "less_broadcast", + "code": "node = onnx.helper.make_node(\n 'Less',\n inputs=['x', 'y'],\n outputs=['less'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_bcast')" + }, + { + "summary": "less_broadcast", + "code": "node = onnx.helper.make_node(\n 'LessOrEqual',\n inputs=['x', 'y'],\n outputs=['less_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "Less", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `less` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "less", + "code": "node = onnx.helper.make_node(\n 'Less',\n inputs=['x', 'y'],\n outputs=['less'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less')" + }, + { + "summary": "less", + "code": "node = onnx.helper.make_node(\n 'LessOrEqual',\n inputs=['x', 'y'],\n outputs=['less_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_equal')" + }, + { + "summary": "less_broadcast", + "code": "node = onnx.helper.make_node(\n 'Less',\n inputs=['x', 'y'],\n outputs=['less'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_bcast')" + }, + { + "summary": "less_broadcast", + "code": "node = onnx.helper.make_node(\n 'LessOrEqual',\n inputs=['x', 'y'],\n outputs=['less_equal'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_less_equal_bcast')" + } + ], + "category": "Logic" + }, + { + "name": "LessOrEqual", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `less_equal` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ] + }, + { + "name": "LessOrEqual", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `less_equal` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ] + }, + { + "name": "LinearClassifier", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Linear classifier\n", + "attributes": [ + { + "name": "classlabels_ints", + "type": "int64[]", + "required": false, + "description": "Class labels when using integer labels. One and only one 'classlabels' attribute must be defined." + }, + { + "name": "classlabels_strings", + "type": "string[]", + "required": false, + "description": "Class labels when using string labels. One and only one 'classlabels' attribute must be defined." + }, + { + "name": "coefficients", + "type": "float32[]", + "required": true, + "description": "A collection of weights of the model(s)." + }, + { + "name": "intercepts", + "type": "float32[]", + "required": false, + "description": "A collection of intercepts." + }, + { + "name": "multi_class", + "type": "int64", + "required": false, + "description": "Indicates whether to do OvR or multinomial (0=OvR is the default)." + }, + { + "name": "post_transform", + "type": "string", + "required": false, + "default": "NONE", + "description": "Indicates the transform to apply to the scores vector.
One of 'NONE,' 'SOFTMAX,' 'LOGISTIC,' 'SOFTMAX_ZERO,' or 'PROBIT'" + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Data to be classified." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "Classification outputs (one class per example)." + }, + { + "name": "Z", + "type": "tensor(float)", + "description": "Classification scores ([N,E] - one score for each class and example" + } + ], + "min_output": 2, + "max_output": 2, + "type_constraints": [ + { + "description": "The input must be a tensor of a numeric type, and of of shape [N,C] or [C]. In the latter case, it will be treated as [1,C]", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + }, + { + "description": "The output will be a tensor of strings or integers.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "LinearRegressor", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Generalized linear regression evaluation.
\n If targets is set to 1 (default) then univariate regression is performed.
\n If targets is set to M then M sets of coefficients must be passed in as a sequence\n and M results will be output for each input n in N.
\n The coefficients array is of length n, and the coefficients for each target are contiguous.\n Intercepts are optional but if provided must match the number of targets.\n", + "attributes": [ + { + "name": "coefficients", + "type": "float32[]", + "required": false, + "description": "Weights of the model(s)." + }, + { + "name": "intercepts", + "type": "float32[]", + "required": false, + "description": "Weights of the intercepts, if used." + }, + { + "name": "post_transform", + "type": "string", + "required": false, + "default": "NONE", + "description": "Indicates the transform to apply to the regression output vector.
One of 'NONE,' 'SOFTMAX,' 'LOGISTIC,' 'SOFTMAX_ZERO,' or 'PROBIT'" + }, + { + "name": "targets", + "type": "int64", + "required": false, + "default": 1, + "description": "The total number of regression targets, 1 if not defined." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Data to be regressed." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(float)", + "description": "Regression outputs (one per target, per example)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input must be a tensor of a numeric type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + } + ] + }, + { + "name": "Log", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Calculates the natural log of the given input tensor, element-wise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The natural log of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "log", + "code": "node = onnx.helper.make_node(\n 'Log',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([1, 10]).astype(np.float32)\ny = np.log(x) # expected output [0., 2.30258512]\nexpect(node, inputs=[x], outputs=[y],\n name='test_log_example')\n\nx = np.exp(np.random.randn(3, 4, 5).astype(np.float32))\ny = np.log(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_log')" + } + ] + }, + { + "name": "Log", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Calculates the natural log of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The natural log of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "log", + "code": "node = onnx.helper.make_node(\n 'Log',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([1, 10]).astype(np.float32)\ny = np.log(x) # expected output [0., 2.30258512]\nexpect(node, inputs=[x], outputs=[y],\n name='test_log_example')\n\nx = np.exp(np.random.randn(3, 4, 5).astype(np.float32))\ny = np.log(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_log')" + } + ] + }, + { + "name": "Log", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Calculates the natural log of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The natural log of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "log", + "code": "node = onnx.helper.make_node(\n 'Log',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([1, 10]).astype(np.float32)\ny = np.log(x) # expected output [0., 2.30258512]\nexpect(node, inputs=[x], outputs=[y],\n name='test_log_example')\n\nx = np.exp(np.random.randn(3, 4, 5).astype(np.float32))\ny = np.log(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_log')" + } + ] + }, + { + "name": "LogSoftmax", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "The operator computes the logsoftmax (log of softmax) values for each layer in the batch\n of the given input. The input is a 2-D tensor (Tensor) of size\n(batch_size x input_feature_dimensions). The output tensor has the same shape\nand contains the logsoftmax values of the corresponding input.\n\nInput does not need to explicitly be a 2D vector; rather, it will be\ncoerced into one. For an arbitrary n-dimensional tensor\ninput \\in [a_0, a_1, ..., a_{k-1}, a_k, ..., a_{n-1}] and k is\nthe axis provided, then input will be coerced into a 2-dimensional tensor with\ndimensions [a_0 * ... * a_{k-1}, a_k * ... * a_{n-1}]. For the default\ncase where axis=1, this means the input tensor will be coerced into a 2D tensor\nof dimensions [a_0, a_1 * ... * a_{n-1}], where a_0 is often the batch size.\nIn this situation, we must have a_0 = N and a_1 * ... * a_{n-1} = D.\nEach of these dimensions must be matched correctly, or else the operator\nwill throw errors.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Describes the axis of the inputs when coerced to 2D; defaults to one because the 0th axis most likely describes the batch_size" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input tensor that's coerced into a 2D matrix of size (NxD) as described above." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output values with the same shape as input tensor (the original size without coercion)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "logsoftmax", + "code": "node = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.array([[-1, 0, 1]]).astype(np.float32)\n# expected output\n# [[-2.4076061 -1.407606 -0.407606 ]]\ny = logsoftmax(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_example_1')" + }, + { + "summary": "logsoftmax_axis", + "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]\n ).astype(np.float32)\n# expected output\n# [[-3.4401896 -2.4401896 -1.4401896 -0.44018966]\n# [-3.4401896 -2.4401896 -1.4401896 -0.44018966]]\ny = logsoftmax(x)\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_large_number')\n\nx = np.abs(np.random.randn(3, 4, 5).astype(np.float32))\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=0,\n)\ny = logsoftmax(x, axis=0)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_axis_0')\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=1,\n)\ny = logsoftmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_axis_1')\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=2,\n)\ny = logsoftmax(x, axis=2)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_axis_2')\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=-1,\n)\ny = logsoftmax(x, axis=-1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_negative_axis')\n\n# default axis is -1\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_default_axis')" + } + ], + "category": "Activation" + }, + { + "name": "LogSoftmax", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "The operator computes the logsoftmax (log of softmax) values for each layer in the batch\n of the given input.\n\nThe input does not need to explicitly be a 2D vector; rather, it will be\ncoerced into one. For an arbitrary n-dimensional tensor\ninput \\in [a_0, a_1, ..., a_{k-1}, a_k, ..., a_{n-1}] and k is\nthe axis provided, then input will be coerced into a 2-dimensional tensor with\ndimensions [a_0 * ... * a_{k-1}, a_k * ... * a_{n-1}]. For the default\ncase where axis=1, this means the input tensor will be coerced into a 2D tensor\nof dimensions [a_0, a_1 * ... * a_{n-1}], where a_0 is often the batch size.\nIn this situation, we must have a_0 = N and a_1 * ... * a_{n-1} = D.\nEach of these dimensions must be matched correctly, or else the operator\nwill throw errors. The output tensor has the same shape\nand contains the logsoftmax values of the corresponding input.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Describes the axis of the inputs when coerced to 2D; defaults to one because the 0th axis most likely describes the batch_size. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input tensor that's coerced into a 2D matrix of size (NxD) as described above." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output values with the same shape as input tensor (the original size without coercion)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "logsoftmax", + "code": "node = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.array([[-1, 0, 1]]).astype(np.float32)\n# expected output\n# [[-2.4076061 -1.407606 -0.407606 ]]\ny = logsoftmax(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_example_1')" + }, + { + "summary": "logsoftmax_axis", + "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]\n ).astype(np.float32)\n# expected output\n# [[-3.4401896 -2.4401896 -1.4401896 -0.44018966]\n# [-3.4401896 -2.4401896 -1.4401896 -0.44018966]]\ny = logsoftmax(x)\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_large_number')\n\nx = np.abs(np.random.randn(3, 4, 5).astype(np.float32))\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=0,\n)\ny = logsoftmax(x, axis=0)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_axis_0')\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=1,\n)\ny = logsoftmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_axis_1')\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=2,\n)\ny = logsoftmax(x, axis=2)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_axis_2')\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=-1,\n)\ny = logsoftmax(x, axis=-1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_negative_axis')\n\n# default axis is -1\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_default_axis')" + } + ], + "category": "Activation" + }, + { + "name": "LogSoftmax", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "The operator computes the log of softmax values for the given input:\n\n LogSoftmax(input, axis) = Log(Softmax(input, axis=axis))\n\nThe \"axis\" attribute indicates the dimension along which LogSoftmax\nwill be performed. The output tensor has the same shape\nand contains the LogSoftmax values of the corresponding input.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": -1, + "description": "\nDescribes the dimension LogSoftmax will be performed on.\nNegative value means counting dimensions\nfrom the back. Accepted range is [-r, r-1] where r = rank(input).\n" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input tensor of rank >= axis." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output values with the same shape as the input tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "logsoftmax", + "code": "node = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.array([[-1, 0, 1]]).astype(np.float32)\n# expected output\n# [[-2.4076061 -1.407606 -0.407606 ]]\ny = logsoftmax(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_example_1')" + }, + { + "summary": "logsoftmax_axis", + "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]\n ).astype(np.float32)\n# expected output\n# [[-3.4401896 -2.4401896 -1.4401896 -0.44018966]\n# [-3.4401896 -2.4401896 -1.4401896 -0.44018966]]\ny = logsoftmax(x)\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_large_number')\n\nx = np.abs(np.random.randn(3, 4, 5).astype(np.float32))\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=0,\n)\ny = logsoftmax(x, axis=0)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_axis_0')\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=1,\n)\ny = logsoftmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_axis_1')\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=2,\n)\ny = logsoftmax(x, axis=2)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_axis_2')\n\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n axis=-1,\n)\ny = logsoftmax(x, axis=-1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_negative_axis')\n\n# default axis is -1\nnode = onnx.helper.make_node(\n 'LogSoftmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_logsoftmax_default_axis')" + } + ], + "category": "Activation" + }, + { + "name": "Loop", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Generic Looping construct. This loop has multiple termination conditions:\n\n1) Trip count. Iteration count specified at runtime. Set by\n specifying the input M. Optional. Set to empty string to omit.\n Note that a static trip count (specified at graph construction time) can be\n specified by passing in a constant node for input M.\n2) Loop termination condition. This is an input to the op that determines\n whether to run the first iteration and also a loop-carried dependency for\n the body graph. The body graph must yield a value for the condition variable,\n whether this input is provided or not.\n\nThis table summarizes the operating modes of this operator with equivalent\nC-style code:\n\n Operator inputs defined as (max_trip_count, condition_var).\n\n input (\"\", \"\"):\n for (int i=0; ; ++i) {\n cond = ... // Note this value is ignored, but is required in the body\n }\n\n input (\"\", cond) // Note this is analogous to a while loop\n bool cond = ...;\n for (int i=0; cond; ++i) {\n cond = ...;\n }\n\n input (\"\", 1) // Note this is analogous to a do-while loop\n bool cond = true\n for (int i=0; cond; ++i) {\n cond = ...;\n }\n\n input (trip_count, \"\") // Note this is analogous to a for loop\n int trip_count = ...\n for (int i=0; i < trip_count; ++i) {\n cond = ...; // ignored\n }\n\n input (trip_count, cond)\n int trip_count = ...;\n bool cond = ...;\n for (int i=0; i < trip_count && cond; ++i) {\n cond = ...;\n }\n\n\n*Sample usage - cond as well as trip count*\n\n graph predict-net {\n %a = Constant[value = ]()\n %b = Constant[value = ]()\n %keepgoing = Constant[value = ]()\n %max_trip_count = Constant[value = ]()\n %keepgoing_out, %b_out, %user_defined_vals = Loop[body = ](%max_trip_count, %keepgoing, %b)\n return\n }\n\n graph body-net (\n %i[INT32, scalar]\n %keepgoing[BOOL, scalar]\n %b[INT32, scalar]\n ) {\n %my_local = Add(%a, %b)\n %b_out = Sub(%a, %b)\n %keepgoing_out = Greater(%my_local, %b_out)\n %user_defined_vals = Add(%b, %b)\n return %keepgoing_out, %b_out, %user_defined_vals\n }\n\n*Sample equivalent C code*\n\n {\n /* User-defined code (enclosing scope) */\n int a = 3, b = 6;\n bool keepgoing = true; // Analogous to input cond\n /* End user-defined code */\n\n /* Implicitly-defined code */\n const int max_trip_count = 10; // Analogous to input M\n int user_defined_vals[]; // Imagine this is resizable\n /* End implicitly-defined code */\n for (int i=0; i < max_trip_count && keepgoing; ++i) {\n /* User-defined code (loop body) */\n int my_local = a + b; // Reading values in the enclosing scope is fine\n b = a - b; // writes fine if we specify b as a loop-carried dependency\n keepgoing = my_local > b; // keepgoing is a loop-carried dependency\n user_defined_vals[i] = b + b;\n /* End user-defined code */\n }\n // my_local = 123; // Can't do this. my_local was defined in the the body\n\n // These below values are live-out from the loop and therefore accessible\n b_out; user_defined_vals; keepgoing_out;\n }\n\nThere are several things of note in this code snippet:\n\n1) Values from the enclosing scope (i.e. variable a here) are in scope and can\n be referenced in the inputs of the loop.\n2) Any variables which you wish to make available in the enclosing scope (i.e.\n the variables b and keepgoing) must be declared as either loop-carried\n dependencies (both at the op inputs and output and at the body net input and\n output) or scan_outputs.\n3) Values created in the body cannot be accessed in the enclosing scope.\n\nNote that the semantics of this op support \"diagonal\" or \"wavefront\" execution.\n(See Step 3 here for an example:\nhttps://devblogs.nvidia.com/optimizing-recurrent-neural-networks-cudnn-5/).\nFrontends should emit multi-layer RNNs as a series of While operators (with\ntime being the inner looping dimension), with each successive layer consuming\nthe scan_outputs from the previous layer, possibly going through several\npoint-wise operators (e.g. dropout, residual connections, linear layer).\n", + "attributes": [ + { + "name": "body", + "type": "graph", + "required": true, + "description": "The graph run each iteration. It has 2+N inputs: (iteration_num, condition, loop carried dependencies...). It has 1+N+K outputs: (condition, loop carried dependencies..., scan_outputs...). Each scan_output is created by concatenating the value of the specified output value at the end of each iteration of the loop. It is an error if the dimensions or data type of these scan_outputs change across loop iterations." + } + ], + "inputs": [ + { + "name": "M", + "type": "I", + "option": "optional", + "description": "A maximum trip-count for the loop specified at runtime. Optional. Pass empty string to skip." + }, + { + "name": "cond", + "type": "B", + "option": "optional", + "description": "A boolean termination condition. Optional. Pass empty string to skip." + }, + { + "name": "v_initial", + "type": "V", + "list": true, + "description": "The initial values of any loop-carried dependencies (values that change across loop iterations)" + } + ], + "min_input": 3, + "max_input": 2147483647, + "outputs": [ + { + "name": "v_final_and_scan_outputs", + "type": "V", + "list": true, + "description": "Final N loop carried dependency values then K scan_outputs" + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "3 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "tensor of int64, which should be a scalar.", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + }, + { + "description": "tensor of bool, which should be a scalar.", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "loop_11", + "code": "# Given a tensor x of values [x1, ..., xN], and initial tensor y\n# sum up its elements using a scan\n# returning the final state (y+x1+x2+...+xN) as well the scan_output\n# [y+x1, y+x1+x2, ..., y+x1+x2+...+xN]\n\ny_in = onnx.helper.make_tensor_value_info('y_in', onnx.TensorProto.FLOAT, [1])\ny_out = onnx.helper.make_tensor_value_info('y_out', onnx.TensorProto.FLOAT, [1])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [1])\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\ny = np.array([-2]).astype(np.float32)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\ni_add_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['iter_count'],\n outputs=['slice_start'],\n axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end'],\n outputs=['slice_end'],\n axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ny_add_node = onnx.helper.make_node(\n 'Add',\n inputs=['y_in', 'slice_out'],\n outputs=['y_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nscan_identity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['y_out'],\n outputs=['scan_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, x_const_node, one_const_node, i_add_node,\n start_unsqueeze_node, end_unsqueeze_node, slice_node, y_add_node,\n scan_identity_node],\n 'loop_body',\n [iter_count, cond_in, y_in],\n [cond_out, y_out, scan_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'y'],\n outputs=['res_y', 'res_scan'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\nres_y = np.array([13]).astype(np.float32)\ncond = np.array(1).astype(bool)\nres_scan = np.array([-1, 1, 4, 8, 13]).astype(np.float32).reshape((5, 1))\nexpect(node, inputs=[trip_count, cond, y], outputs=[res_y, res_scan],\n name='test_loop11', opset_imports=[onnx.helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "loop_13", + "code": "# Given a tensor x of values [x1, ..., xN],\n# Return a sequence of tensors of\n# [[x1], [x1, x2], ..., [x1, ..., xN]]\n\nseq_in = onnx.helper.make_tensor_sequence_value_info('seq_in', onnx.TensorProto.FLOAT, None)\nseq_out = onnx.helper.make_tensor_sequence_value_info('seq_out', onnx.TensorProto.FLOAT, None)\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\nzero_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['slice_start'],\n value=onnx.helper.make_tensor(\n name='const_tensor_zero',\n data_type=onnx.TensorProto.INT64,\n dims=(1,),\n vals=[0]\n )\n)\n\naxes_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['axes'],\n value=onnx.helper.make_tensor(\n name='const_tensor_axes',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[0]\n )\n)\n\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end', 'axes'],\n outputs=['slice_end']\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ninsert_node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['seq_in', 'slice_out'],\n outputs=['seq_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, x_const_node, one_const_node, zero_const_node, add_node,\n axes_node, end_unsqueeze_node, slice_node, insert_node],\n 'loop_body',\n [iter_count, cond_in, seq_in],\n [cond_out, seq_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'seq_empty'],\n outputs=['seq_res'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\nseq_empty: List[Any] = []\nseq_res = [x[:int(i)] for i in x]\ncond = np.array(1).astype(bool)\nexpect(node, inputs=[trip_count, cond, seq_empty], outputs=[seq_res],\n name='test_loop13_seq', opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, trip_count.shape),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, []))])" + }, + { + "summary": "loop_16_none", + "code": "# Given a tensor sequence of values [x1, ..., xN], and an initial optional sequence of tensors [x0],\n# Return a concatenated sequence of tensors of\n# [x0, [x1], [x1, x2], ..., [x1, ..., xN]]\n\nten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\nopt_in_tp = onnx.helper.make_optional_type_proto(seq_in_tp)\nopt_in = onnx.helper.make_value_info('opt_seq_in', opt_in_tp)\nseq_out = onnx.helper.make_tensor_sequence_value_info('seq_out', onnx.TensorProto.FLOAT, [])\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx0 = np.array(0).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n\noptional_has_elem_node = onnx.helper.make_node(\n 'OptionalHasElement',\n inputs=['opt_seq_in'],\n outputs=['optional_has_elem']\n)\n\noptional_is_none = onnx.helper.make_node(\n 'Not',\n inputs=['optional_has_elem'],\n outputs=['optional_is_none']\n)\n\noptional_get_elem = onnx.helper.make_node(\n 'OptionalGetElement',\n inputs=['opt_seq_in'],\n outputs=['seq_in']\n)\n\nconstant_in = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['constant_in'],\n value=onnx.helper.make_tensor(\n name='const_tensor',\n data_type=onnx.TensorProto.FLOAT,\n dims=(),\n vals=[0]\n )\n)\n\nseq_const_in = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['constant_in'],\n outputs=['init_seq_in']\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info('init_seq_in', onnx.TensorProto.FLOAT, [])\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in],\n 'then_body',\n [],\n [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info('seq_in', onnx.TensorProto.FLOAT, [])\nelse_body = onnx.helper.make_graph(\n [optional_get_elem],\n 'else_body',\n [],\n [else_seq_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['optional_is_none'],\n outputs=['sequence'],\n then_branch=then_body,\n else_branch=else_body\n)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\nzero_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['slice_start'],\n value=onnx.helper.make_tensor(\n name='const_tensor_zero',\n data_type=onnx.TensorProto.INT64,\n dims=(1,),\n vals=[0]\n )\n)\n\naxes_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['axes'],\n value=onnx.helper.make_tensor(\n name='const_tensor_axes',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[0]\n )\n)\n\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end', 'axes'],\n outputs=['slice_end']\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ninsert_node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['sequence', 'slice_out'],\n outputs=['seq_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, optional_has_elem_node, optional_is_none, if_node, x_const_node, one_const_node,\n zero_const_node, add_node, axes_node, end_unsqueeze_node, slice_node, insert_node],\n 'loop_body',\n [iter_count, cond_in, opt_in],\n [cond_out, seq_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'opt_seq'],\n outputs=['seq_res'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\ncond = np.array(1).astype(bool)\nseq_res = compute_loop_outputs(x, [x0], trip_count)\nopt_seq_in: List[Any] = [x0]\nexpect(node, inputs=[trip_count, cond, opt_seq_in], outputs=[seq_res],\n name='test_loop16_seq_none', opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, trip_count.shape),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp])" + } + ] + }, + { + "name": "Loop", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Generic Looping construct. This loop has multiple termination conditions:\n\n1) Trip count. Iteration count specified at runtime. Set by\n specifying the input M. Optional. Set to empty string to omit.\n Note that a static trip count (specified at graph construction time) can be\n specified by passing in a constant node for input M.\n2) Loop termination condition. This is an input to the op that determines\n whether to run the first iteration and also a loop-carried dependency for\n the body graph. The body graph must yield a value for the condition variable,\n whether this input is provided or not.\n\nThis table summarizes the operating modes of this operator with equivalent\nC-style code:\n\n Operator inputs defined as (max_trip_count, condition_var).\n\n input (\"\", \"\"):\n for (int i=0; ; ++i) {\n cond = ... // Note this value is ignored, but is required in the body\n }\n\n input (\"\", cond) // Note this is analogous to a while loop\n bool cond = ...;\n for (int i=0; cond; ++i) {\n cond = ...;\n }\n\n input (\"\", 1) // Note this is analogous to a do-while loop\n bool cond = true\n for (int i=0; cond; ++i) {\n cond = ...;\n }\n\n input (trip_count, \"\") // Note this is analogous to a for loop\n int trip_count = ...\n for (int i=0; i < trip_count; ++i) {\n cond = ...; // ignored\n }\n\n input (trip_count, cond)\n int trip_count = ...;\n bool cond = ...;\n for (int i=0; i < trip_count && cond; ++i) {\n cond = ...;\n }\n\n\n*Sample usage - cond as well as trip count*\n\n graph predict-net {\n %a = Constant[value = ]()\n %b = Constant[value = ]()\n %keepgoing = Constant[value = ]()\n %max_trip_count = Constant[value = ]()\n %keepgoing_out, %b_out, %user_defined_vals = Loop[body = ](%max_trip_count, %keepgoing, %b)\n return\n }\n\n graph body-net (\n %i[INT32, scalar] // iteration number\n %keepgoing_in[BOOL, scalar] // incoming loop-termination-condition; not used\n %b_in[INT32, scalar] // incoming value of loop-carried-dependency b\n ) {\n %my_local = Add(%a, %b_in)\n %b_out = Sub(%a, %b_in) // outgoing value of loop-carried-dependency b\n %keepgoing_out = Greater(%my_local, %b_out) // outgoing loop-termination-condition\n %user_defined_val = Add(%b_in, %b_in) // scan-output value to be accumulated\n return %keepgoing_out, %b_out, %user_defined_val\n }\n\n*Sample equivalent C code*\n\n {\n /* User-defined code (enclosing scope) */\n int a = 3, b = 6;\n bool keepgoing = true; // Analogous to input cond\n /* End user-defined code */\n\n /* Implicitly-defined code */\n const int max_trip_count = 10; // Analogous to input M\n int user_defined_vals[]; // Imagine this is resizable\n /* End implicitly-defined code */\n /* initialize loop-carried variables and scan-output variables */\n bool keepgoing_out = keepgoing\n int b_out = b\n\n for (int i=0; i < max_trip_count && keepgoing_out; ++i) {\n /* Implicitly-defined code: bind actual parameter values\n to formal parameter variables of loop-body */\n bool keepgoing_in = keepgoing_out;\n bool b_in = b_out;\n\n /* User-defined code (loop body) */\n int my_local = a + b_in; // Reading value \"a\" from the enclosing scope is fine\n b_out = a - b_in;\n keepgoing_out = my_local > b_out;\n user_defined_val = b_in + b_in; // b_in and b_out are different variables\n /* End user-defined code */\n\n /* Implicitly defined-code */\n user_defined_vals[i] = user_defined_val // accumulate scan-output values\n }\n // int t = my_local; // Can't do this. my_local is not accessible here.\n\n // The values below are bound to the output variables of the loop and therefore accessible\n // b_out; user_defined_vals; keepgoing_out;\n }\n\nThere are several things of note in this code snippet:\n\n1) Values from the enclosing scope (i.e. variable \"a\" here) are in scope and can\n be referenced in the inputs of the loop.\n2) Any values computed in the loop body that needs to be used in a subsequent\n iteration or after the loop are modelled using a pair of variables in the loop-body,\n consisting of an input variable (eg., b_in) and an output variable (eg., b_out).\n These are referred to as loop-carried dependences. The loop operation node\n supplies the input value of the input variable for the first iteration, and\n returns the output value of the output variable produced by the final\n iteration.\n3) Scan_output variables are used to implicitly concatenate values computed across\n all the iterations. In the above example, the value of user_defined_val computed\n over all iterations are concatenated and returned as the value of user_defined_vals\n after the loop.\n4) Values created in the body cannot be accessed in the enclosing scope,\n except using the mechanism described above.\n\nNote that the semantics of this op support \"diagonal\" or \"wavefront\" execution.\n(See Step 3 here for an example:\nhttps://devblogs.nvidia.com/optimizing-recurrent-neural-networks-cudnn-5/).\nFrontends should emit multi-layer RNNs as a series of While operators (with\ntime being the inner looping dimension), with each successive layer consuming\nthe scan_outputs from the previous layer, possibly going through several\npoint-wise operators (e.g. dropout, residual connections, linear layer).\n", + "attributes": [ + { + "name": "body", + "type": "graph", + "required": true, + "description": "The graph run each iteration. It has 2+N inputs: (iteration_num, condition, loop carried dependencies...). It has 1+N+K outputs: (condition, loop carried dependencies..., scan_outputs...). Each scan_output is created by concatenating the value of the specified output value at the end of each iteration of the loop. It is an error if the dimensions or data type of these scan_outputs change across loop iterations." + } + ], + "inputs": [ + { + "name": "M", + "type": "I", + "option": "optional", + "description": "A maximum trip-count for the loop specified at runtime. Optional. Pass empty string to skip." + }, + { + "name": "cond", + "type": "B", + "option": "optional", + "description": "A boolean termination condition. Optional. Pass empty string to skip." + }, + { + "name": "v_initial", + "type": "V", + "list": true, + "description": "The initial values of any loop-carried dependencies (values that change across loop iterations)" + } + ], + "min_input": 2, + "max_input": 2147483647, + "outputs": [ + { + "name": "v_final_and_scan_outputs", + "type": "V", + "list": true, + "description": "Final N loop carried dependency values then K scan_outputs" + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "2 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "tensor of int64, which should be a scalar.", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + }, + { + "description": "tensor of bool, which should be a scalar.", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "loop_11", + "code": "# Given a tensor x of values [x1, ..., xN], and initial tensor y\n# sum up its elements using a scan\n# returning the final state (y+x1+x2+...+xN) as well the scan_output\n# [y+x1, y+x1+x2, ..., y+x1+x2+...+xN]\n\ny_in = onnx.helper.make_tensor_value_info('y_in', onnx.TensorProto.FLOAT, [1])\ny_out = onnx.helper.make_tensor_value_info('y_out', onnx.TensorProto.FLOAT, [1])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [1])\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\ny = np.array([-2]).astype(np.float32)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\ni_add_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['iter_count'],\n outputs=['slice_start'],\n axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end'],\n outputs=['slice_end'],\n axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ny_add_node = onnx.helper.make_node(\n 'Add',\n inputs=['y_in', 'slice_out'],\n outputs=['y_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nscan_identity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['y_out'],\n outputs=['scan_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, x_const_node, one_const_node, i_add_node,\n start_unsqueeze_node, end_unsqueeze_node, slice_node, y_add_node,\n scan_identity_node],\n 'loop_body',\n [iter_count, cond_in, y_in],\n [cond_out, y_out, scan_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'y'],\n outputs=['res_y', 'res_scan'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\nres_y = np.array([13]).astype(np.float32)\ncond = np.array(1).astype(bool)\nres_scan = np.array([-1, 1, 4, 8, 13]).astype(np.float32).reshape((5, 1))\nexpect(node, inputs=[trip_count, cond, y], outputs=[res_y, res_scan],\n name='test_loop11', opset_imports=[onnx.helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "loop_13", + "code": "# Given a tensor x of values [x1, ..., xN],\n# Return a sequence of tensors of\n# [[x1], [x1, x2], ..., [x1, ..., xN]]\n\nseq_in = onnx.helper.make_tensor_sequence_value_info('seq_in', onnx.TensorProto.FLOAT, None)\nseq_out = onnx.helper.make_tensor_sequence_value_info('seq_out', onnx.TensorProto.FLOAT, None)\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\nzero_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['slice_start'],\n value=onnx.helper.make_tensor(\n name='const_tensor_zero',\n data_type=onnx.TensorProto.INT64,\n dims=(1,),\n vals=[0]\n )\n)\n\naxes_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['axes'],\n value=onnx.helper.make_tensor(\n name='const_tensor_axes',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[0]\n )\n)\n\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end', 'axes'],\n outputs=['slice_end']\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ninsert_node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['seq_in', 'slice_out'],\n outputs=['seq_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, x_const_node, one_const_node, zero_const_node, add_node,\n axes_node, end_unsqueeze_node, slice_node, insert_node],\n 'loop_body',\n [iter_count, cond_in, seq_in],\n [cond_out, seq_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'seq_empty'],\n outputs=['seq_res'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\nseq_empty: List[Any] = []\nseq_res = [x[:int(i)] for i in x]\ncond = np.array(1).astype(bool)\nexpect(node, inputs=[trip_count, cond, seq_empty], outputs=[seq_res],\n name='test_loop13_seq', opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, trip_count.shape),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, []))])" + }, + { + "summary": "loop_16_none", + "code": "# Given a tensor sequence of values [x1, ..., xN], and an initial optional sequence of tensors [x0],\n# Return a concatenated sequence of tensors of\n# [x0, [x1], [x1, x2], ..., [x1, ..., xN]]\n\nten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\nopt_in_tp = onnx.helper.make_optional_type_proto(seq_in_tp)\nopt_in = onnx.helper.make_value_info('opt_seq_in', opt_in_tp)\nseq_out = onnx.helper.make_tensor_sequence_value_info('seq_out', onnx.TensorProto.FLOAT, [])\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx0 = np.array(0).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n\noptional_has_elem_node = onnx.helper.make_node(\n 'OptionalHasElement',\n inputs=['opt_seq_in'],\n outputs=['optional_has_elem']\n)\n\noptional_is_none = onnx.helper.make_node(\n 'Not',\n inputs=['optional_has_elem'],\n outputs=['optional_is_none']\n)\n\noptional_get_elem = onnx.helper.make_node(\n 'OptionalGetElement',\n inputs=['opt_seq_in'],\n outputs=['seq_in']\n)\n\nconstant_in = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['constant_in'],\n value=onnx.helper.make_tensor(\n name='const_tensor',\n data_type=onnx.TensorProto.FLOAT,\n dims=(),\n vals=[0]\n )\n)\n\nseq_const_in = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['constant_in'],\n outputs=['init_seq_in']\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info('init_seq_in', onnx.TensorProto.FLOAT, [])\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in],\n 'then_body',\n [],\n [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info('seq_in', onnx.TensorProto.FLOAT, [])\nelse_body = onnx.helper.make_graph(\n [optional_get_elem],\n 'else_body',\n [],\n [else_seq_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['optional_is_none'],\n outputs=['sequence'],\n then_branch=then_body,\n else_branch=else_body\n)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\nzero_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['slice_start'],\n value=onnx.helper.make_tensor(\n name='const_tensor_zero',\n data_type=onnx.TensorProto.INT64,\n dims=(1,),\n vals=[0]\n )\n)\n\naxes_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['axes'],\n value=onnx.helper.make_tensor(\n name='const_tensor_axes',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[0]\n )\n)\n\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end', 'axes'],\n outputs=['slice_end']\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ninsert_node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['sequence', 'slice_out'],\n outputs=['seq_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, optional_has_elem_node, optional_is_none, if_node, x_const_node, one_const_node,\n zero_const_node, add_node, axes_node, end_unsqueeze_node, slice_node, insert_node],\n 'loop_body',\n [iter_count, cond_in, opt_in],\n [cond_out, seq_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'opt_seq'],\n outputs=['seq_res'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\ncond = np.array(1).astype(bool)\nseq_res = compute_loop_outputs(x, [x0], trip_count)\nopt_seq_in: List[Any] = [x0]\nexpect(node, inputs=[trip_count, cond, opt_seq_in], outputs=[seq_res],\n name='test_loop16_seq_none', opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, trip_count.shape),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp])" + } + ] + }, + { + "name": "Loop", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Generic Looping construct. This loop has multiple termination conditions:\n\n1) Trip count. Iteration count specified at runtime. Set by\n specifying the input M. Optional. Set to empty string to omit.\n Note that a static trip count (specified at graph construction time) can be\n specified by passing in a constant node for input M.\n2) Loop termination condition. This is an input to the op that determines\n whether to run the first iteration and also a loop-carried dependency for\n the body graph. The body graph must yield a value for the condition variable,\n whether this input is provided or not.\n\nThis table summarizes the operating modes of this operator with equivalent\nC-style code:\n\n Operator inputs defined as (max_trip_count, condition_var).\n\n input (\"\", \"\"):\n for (int i=0; ; ++i) {\n cond = ... // Note this value is ignored, but is required in the body\n }\n\n input (\"\", cond) // Note this is analogous to a while loop\n bool cond = ...;\n for (int i=0; cond; ++i) {\n cond = ...;\n }\n\n input (\"\", 1) // Note this is analogous to a do-while loop\n bool cond = true\n for (int i=0; cond; ++i) {\n cond = ...;\n }\n\n input (trip_count, \"\") // Note this is analogous to a for loop\n int trip_count = ...\n for (int i=0; i < trip_count; ++i) {\n cond = ...; // ignored\n }\n\n input (trip_count, cond)\n int trip_count = ...;\n bool cond = ...;\n for (int i=0; i < trip_count && cond; ++i) {\n cond = ...;\n }\n\n\n*Sample usage - cond as well as trip count*\n\n graph predict-net {\n %a = Constant[value = ]()\n %b = Constant[value = ]()\n %keepgoing = Constant[value = ]()\n %max_trip_count = Constant[value = ]()\n %keepgoing_out, %b_out, %user_defined_vals = Loop[body = ](%max_trip_count, %keepgoing, %b)\n return\n }\n\n graph body-net (\n %i[INT32, scalar] // iteration number\n %keepgoing_in[BOOL, scalar] // incoming loop-termination-condition; not used\n %b_in[INT32, scalar] // incoming value of loop-carried-dependency b\n ) {\n %my_local = Add(%a, %b_in)\n %b_out = Sub(%a, %b_in) // outgoing value of loop-carried-dependency b\n %keepgoing_out = Greater(%my_local, %b_out) // outgoing loop-termination-condition\n %user_defined_val = Add(%b_in, %b_in) // scan-output value to be accumulated\n return %keepgoing_out, %b_out, %user_defined_val\n }\n\n*Sample equivalent C code*\n\n {\n /* User-defined code (enclosing scope) */\n int a = 3, b = 6;\n bool keepgoing = true; // Analogous to input cond\n /* End user-defined code */\n\n /* Implicitly-defined code */\n const int max_trip_count = 10; // Analogous to input M\n int user_defined_vals[]; // Imagine this is resizable\n /* End implicitly-defined code */\n /* initialize loop-carried variables and scan-output variables */\n bool keepgoing_out = keepgoing\n int b_out = b\n\n for (int i=0; i < max_trip_count && keepgoing_out; ++i) {\n /* Implicitly-defined code: bind actual parameter values\n to formal parameter variables of loop-body */\n bool keepgoing_in = keepgoing_out;\n bool b_in = b_out;\n\n /* User-defined code (loop body) */\n int my_local = a + b_in; // Reading value \"a\" from the enclosing scope is fine\n b_out = a - b_in;\n keepgoing_out = my_local > b_out;\n user_defined_val = b_in + b_in; // b_in and b_out are different variables\n /* End user-defined code */\n\n /* Implicitly defined-code */\n user_defined_vals[i] = user_defined_val // accumulate scan-output values\n }\n // int t = my_local; // Can't do this. my_local is not accessible here.\n\n // The values below are bound to the output variables of the loop and therefore accessible\n // b_out; user_defined_vals; keepgoing_out;\n }\n\nThere are several things of note in this code snippet:\n\n1) Values from the enclosing scope (i.e. variable \"a\" here) are in scope and can\n be referenced in the inputs of the loop.\n2) Any values computed in the loop body that needs to be used in a subsequent\n iteration or after the loop are modelled using a pair of variables in the loop-body,\n consisting of an input variable (eg., b_in) and an output variable (eg., b_out).\n These are referred to as loop-carried dependences. The loop operation node\n supplies the input value of the input variable for the first iteration, and\n returns the output value of the output variable produced by the final\n iteration.\n3) Scan_output variables are used to implicitly concatenate values computed across\n all the iterations. In the above example, the value of user_defined_val computed\n over all iterations are concatenated and returned as the value of user_defined_vals\n after the loop.\n4) Values created in the body cannot be accessed in the enclosing scope,\n except using the mechanism described above.\n\nNote that the semantics of this op support \"diagonal\" or \"wavefront\" execution.\n(See Step 3 here for an example:\nhttps://devblogs.nvidia.com/optimizing-recurrent-neural-networks-cudnn-5/).\nFrontends should emit multi-layer RNNs as a series of While operators (with\ntime being the inner looping dimension), with each successive layer consuming\nthe scan_outputs from the previous layer, possibly going through several\npoint-wise operators (e.g. dropout, residual connections, linear layer).\n\nThe input/output of subgraph (produced by loop node) matching is based on order instead of name. The implementation will figure out the names based on this order.\n", + "attributes": [ + { + "name": "body", + "type": "graph", + "required": true, + "description": "The graph run each iteration. It has 2+N inputs: (iteration_num, condition, loop carried dependencies...). It has 1+N+K outputs: (condition, loop carried dependencies..., scan_outputs...). Each scan_output is created by concatenating the value of the specified output value at the end of each iteration of the loop. It is an error if the dimensions or data type of these scan_outputs change across loop iterations." + } + ], + "inputs": [ + { + "name": "M", + "type": "I", + "option": "optional", + "description": "A maximum trip-count for the loop specified at runtime. Optional. Pass empty string to skip." + }, + { + "name": "cond", + "type": "B", + "option": "optional", + "description": "A boolean termination condition. Optional. Pass empty string to skip." + }, + { + "name": "v_initial", + "type": "V", + "list": true, + "description": "The initial values of any loop-carried dependencies (values that change across loop iterations)" + } + ], + "min_input": 2, + "max_input": 2147483647, + "outputs": [ + { + "name": "v_final_and_scan_outputs", + "type": "V", + "list": true, + "description": "Final N loop carried dependency values then K scan_outputs. Scan outputs must be Tensors." + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "2 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor and Sequence types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)", + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + }, + { + "description": "tensor of int64, which should be a scalar.", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + }, + { + "description": "tensor of bool, which should be a scalar.", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "loop_11", + "code": "# Given a tensor x of values [x1, ..., xN], and initial tensor y\n# sum up its elements using a scan\n# returning the final state (y+x1+x2+...+xN) as well the scan_output\n# [y+x1, y+x1+x2, ..., y+x1+x2+...+xN]\n\ny_in = onnx.helper.make_tensor_value_info('y_in', onnx.TensorProto.FLOAT, [1])\ny_out = onnx.helper.make_tensor_value_info('y_out', onnx.TensorProto.FLOAT, [1])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [1])\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\ny = np.array([-2]).astype(np.float32)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\ni_add_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['iter_count'],\n outputs=['slice_start'],\n axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end'],\n outputs=['slice_end'],\n axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ny_add_node = onnx.helper.make_node(\n 'Add',\n inputs=['y_in', 'slice_out'],\n outputs=['y_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nscan_identity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['y_out'],\n outputs=['scan_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, x_const_node, one_const_node, i_add_node,\n start_unsqueeze_node, end_unsqueeze_node, slice_node, y_add_node,\n scan_identity_node],\n 'loop_body',\n [iter_count, cond_in, y_in],\n [cond_out, y_out, scan_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'y'],\n outputs=['res_y', 'res_scan'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\nres_y = np.array([13]).astype(np.float32)\ncond = np.array(1).astype(bool)\nres_scan = np.array([-1, 1, 4, 8, 13]).astype(np.float32).reshape((5, 1))\nexpect(node, inputs=[trip_count, cond, y], outputs=[res_y, res_scan],\n name='test_loop11', opset_imports=[onnx.helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "loop_13", + "code": "# Given a tensor x of values [x1, ..., xN],\n# Return a sequence of tensors of\n# [[x1], [x1, x2], ..., [x1, ..., xN]]\n\nseq_in = onnx.helper.make_tensor_sequence_value_info('seq_in', onnx.TensorProto.FLOAT, None)\nseq_out = onnx.helper.make_tensor_sequence_value_info('seq_out', onnx.TensorProto.FLOAT, None)\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\nzero_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['slice_start'],\n value=onnx.helper.make_tensor(\n name='const_tensor_zero',\n data_type=onnx.TensorProto.INT64,\n dims=(1,),\n vals=[0]\n )\n)\n\naxes_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['axes'],\n value=onnx.helper.make_tensor(\n name='const_tensor_axes',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[0]\n )\n)\n\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end', 'axes'],\n outputs=['slice_end']\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ninsert_node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['seq_in', 'slice_out'],\n outputs=['seq_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, x_const_node, one_const_node, zero_const_node, add_node,\n axes_node, end_unsqueeze_node, slice_node, insert_node],\n 'loop_body',\n [iter_count, cond_in, seq_in],\n [cond_out, seq_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'seq_empty'],\n outputs=['seq_res'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\nseq_empty: List[Any] = []\nseq_res = [x[:int(i)] for i in x]\ncond = np.array(1).astype(bool)\nexpect(node, inputs=[trip_count, cond, seq_empty], outputs=[seq_res],\n name='test_loop13_seq', opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, trip_count.shape),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, []))])" + }, + { + "summary": "loop_16_none", + "code": "# Given a tensor sequence of values [x1, ..., xN], and an initial optional sequence of tensors [x0],\n# Return a concatenated sequence of tensors of\n# [x0, [x1], [x1, x2], ..., [x1, ..., xN]]\n\nten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\nopt_in_tp = onnx.helper.make_optional_type_proto(seq_in_tp)\nopt_in = onnx.helper.make_value_info('opt_seq_in', opt_in_tp)\nseq_out = onnx.helper.make_tensor_sequence_value_info('seq_out', onnx.TensorProto.FLOAT, [])\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx0 = np.array(0).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n\noptional_has_elem_node = onnx.helper.make_node(\n 'OptionalHasElement',\n inputs=['opt_seq_in'],\n outputs=['optional_has_elem']\n)\n\noptional_is_none = onnx.helper.make_node(\n 'Not',\n inputs=['optional_has_elem'],\n outputs=['optional_is_none']\n)\n\noptional_get_elem = onnx.helper.make_node(\n 'OptionalGetElement',\n inputs=['opt_seq_in'],\n outputs=['seq_in']\n)\n\nconstant_in = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['constant_in'],\n value=onnx.helper.make_tensor(\n name='const_tensor',\n data_type=onnx.TensorProto.FLOAT,\n dims=(),\n vals=[0]\n )\n)\n\nseq_const_in = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['constant_in'],\n outputs=['init_seq_in']\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info('init_seq_in', onnx.TensorProto.FLOAT, [])\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in],\n 'then_body',\n [],\n [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info('seq_in', onnx.TensorProto.FLOAT, [])\nelse_body = onnx.helper.make_graph(\n [optional_get_elem],\n 'else_body',\n [],\n [else_seq_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['optional_is_none'],\n outputs=['sequence'],\n then_branch=then_body,\n else_branch=else_body\n)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\nzero_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['slice_start'],\n value=onnx.helper.make_tensor(\n name='const_tensor_zero',\n data_type=onnx.TensorProto.INT64,\n dims=(1,),\n vals=[0]\n )\n)\n\naxes_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['axes'],\n value=onnx.helper.make_tensor(\n name='const_tensor_axes',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[0]\n )\n)\n\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end', 'axes'],\n outputs=['slice_end']\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ninsert_node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['sequence', 'slice_out'],\n outputs=['seq_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, optional_has_elem_node, optional_is_none, if_node, x_const_node, one_const_node,\n zero_const_node, add_node, axes_node, end_unsqueeze_node, slice_node, insert_node],\n 'loop_body',\n [iter_count, cond_in, opt_in],\n [cond_out, seq_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'opt_seq'],\n outputs=['seq_res'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\ncond = np.array(1).astype(bool)\nseq_res = compute_loop_outputs(x, [x0], trip_count)\nopt_seq_in: List[Any] = [x0]\nexpect(node, inputs=[trip_count, cond, opt_seq_in], outputs=[seq_res],\n name='test_loop16_seq_none', opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, trip_count.shape),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp])" + } + ] + }, + { + "name": "Loop", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "Generic Looping construct. This loop has multiple termination conditions:\n\n1) Trip count. Iteration count specified at runtime. Set by\n specifying the input M. Optional. Set to empty string to omit.\n Note that a static trip count (specified at graph construction time) can be\n specified by passing in a constant node for input M.\n2) Loop termination condition. This is an input to the op that determines\n whether to run the first iteration and also a loop-carried dependency for\n the body graph. The body graph must yield a value for the condition variable,\n whether this input is provided or not.\n\nThis table summarizes the operating modes of this operator with equivalent\nC-style code:\n\n Operator inputs defined as (max_trip_count, condition_var).\n\n input (\"\", \"\"):\n for (int i=0; ; ++i) {\n cond = ... // Note this value is ignored, but is required in the body\n }\n\n input (\"\", cond) // Note this is analogous to a while loop\n bool cond = ...;\n for (int i=0; cond; ++i) {\n cond = ...;\n }\n\n input (\"\", 1) // Note this is analogous to a do-while loop\n bool cond = true\n for (int i=0; cond; ++i) {\n cond = ...;\n }\n\n input (trip_count, \"\") // Note this is analogous to a for loop\n int trip_count = ...\n for (int i=0; i < trip_count; ++i) {\n cond = ...; // ignored\n }\n\n input (trip_count, cond)\n int trip_count = ...;\n bool cond = ...;\n for (int i=0; i < trip_count && cond; ++i) {\n cond = ...;\n }\n\n\n*Sample usage - cond as well as trip count*\n\n graph predict-net {\n %a = Constant[value = ]()\n %b = Constant[value = ]()\n %keepgoing = Constant[value = ]()\n %max_trip_count = Constant[value = ]()\n %keepgoing_out, %b_out, %user_defined_vals = Loop[body = ](%max_trip_count, %keepgoing, %b)\n return\n }\n\n graph body-net (\n %i[INT32, scalar] // iteration number\n %keepgoing_in[BOOL, scalar] // incoming loop-termination-condition; not used\n %b_in[INT32, scalar] // incoming value of loop-carried-dependency b\n ) {\n %my_local = Add(%a, %b_in)\n %b_out = Sub(%a, %b_in) // outgoing value of loop-carried-dependency b\n %keepgoing_out = Greater(%my_local, %b_out) // outgoing loop-termination-condition\n %user_defined_val = Add(%b_in, %b_in) // scan-output value to be accumulated\n return %keepgoing_out, %b_out, %user_defined_val\n }\n\n*Sample equivalent C code*\n\n {\n /* User-defined code (enclosing scope) */\n int a = 3, b = 6;\n bool keepgoing = true; // Analogous to input cond\n /* End user-defined code */\n\n /* Implicitly-defined code */\n const int max_trip_count = 10; // Analogous to input M\n int user_defined_vals[]; // Imagine this is resizable\n /* End implicitly-defined code */\n /* initialize loop-carried variables and scan-output variables */\n bool keepgoing_out = keepgoing\n int b_out = b\n\n for (int i=0; i < max_trip_count && keepgoing_out; ++i) {\n /* Implicitly-defined code: bind actual parameter values\n to formal parameter variables of loop-body */\n bool keepgoing_in = keepgoing_out;\n bool b_in = b_out;\n\n /* User-defined code (loop body) */\n int my_local = a + b_in; // Reading value \"a\" from the enclosing scope is fine\n b_out = a - b_in;\n keepgoing_out = my_local > b_out;\n user_defined_val = b_in + b_in; // b_in and b_out are different variables\n /* End user-defined code */\n\n /* Implicitly defined-code */\n user_defined_vals[i] = user_defined_val // accumulate scan-output values\n }\n // int t = my_local; // Can't do this. my_local is not accessible here.\n\n // The values below are bound to the output variables of the loop and therefore accessible\n // b_out; user_defined_vals; keepgoing_out;\n }\n\nThere are several things of note in this code snippet:\n\n1) Values from the enclosing scope (i.e. variable \"a\" here) are in scope and can\n be referenced in the inputs of the loop.\n2) Any values computed in the loop body that needs to be used in a subsequent\n iteration or after the loop are modelled using a pair of variables in the loop-body,\n consisting of an input variable (eg., b_in) and an output variable (eg., b_out).\n These are referred to as loop-carried dependences. The loop operation node\n supplies the input value of the input variable for the first iteration, and\n returns the output value of the output variable produced by the final\n iteration.\n3) Scan_output variables are used to implicitly concatenate values computed across\n all the iterations. In the above example, the value of user_defined_val computed\n over all iterations are concatenated and returned as the value of user_defined_vals\n after the loop.\n4) Values created in the body cannot be accessed in the enclosing scope,\n except using the mechanism described above.\n\nNote that the semantics of this op support \"diagonal\" or \"wavefront\" execution.\n(See Step 3 here for an example:\nhttps://devblogs.nvidia.com/optimizing-recurrent-neural-networks-cudnn-5/).\nFrontends should emit multi-layer RNNs as a series of While operators (with\ntime being the inner looping dimension), with each successive layer consuming\nthe scan_outputs from the previous layer, possibly going through several\npoint-wise operators (e.g. dropout, residual connections, linear layer).\n\nThe input/output of subgraph (produced by loop node) matching is based on order instead of name. The implementation will figure out the names based on this order.\n", + "attributes": [ + { + "name": "body", + "type": "graph", + "required": true, + "description": "The graph run each iteration. It has 2+N inputs: (iteration_num, condition, loop carried dependencies...). It has 1+N+K outputs: (condition, loop carried dependencies..., scan_outputs...). Each scan_output is created by concatenating the value of the specified output value at the end of each iteration of the loop. It is an error if the dimensions or data type of these scan_outputs change across loop iterations." + } + ], + "inputs": [ + { + "name": "M", + "type": "I", + "option": "optional", + "description": "A maximum trip-count for the loop specified at runtime. Optional. Pass empty string to skip." + }, + { + "name": "cond", + "type": "B", + "option": "optional", + "description": "A boolean termination condition. Optional. Pass empty string to skip." + }, + { + "name": "v_initial", + "type": "V", + "list": true, + "description": "The initial values of any loop-carried dependencies (values that change across loop iterations)" + } + ], + "min_input": 2, + "max_input": 2147483647, + "outputs": [ + { + "name": "v_final_and_scan_outputs", + "type": "V", + "list": true, + "description": "Final N loop carried dependency values then K scan_outputs. Scan outputs must be Tensors." + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "2 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor, Sequence(Tensor), Optional(Tensor), and Optional(Sequence(Tensor)) types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)", + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(bfloat16))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))", + "optional(seq(tensor(uint8)))", + "optional(seq(tensor(uint16)))", + "optional(seq(tensor(uint32)))", + "optional(seq(tensor(uint64)))", + "optional(seq(tensor(int8)))", + "optional(seq(tensor(int16)))", + "optional(seq(tensor(int32)))", + "optional(seq(tensor(int64)))", + "optional(seq(tensor(bfloat16)))", + "optional(seq(tensor(float16)))", + "optional(seq(tensor(float)))", + "optional(seq(tensor(double)))", + "optional(seq(tensor(string)))", + "optional(seq(tensor(bool)))", + "optional(seq(tensor(complex64)))", + "optional(seq(tensor(complex128)))", + "optional(tensor(uint8))", + "optional(tensor(uint16))", + "optional(tensor(uint32))", + "optional(tensor(uint64))", + "optional(tensor(int8))", + "optional(tensor(int16))", + "optional(tensor(int32))", + "optional(tensor(int64))", + "optional(tensor(bfloat16))", + "optional(tensor(float16))", + "optional(tensor(float))", + "optional(tensor(double))", + "optional(tensor(string))", + "optional(tensor(bool))", + "optional(tensor(complex64))", + "optional(tensor(complex128))" + ] + }, + { + "description": "tensor of int64, which should be a scalar.", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + }, + { + "description": "tensor of bool, which should be a scalar.", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "loop_11", + "code": "# Given a tensor x of values [x1, ..., xN], and initial tensor y\n# sum up its elements using a scan\n# returning the final state (y+x1+x2+...+xN) as well the scan_output\n# [y+x1, y+x1+x2, ..., y+x1+x2+...+xN]\n\ny_in = onnx.helper.make_tensor_value_info('y_in', onnx.TensorProto.FLOAT, [1])\ny_out = onnx.helper.make_tensor_value_info('y_out', onnx.TensorProto.FLOAT, [1])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [1])\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\ny = np.array([-2]).astype(np.float32)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\ni_add_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['iter_count'],\n outputs=['slice_start'],\n axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end'],\n outputs=['slice_end'],\n axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ny_add_node = onnx.helper.make_node(\n 'Add',\n inputs=['y_in', 'slice_out'],\n outputs=['y_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nscan_identity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['y_out'],\n outputs=['scan_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, x_const_node, one_const_node, i_add_node,\n start_unsqueeze_node, end_unsqueeze_node, slice_node, y_add_node,\n scan_identity_node],\n 'loop_body',\n [iter_count, cond_in, y_in],\n [cond_out, y_out, scan_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'y'],\n outputs=['res_y', 'res_scan'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\nres_y = np.array([13]).astype(np.float32)\ncond = np.array(1).astype(bool)\nres_scan = np.array([-1, 1, 4, 8, 13]).astype(np.float32).reshape((5, 1))\nexpect(node, inputs=[trip_count, cond, y], outputs=[res_y, res_scan],\n name='test_loop11', opset_imports=[onnx.helper.make_opsetid(\"\", 11)])" + }, + { + "summary": "loop_13", + "code": "# Given a tensor x of values [x1, ..., xN],\n# Return a sequence of tensors of\n# [[x1], [x1, x2], ..., [x1, ..., xN]]\n\nseq_in = onnx.helper.make_tensor_sequence_value_info('seq_in', onnx.TensorProto.FLOAT, None)\nseq_out = onnx.helper.make_tensor_sequence_value_info('seq_out', onnx.TensorProto.FLOAT, None)\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\nzero_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['slice_start'],\n value=onnx.helper.make_tensor(\n name='const_tensor_zero',\n data_type=onnx.TensorProto.INT64,\n dims=(1,),\n vals=[0]\n )\n)\n\naxes_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['axes'],\n value=onnx.helper.make_tensor(\n name='const_tensor_axes',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[0]\n )\n)\n\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end', 'axes'],\n outputs=['slice_end']\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ninsert_node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['seq_in', 'slice_out'],\n outputs=['seq_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, x_const_node, one_const_node, zero_const_node, add_node,\n axes_node, end_unsqueeze_node, slice_node, insert_node],\n 'loop_body',\n [iter_count, cond_in, seq_in],\n [cond_out, seq_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'seq_empty'],\n outputs=['seq_res'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\nseq_empty: List[Any] = []\nseq_res = [x[:int(i)] for i in x]\ncond = np.array(1).astype(bool)\nexpect(node, inputs=[trip_count, cond, seq_empty], outputs=[seq_res],\n name='test_loop13_seq', opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, trip_count.shape),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, []))])" + }, + { + "summary": "loop_16_none", + "code": "# Given a tensor sequence of values [x1, ..., xN], and an initial optional sequence of tensors [x0],\n# Return a concatenated sequence of tensors of\n# [x0, [x1], [x1, x2], ..., [x1, ..., xN]]\n\nten_in_tp = onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [])\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\nopt_in_tp = onnx.helper.make_optional_type_proto(seq_in_tp)\nopt_in = onnx.helper.make_value_info('opt_seq_in', opt_in_tp)\nseq_out = onnx.helper.make_tensor_sequence_value_info('seq_out', onnx.TensorProto.FLOAT, [])\ncond_in = onnx.helper.make_tensor_value_info('cond_in', onnx.TensorProto.BOOL, [])\ncond_out = onnx.helper.make_tensor_value_info('cond_out', onnx.TensorProto.BOOL, [])\niter_count = onnx.helper.make_tensor_value_info('iter_count', onnx.TensorProto.INT64, [])\n\nx0 = np.array(0).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n\noptional_has_elem_node = onnx.helper.make_node(\n 'OptionalHasElement',\n inputs=['opt_seq_in'],\n outputs=['optional_has_elem']\n)\n\noptional_is_none = onnx.helper.make_node(\n 'Not',\n inputs=['optional_has_elem'],\n outputs=['optional_is_none']\n)\n\noptional_get_elem = onnx.helper.make_node(\n 'OptionalGetElement',\n inputs=['opt_seq_in'],\n outputs=['seq_in']\n)\n\nconstant_in = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['constant_in'],\n value=onnx.helper.make_tensor(\n name='const_tensor',\n data_type=onnx.TensorProto.FLOAT,\n dims=(),\n vals=[0]\n )\n)\n\nseq_const_in = onnx.helper.make_node(\n 'SequenceConstruct',\n inputs=['constant_in'],\n outputs=['init_seq_in']\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info('init_seq_in', onnx.TensorProto.FLOAT, [])\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in],\n 'then_body',\n [],\n [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info('seq_in', onnx.TensorProto.FLOAT, [])\nelse_body = onnx.helper.make_graph(\n [optional_get_elem],\n 'else_body',\n [],\n [else_seq_out]\n)\n\nif_node = onnx.helper.make_node(\n 'If',\n inputs=['optional_is_none'],\n outputs=['sequence'],\n then_branch=then_body,\n else_branch=else_body\n)\n\nx_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['x'],\n value=onnx.helper.make_tensor(\n name='const_tensor_x',\n data_type=onnx.TensorProto.FLOAT,\n dims=x.shape,\n vals=x.flatten().astype(float),\n )\n)\n\none_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['one'],\n value=onnx.helper.make_tensor(\n name='const_tensor_one',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[1]\n )\n)\n\nzero_const_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['slice_start'],\n value=onnx.helper.make_tensor(\n name='const_tensor_zero',\n data_type=onnx.TensorProto.INT64,\n dims=(1,),\n vals=[0]\n )\n)\n\naxes_node = onnx.helper.make_node(\n 'Constant',\n inputs=[],\n outputs=['axes'],\n value=onnx.helper.make_tensor(\n name='const_tensor_axes',\n data_type=onnx.TensorProto.INT64,\n dims=(),\n vals=[0]\n )\n)\n\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['iter_count', 'one'],\n outputs=['end']\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['end', 'axes'],\n outputs=['slice_end']\n)\n\nslice_node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'slice_start', 'slice_end'],\n outputs=['slice_out']\n)\n\ninsert_node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['sequence', 'slice_out'],\n outputs=['seq_out']\n)\n\nidentity_node = onnx.helper.make_node(\n 'Identity',\n inputs=['cond_in'],\n outputs=['cond_out']\n)\n\nloop_body = onnx.helper.make_graph(\n [identity_node, optional_has_elem_node, optional_is_none, if_node, x_const_node, one_const_node,\n zero_const_node, add_node, axes_node, end_unsqueeze_node, slice_node, insert_node],\n 'loop_body',\n [iter_count, cond_in, opt_in],\n [cond_out, seq_out]\n)\n\nnode = onnx.helper.make_node(\n 'Loop',\n inputs=['trip_count', 'cond', 'opt_seq'],\n outputs=['seq_res'],\n body=loop_body\n)\n\ntrip_count = np.array(5).astype(np.int64)\ncond = np.array(1).astype(bool)\nseq_res = compute_loop_outputs(x, [x0], trip_count)\nopt_seq_in: List[Any] = [x0]\nexpect(node, inputs=[trip_count, cond, opt_seq_in], outputs=[seq_res],\n name='test_loop16_seq_none', opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, trip_count.shape),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp])" + } + ] + }, + { + "name": "LpNormalization", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Given a matrix, apply Lp-normalization along the provided axis.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": -1, + "description": "The axis on which to apply normalization, -1 mean last axis." + }, + { + "name": "p", + "type": "int64", + "required": false, + "default": 2, + "description": "The order of the normalization, only 1 or 2 are supported." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input matrix" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Matrix after normalization" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "category": "Normalization" + }, + { + "name": "LpPool", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "LpPool consumes an input tensor X and applies Lp pooling across the\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n Lp pooling consisting of computing the Lp norm on all values of a subset\n of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing.", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding. DEPRECATION NOTE: auto_pad is only intended to support legacy uses, and for framework authors, one is explicitly encouraged to use explicit padding specified in the pads attribute." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": false, + "description": "The size of the kernel along each axis." + }, + { + "name": "p", + "type": "float32", + "required": false, + "default": 2.0, + "description": "p value of the Lp norm used to pool over the input data, default is 2.0." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimension are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from Lp pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "category": "Pool" + }, + { + "name": "LpPool", + "module": "ai.onnx", + "version": 2, + "support_level": "common", + "description": "LpPool consumes an input tensor X and applies Lp pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n Lp pooling consisting of computing the Lp norm on all values of a subset\n of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing.", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "p", + "type": "int64", + "required": false, + "default": 2, + "description": "p value of the Lp norm used to pool over the input data." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from Lp pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "category": "Pool" + }, + { + "name": "LpPool", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "LpPool consumes an input tensor X and applies Lp pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n Lp pooling consisting of computing the Lp norm on all values of a subset\n of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing.", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = ceil(input_shape[i] / strides[i])` for each axis `i`. The padding is split between the two sides equally or almost equally (depending on whether it is even or odd). In case the padding is an odd number, the extra padding is added at the end for SAME_UPPER and at the beginning for SAME_LOWER." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "p", + "type": "int64", + "required": false, + "default": 2, + "description": "p value of the Lp norm used to pool over the input data." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from Lp pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "category": "Pool" + }, + { + "name": "MatMul", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Matrix product that behaves like numpy.matmul: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matmul.html\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "N-dimensional matrix A" + }, + { + "name": "B", + "type": "T", + "description": "N-dimensional matrix B" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Matrix multiply results from A * B" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "matmul", + "code": "node = onnx.helper.make_node(\n 'MatMul',\n inputs=['a', 'b'],\n outputs=['c'],\n)\n\n# 2d\na = np.random.randn(3, 4).astype(np.float32)\nb = np.random.randn(4, 3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c],\n name='test_matmul_2d')\n\n# 3d\na = np.random.randn(2, 3, 4).astype(np.float32)\nb = np.random.randn(2, 4, 3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c],\n name='test_matmul_3d')\n\n# 4d\na = np.random.randn(1, 2, 3, 4).astype(np.float32)\nb = np.random.randn(1, 2, 4, 3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c],\n name='test_matmul_4d')" + } + ] + }, + { + "name": "MatMul", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Matrix product that behaves like numpy.matmul: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matmul.html\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "N-dimensional matrix A" + }, + { + "name": "B", + "type": "T", + "description": "N-dimensional matrix B" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Matrix multiply results from A * B" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "matmul", + "code": "node = onnx.helper.make_node(\n 'MatMul',\n inputs=['a', 'b'],\n outputs=['c'],\n)\n\n# 2d\na = np.random.randn(3, 4).astype(np.float32)\nb = np.random.randn(4, 3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c],\n name='test_matmul_2d')\n\n# 3d\na = np.random.randn(2, 3, 4).astype(np.float32)\nb = np.random.randn(2, 4, 3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c],\n name='test_matmul_3d')\n\n# 4d\na = np.random.randn(1, 2, 3, 4).astype(np.float32)\nb = np.random.randn(1, 2, 4, 3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c],\n name='test_matmul_4d')" + } + ] + }, + { + "name": "MatMul", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Matrix product that behaves like numpy.matmul: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matmul.html\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "N-dimensional matrix A" + }, + { + "name": "B", + "type": "T", + "description": "N-dimensional matrix B" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Matrix multiply results from A * B" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "matmul", + "code": "node = onnx.helper.make_node(\n 'MatMul',\n inputs=['a', 'b'],\n outputs=['c'],\n)\n\n# 2d\na = np.random.randn(3, 4).astype(np.float32)\nb = np.random.randn(4, 3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c],\n name='test_matmul_2d')\n\n# 3d\na = np.random.randn(2, 3, 4).astype(np.float32)\nb = np.random.randn(2, 4, 3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c],\n name='test_matmul_3d')\n\n# 4d\na = np.random.randn(1, 2, 3, 4).astype(np.float32)\nb = np.random.randn(1, 2, 4, 3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c],\n name='test_matmul_4d')" + } + ] + }, + { + "name": "MatMulInteger", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Matrix product that behaves like numpy.matmul: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matmul.html.\nThe production MUST never overflow. The accumulation may overflow if and only if in 32 bits.\n", + "inputs": [ + { + "name": "A", + "type": "T1", + "description": "N-dimensional matrix A" + }, + { + "name": "B", + "type": "T2", + "description": "N-dimensional matrix B" + }, + { + "name": "a_zero_point", + "type": "T1", + "option": "optional", + "description": "Zero point tensor for input 'A'. It's optional and default value is 0. It could be a scalar or N-D tensor. Scalar refers to per tensor quantization whereas N-D refers to per row quantization. If the input is 2D of shape [M, K] then zero point tensor may be an M element vector [zp_1, zp_2, ..., zp_M]. If the input is N-D tensor with shape [D1, D2, M, K] then zero point tensor may have shape [D1, D2, M, 1]. " + }, + { + "name": "b_zero_point", + "type": "T2", + "option": "optional", + "description": "Zero point tensor for input 'B'. It's optional and default value is 0. It could be a scalar or a N-D tensor, Scalar refers to per tensor quantization whereas N-D refers to per col quantization. If the input is 2D of shape [K, N] then zero point tensor may be an N element vector [zp_1, zp_2, ..., zp_N]. If the input is N-D tensor with shape [D1, D2, K, N] then zero point tensor may have shape [D1, D2, 1, N]. " + } + ], + "min_input": 2, + "max_input": 4, + "outputs": [ + { + "name": "Y", + "type": "T3", + "description": "Matrix multiply results from A * B" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 4", + "type_constraints": [ + { + "description": "Constrain input A data type to 8-bit integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain input B data type to 8-bit integer tensor.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain output Y data type as 32-bit integer tensor.", + "type_param_str": "T3", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "matmulinteger", + "code": "node = onnx.helper.make_node('MatMulInteger',\n inputs=['A', 'B', 'a_zero_point', 'b_zero_point'],\n outputs=['Y'],)\n\nA = np.array([[11, 7, 3],\n [10, 6, 2],\n [9, 5, 1],\n [8, 4, 0], ], dtype=np.uint8)\n\na_zero_point = np.array([12], dtype=np.uint8)\n\nB = np.array([[1, 4],\n [2, 5],\n [3, 6], ], dtype=np.uint8)\n\nb_zero_point = np.array([0], dtype=np.uint8)\n\noutput = np.array([[-38, -83],\n [-44, -98],\n [-50, -113],\n [-56, -128], ], dtype=np.int32)\n\nexpect(node, inputs=[A, B, a_zero_point, b_zero_point], outputs=[output],\n name='test_matmulinteger')" + } + ] + }, + { + "name": "Max", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Element-wise max of each of the input tensors. All inputs and outputs must\nhave the same shape and data type.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for Max." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "max", + "type": "T", + "description": "Output tensor. Same dimension as inputs." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "max", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 3]).astype(np.float32)\nresult = np.array([3, 5, 4]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_max_example')\n\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_max_one_input')\n\nresult = np.maximum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_two_inputs')" + }, + { + "summary": "max_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([3, 4, 4]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "Max", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Element-wise max of each of the input tensors. All inputs and outputs must\nhave the same shape and data type.\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for Max." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "max", + "type": "T", + "description": "Output tensor. Same dimension as inputs." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "max", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 3]).astype(np.float32)\nresult = np.array([3, 5, 4]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_max_example')\n\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_max_one_input')\n\nresult = np.maximum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_two_inputs')" + }, + { + "summary": "max_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([3, 4, 4]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "Max", + "module": "ai.onnx", + "version": 8, + "support_level": "common", + "description": "Element-wise max of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for max." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "max", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "max", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 3]).astype(np.float32)\nresult = np.array([3, 5, 4]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_max_example')\n\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_max_one_input')\n\nresult = np.maximum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_two_inputs')" + }, + { + "summary": "max_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([3, 4, 4]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "Max", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Element-wise max of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for max." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "max", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "max", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 3]).astype(np.float32)\nresult = np.array([3, 5, 4]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_max_example')\n\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_max_one_input')\n\nresult = np.maximum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_two_inputs')" + }, + { + "summary": "max_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([3, 4, 4]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "Max", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Element-wise max of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for max." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "max", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "max", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 3]).astype(np.float32)\nresult = np.array([3, 5, 4]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_max_example')\n\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_max_one_input')\n\nresult = np.maximum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_two_inputs')" + }, + { + "summary": "max_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([3, 4, 4]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Max',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_max_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "MaxPool", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "MaxPool consumes an input tensor X and applies max pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n max pooling consisting of computing the max on all values of a\n subset of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - kernel_spatial_shape[i]) / strides_spatial_shape[i] + 1)\n\n * pad_shape[i] is sum of pads along axis i\n ```\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following:\n ```\n VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - kernel_spatial_shape[i] + 1) / strides_spatial_shape[i])\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\n And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:\n ```\n pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + kernel_spatial_shape[i] - input_spatial_shape[i]\n ```\n The output of each pooling window is maximum number of elements exclude pad.\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from average or max pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes. Floor value of the dimension is used" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "maxpool_1d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2],\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2]\nstrides = [1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_1d_default')" + }, + { + "summary": "maxpool_2d_ceil", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n ceil_mode=True\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_ceil')" + }, + { + "summary": "maxpool_2d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_default')" + }, + { + "summary": "maxpool_2d_dilations", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_dilations')" + }, + { + "summary": "maxpool_2d_pads", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2]\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = pad_top = pad_right = pad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_pads')" + }, + { + "summary": "maxpool_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_pads')" + }, + { + "summary": "maxpool_2d_precomputed_same_upper", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 3, 3]\npad_shape: [2, 2] -> [1, 1, 1, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9, 10],\n [17, 19, 20],\n [22, 24, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_same_upper')" + }, + { + "summary": "maxpool_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_strides')" + }, + { + "summary": "maxpool_2d_same_lower", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_LOWER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)\npad_bottom = pad_shape[0] // 2\npad_top = pad_shape[0] - pad_bottom\npad_right = pad_shape[1] // 2\npad_left = pad_shape[1] - pad_right\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_lower')" + }, + { + "summary": "maxpool_2d_same_upper", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)\npad_top = pad_shape[0] // 2\npad_bottom = pad_shape[0] - pad_top\npad_left = pad_shape[1] // 2\npad_right = pad_shape[1] - pad_left\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_upper')" + }, + { + "summary": "maxpool_2d_strides", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_strides')" + }, + { + "summary": "maxpool_2d_uint8", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.uint8)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_uint8')" + }, + { + "summary": "maxpool_3d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2, 2],\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_3d_default')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\nz = np.array([[[\n [12, 13, 14, 14, 14],\n [17, 18, 19, 19, 19],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_pads')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[2, 2],\n strides=[2, 2],\n storage_order=1\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16],\n [8, 18]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_strides')" + } + ], + "category": "Pool" + }, + { + "name": "MaxPool", + "module": "ai.onnx", + "version": 8, + "support_level": "common", + "description": "MaxPool consumes an input tensor X and applies max pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n max pooling consisting of computing the max on all values of a\n subset of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - kernel_spatial_shape[i]) / strides_spatial_shape[i] + 1)\n\n * pad_shape[i] is sum of pads along axis i\n ```\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following:\n ```\n VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - kernel_spatial_shape[i] + 1) / strides_spatial_shape[i])\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\n And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:\n ```\n pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + kernel_spatial_shape[i] - input_spatial_shape[i]\n ```\n The output of each pooling window is maximum number of elements exclude pad.\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "storage_order", + "type": "int64", + "required": false, + "description": "The storage order of the tensor. 0 is row major, and 1 is column major." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from average or max pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes. Floor value of the dimension is used" + }, + { + "name": "Indices", + "type": "I", + "option": "optional", + "description": "Indices tensor from max pooling across the input tensor. The dimensions of indices are the same as output tensor. The values in indices of are the indices of the selected values during pooling. The indices are computed as flatten 1-D tensor, and the indices do not consider padding. So the values in indices are in [0, N x C x D1 x ... x Dn)." + } + ], + "min_output": 1, + "max_output": 2, + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain index tensor to int64", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "maxpool_1d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2],\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2]\nstrides = [1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_1d_default')" + }, + { + "summary": "maxpool_2d_ceil", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n ceil_mode=True\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_ceil')" + }, + { + "summary": "maxpool_2d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_default')" + }, + { + "summary": "maxpool_2d_dilations", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_dilations')" + }, + { + "summary": "maxpool_2d_pads", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2]\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = pad_top = pad_right = pad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_pads')" + }, + { + "summary": "maxpool_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_pads')" + }, + { + "summary": "maxpool_2d_precomputed_same_upper", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 3, 3]\npad_shape: [2, 2] -> [1, 1, 1, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9, 10],\n [17, 19, 20],\n [22, 24, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_same_upper')" + }, + { + "summary": "maxpool_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_strides')" + }, + { + "summary": "maxpool_2d_same_lower", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_LOWER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)\npad_bottom = pad_shape[0] // 2\npad_top = pad_shape[0] - pad_bottom\npad_right = pad_shape[1] // 2\npad_left = pad_shape[1] - pad_right\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_lower')" + }, + { + "summary": "maxpool_2d_same_upper", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)\npad_top = pad_shape[0] // 2\npad_bottom = pad_shape[0] - pad_top\npad_left = pad_shape[1] // 2\npad_right = pad_shape[1] - pad_left\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_upper')" + }, + { + "summary": "maxpool_2d_strides", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_strides')" + }, + { + "summary": "maxpool_2d_uint8", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.uint8)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_uint8')" + }, + { + "summary": "maxpool_3d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2, 2],\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_3d_default')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\nz = np.array([[[\n [12, 13, 14, 14, 14],\n [17, 18, 19, 19, 19],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_pads')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[2, 2],\n strides=[2, 2],\n storage_order=1\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16],\n [8, 18]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_strides')" + } + ], + "category": "Pool" + }, + { + "name": "MaxPool", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "MaxPool consumes an input tensor X and applies max pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n max pooling consisting of computing the max on all values of a\n subset of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled\n\n ```\n * pad_shape[i] is sum of pads along axis i\n ```\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following:\n ```\n VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1) + 1) / strides_spatial_shape[i])\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\n And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:\n ```\n pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + ((kernel_spatial_shape[i] - 1) * dilations[i] + 1) - input_spatial_shape[i]\n ```\n The output of each pooling window is maximum number of elements exclude pad.\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "ceil_mode", + "type": "int64", + "required": false, + "description": "Whether to use ceil or floor (default) to compute the output shape." + }, + { + "name": "dilations", + "type": "int64[]", + "required": false, + "description": "Dilation value along each spatial axis of filter." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "storage_order", + "type": "int64", + "required": false, + "description": "The storage order of the tensor. 0 is row major, and 1 is column major." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from average or max pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes. Floor value of the dimension is used" + }, + { + "name": "Indices", + "type": "I", + "option": "optional", + "description": "Indices tensor from max pooling across the input tensor. The dimensions of indices are the same as output tensor. The values in indices of are the indices of the selected values during pooling. The indices are computed as flatten 1-D tensor, and the indices do not consider padding. So the values in indices are in [0, N x C x D1 x ... x Dn)." + } + ], + "min_output": 1, + "max_output": 2, + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain index tensor to int64", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "maxpool_1d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2],\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2]\nstrides = [1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_1d_default')" + }, + { + "summary": "maxpool_2d_ceil", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n ceil_mode=True\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_ceil')" + }, + { + "summary": "maxpool_2d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_default')" + }, + { + "summary": "maxpool_2d_dilations", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_dilations')" + }, + { + "summary": "maxpool_2d_pads", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2]\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = pad_top = pad_right = pad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_pads')" + }, + { + "summary": "maxpool_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_pads')" + }, + { + "summary": "maxpool_2d_precomputed_same_upper", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 3, 3]\npad_shape: [2, 2] -> [1, 1, 1, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9, 10],\n [17, 19, 20],\n [22, 24, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_same_upper')" + }, + { + "summary": "maxpool_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_strides')" + }, + { + "summary": "maxpool_2d_same_lower", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_LOWER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)\npad_bottom = pad_shape[0] // 2\npad_top = pad_shape[0] - pad_bottom\npad_right = pad_shape[1] // 2\npad_left = pad_shape[1] - pad_right\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_lower')" + }, + { + "summary": "maxpool_2d_same_upper", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)\npad_top = pad_shape[0] // 2\npad_bottom = pad_shape[0] - pad_top\npad_left = pad_shape[1] // 2\npad_right = pad_shape[1] - pad_left\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_upper')" + }, + { + "summary": "maxpool_2d_strides", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_strides')" + }, + { + "summary": "maxpool_2d_uint8", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.uint8)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_uint8')" + }, + { + "summary": "maxpool_3d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2, 2],\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_3d_default')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\nz = np.array([[[\n [12, 13, 14, 14, 14],\n [17, 18, 19, 19, 19],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_pads')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[2, 2],\n strides=[2, 2],\n storage_order=1\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16],\n [8, 18]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_strides')" + } + ], + "category": "Pool" + }, + { + "name": "MaxPool", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "MaxPool consumes an input tensor X and applies max pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n max pooling consisting of computing the max on all values of a\n subset of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled\n\n ```\n * pad_shape[i] is sum of pads along axis i\n ```\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following:\n ```\n VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1) + 1) / strides_spatial_shape[i])\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\n And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:\n ```\n pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + ((kernel_spatial_shape[i] - 1) * dilations[i] + 1) - input_spatial_shape[i]\n ```\n The output of each pooling window is maximum number of elements exclude pad.\n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that the output spatial size match the input.In case of odd number add the extra padding at the end for SAME_UPPER and at the beginning for SAME_LOWER. VALID mean no padding." + }, + { + "name": "ceil_mode", + "type": "int64", + "required": false, + "description": "Whether to use ceil or floor (default) to compute the output shape." + }, + { + "name": "dilations", + "type": "int64[]", + "required": false, + "description": "Dilation value along each spatial axis of filter. If not present, the dilation defaults to 1 along each spatial axis." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "storage_order", + "type": "int64", + "required": false, + "description": "The storage order of the tensor. 0 is row major, and 1 is column major." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from average or max pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes. Floor value of the dimension is used" + }, + { + "name": "Indices", + "type": "I", + "option": "optional", + "description": "Indices tensor from max pooling across the input tensor. The dimensions of indices are the same as output tensor. The values in indices of are the indices of the selected values during pooling. The indices are computed as flatten 1-D tensor, and the indices do not consider padding. So the values in indices are in [0, N x C x D1 x ... x Dn)." + } + ], + "min_output": 1, + "max_output": 2, + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain index tensor to int64", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "maxpool_1d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2],\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2]\nstrides = [1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_1d_default')" + }, + { + "summary": "maxpool_2d_ceil", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n ceil_mode=True\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_ceil')" + }, + { + "summary": "maxpool_2d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_default')" + }, + { + "summary": "maxpool_2d_dilations", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_dilations')" + }, + { + "summary": "maxpool_2d_pads", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2]\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = pad_top = pad_right = pad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_pads')" + }, + { + "summary": "maxpool_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_pads')" + }, + { + "summary": "maxpool_2d_precomputed_same_upper", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 3, 3]\npad_shape: [2, 2] -> [1, 1, 1, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9, 10],\n [17, 19, 20],\n [22, 24, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_same_upper')" + }, + { + "summary": "maxpool_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_strides')" + }, + { + "summary": "maxpool_2d_same_lower", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_LOWER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)\npad_bottom = pad_shape[0] // 2\npad_top = pad_shape[0] - pad_bottom\npad_right = pad_shape[1] // 2\npad_left = pad_shape[1] - pad_right\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_lower')" + }, + { + "summary": "maxpool_2d_same_upper", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)\npad_top = pad_shape[0] // 2\npad_bottom = pad_shape[0] - pad_top\npad_left = pad_shape[1] // 2\npad_right = pad_shape[1] - pad_left\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_upper')" + }, + { + "summary": "maxpool_2d_strides", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_strides')" + }, + { + "summary": "maxpool_2d_uint8", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.uint8)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_uint8')" + }, + { + "summary": "maxpool_3d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2, 2],\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_3d_default')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\nz = np.array([[[\n [12, 13, 14, 14, 14],\n [17, 18, 19, 19, 19],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_pads')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[2, 2],\n strides=[2, 2],\n storage_order=1\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16],\n [8, 18]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_strides')" + } + ], + "category": "Pool" + }, + { + "name": "MaxPool", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "MaxPool consumes an input tensor X and applies max pooling across\n the tensor according to kernel sizes, stride sizes, and pad lengths.\n max pooling consisting of computing the max on all values of a\n subset of the input tensor according to the kernel size and downsampling the\n data into the output tensor Y for further processing. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled\n\n ```\n * pad_shape[i] is sum of pads along axis i\n ```\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following:\n ```\n VALID: output_spatial_shape[i] = ceil((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1) + 1) / strides_spatial_shape[i])\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\n And pad shape will be following if `SAME_UPPER` or `SAME_LOWER`:\n ```\n pad_shape[i] = (output_spatial_shape[i] - 1) * strides_spatial_shape[i] + ((kernel_spatial_shape[i] - 1) * dilations[i] + 1) - input_spatial_shape[i]\n ```\n The output of each pooling window is maximum number of elements exclude pad. \n ", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = ceil(input_shape[i] / strides[i])` for each axis `i`. The padding is split between the two sides equally or almost equally (depending on whether it is even or odd). In case the padding is an odd number, the extra padding is added at the end for SAME_UPPER and at the beginning for SAME_LOWER." + }, + { + "name": "ceil_mode", + "type": "int64", + "required": false, + "description": "Whether to use ceil or floor (default) to compute the output shape." + }, + { + "name": "dilations", + "type": "int64[]", + "required": false, + "description": "Dilation value along each spatial axis of filter. If not present, the dilation defaults to 1 along each spatial axis." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "storage_order", + "type": "int64", + "required": false, + "description": "The storage order of the tensor. 0 is row major, and 1 is column major." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output data tensor from average or max pooling across the input tensor. Dimensions will vary based on various kernel, stride, and pad sizes. Floor value of the dimension is used" + }, + { + "name": "Indices", + "type": "I", + "option": "optional", + "description": "Indices tensor from max pooling across the input tensor. The dimensions of indices are the same as output tensor. The values in indices of are the indices of the selected values during pooling. The indices are computed as flatten 1-D tensor, and the indices do not consider padding. So the values in indices are in [0, N x C x D1 x ... x Dn)." + } + ], + "min_output": 1, + "max_output": 2, + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float and 8 bit tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain index tensor to int64", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "maxpool_1d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2],\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2]\nstrides = [1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_1d_default')" + }, + { + "summary": "maxpool_2d_ceil", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n ceil_mode=True\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_ceil')" + }, + { + "summary": "maxpool_2d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_default')" + }, + { + "summary": "maxpool_2d_dilations", + "code": "\"\"\"\ninput_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]]).astype(np.float32)\ny = np.array([[[\n [11, 12],\n [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_dilations')" + }, + { + "summary": "maxpool_2d_pads", + "code": "\"\"\"\ninput_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2]\n)\nx = np.random.randn(1, 3, 28, 28).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = pad_top = pad_right = pad_left = 2\npad_shape = [pad_top + pad_bottom, pad_left + pad_right]\nout_shape = get_output_shape('VALID', np.add(x_shape[2:], pad_shape), kernel_shape, strides)\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_pads')" + }, + { + "summary": "maxpool_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_pads')" + }, + { + "summary": "maxpool_2d_precomputed_same_upper", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 3, 3]\npad_shape: [2, 2] -> [1, 1, 1, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[3, 3],\n strides=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9, 10],\n [17, 19, 20],\n [22, 24, 25]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_same_upper')" + }, + { + "summary": "maxpool_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_precomputed_strides')" + }, + { + "summary": "maxpool_2d_same_lower", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_LOWER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_LOWER', x_shape[2:], kernel_shape, strides, out_shape)\npad_bottom = pad_shape[0] // 2\npad_top = pad_shape[0] - pad_bottom\npad_right = pad_shape[1] // 2\npad_left = pad_shape[1] - pad_right\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_lower')" + }, + { + "summary": "maxpool_2d_same_upper", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2],\n auto_pad='SAME_UPPER'\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape = get_output_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides)\npad_shape = get_pad_shape('SAME_UPPER', x_shape[2:], kernel_shape, strides, out_shape)\npad_top = pad_shape[0] // 2\npad_bottom = pad_shape[0] - pad_top\npad_left = pad_shape[1] // 2\npad_right = pad_shape[1] - pad_left\npadded = np.pad(x, ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)), mode='constant',\n constant_values=np.nan)\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, pad_shape, 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_same_upper')" + }, + { + "summary": "maxpool_2d_strides", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, (0, 0), 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_strides')" + }, + { + "summary": "maxpool_2d_uint8", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.uint8)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_2d_uint8')" + }, + { + "summary": "maxpool_3d_default", + "code": "\"\"\"\ninput_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y'],\n kernel_shape=[2, 2, 2],\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape = get_output_shape('VALID', x_shape[2:], kernel_shape, strides)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, [0, 0, 0], 'MAX')\n\nexpect(node, inputs=[x], outputs=[y], name='test_maxpool_3d_default')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_pads", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 5, 5]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[5, 5],\n pads=[2, 2, 2, 2]\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[\n [13, 14, 15, 15, 15],\n [18, 19, 20, 20, 20],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25],\n [23, 24, 25, 25, 25]]]]).astype(np.float32)\nz = np.array([[[\n [12, 13, 14, 14, 14],\n [17, 18, 19, 19, 19],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24],\n [22, 23, 24, 24, 24]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_pads')" + }, + { + "summary": "maxpool_with_argmax_2d_precomputed_strides", + "code": "\"\"\"\ninput_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n 'MaxPool',\n inputs=['x'],\n outputs=['y', 'z'],\n kernel_shape=[2, 2],\n strides=[2, 2],\n storage_order=1\n)\nx = np.array([[[\n [1, 2, 3, 4, 5],\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n [21, 22, 23, 24, 25],\n]]]).astype(np.float32)\ny = np.array([[[[7, 9],\n [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16],\n [8, 18]]]]).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y, z], name='test_maxpool_with_argmax_2d_precomputed_strides')" + } + ], + "category": "Pool" + }, + { + "name": "MaxRoiPool", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "ROI max pool consumes an input tensor X and region of interests (RoIs) to\n apply max pooling across each RoI, to produce output 4-D tensor of shape\n (num_rois, channels, pooled_shape[0], pooled_shape[1]).", + "attributes": [ + { + "name": "pooled_shape", + "type": "int64[]", + "required": true, + "description": "ROI pool output shape (height, width)." + }, + { + "name": "spatial_scale", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Multiplicative spatial scale factor to translate ROI coordinates from their input scale to the scale used when pooling." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input data tensor from the previous operator; dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data." + }, + { + "name": "rois", + "type": "T", + "description": "RoIs (Regions of Interest) to pool over. Should be a 2-D tensor of shape (num_rois, 5) given as [[batch_id, x1, y1, x2, y2], ...]." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "RoI pooled output 4-D tensor of shape (num_rois, channels, pooled_shape[0], pooled_shape[1])." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "category": "Pool" + }, + { + "name": "MaxUnpool", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "MaxUnpool essentially computes the partial inverse of the MaxPool op.\n The input information to this op is typically the the output information from a MaxPool op. The first\n input tensor X is the tensor that needs to be unpooled, which is typically the pooled tensor (first output)\n from MaxPool. The second input tensor, I, contains the indices to the (locally maximal) elements corrsponding\n to the elements in the first input tensor X. Input tensor I is typically the second output of the MaxPool op.\n The third (optional) input is a tensor that specifies the output size of the unpooling operation.\n\nMaxUnpool is intended to do 'partial' inverse of the MaxPool op. 'Partial' because all the non-maximal\n values from the original input to MaxPool are set to zero in the output of the MaxUnpool op. Pooling\n the result of an unpooling operation should give back the original input to the unpooling op.\n\nMaxUnpool can produce the same output size for several input sizes, which makes unpooling op ambiguous.\n The third input argument, output_size, is meant to disambiguate the op and produce output tensor of\n known/predictable size.\n\nIn addition to the inputs, MaxUnpool takes three attributes, namely kernel_shape, strides, and pads,\n which define the exact unpooling op. The attributes typically have the same values as the corrsponding\n pooling op that the unpooling op is trying to invert.\n", + "attributes": [ + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Input data tensor that has to be unpooled. This tensor is typically the first output of the MaxPool op.Dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non-image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + }, + { + "name": "I", + "type": "T2", + "description": "Input data tensor containing the indices corresponding to elements in the first input tensor X.This tensor is typically the second output of the MaxPool op.Dimensions must be the same as input tensor X. The indices are linear, i.e. computed considering the tensor as flattened 1-D tensor, assuming row-major storage. Also, the linear indices should not consider padding. So the values in indices are in the range [0, N x C x D1 x ... x Dn)." + }, + { + "name": "output_shape", + "type": "T2", + "option": "optional", + "description": "The shape of the output can be explicitly set which will cause pads values to be auto generated. If 'output_shape' is specified, 'pads' values are ignored." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T1", + "description": "Output data tensor that contains the result of the unpooling." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain index tensor to int64", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "with_output_shape", + "code": "node = onnx.helper.make_node(\n 'MaxUnpool',\n inputs=['xT', 'xI', 'output_shape'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nxT = np.array([[[[5, 6],\n [7, 8]]]], dtype=np.float32)\nxI = np.array([[[[5, 7],\n [13, 15]]]], dtype=np.int64)\noutput_shape = np.array((1, 1, 5, 5), dtype=np.int64)\ny = np.array([[[[0, 0, 0, 0, 0],\n [0, 5, 0, 6, 0],\n [0, 0, 0, 0, 0],\n [0, 7, 0, 8, 0],\n [0, 0, 0, 0, 0]]]], dtype=np.float32)\nexpect(node, inputs=[xT, xI, output_shape], outputs=[y], name='test_maxunpool_export_with_output_shape')" + }, + { + "summary": "without_output_shape", + "code": "node = onnx.helper.make_node(\n 'MaxUnpool',\n inputs=['xT', 'xI'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nxT = np.array([[[[1, 2],\n [3, 4]]]], dtype=np.float32)\nxI = np.array([[[[5, 7],\n [13, 15]]]], dtype=np.int64)\ny = np.array([[[[0, 0, 0, 0],\n [0, 1, 0, 2],\n [0, 0, 0, 0],\n [0, 3, 0, 4]]]], dtype=np.float32)\nexpect(node, inputs=[xT, xI], outputs=[y], name='test_maxunpool_export_without_output_shape')" + } + ] + }, + { + "name": "MaxUnpool", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "MaxUnpool essentially computes the partial inverse of the MaxPool op.\n The input information to this op is typically the the output information from a MaxPool op. The first\n input tensor X is the tensor that needs to be unpooled, which is typically the pooled tensor (first output)\n from MaxPool. The second input tensor, I, contains the indices to the (locally maximal) elements corrsponding\n to the elements in the first input tensor X. Input tensor I is typically the second output of the MaxPool op.\n The third (optional) input is a tensor that specifies the output size of the unpooling operation.\n\nMaxUnpool is intended to do 'partial' inverse of the MaxPool op. 'Partial' because all the non-maximal\n values from the original input to MaxPool are set to zero in the output of the MaxUnpool op. Pooling\n the result of an unpooling operation should give back the original input to the unpooling op.\n\nMaxUnpool can produce the same output size for several input sizes, which makes unpooling op ambiguous.\n The third input argument, output_size, is meant to disambiguate the op and produce output tensor of\n known/predictable size.\n\nIn addition to the inputs, MaxUnpool takes three attributes, namely kernel_shape, strides, and pads,\n which define the exact unpooling op. The attributes typically have the same values as the corrsponding\n pooling op that the unpooling op is trying to invert.\n", + "attributes": [ + { + "name": "kernel_shape", + "type": "int64[]", + "required": true, + "description": "The size of the kernel along each axis." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0. The value represent the number of pixels added to the beginning and end part of the corresponding axis. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`. This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaults to 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Input data tensor that has to be unpooled. This tensor is typically the first output of the MaxPool op.Dimensions for image case are (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data. For non-image case, the dimensions are in the form of (N x C x D1 x D2 ... Dn), where N is the batch size. Optionally, if dimension denotation is in effect, the operation expects the input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + }, + { + "name": "I", + "type": "T2", + "description": "Input data tensor containing the indices corresponding to elements in the first input tensor X.This tensor is typically the second output of the MaxPool op.Dimensions must be the same as input tensor X. The indices are linear, i.e. computed considering the tensor as flattened 1-D tensor, assuming row-major storage. Also, the linear indices should not consider padding. So the values in indices are in the range [0, N x C x D1 x ... x Dn)." + }, + { + "name": "output_shape", + "type": "T2", + "option": "optional", + "description": "The shape of the output can be explicitly set which will cause pads values to be auto generated. If 'output_shape' is specified, 'pads' values are ignored." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T1", + "description": "Output data tensor that contains the result of the unpooling." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain index tensor to int64", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "with_output_shape", + "code": "node = onnx.helper.make_node(\n 'MaxUnpool',\n inputs=['xT', 'xI', 'output_shape'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nxT = np.array([[[[5, 6],\n [7, 8]]]], dtype=np.float32)\nxI = np.array([[[[5, 7],\n [13, 15]]]], dtype=np.int64)\noutput_shape = np.array((1, 1, 5, 5), dtype=np.int64)\ny = np.array([[[[0, 0, 0, 0, 0],\n [0, 5, 0, 6, 0],\n [0, 0, 0, 0, 0],\n [0, 7, 0, 8, 0],\n [0, 0, 0, 0, 0]]]], dtype=np.float32)\nexpect(node, inputs=[xT, xI, output_shape], outputs=[y], name='test_maxunpool_export_with_output_shape')" + }, + { + "summary": "without_output_shape", + "code": "node = onnx.helper.make_node(\n 'MaxUnpool',\n inputs=['xT', 'xI'],\n outputs=['y'],\n kernel_shape=[2, 2],\n strides=[2, 2]\n)\nxT = np.array([[[[1, 2],\n [3, 4]]]], dtype=np.float32)\nxI = np.array([[[[5, 7],\n [13, 15]]]], dtype=np.int64)\ny = np.array([[[[0, 0, 0, 0],\n [0, 1, 0, 2],\n [0, 0, 0, 0],\n [0, 3, 0, 4]]]], dtype=np.float32)\nexpect(node, inputs=[xT, xI], outputs=[y], name='test_maxunpool_export_without_output_shape')" + } + ] + }, + { + "name": "Mean", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Element-wise mean of each of the input tensors. All inputs and outputs must\nhave the same shape and data type.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for Mean." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "mean", + "type": "T", + "description": "Output tensor. Same dimension as inputs." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "mean", + "code": "data_0 = np.array([3, 0, 2]).astype(np.float32)\ndata_1 = np.array([1, 3, 4]).astype(np.float32)\ndata_2 = np.array([2, 6, 6]).astype(np.float32)\nresult = np.array([2, 3, 4]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_mean_example')\n\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_mean_one_input')\n\nresult = np.divide(np.add(data_0, data_1), 2.)\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_mean_two_inputs')" + } + ] + }, + { + "name": "Mean", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Element-wise mean of each of the input tensors. All inputs and outputs must\nhave the same shape and data type.\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for Mean." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "mean", + "type": "T", + "description": "Output tensor. Same dimension as inputs." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "mean", + "code": "data_0 = np.array([3, 0, 2]).astype(np.float32)\ndata_1 = np.array([1, 3, 4]).astype(np.float32)\ndata_2 = np.array([2, 6, 6]).astype(np.float32)\nresult = np.array([2, 3, 4]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_mean_example')\n\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_mean_one_input')\n\nresult = np.divide(np.add(data_0, data_1), 2.)\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_mean_two_inputs')" + } + ] + }, + { + "name": "Mean", + "module": "ai.onnx", + "version": 8, + "support_level": "common", + "description": "Element-wise mean of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for mean." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "mean", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "mean", + "code": "data_0 = np.array([3, 0, 2]).astype(np.float32)\ndata_1 = np.array([1, 3, 4]).astype(np.float32)\ndata_2 = np.array([2, 6, 6]).astype(np.float32)\nresult = np.array([2, 3, 4]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_mean_example')\n\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_mean_one_input')\n\nresult = np.divide(np.add(data_0, data_1), 2.)\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_mean_two_inputs')" + } + ] + }, + { + "name": "Mean", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Element-wise mean of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for mean." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "mean", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "mean", + "code": "data_0 = np.array([3, 0, 2]).astype(np.float32)\ndata_1 = np.array([1, 3, 4]).astype(np.float32)\ndata_2 = np.array([2, 6, 6]).astype(np.float32)\nresult = np.array([2, 3, 4]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_mean_example')\n\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_mean_one_input')\n\nresult = np.divide(np.add(data_0, data_1), 2.)\nnode = onnx.helper.make_node(\n 'Mean',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_mean_two_inputs')" + } + ] + }, + { + "name": "MeanVarianceNormalization", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "A MeanVarianceNormalization Function: Perform mean variance normalization\n on the input tensor X using formula:
``` (X-EX)/sqrt(E(X-EX)^2) ```\n", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to caculate along axes [0,2,3] for calculating mean and variance along each channel. Two variables with the same C-coordinate are associated with the same mean and variance." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "meanvariancenormalization", + "code": "node = onnx.helper.make_node(\n 'MeanVarianceNormalization',\n inputs=['X'],\n outputs=['Y']\n)\n\ninput_data = np.array([[[[0.8439683], [0.5665144], [0.05836735]],\n [[0.02916367], [0.12964272], [0.5060197]],\n [[0.79538304], [0.9411346], [0.9546573]]],\n [[[0.17730942], [0.46192095], [0.26480448]],\n [[0.6746842], [0.01665257], [0.62473077]],\n [[0.9240844], [0.9722341], [0.11965699]]],\n [[[0.41356155], [0.9129373], [0.59330076]],\n [[0.81929934], [0.7862604], [0.11799799]],\n [[0.69248444], [0.54119414], [0.07513223]]]], dtype=np.float32)\n\n# Calculate expected output data\ndata_mean = np.mean(input_data, axis=(0, 2, 3), keepdims=1)\ndata_mean_squared = np.power(data_mean, 2)\ndata_squared = np.power(input_data, 2)\ndata_squared_mean = np.mean(data_squared, axis=(0, 2, 3), keepdims=1)\nstd = np.sqrt(data_squared_mean - data_mean_squared)\nexpected_output = (input_data - data_mean) / (std + 1e-9)\n\nexpect(node, inputs=[input_data], outputs=[expected_output],\n name='test_mvn')" + } + ] + }, + { + "name": "MeanVarianceNormalization", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "A MeanVarianceNormalization Function: Perform mean variance normalization\n on the input tensor X using formula:
``` (X-EX)/sqrt(E(X-EX)^2) ```\n", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to caculate along axes [0,2,3] for calculating mean and variance along each channel. Two variables with the same C-coordinate are associated with the same mean and variance." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "meanvariancenormalization", + "code": "node = onnx.helper.make_node(\n 'MeanVarianceNormalization',\n inputs=['X'],\n outputs=['Y']\n)\n\ninput_data = np.array([[[[0.8439683], [0.5665144], [0.05836735]],\n [[0.02916367], [0.12964272], [0.5060197]],\n [[0.79538304], [0.9411346], [0.9546573]]],\n [[[0.17730942], [0.46192095], [0.26480448]],\n [[0.6746842], [0.01665257], [0.62473077]],\n [[0.9240844], [0.9722341], [0.11965699]]],\n [[[0.41356155], [0.9129373], [0.59330076]],\n [[0.81929934], [0.7862604], [0.11799799]],\n [[0.69248444], [0.54119414], [0.07513223]]]], dtype=np.float32)\n\n# Calculate expected output data\ndata_mean = np.mean(input_data, axis=(0, 2, 3), keepdims=1)\ndata_mean_squared = np.power(data_mean, 2)\ndata_squared = np.power(input_data, 2)\ndata_squared_mean = np.mean(data_squared, axis=(0, 2, 3), keepdims=1)\nstd = np.sqrt(data_squared_mean - data_mean_squared)\nexpected_output = (input_data - data_mean) / (std + 1e-9)\n\nexpect(node, inputs=[input_data], outputs=[expected_output],\n name='test_mvn')" + } + ] + }, + { + "name": "Min", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Element-wise min of each of the input tensors. All inputs and outputs must\nhave the same shape and data type.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for Min" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "min", + "type": "T", + "description": "Output tensor. Same dimension as inputs." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "min", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 0]).astype(np.float32)\nresult = np.array([1, 2, 0]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_min_example')\n\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_min_one_input')\n\nresult = np.minimum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_two_inputs')" + }, + { + "summary": "min_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([1, 2, 1]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "Min", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Element-wise min of each of the input tensors. All inputs and outputs must\nhave the same shape and data type.\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for Min" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "min", + "type": "T", + "description": "Output tensor. Same dimension as inputs." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "min", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 0]).astype(np.float32)\nresult = np.array([1, 2, 0]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_min_example')\n\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_min_one_input')\n\nresult = np.minimum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_two_inputs')" + }, + { + "summary": "min_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([1, 2, 1]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "Min", + "module": "ai.onnx", + "version": 8, + "support_level": "common", + "description": "Element-wise min of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for min." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "min", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "min", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 0]).astype(np.float32)\nresult = np.array([1, 2, 0]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_min_example')\n\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_min_one_input')\n\nresult = np.minimum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_two_inputs')" + }, + { + "summary": "min_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([1, 2, 1]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "Min", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Element-wise min of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for min." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "min", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "min", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 0]).astype(np.float32)\nresult = np.array([1, 2, 0]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_min_example')\n\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_min_one_input')\n\nresult = np.minimum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_two_inputs')" + }, + { + "summary": "min_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([1, 2, 1]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "Min", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Element-wise min of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for min." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "min", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "min", + "code": "data_0 = np.array([3, 2, 1]).astype(np.float32)\ndata_1 = np.array([1, 4, 4]).astype(np.float32)\ndata_2 = np.array([2, 5, 0]).astype(np.float32)\nresult = np.array([1, 2, 0]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_min_example')\n\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_min_one_input')\n\nresult = np.minimum(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_two_inputs')" + }, + { + "summary": "min_all_numeric_types", + "code": "for op_dtype in all_numeric_dtypes:\n data_0 = np.array([3, 2, 1]).astype(op_dtype)\n data_1 = np.array([1, 4, 4]).astype(op_dtype)\n result = np.array([1, 2, 1]).astype(op_dtype)\n node = onnx.helper.make_node(\n 'Min',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n )\n expect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_min_{0}'.format(np.dtype(op_dtype).name))" + } + ] + }, + { + "name": "Mod", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Performs element-wise binary modulus (with Numpy-style broadcasting support).\n The sign of the remainder is the same as that of the Divisor.\n\n Mod operator can also behave like C fmod() or numpy.fmod. In this case, the sign of the remainder however, will be the same as the Dividend\n (in contrast to integer mod). To force a behavior like numpy.fmod() an 'fmod' Attribute is provided.\n This attribute is set to 0 by default causing the behavior to be like integer mod.\n Setting this attribute to 1 causes the remainder to be calculated similar to that of numpy.fmod().\n\n If the input type is floating point, then `fmod` attribute must be set to 1.\n\n In case of dividend being zero, the results will be platform dependent.\n\n This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "attributes": [ + { + "name": "fmod", + "type": "int64", + "required": false, + "description": "Whether the operator should behave like fmod (default=0 meaning it will do integer mods); Set this to 1 to force fmod treatment" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Dividend tensor" + }, + { + "name": "B", + "type": "T", + "description": "Divisor tensor" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Remainder tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "mod_broadcast", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.arange(0, 30).reshape([3, 2, 5]).astype(np.int32)\ny = np.array([7]).astype(np.int32)\nz = np.mod(x, y)\n# array([[[0, 1, 2, 3, 4],\n# [5, 6, 0, 1, 2]],\n\n# [[3, 4, 5, 6, 0],\n# [1, 2, 3, 4, 5]],\n\n# [[6, 0, 1, 2, 3],\n# [4, 5, 6, 0, 1]]], dtype=int32)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_broadcast')" + }, + { + "summary": "mod_int64_fmod", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n fmod=1\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int64)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int64)\nz = np.fmod(x, y) # expected output [ 0, 1, 5, 0, -1, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_int64_fmod')" + }, + { + "summary": "mod_mixed_sign_float16", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n fmod=1\n)\n\nx = np.array([-4.3, 7.2, 5.0, 4.3, -7.2, 8.0]).astype(np.float16)\ny = np.array([2.1, -3.4, 8.0, -2.1, 3.4, 5.0]).astype(np.float16)\nz = np.fmod(x, y) # expected output [-0.10156, 0.3984 , 5. , 0.10156, -0.3984 , 3.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_float16')" + }, + { + "summary": "mod_mixed_sign_float32", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n fmod=1\n)\n\nx = np.array([-4.3, 7.2, 5.0, 4.3, -7.2, 8.0]).astype(np.float32)\ny = np.array([2.1, -3.4, 8.0, -2.1, 3.4, 5.0]).astype(np.float32)\nz = np.fmod(x, y) # expected output [-0.10000038, 0.39999962, 5. , 0.10000038, -0.39999962, 3.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_float32')" + }, + { + "summary": "mod_mixed_sign_float64", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n fmod=1\n)\n\nx = np.array([-4.3, 7.2, 5.0, 4.3, -7.2, 8.0]).astype(np.float64)\ny = np.array([2.1, -3.4, 8.0, -2.1, 3.4, 5.0]).astype(np.float64)\nz = np.fmod(x, y) # expected output [-0.1, 0.4, 5. , 0.1, -0.4, 3.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_float64')" + }, + { + "summary": "mod_mixed_sign_int16", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int16)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int16)\nz = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_int16')" + }, + { + "summary": "mod_mixed_sign_int32", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int32)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int32)\nz = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_int32')" + }, + { + "summary": "mod_mixed_sign_int64", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int64)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int64)\nz = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_int64')" + }, + { + "summary": "mod_mixed_sign_int8", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int8)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int8)\nz = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_int8')" + }, + { + "summary": "mod_uint16", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([4, 7, 5]).astype(np.uint16)\ny = np.array([2, 3, 8]).astype(np.uint16)\nz = np.mod(x, y) # expected output [0, 1, 5]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_uint16')" + }, + { + "summary": "mod_uint32", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([4, 7, 5]).astype(np.uint32)\ny = np.array([2, 3, 8]).astype(np.uint32)\nz = np.mod(x, y) # expected output [0, 1, 5]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_uint32')" + }, + { + "summary": "mod_uint64", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([4, 7, 5]).astype(np.uint64)\ny = np.array([2, 3, 8]).astype(np.uint64)\nz = np.mod(x, y) # expected output [0, 1, 5]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_uint64')" + }, + { + "summary": "mod_uint8", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([4, 7, 5]).astype(np.uint8)\ny = np.array([2, 3, 8]).astype(np.uint8)\nz = np.mod(x, y) # expected output [0, 1, 5]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_uint8')" + } + ] + }, + { + "name": "Mod", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Performs element-wise binary modulus (with Numpy-style broadcasting support).\n The sign of the remainder is the same as that of the Divisor.\n\n Mod operator can also behave like C fmod() or numpy.fmod. In this case, the sign of the remainder however, will be the same as the Dividend\n (in contrast to integer mod). To force a behavior like numpy.fmod() an 'fmod' Attribute is provided.\n This attribute is set to 0 by default causing the behavior to be like integer mod.\n Setting this attribute to 1 causes the remainder to be calculated similar to that of numpy.fmod().\n\n If the input type is floating point, then `fmod` attribute must be set to 1.\n\n In case of dividend being zero, the results will be platform dependent.\n\n This operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "attributes": [ + { + "name": "fmod", + "type": "int64", + "required": false, + "description": "Whether the operator should behave like fmod (default=0 meaning it will do integer mods); Set this to 1 to force fmod treatment" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Dividend tensor" + }, + { + "name": "B", + "type": "T", + "description": "Divisor tensor" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Remainder tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "mod_broadcast", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.arange(0, 30).reshape([3, 2, 5]).astype(np.int32)\ny = np.array([7]).astype(np.int32)\nz = np.mod(x, y)\n# array([[[0, 1, 2, 3, 4],\n# [5, 6, 0, 1, 2]],\n\n# [[3, 4, 5, 6, 0],\n# [1, 2, 3, 4, 5]],\n\n# [[6, 0, 1, 2, 3],\n# [4, 5, 6, 0, 1]]], dtype=int32)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_broadcast')" + }, + { + "summary": "mod_int64_fmod", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n fmod=1\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int64)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int64)\nz = np.fmod(x, y) # expected output [ 0, 1, 5, 0, -1, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_int64_fmod')" + }, + { + "summary": "mod_mixed_sign_float16", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n fmod=1\n)\n\nx = np.array([-4.3, 7.2, 5.0, 4.3, -7.2, 8.0]).astype(np.float16)\ny = np.array([2.1, -3.4, 8.0, -2.1, 3.4, 5.0]).astype(np.float16)\nz = np.fmod(x, y) # expected output [-0.10156, 0.3984 , 5. , 0.10156, -0.3984 , 3.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_float16')" + }, + { + "summary": "mod_mixed_sign_float32", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n fmod=1\n)\n\nx = np.array([-4.3, 7.2, 5.0, 4.3, -7.2, 8.0]).astype(np.float32)\ny = np.array([2.1, -3.4, 8.0, -2.1, 3.4, 5.0]).astype(np.float32)\nz = np.fmod(x, y) # expected output [-0.10000038, 0.39999962, 5. , 0.10000038, -0.39999962, 3.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_float32')" + }, + { + "summary": "mod_mixed_sign_float64", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n fmod=1\n)\n\nx = np.array([-4.3, 7.2, 5.0, 4.3, -7.2, 8.0]).astype(np.float64)\ny = np.array([2.1, -3.4, 8.0, -2.1, 3.4, 5.0]).astype(np.float64)\nz = np.fmod(x, y) # expected output [-0.1, 0.4, 5. , 0.1, -0.4, 3.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_float64')" + }, + { + "summary": "mod_mixed_sign_int16", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int16)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int16)\nz = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_int16')" + }, + { + "summary": "mod_mixed_sign_int32", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int32)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int32)\nz = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_int32')" + }, + { + "summary": "mod_mixed_sign_int64", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int64)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int64)\nz = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_int64')" + }, + { + "summary": "mod_mixed_sign_int8", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([-4, 7, 5, 4, -7, 8]).astype(np.int8)\ny = np.array([2, -3, 8, -2, 3, 5]).astype(np.int8)\nz = np.mod(x, y) # expected output [ 0, -2, 5, 0, 2, 3]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_mixed_sign_int8')" + }, + { + "summary": "mod_uint16", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([4, 7, 5]).astype(np.uint16)\ny = np.array([2, 3, 8]).astype(np.uint16)\nz = np.mod(x, y) # expected output [0, 1, 5]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_uint16')" + }, + { + "summary": "mod_uint32", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([4, 7, 5]).astype(np.uint32)\ny = np.array([2, 3, 8]).astype(np.uint32)\nz = np.mod(x, y) # expected output [0, 1, 5]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_uint32')" + }, + { + "summary": "mod_uint64", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([4, 7, 5]).astype(np.uint64)\ny = np.array([2, 3, 8]).astype(np.uint64)\nz = np.mod(x, y) # expected output [0, 1, 5]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_uint64')" + }, + { + "summary": "mod_uint8", + "code": "node = onnx.helper.make_node(\n 'Mod',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([4, 7, 5]).astype(np.uint8)\ny = np.array([2, 3, 8]).astype(np.uint8)\nz = np.mod(x, y) # expected output [0, 1, 5]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mod_uint8')" + } + ] + }, + { + "name": "Momentum", + "module": "ai.onnx.preview.training", + "version": 1, + "support_level": "common", + "description": "Compute one iteration of stochastic gradient update with momentum.\n This operator can conduct the optimization of multiple tensor variables.\n\n Let's define the behavior of this operator. As you can imagine, SG with momentum requires\n several parameters:\n\n - The learning-rate \"R\".\n - The update count \"T\". That is, the number of conducted training iterations. It should\n be zero in the first training iteration.\n - A L2-norm regularization coefficient \"norm_coefficient\".\n - A decay coefficient of previous accumulated gradient (i.e., momentum) \"alpha\".\n - The scaling coefficient of current gradient \"beta\".\n - An attribute to choose either standard momentum or Nesterov's momentum \"mode\" should\n be used.\n\n For the sake of simplicity, assume that there is only one tensor (called \"X\") to be optimized.\n Other necessary inputs are \"X\"'s gradient (called \"G\") and \"X\"'s momentum (called \"V\"). This\n Momentum operator maps all these inputs to the new value of \"X\" (called \"X_new\") and its new\n momentum (called \"V_new\").\n\n This operator supports two different momentum algorithms. Set the attribute \"mode\" to\n \"nesterov\" if Nesterov's momentum is desired. Otherwise, set the attribute \"model\" to\n \"standard\" to use standard momentum. Computation details are described subsequently.\n\n Let \"+\", \"-\", \"*\", and \"/\" are all element-wise operations with numpy-style broadcasting.\n\n Pseudo code for SG with standard momentum:\n\n // Add gradient of 0.5 * norm_coefficient * ||X||^2, where ||X|| is the sum of squared\n // values of all elements in X.\n G_regularized = norm_coefficient * X + G\n\n // In the first training iteration, beta should always be 1.\n beta_adjusted = T > 0 ? beta : 1\n\n // Compute the current momentum based on previous momentum and the current gradient.\n V_new = alpha * V + beta_adjusted * G_regularized\n\n // Update X.\n X_new = X - R * V_new\n\n Pseudo code for SG with Nesterov's momentum:\n\n // Add gradient of 0.5 * norm_coefficient * ||X||^2, where ||X|| is the sum of squared\n // values of all elements in X.\n G_regularized = norm_coefficient * X + G;\n\n // In the first training iteration, beta should always be 1.\n beta_adjusted = T > 0 ? beta : 1\n\n // Compute the current momentum based on previous momentum and the current gradient.\n V_new = alpha * V + beta_adjusted * G_regularized;\n\n // Compute final update direction and then update X.\n X_new = X - R * (G_regularized + alpha * V_new)\n\n If one assign this operators to optimize multiple inputs, for example, \"X_1\" and \"X_2\". The same\n pseudo code would be extended to handle all tensors jointly. More specifically, we can view \"X\" as a\n concatenation of \"X_1\" and \"X_2\" (of course, their gradient and accumulate gradient should\n be concatenated too) and then our pseudo code becomes applicable.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": true, + "description": "The decay factor of momentum. It should be a scalar." + }, + { + "name": "beta", + "type": "float32", + "required": true, + "description": "The coefficient of gradient in computing new momentum. It should be a scalar." + }, + { + "name": "mode", + "type": "string", + "required": true, + "description": "Its value should be either \"nesterov\" or \"standard\". The value \"nesterov\" leads to the use of Nesterov's momentum while \"standard\" invokes stochastic gradient method using standard momentum" + }, + { + "name": "norm_coefficient", + "type": "float32", + "required": true, + "description": "Coefficient of 0.5 * norm_coefficient * ||X||^2." + } + ], + "inputs": [ + { + "name": "R", + "type": "T1", + "description": "The learning rate." + }, + { + "name": "T", + "type": "T2", + "description": "Update count of \"X\". It should be a scalar." + }, + { + "name": "inputs", + "type": "T3", + "list": true, + "description": "It sequentially contains the current values of optimized tensors, then their gradient tensors, and finally their momentum tensors. For example, if two tensors \"X_1\" and \"X_2\" are optimized, The expected input list would be [\"X_1\", \"X_2\", gradient of \"X_1\", gradient of \"X_2\", momentum of \"X_1\", momentum of \"X_2\"]." + } + ], + "min_input": 3, + "max_input": 2147483647, + "outputs": [ + { + "name": "outputs", + "type": "T3", + "list": true, + "description": "It sequentially contains the new values of optimized tensors and then the new values of their momentum tensors. For example, if two tensors \"X_1\" and \"X_2\" are optimized, the output list would be [new value of \"X_1,\" new value of \"X_2\" new momentum of \"X_1\", new momentum of \"X_2\"]." + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "3 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input types to float scalars.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain input types to 64-bit integer scalars.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int64)" + ] + }, + { + "description": "Constrain input types to float tensors.", + "type_param_str": "T3", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "momentum", + "code": "# Define operator attributes.\nnorm_coefficient = 0.001\nalpha = 0.95\nbeta = 0.1\n\n# Create operator.\nnode = onnx.helper.make_node('Momentum',\n inputs=['R', 'T', 'X', 'G', 'V'],\n outputs=['X_new', 'V_new'],\n norm_coefficient=norm_coefficient,\n alpha=alpha,\n beta=beta,\n mode='standard',\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN\n )\n\n# Define operator inputs.\nr = np.array(0.1, dtype=np.float32) # scalar\nt = np.array(0, dtype=np.int64) # scalar\nx = np.array([1.2, 2.8], dtype=np.float32)\ng = np.array([-0.94, -2.5], dtype=np.float32)\nv = np.array([1.7, 3.6], dtype=np.float32)\n\n# Compute expected outputs of Momentum.\nx_new, v_new = apply_momentum(r, t, x, g, v,\n norm_coefficient, alpha, beta)\n\n# Check results.\nexpect(node, inputs=[r, t, x, g, v],\n outputs=[x_new, v_new], name='test_momentum',\n opset_imports=[onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)])" + }, + { + "summary": "momentum_multiple", + "code": "# Define operator attributes.\nnorm_coefficient = 0.001\nalpha = 0.95\nbeta = 0.85\n\nnode = onnx.helper.make_node('Momentum',\n inputs=['R', 'T', 'X1', 'X2',\n 'G1', 'G2', 'H1', 'H2'],\n outputs=['X1_new', 'X2_new',\n 'V1_new', 'V2_new'],\n norm_coefficient=norm_coefficient,\n alpha=alpha,\n beta=beta,\n mode='standard',\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN\n )\n\n# Define operator inputs.\nr = np.array(0.1, dtype=np.float32) # scalar\nt = np.array(0, dtype=np.int64) # scalar\n\nx1 = np.array([1.0], dtype=np.float32)\ng1 = np.array([-1.0], dtype=np.float32)\nv1 = np.array([2.0], dtype=np.float32)\n\nx2 = np.array([1.0, 2.0], dtype=np.float32)\ng2 = np.array([-1.0, -3.0], dtype=np.float32)\nv2 = np.array([4.0, 1.0], dtype=np.float32)\n\n# Compute expected outputs of Momentum.\nx1_new, v1_new = apply_momentum(r, t, x1, g1, v1,\n norm_coefficient, alpha, beta)\nx2_new, v2_new = apply_momentum(r, t, x2, g2, v2,\n norm_coefficient, alpha, beta)\n\n# Check results.\nexpect(node, inputs=[r, t, x1, x2, g1, g2, v1, v2],\n outputs=[x1_new, x2_new, v1_new, v2_new], name='test_momentum_multiple',\n opset_imports=[onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)])" + }, + { + "summary": "nesterov_momentum", + "code": "# Define operator attributes.\nnorm_coefficient = 0.01\nalpha = 0.95\nbeta = 1.0\n\n# Create operator.\nnode = onnx.helper.make_node('Momentum',\n inputs=['R', 'T', 'X', 'G', 'V'],\n outputs=['X_new', 'V_new'],\n norm_coefficient=norm_coefficient,\n alpha=alpha,\n beta=beta,\n mode='nesterov',\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN\n )\n\n# Define operator inputs.\nr = np.array(0.1, dtype=np.float32) # scalar\nt = np.array(0, dtype=np.int64) # scalar\nx = np.array([1.2, 2.8], dtype=np.float32)\ng = np.array([-0.94, -2.5], dtype=np.float32)\nv = np.array([1.7, 3.6], dtype=np.float32)\n\n# Compute expected outputs of Momentum.\nx_new, v_new = apply_nesterov(r, t, x, g, v,\n norm_coefficient, alpha, beta)\n\n# Check results.\nexpect(node, inputs=[r, t, x, g, v],\n outputs=[x_new, v_new], name='test_nesterov_momentum',\n opset_imports=[onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)])" + } + ] + }, + { + "name": "Mul", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Performs element-wise binary multiplication (with limited broadcast support).\n\nIf necessary the right-hand-side argument will be broadcasted to match the\nshape of left-hand-side argument. When broadcasting is specified, the second\ntensor can either be of element size 1 (including a scalar tensor and any\ntensor with rank equal to or smaller than the first tensor), or having its\nshape as a contiguous subset of the first tensor's shape. The starting of the\nmutually equal shape is specified by the argument \"axis\", and if it is not set,\nsuffix matching is assumed. 1-dim expansion doesn't work yet.\n\nFor example, the following tensor shapes are supported (with broadcast=1):\n\n shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (5,)\n shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)\n shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1\n shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0\n\nAttribute `broadcast=1` needs to be passed to enable broadcasting.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions. See doc for details." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Pass 1 to enable broadcasting" + }, + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand, should share the type with the second operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same dimensions and type as A" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "mul", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = x * y # expected output [4., 10., 18.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul')\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_uint8')" + }, + { + "summary": "mul_broadcast", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_bcast')" + } + ] + }, + { + "name": "Mul", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Performs element-wise binary multiplication (with limited broadcast support).\n\nIf necessary the right-hand-side argument will be broadcasted to match the\nshape of left-hand-side argument. When broadcasting is specified, the second\ntensor can either be of element size 1 (including a scalar tensor and any\ntensor with rank equal to or smaller than the first tensor), or having its\nshape as a contiguous subset of the first tensor's shape. The starting of the\nmutually equal shape is specified by the argument \"axis\", and if it is not set,\nsuffix matching is assumed. 1-dim expansion doesn't work yet.\n\nFor example, the following tensor shapes are supported (with broadcast=1):\n\n shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (5,)\n shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)\n shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1\n shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0\n\nAttribute `broadcast=1` needs to be passed to enable broadcasting.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions. See doc for details." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Pass 1 to enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand, should share the type with the second operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same dimensions and type as A" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "mul", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = x * y # expected output [4., 10., 18.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul')\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_uint8')" + }, + { + "summary": "mul_broadcast", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_bcast')" + } + ] + }, + { + "name": "Mul", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Performs element-wise binary multiplication (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "mul", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = x * y # expected output [4., 10., 18.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul')\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_uint8')" + }, + { + "summary": "mul_broadcast", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_bcast')" + } + ] + }, + { + "name": "Mul", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Performs element-wise binary multiplication (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "mul", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = x * y # expected output [4., 10., 18.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul')\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_uint8')" + }, + { + "summary": "mul_broadcast", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_bcast')" + } + ] + }, + { + "name": "Mul", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Performs element-wise binary multiplication (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n\n(Opset 14 change): Extend supported types to include uint8, int8, uint16, and int16.\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "mul", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = x * y # expected output [4., 10., 18.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul')\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_uint8')" + }, + { + "summary": "mul_broadcast", + "code": "node = onnx.helper.make_node(\n 'Mul',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_mul_bcast')" + } + ] + }, + { + "name": "Multinomial", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Generate a tensor of samples from a multinomial distribution according to the probabilities\nof each of the possible outcomes.\n", + "attributes": [ + { + "name": "dtype", + "type": "int64", + "required": false, + "default": 6, + "description": "(Optional) The data type for the elements of the output tensor, if not specified, we will use int32." + }, + { + "name": "sample_size", + "type": "int64", + "required": false, + "default": 1, + "description": "Number of times to sample." + }, + { + "name": "seed", + "type": "float32", + "required": false, + "description": "(Optional) Seed to the random generator, if not specified we will auto generate one." + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "Input tensor with shape [batch_size, class_size], where class_size is the number of all possible outcomes. Each value along the axis zero represents the unnormalized log-probability of each corresponding outcome in a batch." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor with shape [batch_size, sample_size], where sample_size is the number of times to sample. Each value along the axis zero represents the outcome of the corresponding sample in a batch." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain output types to integral tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "Neg", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Neg takes one input data (Tensor) and produces one output data\n(Tensor) where each element flipped sign, y = -x, is applied to\nthe tensor elementwise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "neg", + "code": "node = onnx.helper.make_node(\n 'Neg',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-4, 2]).astype(np.float32)\ny = np.negative(x) # expected output [4., -2.],\nexpect(node, inputs=[x], outputs=[y],\n name='test_neg_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.negative(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_neg')" + } + ] + }, + { + "name": "Neg", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Neg takes one input data (Tensor) and produces one output data\n(Tensor) where each element flipped sign, y = -x, is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to signed numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(int32)", + "tensor(int8)", + "tensor(int16)", + "tensor(int64)", + "tensor(float16)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "neg", + "code": "node = onnx.helper.make_node(\n 'Neg',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-4, 2]).astype(np.float32)\ny = np.negative(x) # expected output [4., -2.],\nexpect(node, inputs=[x], outputs=[y],\n name='test_neg_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.negative(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_neg')" + } + ] + }, + { + "name": "Neg", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Neg takes one input data (Tensor) and produces one output data\n(Tensor) where each element flipped sign, y = -x, is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to signed numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(int32)", + "tensor(int8)", + "tensor(int16)", + "tensor(int64)", + "tensor(float16)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "neg", + "code": "node = onnx.helper.make_node(\n 'Neg',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-4, 2]).astype(np.float32)\ny = np.negative(x) # expected output [4., -2.],\nexpect(node, inputs=[x], outputs=[y],\n name='test_neg_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.negative(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_neg')" + } + ] + }, + { + "name": "NegativeLogLikelihoodLoss", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "A NegativeLogLikelihoodLoss operator computes (weighted) negative log likelihood loss.\nIts \"input\" tensor has the shape of (N, C, d1, d2, ..., dk) where k >= 0.\nThe \"input\" tensor contains log-probabilities for input[n, :, d_1, d_2,..., d_k] being in a class of [0, C).\nThe operator's \"target\" input tensor has the shape of (N, d1, d2, ..., dk). It encodes class labels (one of C classes)\nor it may contain a special value (indicated by an attribute ignore_index) for N x d1 x d2 x ... x dk samples.\nThe loss value for input[n, :, d_1, d_2,...d_k] being classified as class c = target[n][d_1][d_2]...[d_k] is computed as:\n loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k].\nWhen an optional \"weight\" is provided, the sample loss is calculated as:\n loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k] * weight[c].\nloss is zero for the case when target-value equals ignore_index.\n\n loss[n][d_1][d_2]...[d_k] = 0, when target[n][d_1][d_2]...[d_k] = ignore_index\nIf \"reduction\" attribute is set to \"none\", the operator's output will be the above loss with shape (N, d1, d2, ..., dk).\nIf \"reduction\" attribute is set to \"mean\" (the default attribute value), the output loss is (weight) averaged:\n mean(loss), if \"weight\" is not provided,\nor if weight is provided,\n sum(loss) / sum(weight[target[n][d_1][d_2]...[d_k]]]), for all samples.\nIf \"reduction\" attribute is set to \"sum\", the output is a scalar:\n sum(loss).\nSee also https://pytorch.org/docs/stable/nn.html#torch.nn.NLLLoss.\nExample 1:\n // negative log likelihood loss, \"none\" reduction\n N, C, d1 = 2, 3, 2\n input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\n target = [[2, 1], [0, 2]]\n loss = np.zeros((N, d1))\n for n in range(N):\n for d_1 in range(d1):\n c = target[n][d_1]\n loss[n][d_1] = -input[n][c][d_1]\n // print(loss)\n // [[-3. -2.]\n // [-0. -2.]]\nExample 2:\n // weighted negative log likelihood loss, sum reduction\n N, C, d1 = 2, 3, 2\n input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\n target = [[2, 1], [0, 2]]\n weight = [0.2, 0.3, 0.1]\n loss = np.zeros((N, d1))\n for n in range(N):\n for d_1 in range(d1):\n c = target[n][d_1]\n loss[n][d_1] = -input[n][c][d_1] * weight[c]\n loss = np.sum(loss)\n // print(loss)\n // -1.1\nExample 3:\n // weighted negative log likelihood loss, mean reduction\n N, C, d1 = 2, 3, 2\n input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\n target = [[2, 1], [0, 2]]\n weight = [0.2, 0.3, 0.1]\n loss = np.zeros((N, d1))\n weight_total = 0\n for n in range(N):\n for d_1 in range(d1):\n c = target[n][d_1]\n loss[n][d_1] = -input[n][c][d_1] * weight[c]\n weight_total = weight_total + weight[c]\n loss = np.sum(loss) / weight_total\n // print(loss)\n // -1.57\n", + "attributes": [ + { + "name": "ignore_index", + "type": "int64", + "required": false, + "description": "Specifies a target value that is ignored and does not contribute to the input gradient. It's an optional value." + }, + { + "name": "reduction", + "type": "string", + "required": false, + "default": "mean", + "description": "Type of reduction to apply to loss: none, sum, mean (default). 'none': the output is the loss for each sample. 'sum': the output will be summed. 'mean': the sum of the output will be divided by the sum of applied weights." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of shape (N, C) or (N, C, d1, d2, ..., dk)." + }, + { + "name": "target", + "type": "Tind", + "description": "Target tensor of shape (N) or (N, d1, d2, ..., dk). Target element value shall be in range of [0, C). If ignore_index is specified, it may have a value outside [0, C) and the target values should either be in the range [0, C) or have the value ignore_index." + }, + { + "name": "weight", + "type": "T", + "option": "optional", + "description": "Optional rescaling weight tensor. If given, it has to be a tensor of size C. Otherwise, it is treated as if having all ones." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "loss", + "type": "T", + "description": "The negative log likelihood loss" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input, weight, and output types to floating-point tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain target to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "input_shape_is_NC", + "code": "reduction = 'none'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C = 3, 5\nnp.random.seed(0)\ninput = np.random.rand(N, C).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, )).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NC')" + }, + { + "summary": "input_shape_is_NCd1", + "code": "reduction = 'mean'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, d1 = 3, 5, 2\nnp.random.seed(0)\ninput = np.random.rand(N, C, d1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, d1)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1')" + }, + { + "summary": "input_shape_is_NCd1_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(1)\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index\n)\n\nN, C, d1 = 3, 5, 2\nnp.random.seed(0)\ninput = np.random.rand(N, C, d1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, d1)).astype(np.int64)\ntarget[0][0] = np.int64(1)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction, ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1_ii')" + }, + { + "summary": "input_shape_is_NCd1_mean_weight_negative_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(-1)\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1 = 3, 5, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1)).astype(np.int64)\ntarget[0][0] = -1\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1_mean_weight_negative_ii')" + }, + { + "summary": "input_shape_is_NCd1_weight", + "code": "reduction = 'mean'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, d1 = 3, 5, 2\nnp.random.seed(0)\ninput = np.random.rand(N, C, d1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, d1)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1_weight')" + }, + { + "summary": "input_shape_is_NCd1_weight_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(1)\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index\n)\n\nN, C, d1 = 3, 5, 2\nnp.random.seed(0)\ninput = np.random.rand(N, C, d1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, d1)).astype(np.int64)\ntarget[0][0] = np.int64(1)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction, ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1_weight_ii')" + }, + { + "summary": "input_shape_is_NCd1d2", + "code": "reduction = 'none'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2')" + }, + { + "summary": "input_shape_is_NCd1d2_no_weight_reduction_mean_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(1)\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\ntarget[0][0][0] = np.int64(1)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, reduction=reduction, ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_no_weight_reduction_mean_ii')" + }, + { + "summary": "input_shape_is_NCd1d2_reduction_mean", + "code": "reduction = 'mean'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_reduction_mean')" + }, + { + "summary": "input_shape_is_NCd1d2_reduction_sum", + "code": "reduction = 'sum'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2))\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_reduction_sum')" + }, + { + "summary": "input_shape_is_NCd1d2_with_weight", + "code": "reduction = 'none'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_with_weight')" + }, + { + "summary": "input_shape_is_NCd1d2_with_weight_reduction_mean", + "code": "reduction = 'mean'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_with_weight_reduction_mean')" + }, + { + "summary": "input_shape_is_NCd1d2_with_weight_reduction_sum", + "code": "reduction = 'sum'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_with_weight_reduction_sum')" + }, + { + "summary": "input_shape_is_NCd1d2_with_weight_reduction_sum_ii", + "code": "reduction = 'sum'\nignore_index = np.int64(0)\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\ntarget[0][0][0] = np.int64(0)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction, ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_with_weight_reduction_sum_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3_none_no_weight_negative_ii", + "code": "reduction = 'none'\nignore_index = np.int64(-5)\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1, dim2, dim3 = 3, 5, 6, 6, 5\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2, dim3).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3)).astype(np.int64)\ntarget[0][0][0][0] = -5\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2d3_none_no_weight_negative_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3_sum_weight_high_ii", + "code": "reduction = 'sum'\nignore_index = np.int64(10)\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C = 3, 5\nnp.random.seed(0)\ninput = np.random.rand(N, C).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N))\ntarget[0] = 10\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2d3_sum_weight_high_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight", + "code": "reduction = 'mean'\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n weight=weight,\n reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2d3d4d5_mean_weight')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight", + "code": "reduction = 'none'\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2d3d4d5_none_no_weight')" + } + ] + }, + { + "name": "NegativeLogLikelihoodLoss", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "A NegativeLogLikelihoodLoss operator computes (weighted) negative log likelihood loss.\nIts \"input\" tensor has the shape of (N, C, d1, d2, ..., dk) where k >= 0.\nThe \"input\" tensor contains log-probabilities for input[n, :, d_1, d_2,..., d_k] being in a class of [0, C).\nThe operator's \"target\" input tensor has the shape of (N, d1, d2, ..., dk). It encodes class labels (one of C classes)\nor it may contain a special value (indicated by an attribute ignore_index) for N x d1 x d2 x ... x dk samples.\nThe loss value for input[n, :, d_1, d_2,...d_k] being classified as class c = target[n][d_1][d_2]...[d_k] is computed as:\n\n loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k].\n\nWhen an optional \"weight\" is provided, the sample loss is calculated as:\n\n loss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k] * weight[c].\n\nloss is zero for the case when target-value equals ignore_index.\n\n loss[n][d_1][d_2]...[d_k] = 0, when target[n][d_1][d_2]...[d_k] = ignore_index\n\nIf \"reduction\" attribute is set to \"none\", the operator's output will be the above loss with shape (N, d1, d2, ..., dk).\nIf \"reduction\" attribute is set to \"mean\" (the default attribute value), the output loss is (weight) averaged:\n\n mean(loss), if \"weight\" is not provided,\n\nor if weight is provided,\n\n sum(loss) / sum(weight[target[n][d_1][d_2]...[d_k]]]), for all samples.\n\nIf \"reduction\" attribute is set to \"sum\", the output is a scalar:\n sum(loss).\n\nSee also https://pytorch.org/docs/stable/nn.html#torch.nn.NLLLoss.\n\nExample 1:\n\n // negative log likelihood loss, \"none\" reduction\n N, C, d1 = 2, 3, 2\n input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\n target = [[2, 1], [0, 2]]\n\n loss = np.zeros((N, d1))\n for n in range(N):\n for d_1 in range(d1):\n c = target[n][d_1]\n loss[n][d_1] = -input[n][c][d_1]\n\n // print(loss)\n // [[-3. -2.]\n // [-0. -2.]]\n\nExample 2:\n\n // weighted negative log likelihood loss, sum reduction\n N, C, d1 = 2, 3, 2\n input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\n target = [[2, 1], [0, 2]]\n weight = [0.2, 0.3, 0.1]\n loss = np.zeros((N, d1))\n for n in range(N):\n for d_1 in range(d1):\n c = target[n][d_1]\n loss[n][d_1] = -input[n][c][d_1] * weight[c]\n\n loss = np.sum(loss)\n // print(loss)\n // -1.1\n\nExample 3:\n\n // weighted negative log likelihood loss, mean reduction\n N, C, d1 = 2, 3, 2\n input = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\n target = [[2, 1], [0, 2]]\n weight = [0.2, 0.3, 0.1]\n loss = np.zeros((N, d1))\n weight_total = 0\n for n in range(N):\n for d_1 in range(d1):\n c = target[n][d_1]\n loss[n][d_1] = -input[n][c][d_1] * weight[c]\n weight_total = weight_total + weight[c]\n\n loss = np.sum(loss) / weight_total\n // print(loss)\n // -1.57\n", + "attributes": [ + { + "name": "ignore_index", + "type": "int64", + "required": false, + "description": "Specifies a target value that is ignored and does not contribute to the input gradient. It's an optional value." + }, + { + "name": "reduction", + "type": "string", + "required": false, + "default": "mean", + "description": "Type of reduction to apply to loss: none, sum, mean (default). 'none': the output is the loss for each sample. 'sum': the output will be summed. 'mean': the sum of the output will be divided by the sum of applied weights." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of shape (N, C) or (N, C, d1, d2, ..., dk)." + }, + { + "name": "target", + "type": "Tind", + "description": "Target tensor of shape (N) or (N, d1, d2, ..., dk). Target element value shall be in range of [0, C). If ignore_index is specified, it may have a value outside [0, C) and the target values should either be in the range [0, C) or have the value ignore_index." + }, + { + "name": "weight", + "type": "T", + "option": "optional", + "description": "Optional rescaling weight tensor. If given, it has to be a tensor of size C. Otherwise, it is treated as if having all ones." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "loss", + "type": "T", + "description": "The negative log likelihood loss" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input, weight, and output types to floating-point tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain target to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "input_shape_is_NC", + "code": "reduction = 'none'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C = 3, 5\nnp.random.seed(0)\ninput = np.random.rand(N, C).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, )).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NC')" + }, + { + "summary": "input_shape_is_NCd1", + "code": "reduction = 'mean'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, d1 = 3, 5, 2\nnp.random.seed(0)\ninput = np.random.rand(N, C, d1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, d1)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1')" + }, + { + "summary": "input_shape_is_NCd1_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(1)\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index\n)\n\nN, C, d1 = 3, 5, 2\nnp.random.seed(0)\ninput = np.random.rand(N, C, d1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, d1)).astype(np.int64)\ntarget[0][0] = np.int64(1)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction, ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1_ii')" + }, + { + "summary": "input_shape_is_NCd1_mean_weight_negative_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(-1)\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1 = 3, 5, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1)).astype(np.int64)\ntarget[0][0] = -1\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1_mean_weight_negative_ii')" + }, + { + "summary": "input_shape_is_NCd1_weight", + "code": "reduction = 'mean'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, d1 = 3, 5, 2\nnp.random.seed(0)\ninput = np.random.rand(N, C, d1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, d1)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1_weight')" + }, + { + "summary": "input_shape_is_NCd1_weight_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(1)\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index\n)\n\nN, C, d1 = 3, 5, 2\nnp.random.seed(0)\ninput = np.random.rand(N, C, d1).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, d1)).astype(np.int64)\ntarget[0][0] = np.int64(1)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction, ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1_weight_ii')" + }, + { + "summary": "input_shape_is_NCd1d2", + "code": "reduction = 'none'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2')" + }, + { + "summary": "input_shape_is_NCd1d2_no_weight_reduction_mean_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(1)\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\ntarget[0][0][0] = np.int64(1)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, reduction=reduction, ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_no_weight_reduction_mean_ii')" + }, + { + "summary": "input_shape_is_NCd1d2_reduction_mean", + "code": "reduction = 'mean'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_reduction_mean')" + }, + { + "summary": "input_shape_is_NCd1d2_reduction_sum", + "code": "reduction = 'sum'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2))\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=None, reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_reduction_sum')" + }, + { + "summary": "input_shape_is_NCd1d2_with_weight", + "code": "reduction = 'none'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_with_weight')" + }, + { + "summary": "input_shape_is_NCd1d2_with_weight_reduction_mean", + "code": "reduction = 'mean'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_with_weight_reduction_mean')" + }, + { + "summary": "input_shape_is_NCd1d2_with_weight_reduction_sum", + "code": "reduction = 'sum'\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_with_weight_reduction_sum')" + }, + { + "summary": "input_shape_is_NCd1d2_with_weight_reduction_sum_ii", + "code": "reduction = 'sum'\nignore_index = np.int64(0)\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index\n)\n\nN, C, dim1, dim2 = 3, 5, 6, 6\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2)).astype(np.int64)\ntarget[0][0][0] = np.int64(0)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input, target, weight=weight, reduction=reduction, ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2_with_weight_reduction_sum_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3_none_no_weight_negative_ii", + "code": "reduction = 'none'\nignore_index = np.int64(-5)\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1, dim2, dim3 = 3, 5, 6, 6, 5\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2, dim3).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3)).astype(np.int64)\ntarget[0][0][0][0] = -5\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2d3_none_no_weight_negative_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3_sum_weight_high_ii", + "code": "reduction = 'sum'\nignore_index = np.int64(10)\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C = 3, 5\nnp.random.seed(0)\ninput = np.random.rand(N, C).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N))\ntarget[0] = 10\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2d3_sum_weight_high_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight", + "code": "reduction = 'mean'\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target', 'weight'],\n outputs=['loss'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n weight=weight,\n reduction=reduction)\n\nexpect(node, inputs=[input, target, weight], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2d3d4d5_mean_weight')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight", + "code": "reduction = 'none'\n\nnode = onnx.helper.make_node(\n 'NegativeLogLikelihoodLoss',\n inputs=['input', 'target'],\n outputs=['loss'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\ninput = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\ntarget = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(input,\n target,\n reduction=reduction)\n\nexpect(node, inputs=[input, target], outputs=[negative_log_likelihood_loss],\n name='test_nllloss_NCd1d2d3d4d5_none_no_weight')" + } + ] + }, + { + "name": "NonMaxSuppression", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Filter out boxes that have high intersection-over-union (IOU) overlap with previously selected boxes.\nBounding boxes with score less than score_threshold are removed. Bounding box format is indicated by attribute center_point_box.\nNote that this algorithm is agnostic to where the origin is in the coordinate system and more generally is invariant to\northogonal transformations and translations of the coordinate system; thus translating or reflections of the coordinate system\nresult in the same boxes being selected by the algorithm.\nThe selected_indices output is a set of integers indexing into the input collection of bounding boxes representing the selected boxes.\nThe bounding box coordinates corresponding to the selected indices can then be obtained using the Gather or GatherND operation.\n", + "attributes": [ + { + "name": "center_point_box", + "type": "int64", + "required": false, + "description": "Integer indicate the format of the box data. The default is 0. 0 - the box data is supplied as [y1, x1, y2, x2] where (y1, x1) and (y2, x2) are the coordinates of any diagonal pair of box corners and the coordinates can be provided as normalized (i.e., lying in the interval [0, 1]) or absolute. Mostly used for TF models. 1 - the box data is supplied as [x_center, y_center, width, height]. Mostly used for Pytorch models." + } + ], + "inputs": [ + { + "name": "boxes", + "type": "tensor(float)", + "description": "An input tensor with shape [num_batches, spatial_dimension, 4]. The single box data format is indicated by center_point_box." + }, + { + "name": "scores", + "type": "tensor(float)", + "description": "An input tensor with shape [num_batches, num_classes, spatial_dimension]" + }, + { + "name": "max_output_boxes_per_class", + "type": "tensor(int64)", + "option": "optional", + "description": "Integer representing the maximum number of boxes to be selected per batch per class. It is a scalar. Default to 0, which means no output." + }, + { + "name": "iou_threshold", + "type": "tensor(float)", + "option": "optional", + "description": "Float representing the threshold for deciding whether boxes overlap too much with respect to IOU. It is scalar. Value range [0, 1]. Default to 0." + }, + { + "name": "score_threshold", + "type": "tensor(float)", + "option": "optional", + "description": "Float representing the threshold for deciding when to remove boxes based on score. It is a scalar." + } + ], + "min_input": 2, + "max_input": 5, + "outputs": [ + { + "name": "selected_indices", + "type": "tensor(int64)", + "description": "selected indices from the boxes tensor. [num_selected_indices, 3], the selected index format is [batch_index, class_index, box_index]." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 5", + "examples": [ + { + "summary": "nonmaxsuppression_center_point_box_format", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices'],\n center_point_box=1\n)\nboxes = np.array([[\n [0.5, 0.5, 1.0, 1.0],\n [0.5, 0.6, 1.0, 1.0],\n [0.5, 0.4, 1.0, 1.0],\n [0.5, 10.5, 1.0, 1.0],\n [0.5, 10.6, 1.0, 1.0],\n [0.5, 100.5, 1.0, 1.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 0, 5]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_center_point_box_format')" + }, + { + "summary": "nonmaxsuppression_flipped_coordinates", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [1.0, 1.0, 0.0, 0.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, 0.9, 1.0, -0.1],\n [0.0, 10.0, 1.0, 11.0],\n [1.0, 10.1, 0.0, 11.1],\n [1.0, 101.0, 0.0, 100.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 0, 5]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_flipped_coordinates')" + }, + { + "summary": "nonmaxsuppression_identical_boxes", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_identical_boxes')" + }, + { + "summary": "nonmaxsuppression_limit_output_size", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([2]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_limit_output_size')" + }, + { + "summary": "nonmaxsuppression_single_box", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_single_box')" + }, + { + "summary": "nonmaxsuppression_suppress_by_IOU", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 0, 5]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_suppress_by_IOU')" + }, + { + "summary": "nonmaxsuppression_suppress_by_IOU_and_scores", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.4]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_suppress_by_IOU_and_scores')" + }, + { + "summary": "nonmaxsuppression_two_batches", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[[0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]],\n [[0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]],\n [[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([2]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [1, 0, 3], [1, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_two_batches')" + }, + { + "summary": "nonmaxsuppression_two_classes", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3],\n [0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([2]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 1, 3], [0, 1, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_two_classes')" + } + ] + }, + { + "name": "NonMaxSuppression", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Filter out boxes that have high intersection-over-union (IOU) overlap with previously selected boxes.\nBounding boxes with score less than score_threshold are removed. Bounding box format is indicated by attribute center_point_box.\nNote that this algorithm is agnostic to where the origin is in the coordinate system and more generally is invariant to\northogonal transformations and translations of the coordinate system; thus translating or reflections of the coordinate system\nresult in the same boxes being selected by the algorithm.\nThe selected_indices output is a set of integers indexing into the input collection of bounding boxes representing the selected boxes.\nThe bounding box coordinates corresponding to the selected indices can then be obtained using the Gather or GatherND operation.\n", + "attributes": [ + { + "name": "center_point_box", + "type": "int64", + "required": false, + "description": "Integer indicate the format of the box data. The default is 0. 0 - the box data is supplied as [y1, x1, y2, x2] where (y1, x1) and (y2, x2) are the coordinates of any diagonal pair of box corners and the coordinates can be provided as normalized (i.e., lying in the interval [0, 1]) or absolute. Mostly used for TF models. 1 - the box data is supplied as [x_center, y_center, width, height]. Mostly used for Pytorch models." + } + ], + "inputs": [ + { + "name": "boxes", + "type": "tensor(float)", + "description": "An input tensor with shape [num_batches, spatial_dimension, 4]. The single box data format is indicated by center_point_box." + }, + { + "name": "scores", + "type": "tensor(float)", + "description": "An input tensor with shape [num_batches, num_classes, spatial_dimension]" + }, + { + "name": "max_output_boxes_per_class", + "type": "tensor(int64)", + "option": "optional", + "description": "Integer representing the maximum number of boxes to be selected per batch per class. It is a scalar. Default to 0, which means no output." + }, + { + "name": "iou_threshold", + "type": "tensor(float)", + "option": "optional", + "description": "Float representing the threshold for deciding whether boxes overlap too much with respect to IOU. It is scalar. Value range [0, 1]. Default to 0." + }, + { + "name": "score_threshold", + "type": "tensor(float)", + "option": "optional", + "description": "Float representing the threshold for deciding when to remove boxes based on score. It is a scalar." + } + ], + "min_input": 2, + "max_input": 5, + "outputs": [ + { + "name": "selected_indices", + "type": "tensor(int64)", + "description": "selected indices from the boxes tensor. [num_selected_indices, 3], the selected index format is [batch_index, class_index, box_index]." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 5", + "examples": [ + { + "summary": "nonmaxsuppression_center_point_box_format", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices'],\n center_point_box=1\n)\nboxes = np.array([[\n [0.5, 0.5, 1.0, 1.0],\n [0.5, 0.6, 1.0, 1.0],\n [0.5, 0.4, 1.0, 1.0],\n [0.5, 10.5, 1.0, 1.0],\n [0.5, 10.6, 1.0, 1.0],\n [0.5, 100.5, 1.0, 1.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 0, 5]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_center_point_box_format')" + }, + { + "summary": "nonmaxsuppression_flipped_coordinates", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [1.0, 1.0, 0.0, 0.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, 0.9, 1.0, -0.1],\n [0.0, 10.0, 1.0, 11.0],\n [1.0, 10.1, 0.0, 11.1],\n [1.0, 101.0, 0.0, 100.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 0, 5]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_flipped_coordinates')" + }, + { + "summary": "nonmaxsuppression_identical_boxes", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.0, 1.0, 1.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_identical_boxes')" + }, + { + "summary": "nonmaxsuppression_limit_output_size", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([2]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_limit_output_size')" + }, + { + "summary": "nonmaxsuppression_single_box", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_single_box')" + }, + { + "summary": "nonmaxsuppression_suppress_by_IOU", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 0, 5]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_suppress_by_IOU')" + }, + { + "summary": "nonmaxsuppression_suppress_by_IOU_and_scores", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.4]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_suppress_by_IOU_and_scores')" + }, + { + "summary": "nonmaxsuppression_two_batches", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[[0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]],\n [[0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]],\n [[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([2]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [1, 0, 3], [1, 0, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_two_batches')" + }, + { + "summary": "nonmaxsuppression_two_classes", + "code": "node = onnx.helper.make_node(\n 'NonMaxSuppression',\n inputs=['boxes', 'scores', 'max_output_boxes_per_class', 'iou_threshold', 'score_threshold'],\n outputs=['selected_indices']\n)\nboxes = np.array([[\n [0.0, 0.0, 1.0, 1.0],\n [0.0, 0.1, 1.0, 1.1],\n [0.0, -0.1, 1.0, 0.9],\n [0.0, 10.0, 1.0, 11.0],\n [0.0, 10.1, 1.0, 11.1],\n [0.0, 100.0, 1.0, 101.0]\n]]).astype(np.float32)\nscores = np.array([[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3],\n [0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([2]).astype(np.int64)\niou_threshold = np.array([0.5]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\nselected_indices = np.array([[0, 0, 3], [0, 0, 0], [0, 1, 3], [0, 1, 0]]).astype(np.int64)\n\nexpect(node, inputs=[boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold], outputs=[selected_indices], name='test_nonmaxsuppression_two_classes')" + } + ] + }, + { + "name": "NonZero", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Returns the indices of the elements that are non-zero\n (in row-major order - by dimension).\n NonZero behaves similar to numpy.nonzero:\n https://docs.scipy.org/doc/numpy/reference/generated/numpy.nonzero.html\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "input" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(int64)", + "description": "output" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "nonzero", + "code": "node = onnx.helper.make_node(\n 'NonZero',\n inputs=['condition'],\n outputs=['result'],\n)\n\ncondition = np.array([[1, 0], [1, 1]], dtype=bool)\nresult = np.array(np.nonzero(condition), dtype=np.int64) # expected output [[0, 1, 1], [0, 0, 1]]\nexpect(node, inputs=[condition], outputs=[result],\n name='test_nonzero_example')" + } + ] + }, + { + "name": "NonZero", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Returns the indices of the elements that are non-zero\n (in row-major order - by dimension).\n NonZero behaves similar to numpy.nonzero:\n https://docs.scipy.org/doc/numpy/reference/generated/numpy.nonzero.html\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "input" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(int64)", + "description": "output" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "nonzero", + "code": "node = onnx.helper.make_node(\n 'NonZero',\n inputs=['condition'],\n outputs=['result'],\n)\n\ncondition = np.array([[1, 0], [1, 1]], dtype=bool)\nresult = np.array(np.nonzero(condition), dtype=np.int64) # expected output [[0, 1, 1], [0, 0, 1]]\nexpect(node, inputs=[condition], outputs=[result],\n name='test_nonzero_example')" + } + ] + }, + { + "name": "Normalizer", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Normalize the input. There are three normalization modes, which have the corresponding formulas,\n defined using element-wise infix operators '/' and '^' and tensor-wide functions 'max' and 'sum':
\n
\n Max: Y = X / max(X)
\n L1: Y = X / sum(X)
\n L2: Y = sqrt(X^2 / sum(X^2)}
\n In all modes, if the divisor is zero, Y == X.\n
\n For batches, that is, [N,C] tensors, normalization is done along the C axis. In other words, each row\n of the batch is normalized independently.\n", + "attributes": [ + { + "name": "norm", + "type": "string", + "required": false, + "default": "MAX", + "description": "One of 'MAX,' 'L1,' 'L2'" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Data to be encoded, a tensor of shape [N,C] or [C]" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(float)", + "description": "Encoded output data" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input must be a tensor of a numeric type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + } + ] + }, + { + "name": "Not", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Returns the negation of the input tensor element-wise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input/output to boolean tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "not", + "code": "node = onnx.helper.make_node(\n 'Not',\n inputs=['x'],\n outputs=['not'],\n)\n\n# 2d\nx = (np.random.randn(3, 4) > 0).astype(bool)\nexpect(node, inputs=[x], outputs=[np.logical_not(x)],\n name='test_not_2d')\n\n# 3d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\nexpect(node, inputs=[x], outputs=[np.logical_not(x)],\n name='test_not_3d')\n\n# 4d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\nexpect(node, inputs=[x], outputs=[np.logical_not(x)],\n name='test_not_4d')" + } + ], + "category": "Logic" + }, + { + "name": "OneHot", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Produces a one-hot tensor based on inputs.\n The locations represented by the index values in the 'indices' input tensor will have 'on_value'\n and the other locations will have 'off_value' in the output tensor, where 'on_value' and 'off_value'\n are specified as part of required input argument 'values', which is a two-element tensor of format\n [off_value, on_value]. The rank of the output tensor will be one greater than the rank of the\n input tensor. The additional dimension is for one-hot representation. The additional dimension will\n be inserted at the position specified by 'axis'. If 'axis' is not specified then then additional\n dimension will be inserted as the innermost dimension, i.e. axis=-1. The size of the additional\n dimension is specified by required scalar input 'depth'. The type of the output tensor is the same\n as the type of the 'values' input. Any entries in the 'indices' input tensor with values outside\n the range [0, depth) will result in one-hot representation with all 'off_value' values in the\n output tensor.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": -1, + "description": "(Optional) Axis along which one-hot representation in added. Default: axis=-1. axis=-1 means that the additional dimension will be inserted as the innermost/last dimension in the output tensor." + } + ], + "inputs": [ + { + "name": "indices", + "type": "T1", + "description": "Input tensor containing indices. The values must be non-negative integers. Any entries in the 'indices' input tensor with values outside the range [0, depth) will result in one-hot representation with all 'off_value' values in the output tensor.In case 'indices' is of non-integer type, the values will be casted to int64 before use." + }, + { + "name": "depth", + "type": "T2", + "description": "Scalar specifying the number of classes in one-hot tensor. This is also the size of the one-hot dimension (specified by 'axis' attribute) added on in the output tensor. The values in the 'indices' input tensor are expected to be in the range [0, depth). In case 'depth' is of non-integer type, it will be casted to int64 before use." + }, + { + "name": "values", + "type": "T3", + "description": "Rank 1 tensor containing exactly two elements, in the format [off_value, on_value], where 'on_value' is the value used for filling locations specified in 'indices' input tensor, and 'off_value' is the value used for filling locations other than those specified in 'indices' input tensor. " + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T3", + "description": "Tensor of rank one greater than input tensor 'indices', i.e. rank(output) = rank(indices) + 1. The data type for the elements of the output tensor is the same as the type of input 'values' is used." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to only numeric types.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain input to only numeric types.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain to any tensor type.", + "type_param_str": "T3", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "with_axis", + "code": "axisValue = 1\non_value = 3\noff_value = 1\noutput_type = np.float32\nnode = onnx.helper.make_node(\n 'OneHot',\n inputs=['indices', 'depth', 'values'],\n outputs=['y'],\n axis=axisValue\n)\nindices = np.array([[1, 9],\n [2, 4]], dtype=np.float32)\ndepth = np.float32(10)\nvalues = np.array([off_value, on_value], dtype=output_type)\ny = one_hot(indices, depth, axis=axisValue, dtype=output_type)\ny = y * (on_value - off_value) + off_value\nexpect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_with_axis')" + }, + { + "summary": "with_negative_axis", + "code": "axisValue = -2\non_value = 3\noff_value = 1\noutput_type = np.float32\nnode = onnx.helper.make_node(\n 'OneHot',\n inputs=['indices', 'depth', 'values'],\n outputs=['y'],\n axis=axisValue\n)\nindices = np.array([[1, 9],\n [2, 4]], dtype=np.float32)\ndepth = np.float32(10)\nvalues = np.array([off_value, on_value], dtype=output_type)\ny = one_hot(indices, depth, axis=axisValue, dtype=output_type)\ny = y * (on_value - off_value) + off_value\nexpect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_with_negative_axis')" + }, + { + "summary": "with_negative_indices", + "code": "axisValue = 1\non_value = 3\noff_value = 1\noutput_type = np.float32\nnode = onnx.helper.make_node(\n 'OneHot',\n inputs=['indices', 'depth', 'values'],\n outputs=['y'],\n axis=axisValue\n)\nindices = np.array([0, -7, -8], dtype=np.int64)\n\n# print(y)\n# [[3. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n# [1. 1. 1. 3. 1. 1. 1. 1. 1. 1.]\n# [1. 1. 3. 1. 1. 1. 1. 1. 1. 1.]]\n\ndepth = np.float32(10)\nvalues = np.array([off_value, on_value], dtype=output_type)\ny = one_hot(indices, depth, axis=axisValue, dtype=output_type)\ny = y * (on_value - off_value) + off_value\nexpect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_negative_indices')" + }, + { + "summary": "without_axis", + "code": "on_value = 5\noff_value = 2\noutput_type = np.int32\nnode = onnx.helper.make_node(\n 'OneHot',\n inputs=['indices', 'depth', 'values'],\n outputs=['y']\n)\nindices = np.array([0, 7, 8], dtype=np.int64)\ndepth = np.float32(12)\nvalues = np.array([off_value, on_value], dtype=output_type)\ny = one_hot(indices, depth, dtype=output_type)\ny = y * (on_value - off_value) + off_value\nexpect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_without_axis')" + } + ] + }, + { + "name": "OneHot", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Produces a one-hot tensor based on inputs.\n The locations represented by the index values in the 'indices' input tensor will have 'on_value'\n and the other locations will have 'off_value' in the output tensor, where 'on_value' and 'off_value'\n are specified as part of required input argument 'values', which is a two-element tensor of format\n [off_value, on_value]. The rank of the output tensor will be one greater than the rank of the\n input tensor. The additional dimension is for one-hot representation. The additional dimension will\n be inserted at the position specified by 'axis'. If 'axis' is not specified then then additional\n dimension will be inserted as the innermost dimension, i.e. axis=-1. The size of the additional\n dimension is specified by required scalar input 'depth'. The type of the output tensor is the same\n as the type of the 'values' input. Any entries in the 'indices' input tensor with values outside\n the range [-depth, depth-1] will result in one-hot representation with all 'off_value' values in the\n output tensor.\n\n when axis = 0:\n output[input[i, j, k], i, j, k] = 1 for all i, j, k and 0 otherwise.\n\n when axis = -1:\n output[i, j, k, input[i, j, k]] = 1 for all i, j, k and 0 otherwise.\n\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": -1, + "description": "(Optional) Axis along which one-hot representation in added. Default: axis=-1. axis=-1 means that the additional dimension will be inserted as the innermost/last dimension in the output tensor. Negative value means counting dimensions from the back. Accepted range is [-r-1, r] where r = rank(indices)." + } + ], + "inputs": [ + { + "name": "indices", + "type": "T1", + "description": "Input tensor containing indices. Any entries in the 'indices' input tensor with values outside the range [-depth, depth-1] will result in one-hot representation with all 'off_value' values in the output tensor.In case 'indices' is of non-integer type, the values will be casted to int64 before use." + }, + { + "name": "depth", + "type": "T2", + "description": "Scalar specifying the number of classes in one-hot tensor. This is also the size of the one-hot dimension (specified by 'axis' attribute) added on in the output tensor. The values in the 'indices' input tensor are expected to be in the range [-depth, depth-1]. In case 'depth' is of non-integer type, it will be casted to int64 before use." + }, + { + "name": "values", + "type": "T3", + "description": "Rank 1 tensor containing exactly two elements, in the format [off_value, on_value], where 'on_value' is the value used for filling locations specified in 'indices' input tensor, and 'off_value' is the value used for filling locations other than those specified in 'indices' input tensor. " + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T3", + "description": "Tensor of rank one greater than input tensor 'indices', i.e. rank(output) = rank(indices) + 1. The data type for the elements of the output tensor is the same as the type of input 'values' is used." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to only numeric types.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain input to only numeric types.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain to any tensor type.", + "type_param_str": "T3", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "with_axis", + "code": "axisValue = 1\non_value = 3\noff_value = 1\noutput_type = np.float32\nnode = onnx.helper.make_node(\n 'OneHot',\n inputs=['indices', 'depth', 'values'],\n outputs=['y'],\n axis=axisValue\n)\nindices = np.array([[1, 9],\n [2, 4]], dtype=np.float32)\ndepth = np.float32(10)\nvalues = np.array([off_value, on_value], dtype=output_type)\ny = one_hot(indices, depth, axis=axisValue, dtype=output_type)\ny = y * (on_value - off_value) + off_value\nexpect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_with_axis')" + }, + { + "summary": "with_negative_axis", + "code": "axisValue = -2\non_value = 3\noff_value = 1\noutput_type = np.float32\nnode = onnx.helper.make_node(\n 'OneHot',\n inputs=['indices', 'depth', 'values'],\n outputs=['y'],\n axis=axisValue\n)\nindices = np.array([[1, 9],\n [2, 4]], dtype=np.float32)\ndepth = np.float32(10)\nvalues = np.array([off_value, on_value], dtype=output_type)\ny = one_hot(indices, depth, axis=axisValue, dtype=output_type)\ny = y * (on_value - off_value) + off_value\nexpect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_with_negative_axis')" + }, + { + "summary": "with_negative_indices", + "code": "axisValue = 1\non_value = 3\noff_value = 1\noutput_type = np.float32\nnode = onnx.helper.make_node(\n 'OneHot',\n inputs=['indices', 'depth', 'values'],\n outputs=['y'],\n axis=axisValue\n)\nindices = np.array([0, -7, -8], dtype=np.int64)\n\n# print(y)\n# [[3. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n# [1. 1. 1. 3. 1. 1. 1. 1. 1. 1.]\n# [1. 1. 3. 1. 1. 1. 1. 1. 1. 1.]]\n\ndepth = np.float32(10)\nvalues = np.array([off_value, on_value], dtype=output_type)\ny = one_hot(indices, depth, axis=axisValue, dtype=output_type)\ny = y * (on_value - off_value) + off_value\nexpect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_negative_indices')" + }, + { + "summary": "without_axis", + "code": "on_value = 5\noff_value = 2\noutput_type = np.int32\nnode = onnx.helper.make_node(\n 'OneHot',\n inputs=['indices', 'depth', 'values'],\n outputs=['y']\n)\nindices = np.array([0, 7, 8], dtype=np.int64)\ndepth = np.float32(12)\nvalues = np.array([off_value, on_value], dtype=output_type)\ny = one_hot(indices, depth, dtype=output_type)\ny = y * (on_value - off_value) + off_value\nexpect(node, inputs=[indices, depth, values], outputs=[y], name='test_onehot_without_axis')" + } + ] + }, + { + "name": "OneHotEncoder", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Replace each input element with an array of ones and zeros, where a single\n one is placed at the index of the category that was passed in. The total category count\n will determine the size of the extra dimension of the output array Y.
\n For example, if we pass a tensor with a single value of 4, and a category count of 8,\n the output will be a tensor with ``[0,0,0,0,1,0,0,0]``.
\n This operator assumes every input feature is from the same set of categories.
\n If the input is a tensor of float, int32, or double, the data will be cast\n to integers and the cats_int64s category list will be used for the lookups.\n", + "attributes": [ + { + "name": "cats_int64s", + "type": "int64[]", + "required": false, + "description": "List of categories, ints.
One and only one of the 'cats_*' attributes must be defined." + }, + { + "name": "cats_strings", + "type": "string[]", + "required": false, + "description": "List of categories, strings.
One and only one of the 'cats_*' attributes must be defined." + }, + { + "name": "zeros", + "type": "int64", + "required": false, + "default": 1, + "description": "If true and category is not present, will return all zeros; if false and a category if not found, the operator will fail." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Data to be encoded." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(float)", + "description": "Encoded output data, having one more dimension than X." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input must be a tensor of a numeric type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)", + "tensor(int32)", + "tensor(float)", + "tensor(double)" + ] + } + ] + }, + { + "name": "Optional", + "module": "ai.onnx", + "version": 15, + "support_level": "common", + "description": "Constructs an optional-type value containing either an empty optional of a certain type specified by the attribute,\nor a non-empty value containing the input element.\n", + "attributes": [ + { + "name": "type", + "required": false, + "description": "Type of the element in the optional output" + } + ], + "inputs": [ + { + "name": "input", + "type": "V", + "option": "optional", + "description": "The input element." + } + ], + "min_input": 0, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "O", + "description": "The optional output enclosing the input element." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "0 - 1", + "type_constraints": [ + { + "description": "Constrain input type to all tensor and sequence types.", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)", + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + }, + { + "description": "Constrain output type to all optional tensor or optional sequence types.", + "type_param_str": "O", + "allowed_type_strs": [ + "optional(seq(tensor(uint8)))", + "optional(seq(tensor(uint16)))", + "optional(seq(tensor(uint32)))", + "optional(seq(tensor(uint64)))", + "optional(seq(tensor(int8)))", + "optional(seq(tensor(int16)))", + "optional(seq(tensor(int32)))", + "optional(seq(tensor(int64)))", + "optional(seq(tensor(float16)))", + "optional(seq(tensor(float)))", + "optional(seq(tensor(double)))", + "optional(seq(tensor(string)))", + "optional(seq(tensor(bool)))", + "optional(seq(tensor(complex64)))", + "optional(seq(tensor(complex128)))", + "optional(tensor(uint8))", + "optional(tensor(uint16))", + "optional(tensor(uint32))", + "optional(tensor(uint64))", + "optional(tensor(int8))", + "optional(tensor(int16))", + "optional(tensor(int32))", + "optional(tensor(int64))", + "optional(tensor(float16))", + "optional(tensor(float))", + "optional(tensor(double))", + "optional(tensor(string))", + "optional(tensor(bool))", + "optional(tensor(complex64))", + "optional(tensor(complex128))" + ] + } + ] + }, + { + "name": "OptionalGetElement", + "module": "ai.onnx", + "version": 15, + "support_level": "common", + "description": "Outputs the element in the optional-type input. It is an error if the input value does not have an element\nand the behavior is undefined in this case.\n", + "inputs": [ + { + "name": "input", + "type": "O", + "description": "The optional input." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "V", + "description": "Output element in the optional input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input type to optional tensor and optional sequence types.", + "type_param_str": "O", + "allowed_type_strs": [ + "optional(seq(tensor(uint8)))", + "optional(seq(tensor(uint16)))", + "optional(seq(tensor(uint32)))", + "optional(seq(tensor(uint64)))", + "optional(seq(tensor(int8)))", + "optional(seq(tensor(int16)))", + "optional(seq(tensor(int32)))", + "optional(seq(tensor(int64)))", + "optional(seq(tensor(float16)))", + "optional(seq(tensor(float)))", + "optional(seq(tensor(double)))", + "optional(seq(tensor(string)))", + "optional(seq(tensor(bool)))", + "optional(seq(tensor(complex64)))", + "optional(seq(tensor(complex128)))", + "optional(tensor(uint8))", + "optional(tensor(uint16))", + "optional(tensor(uint32))", + "optional(tensor(uint64))", + "optional(tensor(int8))", + "optional(tensor(int16))", + "optional(tensor(int32))", + "optional(tensor(int64))", + "optional(tensor(float16))", + "optional(tensor(float))", + "optional(tensor(double))", + "optional(tensor(string))", + "optional(tensor(bool))", + "optional(tensor(complex64))", + "optional(tensor(complex128))" + ] + }, + { + "description": "Constrain output type to all tensor or sequence types.", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)", + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + } + ] + }, + { + "name": "OptionalHasElement", + "module": "ai.onnx", + "version": 15, + "support_level": "common", + "description": "Returns true if the optional-type input contains an element. If it is an empty optional-type, this op returns false.\n", + "inputs": [ + { + "name": "input", + "type": "O", + "description": "The optional input." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "B", + "description": "A scalar boolean tensor. If true, it indicates that optional-type input contains an element. Otherwise, it is empty." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input type to optional tensor and optional sequence types.", + "type_param_str": "O", + "allowed_type_strs": [ + "optional(seq(tensor(uint8)))", + "optional(seq(tensor(uint16)))", + "optional(seq(tensor(uint32)))", + "optional(seq(tensor(uint64)))", + "optional(seq(tensor(int8)))", + "optional(seq(tensor(int16)))", + "optional(seq(tensor(int32)))", + "optional(seq(tensor(int64)))", + "optional(seq(tensor(float16)))", + "optional(seq(tensor(float)))", + "optional(seq(tensor(double)))", + "optional(seq(tensor(string)))", + "optional(seq(tensor(bool)))", + "optional(seq(tensor(complex64)))", + "optional(seq(tensor(complex128)))", + "optional(tensor(uint8))", + "optional(tensor(uint16))", + "optional(tensor(uint32))", + "optional(tensor(uint64))", + "optional(tensor(int8))", + "optional(tensor(int16))", + "optional(tensor(int32))", + "optional(tensor(int64))", + "optional(tensor(float16))", + "optional(tensor(float))", + "optional(tensor(double))", + "optional(tensor(string))", + "optional(tensor(bool))", + "optional(tensor(complex64))", + "optional(tensor(complex128))" + ] + }, + { + "description": "Constrain output to a boolean tensor.", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "empty", + "code": "optional = None\ntensor_type_proto = onnx.helper.make_tensor_type_proto(elem_type=onnx.TensorProto.INT32, shape=[])\ninput_type_proto = onnx.helper.make_optional_type_proto(tensor_type_proto)\nnode = onnx.helper.make_node(\n 'OptionalHasElement',\n inputs=['optional_input'],\n outputs=['output']\n)\noutput = optional_has_element_reference_implementation(optional)\nexpect(node, inputs=[optional], outputs=[output],\n input_type_protos=[input_type_proto],\n name='test_optional_has_element_empty')" + }, + { + "summary": "get_element_sequence", + "code": "optional = [np.array([1, 2, 3, 4]).astype(np.int32)]\ntensor_type_proto = onnx.helper.make_tensor_type_proto(elem_type=onnx.TensorProto.INT32, shape=[4, ])\nseq_type_proto = onnx.helper.make_sequence_type_proto(tensor_type_proto)\ninput_type_proto = onnx.helper.make_optional_type_proto(seq_type_proto)\n\nnode = onnx.helper.make_node(\n 'OptionalGetElement',\n inputs=['optional_input'],\n outputs=['output']\n)\noutput = optional_get_element_reference_implementation(optional)\nexpect(node, inputs=[optional], outputs=[output],\n input_type_protos=[input_type_proto],\n name='test_optional_get_element_sequence')" + }, + { + "summary": "get_element_tensor", + "code": "optional = np.array([1, 2, 3, 4]).astype(np.float32)\ntensor_type_proto = onnx.helper.make_tensor_type_proto(elem_type=onnx.TensorProto.FLOAT, shape=[4, ])\ninput_type_proto = onnx.helper.make_optional_type_proto(tensor_type_proto)\n\nnode = onnx.helper.make_node(\n 'OptionalGetElement',\n inputs=['optional_input'],\n outputs=['output']\n)\noutput = optional_get_element_reference_implementation(optional)\nexpect(node, inputs=[optional], outputs=[output],\n input_type_protos=[input_type_proto],\n name='test_optional_get_element')" + }, + { + "summary": "optionalhaselement", + "code": "optional = np.array([1, 2, 3, 4]).astype(np.float32)\ntensor_type_proto = onnx.helper.make_tensor_type_proto(elem_type=onnx.TensorProto.FLOAT, shape=[4, ])\ninput_type_proto = onnx.helper.make_optional_type_proto(tensor_type_proto)\nnode = onnx.helper.make_node(\n 'OptionalHasElement',\n inputs=['optional_input'],\n outputs=['output']\n)\noutput = optional_has_element_reference_implementation(optional)\nexpect(node, inputs=[optional], outputs=[output],\n input_type_protos=[input_type_proto],\n name='test_optional_has_element')" + } + ] + }, + { + "name": "Or", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `or` logical operation\nelementwise on the input tensors `A` and `B`.\n\nIf broadcasting is enabled, the right-hand-side argument will be broadcasted\nto match the shape of left-hand-side argument. See the doc of `Add` for a\ndetailed description of the broadcasting rules.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Left input tensor for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Right input tensor for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to boolean tensor.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "or", + "code": "node = onnx.helper.make_node(\n 'Or',\n inputs=['x', 'y'],\n outputs=['or'],\n)\n\n# 2d\nx = (np.random.randn(3, 4) > 0).astype(bool)\ny = (np.random.randn(3, 4) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or2d')\n\n# 3d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or3d')\n\n# 4d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or4d')" + }, + { + "summary": "or_broadcast", + "code": "node = onnx.helper.make_node(\n 'Or',\n inputs=['x', 'y'],\n outputs=['or'],\n)\n\n# 3d vs 1d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(5) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast3v1d')\n\n# 3d vs 2d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(4, 5) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast3v2d')\n\n# 4d vs 2d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(5, 6) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast4v2d')\n\n# 4d vs 3d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(4, 5, 6) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast4v3d')\n\n# 4d vs 4d\nx = (np.random.randn(1, 4, 1, 6) > 0).astype(bool)\ny = (np.random.randn(3, 1, 5, 6) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast4v4d')" + } + ], + "category": "Logic" + }, + { + "name": "Or", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `or` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to boolean tensor.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "or", + "code": "node = onnx.helper.make_node(\n 'Or',\n inputs=['x', 'y'],\n outputs=['or'],\n)\n\n# 2d\nx = (np.random.randn(3, 4) > 0).astype(bool)\ny = (np.random.randn(3, 4) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or2d')\n\n# 3d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or3d')\n\n# 4d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or4d')" + }, + { + "summary": "or_broadcast", + "code": "node = onnx.helper.make_node(\n 'Or',\n inputs=['x', 'y'],\n outputs=['or'],\n)\n\n# 3d vs 1d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(5) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast3v1d')\n\n# 3d vs 2d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(4, 5) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast3v2d')\n\n# 4d vs 2d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(5, 6) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast4v2d')\n\n# 4d vs 3d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(4, 5, 6) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast4v3d')\n\n# 4d vs 4d\nx = (np.random.randn(1, 4, 1, 6) > 0).astype(bool)\ny = (np.random.randn(3, 1, 5, 6) > 0).astype(bool)\nz = np.logical_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_or_bcast4v4d')" + } + ], + "category": "Logic" + }, + { + "name": "PRelu", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "PRelu takes input data (Tensor) and slope tensor as input, and produces one\noutput data (Tensor) where the function `f(x) = slope * x for x < 0`,\n`f(x) = x for x >= 0`., is applied to the data tensor elementwise.\n\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + }, + { + "name": "slope", + "type": "T", + "description": "Slope tensor. If `Slope` is of size 1, the value is sharedacross different channels" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "prelu", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_example')" + }, + { + "summary": "prelu_broadcast", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_broadcast')" + } + ], + "category": "Activation" + }, + { + "name": "PRelu", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "PRelu takes input data (Tensor) and slope tensor as input, and produces one\noutput data (Tensor) where the function `f(x) = slope * x for x < 0`,\n`f(x) = x for x >= 0`., is applied to the data tensor elementwise.\n\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + }, + { + "name": "slope", + "type": "T", + "description": "Slope tensor. If `Slope` is of size 1, the value is sharedacross different channels" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "prelu", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_example')" + }, + { + "summary": "prelu_broadcast", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_broadcast')" + } + ], + "category": "Activation" + }, + { + "name": "PRelu", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "PRelu takes input data (Tensor) and slope tensor as input, and produces one\noutput data (Tensor) where the function `f(x) = slope * x for x < 0`,\n`f(x) = x for x >= 0`., is applied to the data tensor elementwise.\nThis operator supports **unidirectional broadcasting** (tensor slope should be unidirectional broadcastable to input tensor X); for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + }, + { + "name": "slope", + "type": "T", + "description": "Slope tensor. The shape of slope can be smaller then first input X; if so, its shape must be unidirectional broadcastable to X" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor (same size as X)" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "prelu", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_example')" + }, + { + "summary": "prelu_broadcast", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_broadcast')" + } + ], + "category": "Activation" + }, + { + "name": "PRelu", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "PRelu takes input data (Tensor) and slope tensor as input, and produces one\noutput data (Tensor) where the function `f(x) = slope * x for x < 0`,\n`f(x) = x for x >= 0`., is applied to the data tensor elementwise.\nThis operator supports **unidirectional broadcasting** (tensor slope should be unidirectional broadcastable to input tensor X); for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + }, + { + "name": "slope", + "type": "T", + "description": "Slope tensor. The shape of slope can be smaller then first input X; if so, its shape must be unidirectional broadcastable to X" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor (same size as X)" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "prelu", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_example')" + }, + { + "summary": "prelu_broadcast", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_broadcast')" + } + ], + "category": "Activation" + }, + { + "name": "PRelu", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "PRelu takes input data (Tensor) and slope tensor as input, and produces one\noutput data (Tensor) where the function `f(x) = slope * x for x < 0`,\n`f(x) = x for x >= 0`., is applied to the data tensor elementwise.\n\n**History**\n- Version 16 adds bfloat16 to the types allowed.\nThis operator supports **unidirectional broadcasting** (tensor slope should be unidirectional broadcastable to input tensor X); for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + }, + { + "name": "slope", + "type": "T", + "description": "Slope tensor. The shape of slope can be smaller then first input X; if so, its shape must be unidirectional broadcastable to X" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor (same size as X)" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "prelu", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_example')" + }, + { + "summary": "prelu_broadcast", + "code": "node = onnx.helper.make_node(\n 'PRelu',\n inputs=['x', 'slope'],\n outputs=['y'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\nslope = np.random.randn(5).astype(np.float32)\ny = np.clip(x, 0, np.inf) + np.clip(x, -np.inf, 0) * slope\n\nexpect(node, inputs=[x, slope], outputs=[y],\n name='test_prelu_broadcast')" + } + ], + "category": "Activation" + }, + { + "name": "Pad", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Given `data` tensor, paddings, mode, and value.\nExample:\n Insert 0 paddings to the beginning of the second dimension.\n data = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n paddings = [0, 0, 2, 0]\n output = [\n [\n [0.0, 0.0, 1.0, 1.2],\n [0.0, 0.0, 2.3, 3.4],\n [0.0, 0.0, 4.5, 5.7],\n ],\n ]\n", + "attributes": [ + { + "name": "mode", + "type": "string", + "required": false, + "default": "constant", + "description": "Three modes: constant(default), reflect, edge" + }, + { + "name": "paddings", + "type": "int64[]", + "required": true, + "description": "List of integers indicate the padding element count at the beginning and end of each axis, for 2D it is the number of pixel. `paddings` rank should be double of the input's rank. `paddings` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`." + }, + { + "name": "value", + "type": "float32", + "required": false, + "description": "One float, indicates the value to be filled, default is 0" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor after padding." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "constant_pad", + "code": "node = onnx.helper.make_node(\n 'Pad',\n inputs=['x', 'pads', 'value'],\n outputs=['y'],\n mode='constant'\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 0, 1, 3, 0, 0, 2, 4]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(\n x,\n pads,\n 'constant',\n 1.2\n)\n\nexpect(node, inputs=[x, pads, value], outputs=[y],\n name='test_constant_pad')" + }, + { + "summary": "reflection_and_edge_pad", + "code": "for mode in ['edge', 'reflect']:\n node = onnx.helper.make_node(\n 'Pad',\n inputs=['x', 'pads'],\n outputs=['y'],\n mode=mode\n )\n x = np.random.randn(1, 3, 4, 5).astype(np.int32)\n pads = np.array([0, 0, 1, 1, 0, 0, 1, 1]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(\n x,\n pads,\n mode\n )\n\n expect(node, inputs=[x, pads], outputs=[y],\n name='test_{}_pad'.format(mode))" + } + ], + "category": "Tensor" + }, + { + "name": "Pad", + "module": "ai.onnx", + "version": 2, + "support_level": "common", + "description": "Given `data` tensor, pads, mode, and value.\nExample:\n Insert 0 pads to the beginning of the second dimension.\n data = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n pads = [0, 2, 0, 0]\n output = [\n [\n [0.0, 0.0, 1.0, 1.2],\n [0.0, 0.0, 2.3, 3.4],\n [0.0, 0.0, 4.5, 5.7],\n ],\n ]\n", + "attributes": [ + { + "name": "mode", + "type": "string", + "required": false, + "default": "constant", + "description": "Three modes: constant(default), reflect, edge" + }, + { + "name": "pads", + "type": "int64[]", + "required": true, + "description": "List of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D it is the number of pixels. `pads` rank should be double of the input's rank. `pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number of pixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`." + }, + { + "name": "value", + "type": "float32", + "required": false, + "description": "One float, indicates the value to be filled." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor after padding." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "constant_pad", + "code": "node = onnx.helper.make_node(\n 'Pad',\n inputs=['x', 'pads', 'value'],\n outputs=['y'],\n mode='constant'\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 0, 1, 3, 0, 0, 2, 4]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(\n x,\n pads,\n 'constant',\n 1.2\n)\n\nexpect(node, inputs=[x, pads, value], outputs=[y],\n name='test_constant_pad')" + }, + { + "summary": "reflection_and_edge_pad", + "code": "for mode in ['edge', 'reflect']:\n node = onnx.helper.make_node(\n 'Pad',\n inputs=['x', 'pads'],\n outputs=['y'],\n mode=mode\n )\n x = np.random.randn(1, 3, 4, 5).astype(np.int32)\n pads = np.array([0, 0, 1, 1, 0, 0, 1, 1]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(\n x,\n pads,\n mode\n )\n\n expect(node, inputs=[x, pads], outputs=[y],\n name='test_{}_pad'.format(mode))" + } + ], + "category": "Tensor" + }, + { + "name": "Pad", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Given a tensor containing the data to be padded (`data`), a tensor containing the number of start and end pad values for axis (`pads`), (optionally) a `mode`, and (optionally) `constant_value`,\na padded tensor (`output`) is generated.\n\nThe three supported `modes` are (similar to corresponding modes supported by `numpy.pad`):\n\n1) `constant`(default) - pads with a given constant value as specified by `constant_value` (which defaults to 0)\n\n2) `reflect` - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis\n\n3) `edge` - pads with the edge values of array\n\n\nExample 1 (`constant` mode):\n Insert 0 pads to the beginning of the second dimension.\n\n data =\n [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n\n pads = [0, 2, 0, 0]\n\n mode = 'constant'\n\n constant_value = 0.0\n\n output =\n [\n [0.0, 0.0, 1.0, 1.2],\n [0.0, 0.0, 2.3, 3.4],\n [0.0, 0.0, 4.5, 5.7],\n ]\n\n\nExample 2 (`reflect` mode):\n data =\n [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n\n pads = [0, 2, 0, 0]\n\n mode = 'reflect'\n\n output =\n [\n [1.0, 1.2, 1.0, 1.2],\n [2.3, 3.4, 2.3, 3.4],\n [4.5, 5.7, 4.5, 5.7],\n ]\n\n\nExample 3 (`edge` mode):\n data =\n [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n\n pads = [0, 2, 0, 0]\n\n mode = 'edge'\n\n output =\n [\n [1.0, 1.0, 1.0, 1.2],\n [2.3, 2.3, 2.3, 3.4],\n [4.5, 4.5, 4.5, 5.7],\n ]\n\n", + "attributes": [ + { + "name": "mode", + "type": "string", + "required": false, + "default": "constant", + "description": "Supported modes: `constant`(default), `reflect`, `edge`" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Input tensor." + }, + { + "name": "pads", + "type": "tensor(int64)", + "description": "Tensor of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D input tensor, it is the number of pixels. `pads` should be a 1D tensor of shape [2 * input_rank]. `pads` format should be: [x1_begin, x2_begin,...,x1_end, x2_end,...], where xi_begin is the number of pad values added at the beginning of axis `i` and xi_end, the number of pad values added at the end of axis `i`." + }, + { + "name": "constant_value", + "type": "T", + "option": "optional", + "description": "(Optional) A scalar value to be used if the mode chosen is `constant` (by default it is 0)." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor after padding." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output to only numeric types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "constant_pad", + "code": "node = onnx.helper.make_node(\n 'Pad',\n inputs=['x', 'pads', 'value'],\n outputs=['y'],\n mode='constant'\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 0, 1, 3, 0, 0, 2, 4]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(\n x,\n pads,\n 'constant',\n 1.2\n)\n\nexpect(node, inputs=[x, pads, value], outputs=[y],\n name='test_constant_pad')" + }, + { + "summary": "reflection_and_edge_pad", + "code": "for mode in ['edge', 'reflect']:\n node = onnx.helper.make_node(\n 'Pad',\n inputs=['x', 'pads'],\n outputs=['y'],\n mode=mode\n )\n x = np.random.randn(1, 3, 4, 5).astype(np.int32)\n pads = np.array([0, 0, 1, 1, 0, 0, 1, 1]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(\n x,\n pads,\n mode\n )\n\n expect(node, inputs=[x, pads], outputs=[y],\n name='test_{}_pad'.format(mode))" + } + ], + "category": "Tensor" + }, + { + "name": "Pad", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Given a tensor containing the data to be padded (`data`), a tensor containing the number of start and end pad values for axis (`pads`), (optionally) a `mode`, and (optionally) `constant_value`,\na padded tensor (`output`) is generated.\n\nThe three supported `modes` are (similar to corresponding modes supported by `numpy.pad`):\n\n1) `constant`(default) - pads with a given constant value as specified by `constant_value` (which defaults to 0, empty string, or False)\n\n2) `reflect` - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis\n\n3) `edge` - pads with the edge values of array\n\n\nExample 1 (`constant` mode):\n Insert 0 pads to the beginning of the second dimension.\n\n data =\n [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n\n pads = [0, 2, 0, 0]\n\n mode = 'constant'\n\n constant_value = 0.0\n\n output =\n [\n [0.0, 0.0, 1.0, 1.2],\n [0.0, 0.0, 2.3, 3.4],\n [0.0, 0.0, 4.5, 5.7],\n ]\n\n\nExample 2 (`reflect` mode):\n data =\n [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n\n pads = [0, 2, 0, 0]\n\n mode = 'reflect'\n\n output =\n [\n [1.0, 1.2, 1.0, 1.2],\n [2.3, 3.4, 2.3, 3.4],\n [4.5, 5.7, 4.5, 5.7],\n ]\n\n\nExample 3 (`edge` mode):\n data =\n [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n ]\n\n pads = [0, 2, 0, 0]\n\n mode = 'edge'\n\n output =\n [\n [1.0, 1.0, 1.0, 1.2],\n [2.3, 2.3, 2.3, 3.4],\n [4.5, 4.5, 4.5, 5.7],\n ]\n\n", + "attributes": [ + { + "name": "mode", + "type": "string", + "required": false, + "default": "constant", + "description": "Supported modes: `constant`(default), `reflect`, `edge`" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Input tensor." + }, + { + "name": "pads", + "type": "tensor(int64)", + "description": "Tensor of integers indicating the number of padding elements to add or remove (if negative) at the beginning and end of each axis. For 2D input tensor, it is the number of pixels. `pads` should be a 1D tensor of shape [2 * input_rank]. `pads` format should be: [x1_begin, x2_begin,...,x1_end, x2_end,...], where xi_begin is the number of pad values added at the beginning of axis `i` and xi_end, the number of pad values added at the end of axis `i`." + }, + { + "name": "constant_value", + "type": "T", + "option": "optional", + "description": "(Optional) A scalar value to be used if the mode chosen is `constant` (by default it is 0, empty string or False)." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor after padding." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "constant_pad", + "code": "node = onnx.helper.make_node(\n 'Pad',\n inputs=['x', 'pads', 'value'],\n outputs=['y'],\n mode='constant'\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 0, 1, 3, 0, 0, 2, 4]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(\n x,\n pads,\n 'constant',\n 1.2\n)\n\nexpect(node, inputs=[x, pads, value], outputs=[y],\n name='test_constant_pad')" + }, + { + "summary": "reflection_and_edge_pad", + "code": "for mode in ['edge', 'reflect']:\n node = onnx.helper.make_node(\n 'Pad',\n inputs=['x', 'pads'],\n outputs=['y'],\n mode=mode\n )\n x = np.random.randn(1, 3, 4, 5).astype(np.int32)\n pads = np.array([0, 0, 1, 1, 0, 0, 1, 1]).astype(np.int64) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(\n x,\n pads,\n mode\n )\n\n expect(node, inputs=[x, pads], outputs=[y],\n name='test_{}_pad'.format(mode))" + } + ], + "category": "Tensor" + }, + { + "name": "Pow", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Pow takes input data (Tensor) and exponent Tensor, and\nproduces one output data (Tensor) where the function `f(x) = x^exponent`,\nis applied to the data tensor elementwise.\n\nIf necessary the right-hand-side argument will be broadcasted to match the\nshape of left-hand-side argument. When broadcasting is specified, the second\ntensor can either be of element size 1 (including a scalar tensor and any\ntensor with rank equal to or smaller than the first tensor), or having its\nshape as a contiguous subset of the first tensor's shape. The starting of the\nmutually equal shape is specified by the argument \"axis\", and if it is not set,\nsuffix matching is assumed. 1-dim expansion doesn't work yet.\n\nFor example, the following tensor shapes are supported (with broadcast=1):\n\n shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (5,)\n shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)\n shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1\n shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0\n\nAttribute `broadcast=1` needs to be passed to enable broadcasting.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions. See doc for details." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Pass 1 to enable broadcasting" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor of any shape, base of the exponent." + }, + { + "name": "Y", + "type": "T", + "description": "Input tensor of any shape broadcastable to X shape, the exponent component." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Z", + "type": "T", + "description": "Output tensor (same size as X)" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "pow", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_example')\n\nx = np.arange(60).reshape(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow')" + }, + { + "summary": "pow_broadcast", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array(2).astype(np.float32)\nz = pow(x, y) # expected output [1., 4., 9.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_scalar')\n\nnode = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\nx = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)\ny = np.array([1, 2, 3]).astype(np.float32)\n# expected output [[1, 4, 27], [4, 25, 216]]\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_array')" + }, + { + "summary": "types", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int64')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int32')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint64')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint32')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_int64')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_int32')" + } + ] + }, + { + "name": "Pow", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Pow takes input data (Tensor) and exponent Tensor, and\nproduces one output data (Tensor) where the function `f(x) = x^exponent`,\nis applied to the data tensor elementwise.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "First operand, base of the exponent." + }, + { + "name": "Y", + "type": "T", + "description": "Second operand, power of the exponent." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Z", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "pow", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_example')\n\nx = np.arange(60).reshape(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow')" + }, + { + "summary": "pow_broadcast", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array(2).astype(np.float32)\nz = pow(x, y) # expected output [1., 4., 9.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_scalar')\n\nnode = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\nx = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)\ny = np.array([1, 2, 3]).astype(np.float32)\n# expected output [[1, 4, 27], [4, 25, 216]]\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_array')" + }, + { + "summary": "types", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int64')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int32')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint64')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint32')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_int64')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_int32')" + } + ] + }, + { + "name": "Pow", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Pow takes input data (Tensor) and exponent Tensor, and\nproduces one output data (Tensor) where the function `f(x) = x^exponent`,\nis applied to the data tensor elementwise.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "First operand, base of the exponent." + }, + { + "name": "Y", + "type": "T1", + "description": "Second operand, power of the exponent." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Z", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input X and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain input Y types to float/int tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "pow", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_example')\n\nx = np.arange(60).reshape(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow')" + }, + { + "summary": "pow_broadcast", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array(2).astype(np.float32)\nz = pow(x, y) # expected output [1., 4., 9.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_scalar')\n\nnode = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\nx = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)\ny = np.array([1, 2, 3]).astype(np.float32)\n# expected output [[1, 4, 27], [4, 25, 216]]\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_array')" + }, + { + "summary": "types", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int64')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int32')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint64')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint32')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_int64')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_int32')" + } + ] + }, + { + "name": "Pow", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Pow takes input data (Tensor) and exponent Tensor, and\nproduces one output data (Tensor) where the function `f(x) = x^exponent`,\nis applied to the data tensor elementwise.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "First operand, base of the exponent." + }, + { + "name": "Y", + "type": "T1", + "description": "Second operand, power of the exponent." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Z", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input X and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain input Y types to float/int tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "pow", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_example')\n\nx = np.arange(60).reshape(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow')" + }, + { + "summary": "pow_broadcast", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array(2).astype(np.float32)\nz = pow(x, y) # expected output [1., 4., 9.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_scalar')\n\nnode = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\nx = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)\ny = np.array([1, 2, 3]).astype(np.float32)\n# expected output [[1, 4, 27], [4, 25, 216]]\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_array')" + }, + { + "summary": "types", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int64')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int32')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint64')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint32')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_int64')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_int32')" + } + ] + }, + { + "name": "Pow", + "module": "ai.onnx", + "version": 15, + "support_level": "common", + "description": "Pow takes input data (Tensor) and exponent Tensor, and\nproduces one output data (Tensor) where the function `f(x) = x^exponent`,\nis applied to the data tensor elementwise.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "First operand, base of the exponent." + }, + { + "name": "Y", + "type": "T1", + "description": "Second operand, power of the exponent." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Z", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input X and output types to float/int tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain input Y types to float/int tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "pow", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_example')\n\nx = np.arange(60).reshape(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow')" + }, + { + "summary": "pow_broadcast", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array(2).astype(np.float32)\nz = pow(x, y) # expected output [1., 4., 9.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_scalar')\n\nnode = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\nx = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)\ny = np.array([1, 2, 3]).astype(np.float32)\n# expected output [[1, 4, 27], [4, 25, 216]]\nz = pow(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_bcast_array')" + }, + { + "summary": "types", + "code": "node = onnx.helper.make_node(\n 'Pow',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int64')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_int32')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.float32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_float32')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint64)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint64')\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([4, 5, 6]).astype(np.uint32)\nz = pow(x, y) # expected output [1., 32., 729.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_float32_uint32')\n\nx = np.array([1, 2, 3]).astype(np.int64)\ny = np.array([4, 5, 6]).astype(np.int64)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int64_int64')\n\nx = np.array([1, 2, 3]).astype(np.int32)\ny = np.array([4, 5, 6]).astype(np.int32)\nz = pow(x, y) # expected output [1, 32, 729]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_pow_types_int32_int32')" + } + ] + }, + { + "name": "QLinearConv", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "The convolution operator consumes a quantized input tensor, its scale and zero point,\na quantized filter, its scale and zero point, and output's scale and zero point,\nand computes the quantized output. Each scale and zero-point pair must have same shape.\nIt means they must be either scalars (per tensor) or 1-D tensors (per output channel).\nEach input or output and its related zero point must have same type.\nWhen bias is present it must be quantized using scale = input scale * weight scale and\nzero point as 0.\n", + "attributes": [ + { + "name": "auto_pad", + "type": "string", + "required": false, + "default": "NOTSET", + "description": "auto_pad must be either NOTSET, SAME_UPPER, SAME_LOWER or VALID. Where default value is NOTSET, which means explicit padding is used. SAME_UPPER or SAME_LOWER mean pad the input so that `output_shape[i] = ceil(input_shape[i] / strides[i])` for each axis `i`. The padding is split between the two sides equally or almost equally (depending on whether it is even or odd). In case the padding is an odd number, the extra padding is added at the end for SAME_UPPER and at the beginning for SAME_LOWER." + }, + { + "name": "dilations", + "type": "int64[]", + "required": false, + "description": "dilation value along each spatial axis of the filter. If not present, the dilation defaults to 1 along each spatial axis." + }, + { + "name": "group", + "type": "int64", + "required": false, + "default": 1, + "description": "number of groups input channels and output channels are divided into. default is 1." + }, + { + "name": "kernel_shape", + "type": "int64[]", + "required": false, + "description": "The shape of the convolution kernel. If not present, should be inferred from input 'w'." + }, + { + "name": "pads", + "type": "int64[]", + "required": false, + "description": "Padding for the beginning and ending along each spatial axis, it can take any value greater than or equal to 0.The value represent the number of pixels added to the beginning and end part of the corresponding axis.`pads` format should be as follow [x1_begin, x2_begin...x1_end, x2_end,...], where xi_begin the number ofpixels added at the beginning of axis `i` and xi_end, the number of pixels added at the end of axis `i`.This attribute cannot be used simultaneously with auto_pad attribute. If not present, the padding defaultsto 0 along start and end of each spatial axis." + }, + { + "name": "strides", + "type": "int64[]", + "required": false, + "description": "Stride along each spatial axis. If not present, the stride defaults to 1 along each spatial axis." + } + ], + "inputs": [ + { + "name": "x", + "type": "T1", + "description": "Input data tensor from previous layer; has size (N x C x H x W), where N is the batch size, C is the number of channels, and H and W are the height and width. Note that this is for the 2D image. Otherwise the size is (N x C x D1 x D2 ... x Dn). Optionally, if dimension denotation is in effect, the operation expects input data tensor to arrive with the dimension denotation of [DATA_BATCH, DATA_CHANNEL, DATA_FEATURE, DATA_FEATURE ...]." + }, + { + "name": "x_scale", + "type": "tensor(float)", + "description": "Scale tensor for input 'x'. It's a scalar, which means a per-tensor/layer quantization." + }, + { + "name": "x_zero_point", + "type": "T1", + "description": "Zero point tensor for input 'x'. It's a scalar, which means a per-tensor/layer quantization." + }, + { + "name": "w", + "type": "T2", + "description": "The weight tensor that will be used in the convolutions; has size (M x C/group x kH x kW), where C is the number of channels, and kH and kW are the height and width of the kernel, and M is the number of feature maps. For more than 2 dimensions, the kernel shape will be (M x C/group x k1 x k2 x ... x kn), where (k1 x k2 x ... kn) is the dimension of the kernel. Optionally, if dimension denotation is in effect, the operation expects the weight tensor to arrive with the dimension denotation of [FILTER_OUT_CHANNEL, FILTER_IN_CHANNEL, FILTER_SPATIAL, FILTER_SPATIAL ...]. X.shape[1] == (W.shape[1] * group) == C (assuming zero based indices for the shape array). Or in other words FILTER_IN_CHANNEL should be equal to DATA_CHANNEL. " + }, + { + "name": "w_scale", + "type": "tensor(float)", + "description": "Scale tensor for input 'w'. It could be a scalar or a 1-D tensor, which means a per-tensor/layer or per output channel quantization. If it's a 1-D tensor, its number of elements should be equal to the number of output channels (M)." + }, + { + "name": "w_zero_point", + "type": "T2", + "description": "Zero point tensor for input 'w'. It could be a scalar or a 1-D tensor, which means a per-tensor/layer or per output channel quantization. If it's a 1-D tensor, its number of elements should be equal to the number of output channels (M)." + }, + { + "name": "y_scale", + "type": "tensor(float)", + "description": "Scale tensor for output 'y'. It's a scalar, which means a per-tensor/layer quantization." + }, + { + "name": "y_zero_point", + "type": "T3", + "description": "Zero point tensor for output 'y'. It's a scalar, which means a per-tensor/layer quantization." + }, + { + "name": "B", + "type": "T4", + "option": "optional", + "description": "Optional 1D bias to be added to the convolution, has size of M. Bias must be quantized using scale = x_scale * w_scale and zero_point = 0" + } + ], + "min_input": 8, + "max_input": 9, + "outputs": [ + { + "name": "y", + "type": "T3", + "description": "Output data tensor that contains the result of the convolution. The output dimensions are functions of the kernel size, stride size, and pad lengths." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "8 - 9", + "type_constraints": [ + { + "description": "Constrain input type to 8-bit integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain filter type to 8-bit integer tensor.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain output type to 8-bit integer tensor.", + "type_param_str": "T3", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain bias type to 32-bit integer tensor.", + "type_param_str": "T4", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "qlinearconv", + "code": "node = onnx.helper.make_node('QLinearConv',\n inputs=['x', 'x_scale', 'x_zero_point', 'w', 'w_scale', 'w_zero_point', 'y_scale', 'y_zero_point'],\n outputs=['y'],)\n\nx = np.array([[255, 174, 162, 25, 203, 168, 58],\n [15, 59, 237, 95, 129, 0, 64],\n [56, 242, 153, 221, 168, 12, 166],\n [232, 178, 186, 195, 237, 162, 237],\n [188, 39, 124, 77, 80, 102, 43],\n [127, 230, 21, 83, 41, 40, 134],\n [255, 154, 92, 141, 42, 148, 247], ], dtype=np.uint8).reshape((1, 1, 7, 7))\n\nx_scale = np.float32(0.00369204697)\nx_zero_point = np.uint8(132)\n\nw = np.array([0], dtype=np.uint8).reshape((1, 1, 1, 1))\n\nw_scale = np.array([0.00172794575], dtype=np.float32)\nw_zero_point = np.array([255], dtype=np.uint8)\n\ny_scale = np.float32(0.00162681262)\ny_zero_point = np.uint8(123)\n\noutput = np.array([[0, 81, 93, 230, 52, 87, 197],\n [240, 196, 18, 160, 126, 255, 191],\n [199, 13, 102, 34, 87, 243, 89],\n [23, 77, 69, 60, 18, 93, 18],\n [67, 216, 131, 178, 175, 153, 212],\n [128, 25, 234, 172, 214, 215, 121],\n [0, 101, 163, 114, 213, 107, 8], ], dtype=np.uint8).reshape((1, 1, 7, 7))\n\nexpect(node, inputs=[x, x_scale, x_zero_point, w, w_scale, w_zero_point, y_scale, y_zero_point], outputs=[output],\n name='test_qlinearconv')" + } + ] + }, + { + "name": "QLinearMatMul", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Matrix product that behaves like numpy.matmul: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matmul.html.\nIt consumes two quantized input tensors, their scales and zero points, scale and zero point of output, \nand computes the quantized output. The quantization formula is y = saturate((x / y_scale) + y_zero_point). \nFor (x / y_scale), it is rounding to nearest ties to even. Refer to https://en.wikipedia.org/wiki/Rounding for details. \nScale and zero point must have same shape. They must be either scalar (per tensor) or N-D tensor \n(per row for 'a' and per column for 'b'). Scalar refers to per tensor quantization whereas N-D refers to per row \nor per column quantization. If the input is 2D of shape [M, K] then zero point and scale tensor may be \nan M element vector [v_1, v_2, ..., v_M] for per row quantization and K element vector of shape [v_1, v_2, ..., v_K] \nfor per column quantization. If the input is N-D tensor with shape [D1, D2, M, K] then zero point and scale tensor may \nhave shape [D1, D2, M, 1] for per row quantization and shape [D1, D2, 1, K] for per column quantization.\nProduction must never overflow, and accumulation may overflow if and only if in 32 bits.\n", + "inputs": [ + { + "name": "a", + "type": "T1", + "description": "N-dimensional quantized matrix a" + }, + { + "name": "a_scale", + "type": "tensor(float)", + "description": "scale of quantized input a" + }, + { + "name": "a_zero_point", + "type": "T1", + "description": "zero point of quantized input a" + }, + { + "name": "b", + "type": "T2", + "description": "N-dimensional quantized matrix b" + }, + { + "name": "b_scale", + "type": "tensor(float)", + "description": "scale of quantized input b" + }, + { + "name": "b_zero_point", + "type": "T2", + "description": "zero point of quantized input b" + }, + { + "name": "y_scale", + "type": "tensor(float)", + "description": "scale of quantized output y" + }, + { + "name": "y_zero_point", + "type": "T3", + "description": "zero point of quantized output y" + } + ], + "min_input": 8, + "max_input": 8, + "outputs": [ + { + "name": "y", + "type": "T3", + "description": "Quantized matrix multiply results from a * b" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input a and its zero point data type to 8-bit integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain input b and its zero point data type to 8-bit integer tensor.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + }, + { + "description": "Constrain output y and its zero point data type to 8-bit integer tensor.", + "type_param_str": "T3", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + } + ], + "examples": [ + { + "summary": "qlinearmatmul", + "code": "node = onnx.helper.make_node('QLinearMatMul',\n inputs=['a', 'a_scale', 'a_zero_point', 'b', 'b_scale', 'b_zero_point', 'y_scale', 'y_zero_point'],\n outputs=['y'],)\n\n#2D\na = np.array([[208, 236, 0, 238],\n [3, 214, 255, 29], ], dtype=np.uint8)\n\na_scale = np.array([0.0066], dtype=np.float32)\na_zero_point = np.array([113], dtype=np.uint8)\n\nb = np.array([[152, 51, 244],\n [60, 26, 255],\n [0, 127, 246],\n [127, 254, 247]], dtype=np.uint8)\n\nb_scale = np.array([0.00705], dtype=np.float32)\nb_zero_point = np.array([114], dtype=np.uint8)\n\ny_scale = np.array([0.0107], dtype=np.float32)\ny_zero_point = np.array([118], dtype=np.uint8)\n\noutput = np.array([[168, 115, 255],\n [1, 66, 151], ], dtype=np.uint8)\n\nexpect(node, inputs=[a, a_scale, a_zero_point, b, b_scale, b_zero_point, y_scale, y_zero_point], outputs=[output],\n name='test_qlinearmatmul_2D')\n\n#3D\na = np.array([[[208, 236, 0, 238],\n [3, 214, 255, 29]],\n [[208, 236, 0, 238],\n [3, 214, 255, 29]]], dtype=np.uint8)\n\na_scale = np.array([0.0066], dtype=np.float32)\na_zero_point = np.array([113], dtype=np.uint8)\n\nb = np.array([[[152, 51, 244],\n [60, 26, 255],\n [0, 127, 246],\n [127, 254, 247]],\n [[152, 51, 244],\n [60, 26, 255],\n [0, 127, 246],\n [127, 254, 247]]], dtype=np.uint8)\n\nb_scale = np.array([0.00705], dtype=np.float32)\nb_zero_point = np.array([114], dtype=np.uint8)\n\ny_scale = np.array([0.0107], dtype=np.float32)\ny_zero_point = np.array([118], dtype=np.uint8)\n\noutput = np.array([[[168, 115, 255],\n [1, 66, 151]],\n [[168, 115, 255],\n [1, 66, 151]]], dtype=np.uint8)\n\nexpect(node, inputs=[a, a_scale, a_zero_point, b, b_scale, b_zero_point, y_scale, y_zero_point], outputs=[output],\n name='test_qlinearmatmul_3D')" + } + ] + }, + { + "name": "QuantizeLinear", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "The linear per-tensor/layer quantization operator. It consumes a high precision tensor, a scale, a zero point to compute the low precision / quantized tensor.\nThe quantization formula is y = saturate ((x / y_scale) + y_zero_point). For saturation, it saturates to [0, 255] if it's uint8, or [-128, 127] if it's int8.\nFor (x / y_scale), it's rounding to nearest ties to even. Refer to https://en.wikipedia.org/wiki/Rounding for details. 'y_zero_point' and 'y' must have same type.\n", + "inputs": [ + { + "name": "x", + "type": "T1", + "description": "N-D full precision Input tensor to be quantized." + }, + { + "name": "y_scale", + "type": "tensor(float)", + "description": "Scale for doing quantization to get 'y'. It's a scalar, which means a per-tensor/layer quantization." + }, + { + "name": "y_zero_point", + "type": "T2", + "option": "optional", + "description": "Zero point for doing quantization to get 'y'. It's a scalar, which means a per-tensor/layer quantization. Default value is uint8 typed 0 if it's not specified." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "y", + "type": "T2", + "description": "N-D quantized output tensor. It has same shape as input 'x'." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain 'x' to float or int32 tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(int32)" + ] + }, + { + "description": "Constrain 'y_zero_point' and 'y' to 8-bit integer tensor.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + } + ], + "examples": [ + { + "summary": "axis", + "code": "node = onnx.helper.make_node('QuantizeLinear',\n inputs=['x', 'y_scale', 'y_zero_point'],\n outputs=['y'],)\n\nx = np.array([[[[-162, 10],\n [-100, 232],\n [-20, -50]],\n\n [[-76, 0],\n [0, 252],\n [32, -44]],\n\n [[245, -485],\n [-960, -270],\n [-375, -470]], ], ], dtype=np.float32)\ny_scale = np.array([2, 4, 5], dtype=np.float32)\ny_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (x / y_scale.reshape(1, 3, 1, 1) + y_zero_point.reshape(1, 3, 1, 1)).astype(np.uint8)\n\nexpect(node, inputs=[x, y_scale, y_zero_point], outputs=[y],\n name='test_quantizelinear_axis')" + }, + { + "summary": "quantizelinear", + "code": "node = onnx.helper.make_node('QuantizeLinear',\n inputs=['x', 'y_scale', 'y_zero_point'],\n outputs=['y'],)\n\nx = np.array([0, 2, 3, 1000, -254, -1000]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = np.uint8(128)\ny = np.array([128, 129, 130, 255, 1, 0]).astype(np.uint8)\n\nexpect(node, inputs=[x, y_scale, y_zero_point], outputs=[y],\n name='test_quantizelinear')" + } + ] + }, + { + "name": "QuantizeLinear", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "The linear quantization operator. It consumes a high precision tensor, a scale, and a zero point to compute the low precision / quantized tensor.\nThe scale factor and zero point must have same shape, and can be either a scalar for per-tensor / per layer quantization, or a 1-D tensor for per-axis quantization.\nThe quantization formula is y = saturate ((x / y_scale) + y_zero_point).\nFor saturation, it saturates to [0, 255] if it's uint8, or [-128, 127] if it's int8.\nFor (x / y_scale), it's rounding to nearest ties to even. Refer to https://en.wikipedia.org/wiki/Rounding for details. 'y_zero_point' and 'y' must have same type.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "(Optional) The axis of the quantization dimension of the input tensor. Ignored for per-tensor quantization. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + } + ], + "inputs": [ + { + "name": "x", + "type": "T1", + "description": "N-D full precision Input tensor to be quantized." + }, + { + "name": "y_scale", + "type": "tensor(float)", + "description": "Scale for doing quantization to get 'y'. It can be a scalar, which means per-tensor/layer quantization, or a 1-D Tensor for per-axis quantization." + }, + { + "name": "y_zero_point", + "type": "T2", + "option": "optional", + "description": "Zero point for doing quantization to get 'y'. Shape must match y_scale. Default is uint8 with zero point of 0 if it's not specified." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "y", + "type": "T2", + "description": "N-D quantized output tensor. It has same shape as input 'x'." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain 'x' to float or int32 tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(int32)" + ] + }, + { + "description": "Constrain 'y_zero_point' and 'y' to 8-bit integer tensor.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int8)", + "tensor(uint8)" + ] + } + ], + "examples": [ + { + "summary": "axis", + "code": "node = onnx.helper.make_node('QuantizeLinear',\n inputs=['x', 'y_scale', 'y_zero_point'],\n outputs=['y'],)\n\nx = np.array([[[[-162, 10],\n [-100, 232],\n [-20, -50]],\n\n [[-76, 0],\n [0, 252],\n [32, -44]],\n\n [[245, -485],\n [-960, -270],\n [-375, -470]], ], ], dtype=np.float32)\ny_scale = np.array([2, 4, 5], dtype=np.float32)\ny_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (x / y_scale.reshape(1, 3, 1, 1) + y_zero_point.reshape(1, 3, 1, 1)).astype(np.uint8)\n\nexpect(node, inputs=[x, y_scale, y_zero_point], outputs=[y],\n name='test_quantizelinear_axis')" + }, + { + "summary": "quantizelinear", + "code": "node = onnx.helper.make_node('QuantizeLinear',\n inputs=['x', 'y_scale', 'y_zero_point'],\n outputs=['y'],)\n\nx = np.array([0, 2, 3, 1000, -254, -1000]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = np.uint8(128)\ny = np.array([128, 129, 130, 255, 1, 0]).astype(np.uint8)\n\nexpect(node, inputs=[x, y_scale, y_zero_point], outputs=[y],\n name='test_quantizelinear')" + } + ] + }, + { + "name": "RNN", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes an one-layer simple RNN. This operator is usually supported\nvia some custom implementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`i` - input gate\n\n`t` - time step (t-1 means previous time step)\n\n`Wi` - W parameter weight matrix for input gate\n\n`Ri` - R recurrence weight matrix for input gate\n\n`Wbi` - W parameter bias vector for input gate\n\n`Rbi` - R parameter bias vector for input gate\n\n`WBi` - W parameter weight matrix for backward input gate\n\n`RBi` - R recurrence weight matrix for backward input gate\n\n`WBbi` - WR bias vectors for backward input gate\n\n`RBbi` - RR bias vectors for backward input gate\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Tanh):\n\n - Ht = f(Xt*(Wi^T) + Ht-1*Ri + Wbi + Rbi)\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "One (or two if bidirectional) activation function for input gate. The activation function must be one of the activation functions specified above. Optional: Default `Tanh` if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "forward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + }, + { + "name": "output_sequence", + "type": "int64", + "required": false, + "description": "The sequence output for the hidden is optional if 0. Default 0." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for input gate. Concatenation of `Wi` and `WBi` (if bidirectional). The tensor has shape `[num_directions, hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `Ri` and `RBi` (if bidirectional). The tensor has shape `[num_directions, hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for input gate. Concatenation of `[Wbi, Rbi]` and `[WBbi, RBbi]` (if bidirectional). The tensor has shape `[num_directions, 2*hidden_size]`. Optional: If not specified - assumed to be 0." + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_input": 3, + "max_input": 6, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. It is optional if `output_sequence` is 0." + }, + { + "name": "Y_h", + "type": "T", + "option": "optional", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 0, + "max_output": 2, + "inputs_range": "3 - 6", + "outputs_range": "0 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.5\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)\n\nrnn = RNN_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_simple_rnn_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)\n\nrnn = RNN_Helper(X=input, W=W, R=R)\n_, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_simple_rnn_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\ncustom_bias = 0.1\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, hidden_size)).astype(np.float32)\nR_B = np.zeros((1, hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\nrnn = RNN_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)],\n name='test_simple_rnn_with_initial_bias')" + }, + { + "summary": "seq_length", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],\n [[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = np.random.randn(1, hidden_size, input_size).astype(np.float32)\nR = np.random.randn(1, hidden_size, hidden_size).astype(np.float32)\n\n# Adding custom bias\nW_B = np.random.randn(1, hidden_size).astype(np.float32)\nR_B = np.random.randn(1, hidden_size).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\nrnn = RNN_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_rnn_seq_length')" + } + ], + "category": "Layer" + }, + { + "name": "RNN", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Computes an one-layer simple RNN. This operator is usually supported\nvia some custom implementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`i` - input gate\n\n`t` - time step (t-1 means previous time step)\n\n`Wi` - W parameter weight matrix for input gate\n\n`Ri` - R recurrence weight matrix for input gate\n\n`Wbi` - W parameter bias vector for input gate\n\n`Rbi` - R parameter bias vector for input gate\n\n`WBi` - W parameter weight matrix for backward input gate\n\n`RBi` - R recurrence weight matrix for backward input gate\n\n`WBbi` - WR bias vectors for backward input gate\n\n`RBbi` - RR bias vectors for backward input gate\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Tanh):\n\n - Ht = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Wbi + Rbi)\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "One (or two if bidirectional) activation function for input gate. The activation function must be one of the activation functions specified above. Optional: Default `Tanh` if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "forward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for input gate. Concatenation of `Wi` and `WBi` (if bidirectional). The tensor has shape `[num_directions, hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `Ri` and `RBi` (if bidirectional). The tensor has shape `[num_directions, hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for input gate. Concatenation of `[Wbi, Rbi]` and `[WBbi, RBbi]` (if bidirectional). The tensor has shape `[num_directions, 2*hidden_size]`. Optional: If not specified - assumed to be 0." + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_input": 3, + "max_input": 6, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. " + }, + { + "name": "Y_h", + "type": "T", + "option": "optional", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 0, + "max_output": 2, + "inputs_range": "3 - 6", + "outputs_range": "0 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.5\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)\n\nrnn = RNN_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_simple_rnn_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)\n\nrnn = RNN_Helper(X=input, W=W, R=R)\n_, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_simple_rnn_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\ncustom_bias = 0.1\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, hidden_size)).astype(np.float32)\nR_B = np.zeros((1, hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\nrnn = RNN_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)],\n name='test_simple_rnn_with_initial_bias')" + }, + { + "summary": "seq_length", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],\n [[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = np.random.randn(1, hidden_size, input_size).astype(np.float32)\nR = np.random.randn(1, hidden_size, hidden_size).astype(np.float32)\n\n# Adding custom bias\nW_B = np.random.randn(1, hidden_size).astype(np.float32)\nR_B = np.random.randn(1, hidden_size).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\nrnn = RNN_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_rnn_seq_length')" + } + ], + "category": "Layer" + }, + { + "name": "RNN", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Computes an one-layer simple RNN. This operator is usually supported\nvia some custom implementation such as CuDNN.\n\nNotations:\n\n`X` - input tensor\n\n`i` - input gate\n\n`t` - time step (t-1 means previous time step)\n\n`Wi` - W parameter weight matrix for input gate\n\n`Ri` - R recurrence weight matrix for input gate\n\n`Wbi` - W parameter bias vector for input gate\n\n`Rbi` - R parameter bias vector for input gate\n\n`WBi` - W parameter weight matrix for backward input gate\n\n`RBi` - R recurrence weight matrix for backward input gate\n\n`WBbi` - WR bias vectors for backward input gate\n\n`RBbi` - RR bias vectors for backward input gate\n\n`H` - Hidden state\n\n`num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n Relu(x) - max(0, x)\n\n Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n\n Sigmoid(x) - 1/(1 + e^{-x})\n\n (NOTE: Below are optional)\n\n Affine(x) - alpha*x + beta\n\n LeakyRelu(x) - x if x >= 0 else alpha * x\n\n ThresholdedRelu(x) - x if x >= alpha else 0\n\n ScaledTanh(x) - alpha*Tanh(beta*x)\n\n HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n\n Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n\n Softsign(x) - x/(1 + |x|)\n\n Softplus(x) - log(1 + e^x)\n\nEquations (Default: f=Tanh):\n\n - Ht = f(Xt*(Wi^T) + Ht-1*(Ri^T) + Wbi + Rbi)\nThis operator has **optional** inputs/outputs. See [the doc](https://github.com/onnx/onnx/blob/master/docs/IR.md) for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument's name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.\n", + "attributes": [ + { + "name": "activation_alpha", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01." + }, + { + "name": "activation_beta", + "type": "float32[]", + "required": false, + "description": "Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators." + }, + { + "name": "activations", + "type": "string[]", + "required": false, + "description": "One (or two if bidirectional) activation function for input gate. The activation function must be one of the activation functions specified above. Optional: Default `Tanh` if not specified." + }, + { + "name": "clip", + "type": "float32", + "required": false, + "description": "Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified." + }, + { + "name": "direction", + "type": "string", + "required": false, + "default": "forward", + "description": "Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional." + }, + { + "name": "hidden_size", + "type": "int64", + "required": false, + "description": "Number of neurons in the hidden layer" + }, + { + "name": "layout", + "type": "int64", + "required": false, + "description": "The shape format of inputs X, initial_h and outputs Y, Y_h. If 0, the following shapes are expected: X.shape = [seq_length, batch_size, input_size], Y.shape = [seq_length, num_directions, batch_size, hidden_size], initial_h.shape = Y_h.shape = [num_directions, batch_size, hidden_size]. If 1, the following shapes are expected: X.shape = [batch_size, seq_length, input_size], Y.shape = [batch_size, seq_length, num_directions, hidden_size], initial_h.shape = Y_h.shape = [batch_size, num_directions, hidden_size]." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "The input sequences packed (and potentially padded) into one 3-D tensor with the shape of `[seq_length, batch_size, input_size]`." + }, + { + "name": "W", + "type": "T", + "description": "The weight tensor for input gate. Concatenation of `Wi` and `WBi` (if bidirectional). The tensor has shape `[num_directions, hidden_size, input_size]`." + }, + { + "name": "R", + "type": "T", + "description": "The recurrence weight tensor. Concatenation of `Ri` and `RBi` (if bidirectional). The tensor has shape `[num_directions, hidden_size, hidden_size]`." + }, + { + "name": "B", + "type": "T", + "option": "optional", + "description": "The bias tensor for input gate. Concatenation of `[Wbi, Rbi]` and `[WBbi, RBbi]` (if bidirectional). The tensor has shape `[num_directions, 2*hidden_size]`. Optional: If not specified - assumed to be 0." + }, + { + "name": "sequence_lens", + "type": "T1", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length `seq_length`. It has shape `[batch_size]`." + }, + { + "name": "initial_h", + "type": "T", + "option": "optional", + "description": "Optional initial value of the hidden. If not specified - assumed to be 0. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_input": 3, + "max_input": 6, + "outputs": [ + { + "name": "Y", + "type": "T", + "option": "optional", + "description": "A tensor that concats all the intermediate output values of the hidden. It has shape `[seq_length, num_directions, batch_size, hidden_size]`. " + }, + { + "name": "Y_h", + "type": "T", + "option": "optional", + "description": "The last output value of the hidden. It has shape `[num_directions, batch_size, hidden_size]`." + } + ], + "min_output": 0, + "max_output": 2, + "inputs_range": "3 - 6", + "outputs_range": "0 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain seq_lens to integer tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int32)" + ] + } + ], + "examples": [ + { + "summary": "batchwise", + "code": "input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.5\nlayout = 1\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R'],\n outputs=['Y', 'Y_h'],\n hidden_size=hidden_size,\n layout=layout\n)\n\nW = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)\n\nrnn = RNN_Helper(X=input, W=W, R=R, layout=layout)\nY, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_simple_rnn_batchwise')" + }, + { + "summary": "defaults", + "code": "input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)\n\nrnn = RNN_Helper(X=input, W=W, R=R)\n_, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_simple_rnn_defaults')" + }, + { + "summary": "initial_bias", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\ncustom_bias = 0.1\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones((1, hidden_size, input_size)).astype(np.float32)\nR = weight_scale * np.ones((1, hidden_size, hidden_size)).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, hidden_size)).astype(np.float32)\nR_B = np.zeros((1, hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\nrnn = RNN_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)],\n name='test_simple_rnn_with_initial_bias')" + }, + { + "summary": "seq_length", + "code": "input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],\n [[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)\n\ninput_size = 3\nhidden_size = 5\n\nnode = onnx.helper.make_node(\n 'RNN',\n inputs=['X', 'W', 'R', 'B'],\n outputs=['', 'Y_h'],\n hidden_size=hidden_size\n)\n\nW = np.random.randn(1, hidden_size, input_size).astype(np.float32)\nR = np.random.randn(1, hidden_size, hidden_size).astype(np.float32)\n\n# Adding custom bias\nW_B = np.random.randn(1, hidden_size).astype(np.float32)\nR_B = np.random.randn(1, hidden_size).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\nrnn = RNN_Helper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_rnn_seq_length')" + } + ], + "category": "Layer" + }, + { + "name": "RandomNormal", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Generate a tensor with random values drawn from a normal distribution. The shape\nof the tensor is specified by the `shape` argument and the parameter of the normal distribution\nspecified by `mean` and `scale`.\n\nThe data type is specified by the 'dtype' argument. The 'dtype' argument must\nbe one of the data types specified in the 'DataType' enum field in the\nTensorProto message.\n", + "attributes": [ + { + "name": "dtype", + "type": "DataType", + "required": false, + "default": 1, + "description": "The data type for the elements of the output tensor. Default is TensorProto::FLOAT." + }, + { + "name": "mean", + "type": "float32", + "required": false, + "description": "The mean of the normal distribution." + }, + { + "name": "scale", + "type": "float32", + "required": false, + "default": 1.0, + "description": "The standard deviation of the normal distribution." + }, + { + "name": "seed", + "type": "float32", + "required": false, + "description": "(Optional) Seed to the random generator, if not specified we will auto generate one." + }, + { + "name": "shape", + "type": "int64[]", + "required": true, + "description": "The shape of the output tensor." + } + ], + "min_input": 0, + "max_input": 0, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of random values drawn from normal distribution" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ] + }, + { + "name": "RandomNormalLike", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Generate a tensor with random values drawn from a normal distribution.\nThe shape of the output tensor is copied from the shape of the input tensor,\nand the parameters of the normal distribution are specified by `mean` and `scale`.\n\nThe data type is specified by the 'dtype' argument, or copied from the input tensor if not provided.\nThe 'dtype' argument must be one of the data types specified in the 'DataType' enum field in the\nTensorProto message, and be valid as an output type.\n", + "attributes": [ + { + "name": "dtype", + "type": "int64", + "required": false, + "description": "(Optional) The data type for the elements of the output tensor, if not specified, we will use the data type of the input tensor." + }, + { + "name": "mean", + "type": "float32", + "required": false, + "description": "The mean of the normal distribution." + }, + { + "name": "scale", + "type": "float32", + "required": false, + "default": 1.0, + "description": "The standard deviation of the normal distribution." + }, + { + "name": "seed", + "type": "float32", + "required": false, + "description": "(Optional) Seed to the random generator, if not specified we will auto generate one." + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "Input tensor to copy shape and optionally type information from." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor of random values drawn from normal distribution" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain to any tensor type. If the dtype attribute is not provided this must be a valid output type.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain output types to float tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ] + }, + { + "name": "RandomUniform", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Generate a tensor with random values drawn from a uniform distribution. The shape\nof the tensor is specified by the `shape` argument and the range by `low` and `high`.\n\nThe data type is specified by the 'dtype' argument. The 'dtype' argument must\nbe one of the data types specified in the 'DataType' enum field in the\nTensorProto message.\n", + "attributes": [ + { + "name": "dtype", + "type": "int64", + "required": false, + "default": 1, + "description": "The data type for the elements of the output tensor. If not specified, default is TensorProto::FLOAT." + }, + { + "name": "high", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Upper boundary of the output values." + }, + { + "name": "low", + "type": "float32", + "required": false, + "description": "Lower boundary of the output values." + }, + { + "name": "seed", + "type": "float32", + "required": false, + "description": "(Optional) Seed to the random generator, if not specified we will auto generate one." + }, + { + "name": "shape", + "type": "int64[]", + "required": true, + "description": "The shape of the output tensor." + } + ], + "min_input": 0, + "max_input": 0, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of random values drawn from uniform distribution" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ] + }, + { + "name": "RandomUniformLike", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Generate a tensor with random values drawn from a uniform distribution.\nThe shape of the output tensor is copied from the shape of the input tensor,\nand the parameters of the uniform distribution are specified by `low` and `high`.\n\nThe data type is specified by the 'dtype' argument, or copied from the input tensor if not provided.\nThe 'dtype' argument must be one of the data types specified in the 'DataType' enum field in the\nTensorProto message and be valid as an output type.\n", + "attributes": [ + { + "name": "dtype", + "type": "int64", + "required": false, + "description": "(Optional) The data type for the elements of the output tensor, if not specified, we will use the data type of the input tensor." + }, + { + "name": "high", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Upper boundary of the output values." + }, + { + "name": "low", + "type": "float32", + "required": false, + "description": "Lower boundary of the output values." + }, + { + "name": "seed", + "type": "float32", + "required": false, + "description": "(Optional) Seed to the random generator, if not specified we will auto generate one." + } + ], + "inputs": [ + { + "name": "input", + "type": "T1", + "description": "Input tensor to copy shape and optionally type information from." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T2", + "description": "Output tensor of random values drawn from uniform distribution" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain to any tensor type. If the dtype attribute is not provided this must be a valid output type.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain output types to float tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ] + }, + { + "name": "Range", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Generate a tensor containing a sequence of numbers that begin at `start` and extends by increments of `delta`\nup to `limit` (exclusive).\n\nThe number of elements in the output of range is computed as below-\n\n`number_of_elements = max( ceil( (limit - start) / delta ) , 0 )`\n\nThe pseudocode determining the contents of the output is shown below-\n\n`for(int i=0; i) and produces one output data\n(Tensor) where the reciprocal is, y = 1/x, is applied to\nthe tensor elementwise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "reciprocal", + "code": "node = onnx.helper.make_node(\n 'Reciprocal',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-4, 2]).astype(np.float32)\ny = np.reciprocal(x) # expected output [-0.25, 0.5],\nexpect(node, inputs=[x], outputs=[y],\n name='test_reciprocal_example')\n\nx = np.random.rand(3, 4, 5).astype(np.float32) + 0.5\ny = np.reciprocal(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_reciprocal')" + } + ] + }, + { + "name": "Reciprocal", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Reciprocal takes one input data (Tensor) and produces one output data\n(Tensor) where the reciprocal is, y = 1/x, is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "reciprocal", + "code": "node = onnx.helper.make_node(\n 'Reciprocal',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-4, 2]).astype(np.float32)\ny = np.reciprocal(x) # expected output [-0.25, 0.5],\nexpect(node, inputs=[x], outputs=[y],\n name='test_reciprocal_example')\n\nx = np.random.rand(3, 4, 5).astype(np.float32) + 0.5\ny = np.reciprocal(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_reciprocal')" + } + ] + }, + { + "name": "Reciprocal", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Reciprocal takes one input data (Tensor) and produces one output data\n(Tensor) where the reciprocal is, y = 1/x, is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "reciprocal", + "code": "node = onnx.helper.make_node(\n 'Reciprocal',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-4, 2]).astype(np.float32)\ny = np.reciprocal(x) # expected output [-0.25, 0.5],\nexpect(node, inputs=[x], outputs=[y],\n name='test_reciprocal_example')\n\nx = np.random.rand(3, 4, 5).astype(np.float32) + 0.5\ny = np.reciprocal(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_reciprocal')" + } + ] + }, + { + "name": "ReduceL1", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the L1 norm of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[78.]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[3., 7.], [11., 15.], [19., 23.]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_keep_dims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n# print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_negative_axes_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_negative_axes_keep_dims_random')" + } + ] + }, + { + "name": "ReduceL1", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the L1 norm of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[78.]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[3., 7.], [11., 15.], [19., 23.]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_keep_dims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n# print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_negative_axes_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_negative_axes_keep_dims_random')" + } + ] + }, + { + "name": "ReduceL1", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the L1 norm of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[78.]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[3., 7.], [11., 15.], [19., 23.]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_keep_dims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL1',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n# print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_negative_axes_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l1_negative_axes_keep_dims_random')" + } + ] + }, + { + "name": "ReduceL2", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the L2 norm of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=axes, keepdims=keepdims == 1))\n#print(reduced)\n#[[[25.49509757]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=axes, keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n#print(reduced)\n#[[2.23606798, 5.],\n# [7.81024968, 10.63014581],\n# [13.45362405, 16.2788206]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n#print(reduced)\n#[[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_l2_keep_dims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n# print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n# print(reduced)\n#[[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_negative_axes_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_negative_axes_keep_dims_random')" + } + ] + }, + { + "name": "ReduceL2", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the L2 norm of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=axes, keepdims=keepdims == 1))\n#print(reduced)\n#[[[25.49509757]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=axes, keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n#print(reduced)\n#[[2.23606798, 5.],\n# [7.81024968, 10.63014581],\n# [13.45362405, 16.2788206]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n#print(reduced)\n#[[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_l2_keep_dims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n# print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n# print(reduced)\n#[[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_negative_axes_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_negative_axes_keep_dims_random')" + } + ] + }, + { + "name": "ReduceL2", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the L2 norm of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=axes, keepdims=keepdims == 1))\n#print(reduced)\n#[[[25.49509757]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=axes, keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n#print(reduced)\n#[[2.23606798, 5.],\n# [7.81024968, 10.63014581],\n# [13.45362405, 16.2788206]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n#print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n#print(reduced)\n#[[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_l2_keep_dims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceL2',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), shape)\n# print(data)\n#[[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]], [[9., 10.], [11., 12.]]]\n\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n# print(reduced)\n#[[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_negative_axes_keep_dims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(\n a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_l2_negative_axes_keep_dims_random')" + } + ] + }, + { + "name": "ReduceLogSum", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the log sum of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "keepdims", + "code": "node = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, keepdims=True))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_default')" + }, + { + "summary": "negative_axes_keepdims", + "code": "node = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"],\n axes=[-2]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=(-2), keepdims=True))\n# print(reduced)\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_negative_axes')" + }, + { + "summary": "nokeepdims", + "code": "node = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"],\n axes=[2, 1],\n keepdims=0\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=(2, 1), keepdims=False))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_desc_axes')\n\nnode = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"],\n axes=[0, 1],\n keepdims=0\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=(0, 1), keepdims=False))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_asc_axes')" + } + ] + }, + { + "name": "ReduceLogSum", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the log sum of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "keepdims", + "code": "node = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, keepdims=True))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_default')" + }, + { + "summary": "negative_axes_keepdims", + "code": "node = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"],\n axes=[-2]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=(-2), keepdims=True))\n# print(reduced)\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_negative_axes')" + }, + { + "summary": "nokeepdims", + "code": "node = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"],\n axes=[2, 1],\n keepdims=0\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=(2, 1), keepdims=False))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_desc_axes')\n\nnode = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"],\n axes=[0, 1],\n keepdims=0\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=(0, 1), keepdims=False))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_asc_axes')" + } + ] + }, + { + "name": "ReduceLogSum", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the log sum of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "keepdims", + "code": "node = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, keepdims=True))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_default')" + }, + { + "summary": "negative_axes_keepdims", + "code": "node = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"],\n axes=[-2]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=(-2), keepdims=True))\n# print(reduced)\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_negative_axes')" + }, + { + "summary": "nokeepdims", + "code": "node = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"],\n axes=[2, 1],\n keepdims=0\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=(2, 1), keepdims=False))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_desc_axes')\n\nnode = onnx.helper.make_node(\n 'ReduceLogSum',\n inputs=['data'],\n outputs=[\"reduced\"],\n axes=[0, 1],\n keepdims=0\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=(0, 1), keepdims=False))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_asc_axes')" + } + ] + }, + { + "name": "ReduceLogSumExp", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the log sum exponent of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=axes,\n keepdims=keepdims == 1))\n# print(reduced)\n# [[[60.00671387]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=axes,\n keepdims=keepdims == 1))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(\n np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n# print(reduced)\n#[[20., 2.31326175]\n# [40.00004578, 2.31326175]\n# [60.00671387, 2.31326175]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(\n np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n# print(reduced)\n# [[[20., 2.31326175]]\n# [[40.00004578, 2.31326175]]\n# [[60.00671387, 2.31326175]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n# print(reduced)\n# [[[20., 2.31326175]]\n# [[40.00004578, 2.31326175]]\n# [[60.00671387, 2.31326175]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceLogSumExp", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the log sum exponent of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=axes,\n keepdims=keepdims == 1))\n# print(reduced)\n# [[[60.00671387]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=axes,\n keepdims=keepdims == 1))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(\n np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n# print(reduced)\n#[[20., 2.31326175]\n# [40.00004578, 2.31326175]\n# [60.00671387, 2.31326175]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(\n np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n# print(reduced)\n# [[[20., 2.31326175]]\n# [[40.00004578, 2.31326175]]\n# [[60.00671387, 2.31326175]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n# print(reduced)\n# [[[20., 2.31326175]]\n# [[40.00004578, 2.31326175]]\n# [[60.00671387, 2.31326175]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceLogSumExp", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the log sum exponent of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=axes,\n keepdims=keepdims == 1))\n# print(reduced)\n# [[[60.00671387]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=axes,\n keepdims=keepdims == 1))\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(\n np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n# print(reduced)\n#[[20., 2.31326175]\n# [40.00004578, 2.31326175]\n# [60.00671387, 2.31326175]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(\n np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n# print(reduced)\n# [[[20., 2.31326175]]\n# [[40.00004578, 2.31326175]]\n# [[60.00671387, 2.31326175]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceLogSumExp',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n# print(reduced)\n# [[[20., 2.31326175]]\n# [[40.00004578, 2.31326175]]\n# [[60.00671387, 2.31326175]]]\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data),\n axis=tuple(axes),\n keepdims=keepdims == 1))\n\nexpect(node, inputs=[data], outputs=[reduced],\n name='test_reduce_log_sum_exp_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMax", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the max of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n[[[60.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdim_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[20., 2.]\n# [40., 2.]\n# [60., 2.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[20., 2.]]\n# [[40., 2.]]\n# [[60., 2.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[20., 2.]]\n# [[40., 2.]]\n# [[60., 2.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMax", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the max of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n[[[60.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdim_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[20., 2.]\n# [40., 2.]\n# [60., 2.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[20., 2.]]\n# [[40., 2.]]\n# [[60., 2.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[20., 2.]]\n# [[40., 2.]]\n# [[60., 2.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMax", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Computes the max of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision and 8 bit numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(uint8)", + "tensor(int8)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n[[[60.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdim_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[20., 2.]\n# [40., 2.]\n# [60., 2.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[20., 2.]]\n# [[40., 2.]]\n# [[60., 2.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[20., 2.]]\n# [[40., 2.]]\n# [[60., 2.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMax", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the max of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision and 8 bit numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)", + "tensor(uint8)", + "tensor(int8)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n[[[60.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdim_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[20., 2.]\n# [40., 2.]\n# [60., 2.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[20., 2.]]\n# [[40., 2.]]\n# [[60., 2.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMax',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[20., 2.]]\n# [[40., 2.]]\n# [[60., 2.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_max_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMean", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the mean of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[18.25]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[12.5, 1.5]\n# [35., 1.5]\n# [57.5, 1.5]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[12.5, 1.5]]\n# [[35., 1.5]]\n# [[57.5, 1.5]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[12.5, 1.5]]\n# [[35., 1.5]]\n# [[57.5, 1.5]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMean", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the mean of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[18.25]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[12.5, 1.5]\n# [35., 1.5]\n# [57.5, 1.5]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[12.5, 1.5]]\n# [[35., 1.5]]\n# [[57.5, 1.5]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[12.5, 1.5]]\n# [[35., 1.5]]\n# [[57.5, 1.5]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMean", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the mean of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[18.25]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[12.5, 1.5]\n# [35., 1.5]\n# [57.5, 1.5]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[12.5, 1.5]]\n# [[35., 1.5]]\n# [[57.5, 1.5]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMean',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[12.5, 1.5]]\n# [[35., 1.5]]\n# [[57.5, 1.5]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_mean_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMin", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the min of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMin',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[5., 1.]\n# [30., 1.]\n# [55., 1.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin', inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[5., 1.]]\n# [[30., 1.]]\n# [[55., 1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin', inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[5., 1.]]\n# [[30., 1.]]\n# [[55., 1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMin", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the min of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMin',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[5., 1.]\n# [30., 1.]\n# [55., 1.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin', inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[5., 1.]]\n# [[30., 1.]]\n# [[55., 1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin', inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[5., 1.]]\n# [[30., 1.]]\n# [[55., 1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMin", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Computes the min of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision and 8 bit numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(uint8)", + "tensor(int8)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMin',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[5., 1.]\n# [30., 1.]\n# [55., 1.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin', inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[5., 1.]]\n# [[30., 1.]]\n# [[55., 1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin', inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[5., 1.]]\n# [[30., 1.]]\n# [[55., 1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceMin", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the min of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision and 8 bit numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)", + "tensor(uint8)", + "tensor(int8)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceMin',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[5., 1.]\n# [30., 1.]\n# [55., 1.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin', inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[5., 1.]]\n# [[30., 1.]]\n# [[55., 1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceMin', inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[5., 1.]]\n# [[30., 1.]]\n# [[55., 1.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_min_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceProd", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the product of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[4.790016e+08]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[3., 8.]\n# [35., 48.]\n# [99., 120.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceProd", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the product of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[4.790016e+08]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[3., 8.]\n# [35., 48.]\n# [99., 120.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceProd", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the product of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[4.790016e+08]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[3., 8.]\n# [35., 48.]\n# [99., 120.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceProd',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_prod_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceSum", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the sum of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=None, keepdims=keepdims == 1)\n#print(reduced)\n#[[[78.]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=None, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n#print(reduced)\n#[[4., 6.]\n# [12., 14.]\n# [20., 22.]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_do_not_keepdims_random')" + }, + { + "summary": "empty_axes_input_noop", + "code": "shape = [3, 2, 2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims,\n noop_with_empty_axes=True)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\naxes = np.array([], dtype=np.int64)\nreduced = np.array(data)\n#print(reduced)\n#[[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced],\n name='test_reduce_sum_empty_axes_input_noop_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.array(data)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_negative_axes_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n#print(reduced)\n#[[[4., 6.]]\n# [[12., 14.]]\n# [[20., 22.]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([-2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n# print(reduced)\n#[[[4., 6.]]\n# [[12., 14.]]\n# [[20., 22.]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced],\n name='test_reduce_sum_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=tuple(\n axes.tolist()), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced],\n name='test_reduce_sum_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceSum", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the sum of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=None, keepdims=keepdims == 1)\n#print(reduced)\n#[[[78.]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=None, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n#print(reduced)\n#[[4., 6.]\n# [12., 14.]\n# [20., 22.]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_do_not_keepdims_random')" + }, + { + "summary": "empty_axes_input_noop", + "code": "shape = [3, 2, 2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims,\n noop_with_empty_axes=True)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\naxes = np.array([], dtype=np.int64)\nreduced = np.array(data)\n#print(reduced)\n#[[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced],\n name='test_reduce_sum_empty_axes_input_noop_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.array(data)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_negative_axes_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n#print(reduced)\n#[[[4., 6.]]\n# [[12., 14.]]\n# [[20., 22.]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([-2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n# print(reduced)\n#[[[4., 6.]]\n# [[12., 14.]]\n# [[20., 22.]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced],\n name='test_reduce_sum_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=tuple(\n axes.tolist()), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced],\n name='test_reduce_sum_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceSum", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the sum of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + }, + { + "name": "noop_with_empty_axes", + "type": "int64", + "required": false, + "description": "Defines behaviour if 'axes' is empty. Default behaviour with 'false' is to reduce all axes. When axes is empty and this attribute is set to true, input tensor will not be reduced,and the output tensor would be equivalent to input tensor." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + }, + { + "name": "axes", + "type": "tensor(int64)", + "option": "optional", + "description": "Optional input list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor if 'noop_with_empty_axes' is false, else act as an Identity op when 'noop_with_empty_axes' is true. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "min_input": 1, + "max_input": 2, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=None, keepdims=keepdims == 1)\n#print(reduced)\n#[[[78.]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=None, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n#print(reduced)\n#[[4., 6.]\n# [12., 14.]\n# [20., 22.]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_do_not_keepdims_random')" + }, + { + "summary": "empty_axes_input_noop", + "code": "shape = [3, 2, 2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims,\n noop_with_empty_axes=True)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\naxes = np.array([], dtype=np.int64)\nreduced = np.array(data)\n#print(reduced)\n#[[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced],\n name='test_reduce_sum_empty_axes_input_noop_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.array(data)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_negative_axes_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n#print(reduced)\n#[[[4., 6.]]\n# [[12., 14.]]\n# [[20., 22.]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced], name='test_reduce_sum_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = np.array([-2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSum',\n inputs=['data', 'axes'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(data, axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n# print(reduced)\n#[[[4., 6.]]\n# [[12., 14.]]\n# [[20., 22.]]]\n\nexpect(node, inputs=[data, axes], outputs=[reduced],\n name='test_reduce_sum_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(data, axis=tuple(\n axes.tolist()), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data, axes], outputs=[reduced],\n name='test_reduce_sum_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceSumSquare", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Computes the sum square of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[650.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[10., 20.]\n# [74., 100.]\n# [202., 244.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[10., 20.]]\n# [[74., 100.]]\n# [[202., 244.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[10., 20.s]]\n# [[74., 100.]]\n# [[202., 244.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceSumSquare", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Computes the sum square of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equal 0, then\nthe resulted tensor have the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[650.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[10., 20.]\n# [74., 100.]\n# [202., 244.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[10., 20.]]\n# [[74., 100.]]\n# [[202., 244.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[10., 20.s]]\n# [[74., 100.]]\n# [[202., 244.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "ReduceSumSquare", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Computes the sum square of the input tensor's element along the provided axes. The resulting\ntensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then\nthe resulting tensor has the reduced dimension pruned.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults keepdims to\nFalse instead of True.", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "A list of integers, along which to reduce. The default is to reduce over all the dimensions of the input tensor. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the reduced dimension or not, default 1 means keep reduced dimension." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reduced", + "type": "T", + "description": "Reduced output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "default_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=axes, keepdims=keepdims == 1)\n#print(reduced)\n#[[[650.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_default_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=axes, keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_default_axes_keepdims_random')" + }, + { + "summary": "do_not_keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[10., 20.]\n# [74., 100.]\n# [202., 244.]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_do_not_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_do_not_keepdims_random')" + }, + { + "summary": "keepdims", + "code": "shape = [3, 2, 2]\naxes = [1]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n#print(reduced)\n#[[[10., 20.]]\n# [[74., 100.]]\n# [[202., 244.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_keepdims_random')" + }, + { + "summary": "negative_axes_keepdims", + "code": "shape = [3, 2, 2]\naxes = [-2]\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n 'ReduceSumSquare',\n inputs=['data'],\n outputs=['reduced'],\n axes=axes,\n keepdims=keepdims)\n\ndata = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n#[[[10., 20.s]]\n# [[74., 100.]]\n# [[202., 244.]]]\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_negative_axes_keepdims_example')\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n\nexpect(node, inputs=[data], outputs=[reduced], name='test_reduce_sum_square_negative_axes_keepdims_random')" + } + ] + }, + { + "name": "Relu", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Relu takes one input data (Tensor) and produces one output data\n(Tensor) where the rectified linear function, y = max(0, x), is applied to\nthe tensor elementwise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "relu", + "code": "node = onnx.helper.make_node(\n 'Relu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_relu')" + } + ], + "category": "Activation" + }, + { + "name": "Relu", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Relu takes one input data (Tensor) and produces one output data\n(Tensor) where the rectified linear function, y = max(0, x), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "relu", + "code": "node = onnx.helper.make_node(\n 'Relu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_relu')" + } + ], + "category": "Activation" + }, + { + "name": "Relu", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Relu takes one input data (Tensor) and produces one output data\n(Tensor) where the rectified linear function, y = max(0, x), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "relu", + "code": "node = onnx.helper.make_node(\n 'Relu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_relu')" + } + ], + "category": "Activation" + }, + { + "name": "Relu", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Relu takes one input data (Tensor) and produces one output data\n(Tensor) where the rectified linear function, y = max(0, x), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to signed numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(int32)", + "tensor(int8)", + "tensor(int16)", + "tensor(int64)", + "tensor(float16)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "relu", + "code": "node = onnx.helper.make_node(\n 'Relu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_relu')" + } + ], + "category": "Activation" + }, + { + "name": "Reshape", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Reshape the input tensor similar to numpy.reshape.\nIt takes a tensor as input and an argument `shape`. It outputs the reshaped tensor.\nAt most one dimension of the new shape can be -1. In this case, the value is\ninferred from the size of the tensor and the remaining dimensions. A dimension\ncould also be 0, in which case the actual dimension value is unchanged (i.e. taken\nfrom the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar.\nThe input tensor's shape and the output tensor's shape are required to have the same number of elements.", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + }, + { + "name": "shape", + "type": "int64[]", + "required": false, + "description": "New shape" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "reshaped", + "type": "T", + "description": "Reshaped data." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "allowzero", + "code": "original_shape = [0, 3, 4]\ntest_cases = {\n 'allowzero_reordered': np.array([3, 4, 0], dtype=np.int64),\n}\ndata = np.random.random_sample(original_shape).astype(np.float32)\n\nfor test_name, shape in test_cases.items():\n node = onnx.helper.make_node(\n 'Reshape',\n inputs=['data', 'shape'],\n outputs=['reshaped'],\n allowzero=1, # if allowzero=1, final shape = (3, 4, 0)\n # if allowzero=0, final shape = (3, 4, 4)\n )\n\n reshaped = reshape_reference_implementation(data, shape, allowzero=1)\n\n expect(node, inputs=[data, shape], outputs=[reshaped],\n name='test_reshape_' + test_name)" + }, + { + "summary": "reshape", + "code": "original_shape = [2, 3, 4]\ntest_cases = {\n 'reordered_all_dims': np.array([4, 2, 3], dtype=np.int64),\n 'reordered_last_dims': np.array([2, 4, 3], dtype=np.int64),\n 'reduced_dims': np.array([2, 12], dtype=np.int64),\n 'extended_dims': np.array([2, 3, 2, 2], dtype=np.int64),\n 'one_dim': np.array([24], dtype=np.int64),\n 'negative_dim': np.array([2, -1, 2], dtype=np.int64),\n 'negative_extended_dims': np.array([-1, 2, 3, 4], dtype=np.int64),\n 'zero_dim': np.array([2, 0, 4, 1], dtype=np.int64),\n 'zero_and_negative_dim': np.array([2, 0, 1, -1], dtype=np.int64),\n}\ndata = np.random.random_sample(original_shape).astype(np.float32)\n\nfor test_name, shape in test_cases.items():\n node = onnx.helper.make_node(\n 'Reshape',\n inputs=['data', 'shape'],\n outputs=['reshaped'],\n )\n\n reshaped = reshape_reference_implementation(data, shape)\n\n expect(node, inputs=[data, shape], outputs=[reshaped],\n name='test_reshape_' + test_name)" + } + ], + "category": "Shape" + }, + { + "name": "Reshape", + "module": "ai.onnx", + "version": 5, + "support_level": "common", + "description": "Reshape the input tensor similar to numpy.reshape.\nFirst input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor.\nAt most one dimension of the new shape can be -1. In this case, the value is\ninferred from the size of the tensor and the remaining dimensions. A dimension\ncould also be 0, in which case the actual dimension value is unchanged (i.e. taken\nfrom the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar.\nThe input tensor's shape and the output tensor's shape are required to have the same number of elements.", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + }, + { + "name": "shape", + "type": "tensor(int64)", + "description": "Specified shape for output." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "reshaped", + "type": "T", + "description": "Reshaped data." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "allowzero", + "code": "original_shape = [0, 3, 4]\ntest_cases = {\n 'allowzero_reordered': np.array([3, 4, 0], dtype=np.int64),\n}\ndata = np.random.random_sample(original_shape).astype(np.float32)\n\nfor test_name, shape in test_cases.items():\n node = onnx.helper.make_node(\n 'Reshape',\n inputs=['data', 'shape'],\n outputs=['reshaped'],\n allowzero=1, # if allowzero=1, final shape = (3, 4, 0)\n # if allowzero=0, final shape = (3, 4, 4)\n )\n\n reshaped = reshape_reference_implementation(data, shape, allowzero=1)\n\n expect(node, inputs=[data, shape], outputs=[reshaped],\n name='test_reshape_' + test_name)" + }, + { + "summary": "reshape", + "code": "original_shape = [2, 3, 4]\ntest_cases = {\n 'reordered_all_dims': np.array([4, 2, 3], dtype=np.int64),\n 'reordered_last_dims': np.array([2, 4, 3], dtype=np.int64),\n 'reduced_dims': np.array([2, 12], dtype=np.int64),\n 'extended_dims': np.array([2, 3, 2, 2], dtype=np.int64),\n 'one_dim': np.array([24], dtype=np.int64),\n 'negative_dim': np.array([2, -1, 2], dtype=np.int64),\n 'negative_extended_dims': np.array([-1, 2, 3, 4], dtype=np.int64),\n 'zero_dim': np.array([2, 0, 4, 1], dtype=np.int64),\n 'zero_and_negative_dim': np.array([2, 0, 1, -1], dtype=np.int64),\n}\ndata = np.random.random_sample(original_shape).astype(np.float32)\n\nfor test_name, shape in test_cases.items():\n node = onnx.helper.make_node(\n 'Reshape',\n inputs=['data', 'shape'],\n outputs=['reshaped'],\n )\n\n reshaped = reshape_reference_implementation(data, shape)\n\n expect(node, inputs=[data, shape], outputs=[reshaped],\n name='test_reshape_' + test_name)" + } + ], + "category": "Shape" + }, + { + "name": "Reshape", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Reshape the input tensor similar to numpy.reshape.\nFirst input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor.\nAt most one dimension of the new shape can be -1. In this case, the value is\ninferred from the size of the tensor and the remaining dimensions. A dimension\ncould also be 0, in which case the actual dimension value is unchanged (i.e. taken\nfrom the input tensor). Shape (second input) could be an empty shape, which means converting to a scalar.\nThe input tensor's shape and the output tensor's shape are required to have the same number of elements.", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + }, + { + "name": "shape", + "type": "tensor(int64)", + "description": "Specified shape for output." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "reshaped", + "type": "T", + "description": "Reshaped data." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "allowzero", + "code": "original_shape = [0, 3, 4]\ntest_cases = {\n 'allowzero_reordered': np.array([3, 4, 0], dtype=np.int64),\n}\ndata = np.random.random_sample(original_shape).astype(np.float32)\n\nfor test_name, shape in test_cases.items():\n node = onnx.helper.make_node(\n 'Reshape',\n inputs=['data', 'shape'],\n outputs=['reshaped'],\n allowzero=1, # if allowzero=1, final shape = (3, 4, 0)\n # if allowzero=0, final shape = (3, 4, 4)\n )\n\n reshaped = reshape_reference_implementation(data, shape, allowzero=1)\n\n expect(node, inputs=[data, shape], outputs=[reshaped],\n name='test_reshape_' + test_name)" + }, + { + "summary": "reshape", + "code": "original_shape = [2, 3, 4]\ntest_cases = {\n 'reordered_all_dims': np.array([4, 2, 3], dtype=np.int64),\n 'reordered_last_dims': np.array([2, 4, 3], dtype=np.int64),\n 'reduced_dims': np.array([2, 12], dtype=np.int64),\n 'extended_dims': np.array([2, 3, 2, 2], dtype=np.int64),\n 'one_dim': np.array([24], dtype=np.int64),\n 'negative_dim': np.array([2, -1, 2], dtype=np.int64),\n 'negative_extended_dims': np.array([-1, 2, 3, 4], dtype=np.int64),\n 'zero_dim': np.array([2, 0, 4, 1], dtype=np.int64),\n 'zero_and_negative_dim': np.array([2, 0, 1, -1], dtype=np.int64),\n}\ndata = np.random.random_sample(original_shape).astype(np.float32)\n\nfor test_name, shape in test_cases.items():\n node = onnx.helper.make_node(\n 'Reshape',\n inputs=['data', 'shape'],\n outputs=['reshaped'],\n )\n\n reshaped = reshape_reference_implementation(data, shape)\n\n expect(node, inputs=[data, shape], outputs=[reshaped],\n name='test_reshape_' + test_name)" + } + ], + "category": "Shape" + }, + { + "name": "Reshape", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Reshape the input tensor similar to numpy.reshape.\nFirst input is the data tensor, second input is a shape tensor which specifies the output shape. It outputs the reshaped tensor.\nAt most one dimension of the new shape can be -1. In this case, the value is\ninferred from the size of the tensor and the remaining dimensions. A dimension\ncould also be 0, in which case the actual dimension value is unchanged (i.e. taken\nfrom the input tensor). If 'allowzero' is set, and the new shape includes 0, the\ndimension will be set explicitly to zero (i.e. not taken from input tensor).\nShape (second input) could be an empty shape, which means converting to a scalar.\nThe input tensor's shape and the output tensor's shape are required to have the same number of elements.", + "attributes": [ + { + "name": "allowzero", + "type": "int64", + "required": false, + "description": "(Optional) By default, when any value in the 'shape' input is equal to zero the corresponding dimension value is copied from the input tensor dynamically. allowzero=1 indicates that if any value in the 'shape' input is set to zero, the zero value is honored, similar to NumPy." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + }, + { + "name": "shape", + "type": "tensor(int64)", + "description": "Specified shape for output." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "reshaped", + "type": "T", + "description": "Reshaped data." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "allowzero", + "code": "original_shape = [0, 3, 4]\ntest_cases = {\n 'allowzero_reordered': np.array([3, 4, 0], dtype=np.int64),\n}\ndata = np.random.random_sample(original_shape).astype(np.float32)\n\nfor test_name, shape in test_cases.items():\n node = onnx.helper.make_node(\n 'Reshape',\n inputs=['data', 'shape'],\n outputs=['reshaped'],\n allowzero=1, # if allowzero=1, final shape = (3, 4, 0)\n # if allowzero=0, final shape = (3, 4, 4)\n )\n\n reshaped = reshape_reference_implementation(data, shape, allowzero=1)\n\n expect(node, inputs=[data, shape], outputs=[reshaped],\n name='test_reshape_' + test_name)" + }, + { + "summary": "reshape", + "code": "original_shape = [2, 3, 4]\ntest_cases = {\n 'reordered_all_dims': np.array([4, 2, 3], dtype=np.int64),\n 'reordered_last_dims': np.array([2, 4, 3], dtype=np.int64),\n 'reduced_dims': np.array([2, 12], dtype=np.int64),\n 'extended_dims': np.array([2, 3, 2, 2], dtype=np.int64),\n 'one_dim': np.array([24], dtype=np.int64),\n 'negative_dim': np.array([2, -1, 2], dtype=np.int64),\n 'negative_extended_dims': np.array([-1, 2, 3, 4], dtype=np.int64),\n 'zero_dim': np.array([2, 0, 4, 1], dtype=np.int64),\n 'zero_and_negative_dim': np.array([2, 0, 1, -1], dtype=np.int64),\n}\ndata = np.random.random_sample(original_shape).astype(np.float32)\n\nfor test_name, shape in test_cases.items():\n node = onnx.helper.make_node(\n 'Reshape',\n inputs=['data', 'shape'],\n outputs=['reshaped'],\n )\n\n reshaped = reshape_reference_implementation(data, shape)\n\n expect(node, inputs=[data, shape], outputs=[reshaped],\n name='test_reshape_' + test_name)" + } + ], + "category": "Shape" + }, + { + "name": "Resize", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Resize the input tensor.\nEach dimension value of the output tensor is:\n output_dimension = floor(input_dimension * scale).\n", + "attributes": [ + { + "name": "mode", + "type": "string", + "required": false, + "default": "nearest", + "description": "Two interpolation modes: nearest (default), and linear (including bilinear, trilinear, etc)" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "N-D tensor" + }, + { + "name": "scales", + "type": "tensor(float)", + "description": "The scale array along each dimension. It takes value greater than 0. If it's less than 1, it's sampling down, otherwise, it's upsampling. The number of elements of 'scales' should be the same as the rank of input 'X'." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "N-D tensor after resizing" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input 'X' and output 'Y' to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "resize_downsample_scales_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)\n\n# [[[[ 1.47119141 2.78125 4.08251953]\n# [ 6.71142578 8.02148438 9.32275391]\n# [11.91650391 13.2265625 14.52783203]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_cubic')" + }, + { + "summary": "resize_downsample_scales_cubic_A_n0p5_exclude_outside", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n cubic_coeff_a=-0.5,\n exclude_outside=True\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)\n\n# [[[[ 1.36812675 2.6695014 4.0133367 ]\n# [ 6.57362535 7.875 9.2188353 ]\n# [11.94896657 13.25034122 14.59417652]]]]\noutput = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.5), scale_factors=scales,\n exclude_outside=True).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_cubic_A_n0p5_exclude_outside')" + }, + { + "summary": "resize_downsample_scales_cubic_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)\n\n# [[[[ 1. 2.39519159 3.79038317]\n# [ 6.58076634 7.97595793 9.37114951]\n# [12.16153268 13.55672427 14.95191585]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_cubic_align_corners')" + }, + { + "summary": "resize_downsample_scales_linear", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[2.6666665 4.3333331]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_linear')" + }, + { + "summary": "resize_downsample_scales_linear_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.142857]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_linear_align_corners')" + }, + { + "summary": "resize_downsample_scales_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_nearest')" + }, + { + "summary": "resize_downsample_sizes_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 1.63078704 3.00462963 4.37847222]\n# [ 7.12615741 8.5 9.87384259]\n# [12.62152778 13.99537037 15.36921296]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_downsample_sizes_cubic')" + }, + { + "summary": "resize_downsample_sizes_linear_pytorch_half_pixel", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='pytorch_half_pixel'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 3, 1], dtype=np.int64)\n\n# [[[[ 1.6666666]\n# [ 7. ]\n# [12.333333 ]]]]\noutput = interpolate_nd(\n data, linear_coeffs, output_size=sizes, coordinate_transformation_mode='pytorch_half_pixel').astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_downsample_sizes_linear_pytorch_half_pixel')" + }, + { + "summary": "resize_downsample_sizes_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 1, 3], dtype=np.int64)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_downsample_sizes_nearest')" + }, + { + "summary": "resize_tf_crop_and_resize", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', 'roi', '', 'sizes'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='tf_crop_and_resize'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0, 0, 0.4, 0.6, 1, 1, 0.6, 0.8], dtype=np.float32)\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 7.6000004 7.9 8.2 ]\n# [ 8.8 9.1 9.400001 ]\n# [10. 10.3 10.6 ]]]]\noutput = interpolate_nd(data, linear_coeffs, output_size=sizes, roi=roi,\n coordinate_transformation_mode='tf_crop_and_resize').astype(np.float32)\n\nexpect(node, inputs=[data, roi, sizes], outputs=[output],\n name='test_resize_tf_crop_and_resize')" + }, + { + "summary": "resize_tf_crop_and_resize_extrapolation_value", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', 'roi', '', 'sizes'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='tf_crop_and_resize',\n extrapolation_value=10.0\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0, 0, 0.4, 0.6, 1, 1, 1.2, 1.7], dtype=np.float32)\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 7.6000004 10. 10. ]\n# [12.400001 10. 10. ]\n# [10. 10. 10. ]]]]\noutput = interpolate_nd(data, linear_coeffs, output_size=sizes, roi=roi,\n coordinate_transformation_mode='tf_crop_and_resize', extrapolation_value=10.0).astype(np.float32)\n\nexpect(node, inputs=[data, roi, sizes], outputs=[output],\n name='test_resize_tf_crop_and_resize')" + }, + { + "summary": "resize_upsample_scales_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 0.47265625 0.76953125 1.24609375 1.875 2.28125\n# 2.91015625 3.38671875 3.68359375]\n# [ 1.66015625 1.95703125 2.43359375 3.0625 3.46875\n# 4.09765625 4.57421875 4.87109375]\n# [ 3.56640625 3.86328125 4.33984375 4.96875 5.375\n# 6.00390625 6.48046875 6.77734375]\n# [ 6.08203125 6.37890625 6.85546875 7.484375 7.890625\n# 8.51953125 8.99609375 9.29296875]\n# [ 7.70703125 8.00390625 8.48046875 9.109375 9.515625\n# 10.14453125 10.62109375 10.91796875]\n# [10.22265625 10.51953125 10.99609375 11.625 12.03125\n# 12.66015625 13.13671875 13.43359375]\n# [12.12890625 12.42578125 12.90234375 13.53125 13.9375\n# 14.56640625 15.04296875 15.33984375]\n# [13.31640625 13.61328125 14.08984375 14.71875 15.125\n# 15.75390625 16.23046875 16.52734375]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic')" + }, + { + "summary": "resize_upsample_scales_cubic_A_n0p5_exclude_outside", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n cubic_coeff_a=-0.5,\n exclude_outside=True\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 0.55882353 0.81494204 1.35698249 1.89705882 2.39705882\n# 2.93713516 3.47917561 3.73529412]\n# [ 1.58329755 1.83941606 2.38145651 2.92153285 3.42153285\n# 3.96160918 4.50364964 4.75976814]\n# [ 3.75145936 4.00757787 4.54961832 5.08969466 5.58969466\n# 6.12977099 6.67181144 6.92792995]\n# [ 5.91176471 6.16788321 6.70992366 7.25 7.75\n# 8.29007634 8.83211679 9.08823529]\n# [ 7.91176471 8.16788321 8.70992366 9.25 9.75\n# 10.29007634 10.83211679 11.08823529]\n# [10.07207005 10.32818856 10.87022901 11.41030534 11.91030534\n# 12.45038168 12.99242213 13.24854064]\n# [12.24023186 12.49635036 13.03839082 13.57846715 14.07846715\n# 14.61854349 15.16058394 15.41670245]\n# [13.26470588 13.52082439 14.06286484 14.60294118 15.10294118\n# 15.64301751 16.18505796 16.44117647]]]]\noutput = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.5), scale_factors=scales,\n exclude_outside=True).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic_A_n0p5_exclude_outside')" + }, + { + "summary": "resize_upsample_scales_cubic_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 1. 1.34110787 1.80029155 2.32944606 2.67055394\n# 3.19970845 3.65889213 4. ]\n# [ 2.36443149 2.70553936 3.16472303 3.69387755 4.03498542\n# 4.56413994 5.02332362 5.36443149]\n# [ 4.20116618 4.54227405 5.00145773 5.53061224 5.87172012\n# 6.40087464 6.86005831 7.20116618]\n# [ 6.31778426 6.65889213 7.1180758 7.64723032 7.98833819\n# 8.51749271 8.97667638 9.31778426]\n# [ 7.68221574 8.02332362 8.48250729 9.01166181 9.35276968\n# 9.8819242 10.34110787 10.68221574]\n# [ 9.79883382 10.13994169 10.59912536 11.12827988 11.46938776\n# 11.99854227 12.45772595 12.79883382]\n# [11.63556851 11.97667638 12.43586006 12.96501458 13.30612245\n# 13.83527697 14.29446064 14.63556851]\n# [13. 13.34110787 13.80029155 14.32944606 14.67055394\n# 15.19970845 15.65889213 16. ]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic_align_corners')" + }, + { + "summary": "resize_upsample_scales_cubic_asymmetric", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n coordinate_transformation_mode='asymmetric'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 1. 1.40625 2. 2.5 3. 3.59375 4.\n# 4.09375]\n# [ 2.625 3.03125 3.625 4.125 4.625 5.21875 5.625\n# 5.71875]\n# [ 5. 5.40625 6. 6.5 7. 7.59375 8.\n# 8.09375]\n# [ 7. 7.40625 8. 8.5 9. 9.59375 10.\n# 10.09375]\n# [ 9. 9.40625 10. 10.5 11. 11.59375 12.\n# 12.09375]\n# [11.375 11.78125 12.375 12.875 13.375 13.96875 14.375\n# 14.46875]\n# [13. 13.40625 14. 14.5 15. 15.59375 16.\n# 16.09375]\n# [13.375 13.78125 14.375 14.875 15.375 15.96875 16.375\n# 16.46875]]]]\noutput = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.75), scale_factors=scales,\n coordinate_transformation_mode='asymmetric').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic_asymmetric')" + }, + { + "summary": "resize_upsample_scales_linear", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[1. 1.25 1.75 2. ]\n# [1.5 1.75 2.25 2.5 ]\n# [2.5 2.75 3.25 3.5 ]\n# [3. 3.25 3.75 4. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_linear')" + }, + { + "summary": "resize_upsample_scales_linear_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[1. 1.33333333 1.66666667 2. ]\n# [1.66666667 2. 2.33333333 2.66666667]\n# [2.33333333 2.66666667 3. 3.33333333]\n# [3. 3.33333333 3.66666667 4. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_linear_align_corners')" + }, + { + "summary": "resize_upsample_scales_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\n# [[[[1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 2. 2. 2.]\n# [3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_nearest')" + }, + { + "summary": "resize_upsample_sizes_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 9, 10], dtype=np.int64)\n\n# [[[[ 0.45507922 0.64057922 0.97157922 1.42257922 1.90732922\n# 2.22332922 2.70807922 3.15907922 3.49007922 3.67557922]\n# [ 1.39437963 1.57987963 1.91087963 2.36187963 2.84662963\n# 3.16262963 3.64737963 4.09837963 4.42937963 4.61487963]\n# [ 2.95130693 3.13680693 3.46780693 3.91880693 4.40355693\n# 4.71955693 5.20430693 5.65530693 5.98630693 6.17180693]\n# [ 5.20525069 5.39075069 5.72175069 6.17275069 6.65750069\n# 6.97350069 7.45825069 7.90925069 8.24025069 8.42575069]\n# [ 6.88975 7.07525 7.40625 7.85725 8.342\n# 8.658 9.14275 9.59375 9.92475 10.11025 ]\n# [ 8.57424931 8.75974931 9.09074931 9.54174931 10.02649931\n# 10.34249931 10.82724931 11.27824931 11.60924931 11.79474931]\n# [10.82819307 11.01369307 11.34469307 11.79569307 12.28044307\n# 12.59644307 13.08119307 13.53219307 13.86319307 14.04869307]\n# [12.38512037 12.57062037 12.90162037 13.35262037 13.83737037\n# 14.15337037 14.63812037 15.08912037 15.42012037 15.60562037]\n# [13.32442078 13.50992078 13.84092078 14.29192078 14.77667078\n# 15.09267078 15.57742078 16.02842078 16.35942078 16.54492078]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_cubic')" + }, + { + "summary": "resize_upsample_sizes_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 7, 8], dtype=np.int64)\n\n# [[[[1. 1. 1. 1. 2. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2. 2.]\n# [3. 3. 3. 3. 4. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest')" + }, + { + "summary": "resize_upsample_sizes_nearest_ceil_half_pixel", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n coordinate_transformation_mode='half_pixel',\n nearest_mode='ceil'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 8, 8], dtype=np.int64)\n\n# [[[[ 1. 2. 2. 3. 3. 4. 4. 4.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]]]]\noutput = interpolate_nd(\n data, lambda x: nearest_coeffs(x, mode='ceil'), output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest_ceil_half_pixel')" + }, + { + "summary": "resize_upsample_sizes_nearest_floor_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n coordinate_transformation_mode='align_corners',\n nearest_mode='floor'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 8, 8], dtype=np.int64)\n\n# [[[[ 1. 1. 1. 2. 2. 3. 3. 4.]\n# [ 1. 1. 1. 2. 2. 3. 3. 4.]\n# [ 1. 1. 1. 2. 2. 3. 3. 4.]\n# [ 5. 5. 5. 6. 6. 7. 7. 8.]\n# [ 5. 5. 5. 6. 6. 7. 7. 8.]\n# [ 9. 9. 9. 10. 10. 11. 11. 12.]\n# [ 9. 9. 9. 10. 10. 11. 11. 12.]\n# [13. 13. 13. 14. 14. 15. 15. 16.]]]]\noutput = interpolate_nd(\n data, lambda x: nearest_coeffs(x, mode='floor'), output_size=sizes, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest_floor_align_corners')" + }, + { + "summary": "resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n coordinate_transformation_mode='asymmetric',\n nearest_mode='round_prefer_ceil'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 8, 8], dtype=np.int64)\n\n# [[[[ 1. 2. 2. 3. 3. 4. 4. 4.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]]]]\noutput = interpolate_nd(\n data, lambda x: nearest_coeffs(x, mode='round_prefer_ceil'),\n output_size=sizes, coordinate_transformation_mode='asymmetric').astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric')" + } + ] + }, + { + "name": "Resize", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Resize the input tensor. In general, it calculates every value in the output tensor as a weighted average of neighborhood (a.k.a. sampling locations) in the input tensor.\nEach dimension value of the output tensor is:\n output_dimension = floor(input_dimension * (roi_end - roi_start) * scale) if input \\\"sizes\\\" is not specified.\n", + "attributes": [ + { + "name": "coordinate_transformation_mode", + "type": "string", + "required": false, + "default": "half_pixel", + "description": "\nThis attribute describes how to transform the coordinate in the resized tensor to the coordinate in the original tensor.
\n\nThe coordinate of each dimension is transformed individually. Let's describe a case using axis x as an example.\nDenote x_resized as the coordinate of axis x in the resized tensor, x_original as the coordinate of axis x in the original tensor, length_original as the length of the original tensor in axis x, length_resized as the length of the resized tensor in axis x, roi_x = (start_x, end_x) of the axis x in input \"roi\", scale = length_resized / length_original,
\n\nif coordinate_transformation_mode is \"half_pixel\",
\nx_original = (x_resized + 0.5) / scale - 0.5,
\n\nif coordinate_transformation_mode is \"pytorch_half_pixel\",
\nx_original = length_resized > 1 ? (x_resized + 0.5) / scale - 0.5 : 0,
\n\nif coordinate_transformation_mode is \"align_corners\",
\nx_original = x_resized * (length_original - 1) / (length_resized - 1),
\n\nif coordinate_transformation_mode is \"asymmetric\",
\nx_original = x_resized / scale,
\n\nif coordinate_transformation_mode is \"tf_half_pixel_for_nn\",
\nx_original = (x_resized + 0.5) / scale,
\n\nif coordinate_transformation_mode is \"tf_crop_and_resize\",
\nx_original = length_resized > 1 ? start_x * (length_original - 1) + x_resized * (end_x - start_x) * (length_original - 1) / (length_resized - 1) : 0.5 * (start_x + end_x) * (length_original - 1)." + }, + { + "name": "cubic_coeff_a", + "type": "float32", + "required": false, + "default": -0.75, + "description": "The coefficient 'a' used in cubic interpolation. Two common choice are -0.5 (in some cases of TensorFlow) and -0.75 (in PyTorch). Check out Equation (4) in https://ieeexplore.ieee.org/document/1163711 for the details. This attribute is valid only if \"mode\" is \"cubic\"." + }, + { + "name": "exclude_outside", + "type": "int64", + "required": false, + "description": "If set to 1, the weight of sampling locations outside the tensor will be set to 0 and the weight will be renormalized so that their sum is 1.0. The default value is 0." + }, + { + "name": "extrapolation_value", + "type": "float32", + "required": false, + "description": "When coordinate_transformation_mode is \"tf_crop_and_resize\" and x_original is outside the range [0, length_original - 1], this value is used as the corresponding output value. Default is 0.0f." + }, + { + "name": "mode", + "type": "string", + "required": false, + "default": "nearest", + "description": "Three interpolation modes: nearest (default), linear and cubic. The \"linear\" mode includes linear interpolation for 1D tensor and N-linear interpolation for N-D tensor (for example, bilinear interpolation for 2D tensor). The \"cubic\" mode includes cubic interpolation for 1D tensor and N-cubic interpolation for N-D tensor (for example, bicubic interpolation for 2D tensor)." + }, + { + "name": "nearest_mode", + "type": "string", + "required": false, + "default": "round_prefer_floor", + "description": "Four modes: round_prefer_floor (default, as known as round half down), round_prefer_ceil (as known as round half up), floor, ceil. Only used by nearest interpolation. It indicates how to get \"nearest\" pixel in input tensor from x_original, so this attribute is valid only if \"mode\" is \"nearest\"." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "N-D tensor" + }, + { + "name": "roi", + "type": "T2", + "description": "1-D tensor given as [start1, ..., startN, end1, ..., endN], where N is the rank of X. The RoIs' coordinates are normalized in the coordinate system of the input image. It only takes effect when coordinate_transformation_mode is \"tf_crop_and_resize\"" + }, + { + "name": "scales", + "type": "tensor(float)", + "description": "The scale array along each dimension. It takes value greater than 0. If it's less than 1, it's sampling down, otherwise, it's upsampling. The number of elements of 'scales' should be the same as the rank of input 'X'. If 'size' is needed, the user must set 'scales' to an empty tensor." + }, + { + "name": "sizes", + "type": "tensor(int64)", + "option": "optional", + "description": "The size of the output tensor. The number of elements of 'sizes' should be the same as the rank of input 'X'. May only be set if 'scales' is set to an empty tensor." + } + ], + "min_input": 3, + "max_input": 4, + "outputs": [ + { + "name": "Y", + "type": "T1", + "description": "N-D tensor after resizing" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "3 - 4", + "type_constraints": [ + { + "description": "Constrain input 'X' and output 'Y' to all tensor types.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain roi type to float or double.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "resize_downsample_scales_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)\n\n# [[[[ 1.47119141 2.78125 4.08251953]\n# [ 6.71142578 8.02148438 9.32275391]\n# [11.91650391 13.2265625 14.52783203]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_cubic')" + }, + { + "summary": "resize_downsample_scales_cubic_A_n0p5_exclude_outside", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n cubic_coeff_a=-0.5,\n exclude_outside=True\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)\n\n# [[[[ 1.36812675 2.6695014 4.0133367 ]\n# [ 6.57362535 7.875 9.2188353 ]\n# [11.94896657 13.25034122 14.59417652]]]]\noutput = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.5), scale_factors=scales,\n exclude_outside=True).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_cubic_A_n0p5_exclude_outside')" + }, + { + "summary": "resize_downsample_scales_cubic_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)\n\n# [[[[ 1. 2.39519159 3.79038317]\n# [ 6.58076634 7.97595793 9.37114951]\n# [12.16153268 13.55672427 14.95191585]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_cubic_align_corners')" + }, + { + "summary": "resize_downsample_scales_linear", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[2.6666665 4.3333331]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_linear')" + }, + { + "summary": "resize_downsample_scales_linear_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.142857]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_linear_align_corners')" + }, + { + "summary": "resize_downsample_scales_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_nearest')" + }, + { + "summary": "resize_downsample_sizes_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 1.63078704 3.00462963 4.37847222]\n# [ 7.12615741 8.5 9.87384259]\n# [12.62152778 13.99537037 15.36921296]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_downsample_sizes_cubic')" + }, + { + "summary": "resize_downsample_sizes_linear_pytorch_half_pixel", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='pytorch_half_pixel'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 3, 1], dtype=np.int64)\n\n# [[[[ 1.6666666]\n# [ 7. ]\n# [12.333333 ]]]]\noutput = interpolate_nd(\n data, linear_coeffs, output_size=sizes, coordinate_transformation_mode='pytorch_half_pixel').astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_downsample_sizes_linear_pytorch_half_pixel')" + }, + { + "summary": "resize_downsample_sizes_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 1, 3], dtype=np.int64)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_downsample_sizes_nearest')" + }, + { + "summary": "resize_tf_crop_and_resize", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', 'roi', '', 'sizes'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='tf_crop_and_resize'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0, 0, 0.4, 0.6, 1, 1, 0.6, 0.8], dtype=np.float32)\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 7.6000004 7.9 8.2 ]\n# [ 8.8 9.1 9.400001 ]\n# [10. 10.3 10.6 ]]]]\noutput = interpolate_nd(data, linear_coeffs, output_size=sizes, roi=roi,\n coordinate_transformation_mode='tf_crop_and_resize').astype(np.float32)\n\nexpect(node, inputs=[data, roi, sizes], outputs=[output],\n name='test_resize_tf_crop_and_resize')" + }, + { + "summary": "resize_tf_crop_and_resize_extrapolation_value", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', 'roi', '', 'sizes'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='tf_crop_and_resize',\n extrapolation_value=10.0\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0, 0, 0.4, 0.6, 1, 1, 1.2, 1.7], dtype=np.float32)\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 7.6000004 10. 10. ]\n# [12.400001 10. 10. ]\n# [10. 10. 10. ]]]]\noutput = interpolate_nd(data, linear_coeffs, output_size=sizes, roi=roi,\n coordinate_transformation_mode='tf_crop_and_resize', extrapolation_value=10.0).astype(np.float32)\n\nexpect(node, inputs=[data, roi, sizes], outputs=[output],\n name='test_resize_tf_crop_and_resize')" + }, + { + "summary": "resize_upsample_scales_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 0.47265625 0.76953125 1.24609375 1.875 2.28125\n# 2.91015625 3.38671875 3.68359375]\n# [ 1.66015625 1.95703125 2.43359375 3.0625 3.46875\n# 4.09765625 4.57421875 4.87109375]\n# [ 3.56640625 3.86328125 4.33984375 4.96875 5.375\n# 6.00390625 6.48046875 6.77734375]\n# [ 6.08203125 6.37890625 6.85546875 7.484375 7.890625\n# 8.51953125 8.99609375 9.29296875]\n# [ 7.70703125 8.00390625 8.48046875 9.109375 9.515625\n# 10.14453125 10.62109375 10.91796875]\n# [10.22265625 10.51953125 10.99609375 11.625 12.03125\n# 12.66015625 13.13671875 13.43359375]\n# [12.12890625 12.42578125 12.90234375 13.53125 13.9375\n# 14.56640625 15.04296875 15.33984375]\n# [13.31640625 13.61328125 14.08984375 14.71875 15.125\n# 15.75390625 16.23046875 16.52734375]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic')" + }, + { + "summary": "resize_upsample_scales_cubic_A_n0p5_exclude_outside", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n cubic_coeff_a=-0.5,\n exclude_outside=True\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 0.55882353 0.81494204 1.35698249 1.89705882 2.39705882\n# 2.93713516 3.47917561 3.73529412]\n# [ 1.58329755 1.83941606 2.38145651 2.92153285 3.42153285\n# 3.96160918 4.50364964 4.75976814]\n# [ 3.75145936 4.00757787 4.54961832 5.08969466 5.58969466\n# 6.12977099 6.67181144 6.92792995]\n# [ 5.91176471 6.16788321 6.70992366 7.25 7.75\n# 8.29007634 8.83211679 9.08823529]\n# [ 7.91176471 8.16788321 8.70992366 9.25 9.75\n# 10.29007634 10.83211679 11.08823529]\n# [10.07207005 10.32818856 10.87022901 11.41030534 11.91030534\n# 12.45038168 12.99242213 13.24854064]\n# [12.24023186 12.49635036 13.03839082 13.57846715 14.07846715\n# 14.61854349 15.16058394 15.41670245]\n# [13.26470588 13.52082439 14.06286484 14.60294118 15.10294118\n# 15.64301751 16.18505796 16.44117647]]]]\noutput = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.5), scale_factors=scales,\n exclude_outside=True).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic_A_n0p5_exclude_outside')" + }, + { + "summary": "resize_upsample_scales_cubic_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 1. 1.34110787 1.80029155 2.32944606 2.67055394\n# 3.19970845 3.65889213 4. ]\n# [ 2.36443149 2.70553936 3.16472303 3.69387755 4.03498542\n# 4.56413994 5.02332362 5.36443149]\n# [ 4.20116618 4.54227405 5.00145773 5.53061224 5.87172012\n# 6.40087464 6.86005831 7.20116618]\n# [ 6.31778426 6.65889213 7.1180758 7.64723032 7.98833819\n# 8.51749271 8.97667638 9.31778426]\n# [ 7.68221574 8.02332362 8.48250729 9.01166181 9.35276968\n# 9.8819242 10.34110787 10.68221574]\n# [ 9.79883382 10.13994169 10.59912536 11.12827988 11.46938776\n# 11.99854227 12.45772595 12.79883382]\n# [11.63556851 11.97667638 12.43586006 12.96501458 13.30612245\n# 13.83527697 14.29446064 14.63556851]\n# [13. 13.34110787 13.80029155 14.32944606 14.67055394\n# 15.19970845 15.65889213 16. ]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic_align_corners')" + }, + { + "summary": "resize_upsample_scales_cubic_asymmetric", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n coordinate_transformation_mode='asymmetric'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 1. 1.40625 2. 2.5 3. 3.59375 4.\n# 4.09375]\n# [ 2.625 3.03125 3.625 4.125 4.625 5.21875 5.625\n# 5.71875]\n# [ 5. 5.40625 6. 6.5 7. 7.59375 8.\n# 8.09375]\n# [ 7. 7.40625 8. 8.5 9. 9.59375 10.\n# 10.09375]\n# [ 9. 9.40625 10. 10.5 11. 11.59375 12.\n# 12.09375]\n# [11.375 11.78125 12.375 12.875 13.375 13.96875 14.375\n# 14.46875]\n# [13. 13.40625 14. 14.5 15. 15.59375 16.\n# 16.09375]\n# [13.375 13.78125 14.375 14.875 15.375 15.96875 16.375\n# 16.46875]]]]\noutput = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.75), scale_factors=scales,\n coordinate_transformation_mode='asymmetric').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic_asymmetric')" + }, + { + "summary": "resize_upsample_scales_linear", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[1. 1.25 1.75 2. ]\n# [1.5 1.75 2.25 2.5 ]\n# [2.5 2.75 3.25 3.5 ]\n# [3. 3.25 3.75 4. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_linear')" + }, + { + "summary": "resize_upsample_scales_linear_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[1. 1.33333333 1.66666667 2. ]\n# [1.66666667 2. 2.33333333 2.66666667]\n# [2.33333333 2.66666667 3. 3.33333333]\n# [3. 3.33333333 3.66666667 4. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_linear_align_corners')" + }, + { + "summary": "resize_upsample_scales_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\n# [[[[1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 2. 2. 2.]\n# [3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_nearest')" + }, + { + "summary": "resize_upsample_sizes_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 9, 10], dtype=np.int64)\n\n# [[[[ 0.45507922 0.64057922 0.97157922 1.42257922 1.90732922\n# 2.22332922 2.70807922 3.15907922 3.49007922 3.67557922]\n# [ 1.39437963 1.57987963 1.91087963 2.36187963 2.84662963\n# 3.16262963 3.64737963 4.09837963 4.42937963 4.61487963]\n# [ 2.95130693 3.13680693 3.46780693 3.91880693 4.40355693\n# 4.71955693 5.20430693 5.65530693 5.98630693 6.17180693]\n# [ 5.20525069 5.39075069 5.72175069 6.17275069 6.65750069\n# 6.97350069 7.45825069 7.90925069 8.24025069 8.42575069]\n# [ 6.88975 7.07525 7.40625 7.85725 8.342\n# 8.658 9.14275 9.59375 9.92475 10.11025 ]\n# [ 8.57424931 8.75974931 9.09074931 9.54174931 10.02649931\n# 10.34249931 10.82724931 11.27824931 11.60924931 11.79474931]\n# [10.82819307 11.01369307 11.34469307 11.79569307 12.28044307\n# 12.59644307 13.08119307 13.53219307 13.86319307 14.04869307]\n# [12.38512037 12.57062037 12.90162037 13.35262037 13.83737037\n# 14.15337037 14.63812037 15.08912037 15.42012037 15.60562037]\n# [13.32442078 13.50992078 13.84092078 14.29192078 14.77667078\n# 15.09267078 15.57742078 16.02842078 16.35942078 16.54492078]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_cubic')" + }, + { + "summary": "resize_upsample_sizes_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 7, 8], dtype=np.int64)\n\n# [[[[1. 1. 1. 1. 2. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2. 2.]\n# [3. 3. 3. 3. 4. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest')" + }, + { + "summary": "resize_upsample_sizes_nearest_ceil_half_pixel", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n coordinate_transformation_mode='half_pixel',\n nearest_mode='ceil'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 8, 8], dtype=np.int64)\n\n# [[[[ 1. 2. 2. 3. 3. 4. 4. 4.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]]]]\noutput = interpolate_nd(\n data, lambda x: nearest_coeffs(x, mode='ceil'), output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest_ceil_half_pixel')" + }, + { + "summary": "resize_upsample_sizes_nearest_floor_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n coordinate_transformation_mode='align_corners',\n nearest_mode='floor'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 8, 8], dtype=np.int64)\n\n# [[[[ 1. 1. 1. 2. 2. 3. 3. 4.]\n# [ 1. 1. 1. 2. 2. 3. 3. 4.]\n# [ 1. 1. 1. 2. 2. 3. 3. 4.]\n# [ 5. 5. 5. 6. 6. 7. 7. 8.]\n# [ 5. 5. 5. 6. 6. 7. 7. 8.]\n# [ 9. 9. 9. 10. 10. 11. 11. 12.]\n# [ 9. 9. 9. 10. 10. 11. 11. 12.]\n# [13. 13. 13. 14. 14. 15. 15. 16.]]]]\noutput = interpolate_nd(\n data, lambda x: nearest_coeffs(x, mode='floor'), output_size=sizes, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest_floor_align_corners')" + }, + { + "summary": "resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n coordinate_transformation_mode='asymmetric',\n nearest_mode='round_prefer_ceil'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 8, 8], dtype=np.int64)\n\n# [[[[ 1. 2. 2. 3. 3. 4. 4. 4.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]]]]\noutput = interpolate_nd(\n data, lambda x: nearest_coeffs(x, mode='round_prefer_ceil'),\n output_size=sizes, coordinate_transformation_mode='asymmetric').astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric')" + } + ] + }, + { + "name": "Resize", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Resize the input tensor. In general, it calculates every value in the output tensor as a weighted average of neighborhood (a.k.a. sampling locations) in the input tensor.\nEach dimension value of the output tensor is:\n output_dimension = floor(input_dimension * (roi_end - roi_start) * scale) if input \\\"sizes\\\" is not specified.\n", + "attributes": [ + { + "name": "coordinate_transformation_mode", + "type": "string", + "required": false, + "default": "half_pixel", + "description": "\nThis attribute describes how to transform the coordinate in the resized tensor to the coordinate in the original tensor.
\n\nThe coordinate of each dimension is transformed individually. Let's describe a case using axis x as an example.\nDenote x_resized as the coordinate of axis x in the resized tensor, x_original as the coordinate of axis x in the original tensor, length_original as the length of the original tensor in axis x, length_resized as the length of the resized tensor in axis x, roi_x = (start_x, end_x) of the axis x in input \"roi\", scale = length_resized / length_original,
\n\nif coordinate_transformation_mode is \"half_pixel\",
\nx_original = (x_resized + 0.5) / scale - 0.5,
\n\nif coordinate_transformation_mode is \"pytorch_half_pixel\",
\nx_original = length_resized > 1 ? (x_resized + 0.5) / scale - 0.5 : 0,
\n\nif coordinate_transformation_mode is \"align_corners\",
\nx_original = x_resized * (length_original - 1) / (length_resized - 1),
\n\nif coordinate_transformation_mode is \"asymmetric\",
\nx_original = x_resized / scale,
\n\nif coordinate_transformation_mode is \"tf_crop_and_resize\",
\nx_original = length_resized > 1 ? start_x * (length_original - 1) + x_resized * (end_x - start_x) * (length_original - 1) / (length_resized - 1) : 0.5 * (start_x + end_x) * (length_original - 1)." + }, + { + "name": "cubic_coeff_a", + "type": "float32", + "required": false, + "default": -0.75, + "description": "The coefficient 'a' used in cubic interpolation. Two common choice are -0.5 (in some cases of TensorFlow) and -0.75 (in PyTorch). Check out Equation (4) in https://ieeexplore.ieee.org/document/1163711 for the details. This attribute is valid only if \"mode\" is \"cubic\"." + }, + { + "name": "exclude_outside", + "type": "int64", + "required": false, + "description": "If set to 1, the weight of sampling locations outside the tensor will be set to 0 and the weight will be renormalized so that their sum is 1.0. The default value is 0." + }, + { + "name": "extrapolation_value", + "type": "float32", + "required": false, + "description": "When coordinate_transformation_mode is \"tf_crop_and_resize\" and x_original is outside the range [0, length_original - 1], this value is used as the corresponding output value. Default is 0.0f." + }, + { + "name": "mode", + "type": "string", + "required": false, + "default": "nearest", + "description": "Three interpolation modes: nearest (default), linear and cubic. The \"linear\" mode includes linear interpolation for 1D tensor and N-linear interpolation for N-D tensor (for example, bilinear interpolation for 2D tensor). The \"cubic\" mode includes cubic interpolation for 1D tensor and N-cubic interpolation for N-D tensor (for example, bicubic interpolation for 2D tensor)." + }, + { + "name": "nearest_mode", + "type": "string", + "required": false, + "default": "round_prefer_floor", + "description": "Four modes: round_prefer_floor (default, as known as round half down), round_prefer_ceil (as known as round half up), floor, ceil. Only used by nearest interpolation. It indicates how to get \"nearest\" pixel in input tensor from x_original, so this attribute is valid only if \"mode\" is \"nearest\"." + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "N-D tensor" + }, + { + "name": "roi", + "type": "T2", + "option": "optional", + "description": "1-D tensor given as [start1, ..., startN, end1, ..., endN], where N is the rank of X. The RoIs' coordinates are normalized in the coordinate system of the input image. It only takes effect when coordinate_transformation_mode is \"tf_crop_and_resize\"" + }, + { + "name": "scales", + "type": "tensor(float)", + "option": "optional", + "description": "The scale array along each dimension. It takes value greater than 0. If it's less than 1, it's sampling down, otherwise, it's upsampling. The number of elements of 'scales' should be the same as the rank of input 'X'. One of 'scales' and 'sizes' MUST be specified and it is an error if both are specified. If 'sizes' is needed, the user can use an empty string as the name of 'scales' in this operator's input list." + }, + { + "name": "sizes", + "type": "tensor(int64)", + "option": "optional", + "description": "The size of the output tensor. The number of elements of 'sizes' should be the same as the rank of input 'X'. Only one of 'scales' and 'sizes' can be specified." + } + ], + "min_input": 1, + "max_input": 4, + "outputs": [ + { + "name": "Y", + "type": "T1", + "description": "N-D tensor after resizing" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - 4", + "type_constraints": [ + { + "description": "Constrain input 'X' and output 'Y' to all tensor types.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain roi type to float or double.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "resize_downsample_scales_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)\n\n# [[[[ 1.47119141 2.78125 4.08251953]\n# [ 6.71142578 8.02148438 9.32275391]\n# [11.91650391 13.2265625 14.52783203]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_cubic')" + }, + { + "summary": "resize_downsample_scales_cubic_A_n0p5_exclude_outside", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n cubic_coeff_a=-0.5,\n exclude_outside=True\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)\n\n# [[[[ 1.36812675 2.6695014 4.0133367 ]\n# [ 6.57362535 7.875 9.2188353 ]\n# [11.94896657 13.25034122 14.59417652]]]]\noutput = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.5), scale_factors=scales,\n exclude_outside=True).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_cubic_A_n0p5_exclude_outside')" + }, + { + "summary": "resize_downsample_scales_cubic_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.8, 0.8], dtype=np.float32)\n\n# [[[[ 1. 2.39519159 3.79038317]\n# [ 6.58076634 7.97595793 9.37114951]\n# [12.16153268 13.55672427 14.95191585]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_cubic_align_corners')" + }, + { + "summary": "resize_downsample_scales_linear", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[2.6666665 4.3333331]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_linear')" + }, + { + "summary": "resize_downsample_scales_linear_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.142857]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_linear_align_corners')" + }, + { + "summary": "resize_downsample_scales_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_downsample_scales_nearest')" + }, + { + "summary": "resize_downsample_sizes_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 1.63078704 3.00462963 4.37847222]\n# [ 7.12615741 8.5 9.87384259]\n# [12.62152778 13.99537037 15.36921296]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_downsample_sizes_cubic')" + }, + { + "summary": "resize_downsample_sizes_linear_pytorch_half_pixel", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='pytorch_half_pixel'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 3, 1], dtype=np.int64)\n\n# [[[[ 1.6666666]\n# [ 7. ]\n# [12.333333 ]]]]\noutput = interpolate_nd(\n data, linear_coeffs, output_size=sizes, coordinate_transformation_mode='pytorch_half_pixel').astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_downsample_sizes_linear_pytorch_half_pixel')" + }, + { + "summary": "resize_downsample_sizes_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 1, 3], dtype=np.int64)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_downsample_sizes_nearest')" + }, + { + "summary": "resize_tf_crop_and_resize", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', 'roi', '', 'sizes'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='tf_crop_and_resize'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0, 0, 0.4, 0.6, 1, 1, 0.6, 0.8], dtype=np.float32)\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 7.6000004 7.9 8.2 ]\n# [ 8.8 9.1 9.400001 ]\n# [10. 10.3 10.6 ]]]]\noutput = interpolate_nd(data, linear_coeffs, output_size=sizes, roi=roi,\n coordinate_transformation_mode='tf_crop_and_resize').astype(np.float32)\n\nexpect(node, inputs=[data, roi, sizes], outputs=[output],\n name='test_resize_tf_crop_and_resize')" + }, + { + "summary": "resize_tf_crop_and_resize_extrapolation_value", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', 'roi', '', 'sizes'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='tf_crop_and_resize',\n extrapolation_value=10.0\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0, 0, 0.4, 0.6, 1, 1, 1.2, 1.7], dtype=np.float32)\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 7.6000004 10. 10. ]\n# [12.400001 10. 10. ]\n# [10. 10. 10. ]]]]\noutput = interpolate_nd(data, linear_coeffs, output_size=sizes, roi=roi,\n coordinate_transformation_mode='tf_crop_and_resize', extrapolation_value=10.0).astype(np.float32)\n\nexpect(node, inputs=[data, roi, sizes], outputs=[output],\n name='test_resize_tf_crop_and_resize')" + }, + { + "summary": "resize_upsample_scales_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 0.47265625 0.76953125 1.24609375 1.875 2.28125\n# 2.91015625 3.38671875 3.68359375]\n# [ 1.66015625 1.95703125 2.43359375 3.0625 3.46875\n# 4.09765625 4.57421875 4.87109375]\n# [ 3.56640625 3.86328125 4.33984375 4.96875 5.375\n# 6.00390625 6.48046875 6.77734375]\n# [ 6.08203125 6.37890625 6.85546875 7.484375 7.890625\n# 8.51953125 8.99609375 9.29296875]\n# [ 7.70703125 8.00390625 8.48046875 9.109375 9.515625\n# 10.14453125 10.62109375 10.91796875]\n# [10.22265625 10.51953125 10.99609375 11.625 12.03125\n# 12.66015625 13.13671875 13.43359375]\n# [12.12890625 12.42578125 12.90234375 13.53125 13.9375\n# 14.56640625 15.04296875 15.33984375]\n# [13.31640625 13.61328125 14.08984375 14.71875 15.125\n# 15.75390625 16.23046875 16.52734375]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic')" + }, + { + "summary": "resize_upsample_scales_cubic_A_n0p5_exclude_outside", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n cubic_coeff_a=-0.5,\n exclude_outside=True\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 0.55882353 0.81494204 1.35698249 1.89705882 2.39705882\n# 2.93713516 3.47917561 3.73529412]\n# [ 1.58329755 1.83941606 2.38145651 2.92153285 3.42153285\n# 3.96160918 4.50364964 4.75976814]\n# [ 3.75145936 4.00757787 4.54961832 5.08969466 5.58969466\n# 6.12977099 6.67181144 6.92792995]\n# [ 5.91176471 6.16788321 6.70992366 7.25 7.75\n# 8.29007634 8.83211679 9.08823529]\n# [ 7.91176471 8.16788321 8.70992366 9.25 9.75\n# 10.29007634 10.83211679 11.08823529]\n# [10.07207005 10.32818856 10.87022901 11.41030534 11.91030534\n# 12.45038168 12.99242213 13.24854064]\n# [12.24023186 12.49635036 13.03839082 13.57846715 14.07846715\n# 14.61854349 15.16058394 15.41670245]\n# [13.26470588 13.52082439 14.06286484 14.60294118 15.10294118\n# 15.64301751 16.18505796 16.44117647]]]]\noutput = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.5), scale_factors=scales,\n exclude_outside=True).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic_A_n0p5_exclude_outside')" + }, + { + "summary": "resize_upsample_scales_cubic_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 1. 1.34110787 1.80029155 2.32944606 2.67055394\n# 3.19970845 3.65889213 4. ]\n# [ 2.36443149 2.70553936 3.16472303 3.69387755 4.03498542\n# 4.56413994 5.02332362 5.36443149]\n# [ 4.20116618 4.54227405 5.00145773 5.53061224 5.87172012\n# 6.40087464 6.86005831 7.20116618]\n# [ 6.31778426 6.65889213 7.1180758 7.64723032 7.98833819\n# 8.51749271 8.97667638 9.31778426]\n# [ 7.68221574 8.02332362 8.48250729 9.01166181 9.35276968\n# 9.8819242 10.34110787 10.68221574]\n# [ 9.79883382 10.13994169 10.59912536 11.12827988 11.46938776\n# 11.99854227 12.45772595 12.79883382]\n# [11.63556851 11.97667638 12.43586006 12.96501458 13.30612245\n# 13.83527697 14.29446064 14.63556851]\n# [13. 13.34110787 13.80029155 14.32944606 14.67055394\n# 15.19970845 15.65889213 16. ]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic_align_corners')" + }, + { + "summary": "resize_upsample_scales_cubic_asymmetric", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='cubic',\n coordinate_transformation_mode='asymmetric'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[ 1. 1.40625 2. 2.5 3. 3.59375 4.\n# 4.09375]\n# [ 2.625 3.03125 3.625 4.125 4.625 5.21875 5.625\n# 5.71875]\n# [ 5. 5.40625 6. 6.5 7. 7.59375 8.\n# 8.09375]\n# [ 7. 7.40625 8. 8.5 9. 9.59375 10.\n# 10.09375]\n# [ 9. 9.40625 10. 10.5 11. 11.59375 12.\n# 12.09375]\n# [11.375 11.78125 12.375 12.875 13.375 13.96875 14.375\n# 14.46875]\n# [13. 13.40625 14. 14.5 15. 15.59375 16.\n# 16.09375]\n# [13.375 13.78125 14.375 14.875 15.375 15.96875 16.375\n# 16.46875]]]]\noutput = interpolate_nd(data, lambda x: cubic_coeffs(x, A=-0.75), scale_factors=scales,\n coordinate_transformation_mode='asymmetric').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_cubic_asymmetric')" + }, + { + "summary": "resize_upsample_scales_linear", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[1. 1.25 1.75 2. ]\n# [1.5 1.75 2.25 2.5 ]\n# [2.5 2.75 3.25 3.5 ]\n# [3. 3.25 3.75 4. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_linear')" + }, + { + "summary": "resize_upsample_scales_linear_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='linear',\n coordinate_transformation_mode='align_corners'\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)\n\n# [[[[1. 1.33333333 1.66666667 2. ]\n# [1.66666667 2. 2.33333333 2.66666667]\n# [2.33333333 2.66666667 3. 3.33333333]\n# [3. 3.33333333 3.66666667 4. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs, scale_factors=scales, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_linear_align_corners')" + }, + { + "summary": "resize_upsample_scales_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\n# [[[[1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 2. 2. 2.]\n# [3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, scale_factors=scales).astype(np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_resize_upsample_scales_nearest')" + }, + { + "summary": "resize_upsample_sizes_cubic", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='cubic',\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 9, 10], dtype=np.int64)\n\n# [[[[ 0.45507922 0.64057922 0.97157922 1.42257922 1.90732922\n# 2.22332922 2.70807922 3.15907922 3.49007922 3.67557922]\n# [ 1.39437963 1.57987963 1.91087963 2.36187963 2.84662963\n# 3.16262963 3.64737963 4.09837963 4.42937963 4.61487963]\n# [ 2.95130693 3.13680693 3.46780693 3.91880693 4.40355693\n# 4.71955693 5.20430693 5.65530693 5.98630693 6.17180693]\n# [ 5.20525069 5.39075069 5.72175069 6.17275069 6.65750069\n# 6.97350069 7.45825069 7.90925069 8.24025069 8.42575069]\n# [ 6.88975 7.07525 7.40625 7.85725 8.342\n# 8.658 9.14275 9.59375 9.92475 10.11025 ]\n# [ 8.57424931 8.75974931 9.09074931 9.54174931 10.02649931\n# 10.34249931 10.82724931 11.27824931 11.60924931 11.79474931]\n# [10.82819307 11.01369307 11.34469307 11.79569307 12.28044307\n# 12.59644307 13.08119307 13.53219307 13.86319307 14.04869307]\n# [12.38512037 12.57062037 12.90162037 13.35262037 13.83737037\n# 14.15337037 14.63812037 15.08912037 15.42012037 15.60562037]\n# [13.32442078 13.50992078 13.84092078 14.29192078 14.77667078\n# 15.09267078 15.57742078 16.02842078 16.35942078 16.54492078]]]]\noutput = interpolate_nd(\n data, cubic_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_cubic')" + }, + { + "summary": "resize_upsample_sizes_nearest", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 7, 8], dtype=np.int64)\n\n# [[[[1. 1. 1. 1. 2. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2. 2.]\n# [3. 3. 3. 3. 4. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data, nearest_coeffs, output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest')" + }, + { + "summary": "resize_upsample_sizes_nearest_ceil_half_pixel", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n coordinate_transformation_mode='half_pixel',\n nearest_mode='ceil'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 8, 8], dtype=np.int64)\n\n# [[[[ 1. 2. 2. 3. 3. 4. 4. 4.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]]]]\noutput = interpolate_nd(\n data, lambda x: nearest_coeffs(x, mode='ceil'), output_size=sizes).astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest_ceil_half_pixel')" + }, + { + "summary": "resize_upsample_sizes_nearest_floor_align_corners", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n coordinate_transformation_mode='align_corners',\n nearest_mode='floor'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 8, 8], dtype=np.int64)\n\n# [[[[ 1. 1. 1. 2. 2. 3. 3. 4.]\n# [ 1. 1. 1. 2. 2. 3. 3. 4.]\n# [ 1. 1. 1. 2. 2. 3. 3. 4.]\n# [ 5. 5. 5. 6. 6. 7. 7. 8.]\n# [ 5. 5. 5. 6. 6. 7. 7. 8.]\n# [ 9. 9. 9. 10. 10. 11. 11. 12.]\n# [ 9. 9. 9. 10. 10. 11. 11. 12.]\n# [13. 13. 13. 14. 14. 15. 15. 16.]]]]\noutput = interpolate_nd(\n data, lambda x: nearest_coeffs(x, mode='floor'), output_size=sizes, coordinate_transformation_mode='align_corners').astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest_floor_align_corners')" + }, + { + "summary": "resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric", + "code": "node = onnx.helper.make_node(\n 'Resize',\n inputs=['X', '', '', 'sizes'],\n outputs=['Y'],\n mode='nearest',\n coordinate_transformation_mode='asymmetric',\n nearest_mode='round_prefer_ceil'\n)\n\ndata = np.array([[[\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n]]], dtype=np.float32)\n\nsizes = np.array([1, 1, 8, 8], dtype=np.int64)\n\n# [[[[ 1. 2. 2. 3. 3. 4. 4. 4.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 5. 6. 6. 7. 7. 8. 8. 8.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [ 9. 10. 10. 11. 11. 12. 12. 12.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]\n# [13. 14. 14. 15. 15. 16. 16. 16.]]]]\noutput = interpolate_nd(\n data, lambda x: nearest_coeffs(x, mode='round_prefer_ceil'),\n output_size=sizes, coordinate_transformation_mode='asymmetric').astype(np.float32)\n\nexpect(node, inputs=[data, sizes], outputs=[output],\n name='test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric')" + } + ] + }, + { + "name": "ReverseSequence", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Reverse batch of sequences having different lengths specified by `sequence_lens`.\n\nFor each slice i iterating on batch axis, the operator reverses the first sequence_lens[i] elements on time axis,\nand copies elements whose index's beyond sequence_lens[i] to the output. So the output slice i contains reversed\nsequences on the first sequence_lens[i] elements, then have original values copied for the other elements.\n\nExample 1:\n input = [[0.0, 4.0, 8.0, 12.0],\n [1.0, 5.0, 9.0, 13.0],\n [2.0, 6.0, 10.0, 14.0],\n [3.0, 7.0, 11.0, 15.0]]\n sequence_lens = [4, 3, 2, 1]\n time_axis = 0\n batch_axis = 1\n\n output = [[3.0, 6.0, 9.0, 12.0],\n [2.0, 5.0, 8.0, 13.0],\n [1.0, 4.0, 10.0, 14.0],\n [0.0, 7.0, 11.0, 15.0]]\n\nExample 2:\n input = [[0.0, 1.0, 2.0, 3.0 ],\n [4.0, 5.0, 6.0, 7.0 ],\n [8.0, 9.0, 10.0, 11.0],\n [12.0, 13.0, 14.0, 15.0]]\n sequence_lens = [1, 2, 3, 4]\n time_axis = 1\n batch_axis = 0\n\n output = [[0.0, 1.0, 2.0, 3.0 ],\n [5.0, 4.0, 6.0, 7.0 ],\n [10.0, 9.0, 8.0, 11.0],\n [15.0, 14.0, 13.0, 12.0]]\n", + "attributes": [ + { + "name": "batch_axis", + "type": "int64", + "required": false, + "default": 1, + "description": "(Optional) Specify which axis is batch axis. Must be one of 1 (default), or 0." + }, + { + "name": "time_axis", + "type": "int64", + "required": false, + "description": "(Optional) Specify which axis is time axis. Must be one of 0 (default), or 1." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Tensor of rank r >= 2." + }, + { + "name": "sequence_lens", + "type": "tensor(int64)", + "description": "Tensor specifying lengths of the sequences in a batch. It has shape `[batch_size]`." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Tensor with same shape of input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input and output types can be of any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "reversesequence_batch", + "code": "node = onnx.helper.make_node(\n 'ReverseSequence',\n inputs=['x', 'sequence_lens'],\n outputs=['y'],\n time_axis=1,\n batch_axis=0,\n)\nx = np.array([[0.0, 1.0, 2.0, 3.0],\n [4.0, 5.0, 6.0, 7.0],\n [8.0, 9.0, 10.0, 11.0],\n [12.0, 13.0, 14.0, 15.0]], dtype=np.float32)\nsequence_lens = np.array([1, 2, 3, 4], dtype=np.int64)\n\ny = np.array([[0.0, 1.0, 2.0, 3.0],\n [5.0, 4.0, 6.0, 7.0],\n [10.0, 9.0, 8.0, 11.0],\n [15.0, 14.0, 13.0, 12.0]], dtype=np.float32)\n\nexpect(node, inputs=[x, sequence_lens], outputs=[y],\n name='test_reversesequence_batch')" + }, + { + "summary": "reversesequence_time", + "code": "node = onnx.helper.make_node(\n 'ReverseSequence',\n inputs=['x', 'sequence_lens'],\n outputs=['y'],\n time_axis=0,\n batch_axis=1,\n)\nx = np.array([[0.0, 4.0, 8.0, 12.0],\n [1.0, 5.0, 9.0, 13.0],\n [2.0, 6.0, 10.0, 14.0],\n [3.0, 7.0, 11.0, 15.0]], dtype=np.float32)\nsequence_lens = np.array([4, 3, 2, 1], dtype=np.int64)\n\ny = np.array([[3.0, 6.0, 9.0, 12.0],\n [2.0, 5.0, 8.0, 13.0],\n [1.0, 4.0, 10.0, 14.0],\n [0.0, 7.0, 11.0, 15.0]], dtype=np.float32)\n\nexpect(node, inputs=[x, sequence_lens], outputs=[y],\n name='test_reversesequence_time')" + } + ] + }, + { + "name": "RoiAlign", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Region of Interest (RoI) align operation described in the\n[Mask R-CNN paper](https://arxiv.org/abs/1703.06870).\nRoiAlign consumes an input tensor X and region of interests (rois)\nto apply pooling across each RoI; it produces a 4-D tensor of shape\n(num_rois, C, output_height, output_width).\n\nRoiAlign is proposed to avoid the misalignment by removing\nquantizations while converting from original image into feature\nmap and from feature map into RoI feature; in each ROI bin,\nthe value of the sampled locations are computed directly\nthrough bilinear interpolation.\n", + "attributes": [ + { + "name": "mode", + "type": "string", + "required": false, + "default": "avg", + "description": "The pooling method. Two modes are supported: 'avg' and 'max'. Default is 'avg'." + }, + { + "name": "output_height", + "type": "int64", + "required": false, + "default": 1, + "description": "default 1; Pooled output Y's height." + }, + { + "name": "output_width", + "type": "int64", + "required": false, + "default": 1, + "description": "default 1; Pooled output Y's width." + }, + { + "name": "sampling_ratio", + "type": "int64", + "required": false, + "description": "Number of sampling points in the interpolation grid used to compute the output value of each pooled output bin. If > 0, then exactly sampling_ratio x sampling_ratio grid points are used. If == 0, then an adaptive number of grid points are used (computed as ceil(roi_width / output_width), and likewise for height). Default is 0." + }, + { + "name": "spatial_scale", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Multiplicative spatial scale factor to translate ROI coordinates from their input spatial scale to the scale used when pooling, i.e., spatial scale of the input feature map X relative to the input image. E.g.; default is 1.0f. " + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Input data tensor from the previous operator; 4-D feature map of shape (N, C, H, W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data." + }, + { + "name": "rois", + "type": "T1", + "description": "RoIs (Regions of Interest) to pool over; rois is 2-D input of shape (num_rois, 4) given as [[x1, y1, x2, y2], ...]. The RoIs' coordinates are in the coordinate system of the input image. Each coordinate set has a 1:1 correspondence with the 'batch_indices' input." + }, + { + "name": "batch_indices", + "type": "T2", + "description": "1-D tensor of shape (num_rois,) with each element denoting the index of the corresponding image in the batch." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T1", + "description": "RoI pooled output, 4-D tensor of shape (num_rois, C, output_height, output_width). The r-th batch element Y[r-1] is a pooled feature map corresponding to the r-th RoI X[r-1]." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain types to int tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "roialign_aligned_false", + "code": "node = onnx.helper.make_node(\n \"RoiAlign\",\n inputs=[\"X\", \"rois\", \"batch_indices\"],\n outputs=[\"Y\"],\n spatial_scale=1.0,\n output_height=5,\n output_width=5,\n sampling_ratio=2,\n coordinate_transformation_mode=\"output_half_pixel\",\n)\n\nX, batch_indices, rois = get_roi_align_input_values()\n# (num_rois, C, output_height, output_width)\nY = np.array(\n [\n [\n [\n [0.4664, 0.4466, 0.3405, 0.5688, 0.6068],\n [0.3714, 0.4296, 0.3835, 0.5562, 0.3510],\n [0.2768, 0.4883, 0.5222, 0.5528, 0.4171],\n [0.4713, 0.4844, 0.6904, 0.4920, 0.8774],\n [0.6239, 0.7125, 0.6289, 0.3355, 0.3495],\n ]\n ],\n [\n [\n [0.3022, 0.4305, 0.4696, 0.3978, 0.5423],\n [0.3656, 0.7050, 0.5165, 0.3172, 0.7015],\n [0.2912, 0.5059, 0.6476, 0.6235, 0.8299],\n [0.5916, 0.7389, 0.7048, 0.8372, 0.8893],\n [0.6227, 0.6153, 0.7097, 0.6154, 0.4585],\n ]\n ],\n [\n [\n [0.2384, 0.3379, 0.3717, 0.6100, 0.7601],\n [0.3767, 0.3785, 0.7147, 0.9243, 0.9727],\n [0.5749, 0.5826, 0.5709, 0.7619, 0.8770],\n [0.5355, 0.2566, 0.2141, 0.2796, 0.3600],\n [0.4365, 0.3504, 0.2887, 0.3661, 0.2349],\n ]\n ],\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, rois, batch_indices], outputs=[Y], name=\"test_roialign_aligned_false\")" + }, + { + "summary": "roialign_aligned_true", + "code": "node = onnx.helper.make_node(\n \"RoiAlign\",\n inputs=[\"X\", \"rois\", \"batch_indices\"],\n outputs=[\"Y\"],\n spatial_scale=1.0,\n output_height=5,\n output_width=5,\n sampling_ratio=2,\n coordinate_transformation_mode=\"half_pixel\",\n)\n\nX, batch_indices, rois = get_roi_align_input_values()\n# (num_rois, C, output_height, output_width)\nY = np.array(\n [\n [\n [\n [0.5178, 0.3434, 0.3229, 0.4474, 0.6344],\n [0.4031, 0.5366, 0.4428, 0.4861, 0.4023],\n [0.2512, 0.4002, 0.5155, 0.6954, 0.3465],\n [0.3350, 0.4601, 0.5881, 0.3439, 0.6849],\n [0.4932, 0.7141, 0.8217, 0.4719, 0.4039],\n ]\n ],\n [\n [\n [0.3070, 0.2187, 0.3337, 0.4880, 0.4870],\n [0.1871, 0.4914, 0.5561, 0.4192, 0.3686],\n [0.1433, 0.4608, 0.5971, 0.5310, 0.4982],\n [0.2788, 0.4386, 0.6022, 0.7000, 0.7524],\n [0.5774, 0.7024, 0.7251, 0.7338, 0.8163],\n ]\n ],\n [\n [\n [0.2393, 0.4075, 0.3379, 0.2525, 0.4743],\n [0.3671, 0.2702, 0.4105, 0.6419, 0.8308],\n [0.5556, 0.4543, 0.5564, 0.7502, 0.9300],\n [0.6626, 0.5617, 0.4813, 0.4954, 0.6663],\n [0.6636, 0.3721, 0.2056, 0.1928, 0.2478],\n ]\n ],\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, rois, batch_indices], outputs=[Y], name=\"test_roialign_aligned_true\")" + } + ] + }, + { + "name": "RoiAlign", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "Region of Interest (RoI) align operation described in the\n[Mask R-CNN paper](https://arxiv.org/abs/1703.06870).\nRoiAlign consumes an input tensor X and region of interests (rois)\nto apply pooling across each RoI; it produces a 4-D tensor of shape\n(num_rois, C, output_height, output_width).\n\nRoiAlign is proposed to avoid the misalignment by removing\nquantizations while converting from original image into feature\nmap and from feature map into RoI feature; in each ROI bin,\nthe value of the sampled locations are computed directly\nthrough bilinear interpolation.\n", + "attributes": [ + { + "name": "coordinate_transformation_mode", + "type": "string", + "required": false, + "default": "half_pixel", + "description": "Allowed values are 'half_pixel' and 'output_half_pixel'. Use the value 'half_pixel' to pixel shift the input coordinates by -0.5 (the recommended behavior). Use the value 'output_half_pixel' to omit the pixel shift for the input (use this for a backward-compatible behavior)." + }, + { + "name": "mode", + "type": "string", + "required": false, + "default": "avg", + "description": "The pooling method. Two modes are supported: 'avg' and 'max'. Default is 'avg'." + }, + { + "name": "output_height", + "type": "int64", + "required": false, + "default": 1, + "description": "default 1; Pooled output Y's height." + }, + { + "name": "output_width", + "type": "int64", + "required": false, + "default": 1, + "description": "default 1; Pooled output Y's width." + }, + { + "name": "sampling_ratio", + "type": "int64", + "required": false, + "description": "Number of sampling points in the interpolation grid used to compute the output value of each pooled output bin. If > 0, then exactly sampling_ratio x sampling_ratio grid points are used. If == 0, then an adaptive number of grid points are used (computed as ceil(roi_width / output_width), and likewise for height). Default is 0." + }, + { + "name": "spatial_scale", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Multiplicative spatial scale factor to translate ROI coordinates from their input spatial scale to the scale used when pooling, i.e., spatial scale of the input feature map X relative to the input image. E.g.; default is 1.0f. " + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Input data tensor from the previous operator; 4-D feature map of shape (N, C, H, W), where N is the batch size, C is the number of channels, and H and W are the height and the width of the data." + }, + { + "name": "rois", + "type": "T1", + "description": "RoIs (Regions of Interest) to pool over; rois is 2-D input of shape (num_rois, 4) given as [[x1, y1, x2, y2], ...]. The RoIs' coordinates are in the coordinate system of the input image. Each coordinate set has a 1:1 correspondence with the 'batch_indices' input." + }, + { + "name": "batch_indices", + "type": "T2", + "description": "1-D tensor of shape (num_rois,) with each element denoting the index of the corresponding image in the batch." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "Y", + "type": "T1", + "description": "RoI pooled output, 4-D tensor of shape (num_rois, C, output_height, output_width). The r-th batch element Y[r-1] is a pooled feature map corresponding to the r-th RoI X[r-1]." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain types to float tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain types to int tensors.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "roialign_aligned_false", + "code": "node = onnx.helper.make_node(\n \"RoiAlign\",\n inputs=[\"X\", \"rois\", \"batch_indices\"],\n outputs=[\"Y\"],\n spatial_scale=1.0,\n output_height=5,\n output_width=5,\n sampling_ratio=2,\n coordinate_transformation_mode=\"output_half_pixel\",\n)\n\nX, batch_indices, rois = get_roi_align_input_values()\n# (num_rois, C, output_height, output_width)\nY = np.array(\n [\n [\n [\n [0.4664, 0.4466, 0.3405, 0.5688, 0.6068],\n [0.3714, 0.4296, 0.3835, 0.5562, 0.3510],\n [0.2768, 0.4883, 0.5222, 0.5528, 0.4171],\n [0.4713, 0.4844, 0.6904, 0.4920, 0.8774],\n [0.6239, 0.7125, 0.6289, 0.3355, 0.3495],\n ]\n ],\n [\n [\n [0.3022, 0.4305, 0.4696, 0.3978, 0.5423],\n [0.3656, 0.7050, 0.5165, 0.3172, 0.7015],\n [0.2912, 0.5059, 0.6476, 0.6235, 0.8299],\n [0.5916, 0.7389, 0.7048, 0.8372, 0.8893],\n [0.6227, 0.6153, 0.7097, 0.6154, 0.4585],\n ]\n ],\n [\n [\n [0.2384, 0.3379, 0.3717, 0.6100, 0.7601],\n [0.3767, 0.3785, 0.7147, 0.9243, 0.9727],\n [0.5749, 0.5826, 0.5709, 0.7619, 0.8770],\n [0.5355, 0.2566, 0.2141, 0.2796, 0.3600],\n [0.4365, 0.3504, 0.2887, 0.3661, 0.2349],\n ]\n ],\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, rois, batch_indices], outputs=[Y], name=\"test_roialign_aligned_false\")" + }, + { + "summary": "roialign_aligned_true", + "code": "node = onnx.helper.make_node(\n \"RoiAlign\",\n inputs=[\"X\", \"rois\", \"batch_indices\"],\n outputs=[\"Y\"],\n spatial_scale=1.0,\n output_height=5,\n output_width=5,\n sampling_ratio=2,\n coordinate_transformation_mode=\"half_pixel\",\n)\n\nX, batch_indices, rois = get_roi_align_input_values()\n# (num_rois, C, output_height, output_width)\nY = np.array(\n [\n [\n [\n [0.5178, 0.3434, 0.3229, 0.4474, 0.6344],\n [0.4031, 0.5366, 0.4428, 0.4861, 0.4023],\n [0.2512, 0.4002, 0.5155, 0.6954, 0.3465],\n [0.3350, 0.4601, 0.5881, 0.3439, 0.6849],\n [0.4932, 0.7141, 0.8217, 0.4719, 0.4039],\n ]\n ],\n [\n [\n [0.3070, 0.2187, 0.3337, 0.4880, 0.4870],\n [0.1871, 0.4914, 0.5561, 0.4192, 0.3686],\n [0.1433, 0.4608, 0.5971, 0.5310, 0.4982],\n [0.2788, 0.4386, 0.6022, 0.7000, 0.7524],\n [0.5774, 0.7024, 0.7251, 0.7338, 0.8163],\n ]\n ],\n [\n [\n [0.2393, 0.4075, 0.3379, 0.2525, 0.4743],\n [0.3671, 0.2702, 0.4105, 0.6419, 0.8308],\n [0.5556, 0.4543, 0.5564, 0.7502, 0.9300],\n [0.6626, 0.5617, 0.4813, 0.4954, 0.6663],\n [0.6636, 0.3721, 0.2056, 0.1928, 0.2478],\n ]\n ],\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[X, rois, batch_indices], outputs=[Y], name=\"test_roialign_aligned_true\")" + } + ] + }, + { + "name": "Round", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Round takes one input Tensor and rounds the values, element-wise, meaning\nit finds the nearest integer for each value.\nIn case of halfs, the rule is to round them to the nearest even integer.\nThe output tensor has the same shape and type as the input.\n\nExamples:\n```\nround([0.9]) = [1.0]\nround([2.5]) = [2.0]\nround([2.3]) = [2.0]\nround([1.5]) = [2.0]\nround([-4.5]) = [-4.0]\n```\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "round", + "code": "node = onnx.helper.make_node(\n 'Round',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([0.1, 0.5, 0.9, 1.2, 1.5,\n 1.8, 2.3, 2.5, 2.7, -1.1,\n -1.5, -1.9, -2.2, -2.5, -2.8]).astype(np.float32)\ny = np.array([0., 0., 1., 1., 2.,\n 2., 2., 2., 3., -1.,\n -2., -2., -2., -2., -3.]).astype(np.float32) # expected output\nexpect(node, inputs=[x], outputs=[y],\n name='test_round')" + } + ] + }, + { + "name": "SVMClassifier", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Support Vector Machine classifier\n", + "attributes": [ + { + "name": "classlabels_ints", + "type": "int64[]", + "required": false, + "description": "Class labels if using integer labels.
One and only one of the 'classlabels_*' attributes must be defined." + }, + { + "name": "classlabels_strings", + "type": "string[]", + "required": false, + "description": "Class labels if using string labels.
One and only one of the 'classlabels_*' attributes must be defined." + }, + { + "name": "coefficients", + "type": "float32[]", + "required": false, + "description": "" + }, + { + "name": "kernel_params", + "type": "float32[]", + "required": false, + "description": "List of 3 elements containing gamma, coef0, and degree, in that order. Zero if unused for the kernel." + }, + { + "name": "kernel_type", + "type": "string", + "required": false, + "default": "LINEAR", + "description": "The kernel type, one of 'LINEAR,' 'POLY,' 'RBF,' 'SIGMOID'." + }, + { + "name": "post_transform", + "type": "string", + "required": false, + "default": "NONE", + "description": "Indicates the transform to apply to the score.
One of 'NONE,' 'SOFTMAX,' 'LOGISTIC,' 'SOFTMAX_ZERO,' or 'PROBIT'" + }, + { + "name": "prob_a", + "type": "float32[]", + "required": false, + "description": "First set of probability coefficients." + }, + { + "name": "prob_b", + "type": "float32[]", + "required": false, + "description": "Second set of probability coefficients. This array must be same size as prob_a.
If these are provided then output Z are probability estimates, otherwise they are raw scores." + }, + { + "name": "rho", + "type": "float32[]", + "required": false, + "description": "" + }, + { + "name": "support_vectors", + "type": "float32[]", + "required": false, + "description": "" + }, + { + "name": "vectors_per_class", + "type": "int64[]", + "required": false, + "description": "" + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Data to be classified." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "Classification outputs (one class per example)." + }, + { + "name": "Z", + "type": "tensor(float)", + "description": "Class scores (one per class per example), if prob_a and prob_b are provided they are probabilities for each class, otherwise they are raw scores." + } + ], + "min_output": 2, + "max_output": 2, + "type_constraints": [ + { + "description": "The input must be a tensor of a numeric type, either [C] or [N,C].", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + }, + { + "description": "The output type will be a tensor of strings or integers, depending on which of the the classlabels_* attributes is used. Its size will match the bactch size of the input.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "SVMRegressor", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Support Vector Machine regression prediction and one-class SVM anomaly detection.\n", + "attributes": [ + { + "name": "coefficients", + "type": "float32[]", + "required": false, + "description": "Support vector coefficients." + }, + { + "name": "kernel_params", + "type": "float32[]", + "required": false, + "description": "List of 3 elements containing gamma, coef0, and degree, in that order. Zero if unused for the kernel." + }, + { + "name": "kernel_type", + "type": "string", + "required": false, + "default": "LINEAR", + "description": "The kernel type, one of 'LINEAR,' 'POLY,' 'RBF,' 'SIGMOID'." + }, + { + "name": "n_supports", + "type": "int64", + "required": false, + "description": "The number of support vectors." + }, + { + "name": "one_class", + "type": "int64", + "required": false, + "description": "Flag indicating whether the regression is a one-class SVM or not." + }, + { + "name": "post_transform", + "type": "string", + "required": false, + "default": "NONE", + "description": "Indicates the transform to apply to the score.
One of 'NONE,' 'SOFTMAX,' 'LOGISTIC,' 'SOFTMAX_ZERO,' or 'PROBIT.'" + }, + { + "name": "rho", + "type": "float32[]", + "required": false, + "description": "" + }, + { + "name": "support_vectors", + "type": "float32[]", + "required": false, + "description": "Chosen support vectors" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Data to be regressed." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(float)", + "description": "Regression outputs (one score per target per example)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input type must be a tensor of a numeric type, either [C] or [N,C].", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + } + ] + }, + { + "name": "Scaler", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Rescale input data, for example to standardize features by removing the mean and scaling to unit variance.\n", + "attributes": [ + { + "name": "offset", + "type": "float32[]", + "required": false, + "description": "First, offset by this.
Can be length of features in an [N,F] tensor or length 1, in which case it applies to all features, regardless of dimension count." + }, + { + "name": "scale", + "type": "float32[]", + "required": false, + "description": "Second, multiply by this.
Can be length of features in an [N,F] tensor or length 1, in which case it applies to all features, regardless of dimension count.
Must be same length as 'offset'" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Data to be scaled." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(float)", + "description": "Scaled output data." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input must be a tensor of a numeric type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + } + ] + }, + { + "name": "Scan", + "module": "ai.onnx", + "version": 8, + "support_level": "common", + "description": "Scan can be used to iterate over one or more scan_input tensors,\nconstructing zero or more scan_output tensors. It combines ideas from general recurrences,\nfunctional programming constructs such as scan, fold, map, and zip, and is intended to enable\ngeneralizations of RNN-like constructs for sequence-to-sequence processing.\nOther tensors (referred to as state_variables here) can be used to carry a state\nwhen iterating from one element to another (similar to hidden-state in RNNs, also referred\nto as loop-carried dependences in the context of loops). All these tensors are required to\nhave the same shape in each iteration of the loop (a restriction imposed to enable efficient\nmemory allocation). Many common usages involve a single scan_input tensor (where functionality\nsimilar to scan, fold and map can be obtained). When more than one scan_input is used,\na behavior similar to zip is obtained.\n\nThe attribute body must be a graph, specifying the computation to be performed in\nevery iteration. It takes as input the current values of the state_variables and\nthe current iterated element of the scan_inputs. It must return the (updated) values\nof the state_variables and zero or more scan_output_element tensors. The values of the\nscan_output_element tensors are concatenated over all the iterations to produce the\nscan_output values of the scan construct (similar to the concatenated intermediate\nhidden-state values of RNN-like constructs).\n\nThe scan operation returns the final values of the state_variables as well as the\nscan_outputs.\n\nThe operation supports batching, and the batch-axis is required to be 0.\nWhen multiple scan_input tensors are used, they must all have the same batch-size,\nand they must all have the same maximum-sequence-length (the dimensionality of the\nsequence axis or scan axis). The sequence axis or scan axis is required to be 1.\n\nThe operation has an optional sequence_lens input (of shape [BATCH_SIZE]) to\nallow variable length sequences of length <= the maximum-sequence-length. If this\ninput is not specified, all sequences are assumed to be of length equal to\nmaximum-sequence-length. For variable length input sequences, the scan_outputs\nwill consist of a sequence of same length as the input, padded to the\nmaximum-sequence-length.\n\nThe optional attribute directions can be used to scan a sequence in the reverse direction.\nIf this attribute is omitted, all sequences are scanned in the forward direction.\nA bidirectional scan be performed by specifying the same tensor input twice in the\nscan_inputs, once with a forward direction, and once with a backward direction.\n\nNote that because of the ONNX restriction that only the last parameter of an operator can\nbe variadic, the initial-states and scan-inputs are listed together as one input parameter.\nSimilarly, the final-states and scan-outputs are listed together as one output parameter.\nThe attribute num_scan_inputs indicates the number M of scan-inputs.\n\nThe behavior of\n\n Scan <\n num_scan_inputs = m,\n body = loop-body\n > (sequence_lengths, init_1, ..., init_n, scan_1, ..., scan_m)\n\nis equivalent to the following pseudo-code:\n\n // T.shape[0] denotes the batch-size of T\n // The batch-size of scan_1, ..., scan_m are all required to be equal\n batch_size = scan_1.shape[0];\n\n // scan_i.shape[1] denotes the (max) sequence-length of scan_i\n // scan_i.shape[1] is required to be equal to scan_j.shape[1] for all i,j.\n max_sequence_length = scan_1.shape[1];\n\n for (int batch = 0; batch < batch_size; ++batch) {\n // initialize state-variables\n st_1 = init_1; ... st_n = init_n;\n // initialize scan-output variables: [] denotes an empty tensor\n scan_out_1 = []; ...; scan_out_k = [];\n // identify number of iterations:\n N = (sequence_lengths specified) ? sequence_lengths[batch] : max_sequence_length;\n\n // execute loop\n for (int t = 0; t < N; ++t) {\n // generate the scan-input elements: the notation T[t] indicates the sub-tensor\n // of rank one less than T obtained by indexing T at position t along axis k.\n si_1 = (scan_1[batch])[t];\n ... ;\n si_m = (scan_m[batch])[t];\n // execute loop-body\n st_1, ..., st_n, so_1, ..., so_k = loop-body(st_1, ..., st_n, si_1, ..., si_m)\n // accumulate the scan-output elements\n scan_out_1 = Concat(scan_out_1, so_1); ... ; scan_out_k = Concat(scan_out_k, so_k);\n }\n // accumulate the outputs for this batch:\n bst_1[batch] = st_1; ..., bst_n[batch] = st_n;\n // Note scan-outputs will have size max_sequence_length, but only first N values will be meaningful.\n // The remaining values have an undefined value.\n b_scan_out_1[batch] = scan_out_1; ...; b_scan_out_k[batch] = scan_out_k;\n }\n return bst_1, ..., bst_n, b_scan_out_1, ..., b_scan_out_k;\n\n\n\n*Sample usage: Encoding RNN using a Scan*\n\nThe following example shows how a simple RNN over an input tensor %X, with weight tensor %Wi,\nrecurrence weight tensor %Ri, bias tensors %Wbi and %Rbi, and initial hidden-state %H_0 can\nbe encoded as a ScanLoop. Note that the loop-body is a nested graph, and it directly computes\n%Wi, %Ri, %Wbi, and %Rbi (typically constants or initializers in the body graph). If these\nvalues are computed in the outer graph, they need to be passed in as extra state_variables.\n\n graph rnn-encoding {\n %H_0 = ...\n %X = ...\n %Y_h, %Y = Scan[body = , num_scan_inputs=1](\"\", %H_0, %X)\n return %Y, %Y_h\n }\n\n graph rnn-cell-1 (\n %H_tminus1[FLOAT, tensor]\n %X_t[FLOAT, tensor]\n ) {\n %Wi = ...\n %Ri = ...\n %Wbi = ...\n %Rbi = ...\n %t1 = X_t * (Wi^T)\n %t2 = H_tminus1*(Ri^T)\n %t3 = Add(%t1, %t2)\n %t4 = Add(%t3, %Wbi)\n %t5 = Add(%t4, %Rbi)\n %Ht = Tanh(%t5)\n %Accumulate = Identity(%Ht)\n return %Ht, %Accumulate\n }\n\n", + "attributes": [ + { + "name": "body", + "type": "graph", + "required": true, + "description": "The graph run each iteration. It has N+M inputs: (loop state variables..., scan_input_elts...). It has N+K outputs: (loop state variables..., scan_output_elts...). Each scan_output is created by concatenating the value of the specified scan_output_elt value at the end of each iteration of the loop. It is an error if the dimensions of these values change across loop iterations." + }, + { + "name": "directions", + "type": "int64[]", + "required": false, + "description": "An optional list of M flags. The i-th element of the list specifies the direction to be scanned for the i-th scan_input tensor: 0 indicates forward direction and 1 indicates reverse direction. If omitted, all scan_input tensors will be scanned in the forward direction." + }, + { + "name": "num_scan_inputs", + "type": "int64", + "required": true, + "description": "An attribute specifying the number of scan_inputs M. " + } + ], + "inputs": [ + { + "name": "sequence_lens", + "type": "I", + "option": "optional", + "description": "Optional tensor specifying lengths of the sequences in a batch. If this input is not specified, all sequences are assumed to be of the maximum sequence length (the dimension of the sequence axis of the scan_input tensors)." + }, + { + "name": "initial_state_and_scan_inputs", + "type": "V", + "list": true, + "description": "Initial values of the loop's N state variables followed by M scan_inputs" + } + ], + "min_input": 2, + "max_input": 2147483647, + "outputs": [ + { + "name": "final_state_and_scan_outputs", + "type": "V", + "list": true, + "description": "Final values of the loop's N state variables followed by K scan_outputs" + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "2 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Int64 tensor", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + }, + { + "description": "All Tensor types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "scan_8", + "code": "# Given an input sequence [x1, ..., xN], sum up its elements using a scan\n# returning the final state (x1+x2+...+xN) as well the scan_output\n# [x1, x1+x2, ..., x1+x2+...+xN]\n#\n# create graph to represent scan body\nsum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])\nnext = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])\nsum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['sum_in', 'next'],\n outputs=['sum_out']\n)\nid_node = onnx.helper.make_node(\n 'Identity',\n inputs=['sum_out'],\n outputs=['scan_out']\n)\nscan_body = onnx.helper.make_graph(\n [add_node, id_node],\n 'scan_body',\n [sum_in, next],\n [sum_out, scan_out]\n)\n# create scan op node\nno_sequence_lens = '' # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Scan',\n inputs=[no_sequence_lens, 'initial', 'x'],\n outputs=['y', 'z'],\n num_scan_inputs=1,\n body=scan_body\n)\n# create inputs for batch-size 1, sequence-length 3, inner dimension 2\ninitial = np.array([0, 0]).astype(np.float32).reshape((1, 2))\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((1, 3, 2))\n# final state computed = [1 + 3 + 5, 2 + 4 + 6]\ny = np.array([9, 12]).astype(np.float32).reshape((1, 2))\n# scan-output computed\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((1, 3, 2))\n\nexpect(node, inputs=[initial, x], outputs=[y, z],\n name='test_scan_sum', opset_imports=[onnx.helper.make_opsetid(\"\", 8)])" + }, + { + "summary": "scan_9", + "code": "# Given an input sequence [x1, ..., xN], sum up its elements using a scan\n# returning the final state (x1+x2+...+xN) as well the scan_output\n# [x1, x1+x2, ..., x1+x2+...+xN]\n#\n# create graph to represent scan body\nsum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])\nnext = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])\nsum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['sum_in', 'next'],\n outputs=['sum_out']\n)\nid_node = onnx.helper.make_node(\n 'Identity',\n inputs=['sum_out'],\n outputs=['scan_out']\n)\nscan_body = onnx.helper.make_graph(\n [add_node, id_node],\n 'scan_body',\n [sum_in, next],\n [sum_out, scan_out]\n)\n# create scan op node\nnode = onnx.helper.make_node(\n 'Scan',\n inputs=['initial', 'x'],\n outputs=['y', 'z'],\n num_scan_inputs=1,\n body=scan_body\n)\n# create inputs for sequence-length 3, inner dimension 2\ninitial = np.array([0, 0]).astype(np.float32).reshape((2,))\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final state computed = [1 + 3 + 5, 2 + 4 + 6]\ny = np.array([9, 12]).astype(np.float32).reshape((2,))\n# scan-output computed\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(node, inputs=[initial, x], outputs=[y, z],\n name='test_scan9_sum', opset_imports=[onnx.helper.make_opsetid(\"\", 9)])" + } + ] + }, + { + "name": "Scan", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Scan can be used to iterate over one or more scan_input tensors,\nconstructing zero or more scan_output tensors. It combines ideas from general recurrences,\nfunctional programming constructs such as scan, fold, map, and zip, and is intended to enable\ngeneralizations of RNN-like constructs for sequence-to-sequence processing.\nOther tensors (referred to as state_variables here) can be used to carry a state\nwhen iterating from one element to another (similar to hidden-state in RNNs, also referred\nto as loop-carried dependences in the context of loops).\nMany common usages involve a single scan_input tensor (where functionality\nsimilar to scan, fold and map can be obtained). When more than one scan_input is used,\na behavior similar to zip is obtained.\n\nThe attribute body must be a graph, specifying the computation to be performed in\nevery iteration. It takes as input the current values of the state_variables and\nthe current iterated element of the scan_inputs. It must return the (updated) values\nof the state_variables and zero or more scan_output_element tensors. The values of the\nscan_output_element tensors are concatenated over all the iterations to produce the\nscan_output values of the scan construct (similar to the concatenated intermediate\nhidden-state values of RNN-like constructs). All the output tensors (state_variables as\nwell as scan_output_element tensors) are required to have the same shape in each iteration\nof the loop (a restriction imposed to enable efficient memory allocation).\n\nNote that the iterated element passed to the body subgraph does not have a sequence\naxis. It will have a rank one less than the rank of the corresponding scan_input.\n\nThe scan operation returns the final values of the state_variables as well as the\nscan_outputs.\n\nThe optional attribute scan_input_directions specifies the direction (forward or backward)\nfor each scan input. If this attribute is omitted, all sequences are scanned in the forward\ndirection. A bidirectional scan may be performed by specifying the same tensor input twice\nin the scan_inputs, once with a forward direction, and once with a backward direction.\n\nThe scan_output of the operation is produced by concatenating the scan_output_element\nvalues produced by the body in each iteration. The optional attribute scan_output_directions\nspecifies the direction in which scan_output is constructed (by appending or prepending the\nscan_output_element to scan_output in each iteration) for each scan_output. If this attribute\nis omitted, the scan_output_element is appended to the scan_output in each iteration.\n\nThe optional attribute scan_input_axes specifies the axis to be scanned for each scan_input.\nIf omitted, every scan_input will be scanned in axis 0. For example, if axis 0 is the\nbatch axis and axis 1 is the time axis (to be scanned), specify an axis value of 1.\nNote that scanning a non-zero axis may be less efficient than scanning axis zero.\n\nThe optional attribute scan_output_axes specifies the axis along which the scan_outputs\nare accumulated for each scan_output. For example, if axis 1 is the time axis (to be\nscanned) for both inputs and outputs, specify a scan_input axis and scan_output axis\nvalue of 1.\n\nNote that because of the ONNX restriction that only the last parameter of an operator can\nbe variadic, the initial-states and scan-inputs are listed together as one input parameter.\nSimilarly, the final-states and scan-outputs are listed together as one output parameter.\nThe attribute num_scan_inputs indicates the number M of scan-inputs.\n\nThe behavior of\n\n Scan <\n num_scan_inputs = m,\n body = loop-body,\n scan_input_axes = [axis_1, ..., axis_m]\n > (init_1, ..., init_n, scan_1, ..., scan_m)\n\nis equivalent to the following pseudo-code:\n\n // scan_i.shape[axis_i] denotes the (max) sequence-length of scan_i\n // scan_i.shape[axis_i] is required to be equal to scan_j.shape[axis_j] for all i,j.\n sequence_length = scan_1.shape[axis_1];\n\n // initialize state-variables\n st_1 = init_1; ... st_n = init_n;\n // initialize scan-output variables: [] denotes an empty tensor\n scan_out_1 = []; ...; scan_out_k = [];\n // identify number of iterations:\n\n // execute loop\n for (int t = 0; t < sequence_length; ++t) {\n // generate the scan-input elements: the notation T[t] indicates the sub-tensor\n // of rank one less than T obtained by indexing T at position t along axis k.\n si_1 = scan_1[t];\n ... ;\n si_m = scan_m[t];\n // execute loop-body\n st_1, ..., st_n, so_1, ..., so_k = loop-body(st_1, ..., st_n, si_1, ..., si_m)\n // accumulate the scan-output elements\n scan_out_1 = Concat(scan_out_1, so_1); ... ; scan_out_k = Concat(scan_out_k, so_k);\n }\n\n return st_1, ..., st_n, scan_out_1, ..., scan_out_k;\n\n*Sample usage: Encoding RNN using a Scan*\n\nThe following example shows how a simple RNN over an input tensor %X, with weight tensor %Wi,\nrecurrence weight tensor %Ri, bias tensors %Wbi and %Rbi, and initial hidden-state %H_0 can\nbe encoded as a ScanLoop. Note that the loop-body is a nested graph, and it directly computes\n%Wi, %Ri, %Wbi, and %Rbi (typically constants or initializers in the body graph). If these\nvalues are computed in the outer graph, they need to be passed in as extra state_variables.\n\n graph rnn-encoding {\n %H_0 = ...\n %X = ...\n %Y_h, %Y = Scan[body = , num_scan_inputs=1](%H_0, %X)\n return %Y, %Y_h\n }\n\n graph rnn-cell-1 (\n %H_tminus1[FLOAT, tensor]\n %X_t[FLOAT, tensor]\n ) {\n %Wi = ...\n %Ri = ...\n %Wbi = ...\n %Rbi = ...\n %t1 = X_t * (Wi^T)\n %t2 = H_tminus1*(Ri^T)\n %t3 = Add(%t1, %t2)\n %t4 = Add(%t3, %Wbi)\n %t5 = Add(%t4, %Rbi)\n %Ht = Tanh(%t5)\n %Accumulate = Identity(%Ht)\n return %Ht, %Accumulate\n }\n\n", + "attributes": [ + { + "name": "body", + "type": "graph", + "required": true, + "description": "The graph run each iteration. It has N+M inputs: (loop state variables..., scan_input_elts...). It has N+K outputs: (loop state variables..., scan_output_elts...). Each scan_output is created by concatenating the value of the specified scan_output_elt value at the end of each iteration of the loop. It is an error if the dimensions of these values change across loop iterations." + }, + { + "name": "num_scan_inputs", + "type": "int64", + "required": true, + "description": "An attribute specifying the number of scan_inputs M. " + }, + { + "name": "scan_input_axes", + "type": "int64[]", + "required": false, + "description": "An optional list of M flags. The i-th element of the list specifies the axis to be scanned (the sequence axis) for the i-th scan_input. If omitted, 0 will be used as the scan axis for every scan_input." + }, + { + "name": "scan_input_directions", + "type": "int64[]", + "required": false, + "description": "An optional list of M flags. The i-th element of the list specifies the direction to be scanned for the i-th scan_input tensor: 0 indicates forward direction and 1 indicates reverse direction. If omitted, all scan_input tensors will be scanned in the forward direction." + }, + { + "name": "scan_output_axes", + "type": "int64[]", + "required": false, + "description": "An optional list of K flags. The i-th element of the list specifies the axis for the i-th scan_output. The scan outputs are accumulated along the specified axis. If omitted, 0 will be used as the scan axis for every scan_output." + }, + { + "name": "scan_output_directions", + "type": "int64[]", + "required": false, + "description": "An optional list of K flags, one for each scan_output. The i-th element of the list specifies whether the i-th scan_output should be constructed by appending or prepending a new value in each iteration: 0 indicates appending and 1 indicates prepending. If omitted, all scan_output tensors will be produced by appending a value in each iteration." + } + ], + "inputs": [ + { + "name": "initial_state_and_scan_inputs", + "type": "V", + "list": true, + "description": "Initial values of the loop's N state variables followed by M scan_inputs" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "final_state_and_scan_outputs", + "type": "V", + "list": true, + "description": "Final values of the loop's N state variables followed by K scan_outputs" + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "1 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "scan_8", + "code": "# Given an input sequence [x1, ..., xN], sum up its elements using a scan\n# returning the final state (x1+x2+...+xN) as well the scan_output\n# [x1, x1+x2, ..., x1+x2+...+xN]\n#\n# create graph to represent scan body\nsum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])\nnext = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])\nsum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['sum_in', 'next'],\n outputs=['sum_out']\n)\nid_node = onnx.helper.make_node(\n 'Identity',\n inputs=['sum_out'],\n outputs=['scan_out']\n)\nscan_body = onnx.helper.make_graph(\n [add_node, id_node],\n 'scan_body',\n [sum_in, next],\n [sum_out, scan_out]\n)\n# create scan op node\nno_sequence_lens = '' # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Scan',\n inputs=[no_sequence_lens, 'initial', 'x'],\n outputs=['y', 'z'],\n num_scan_inputs=1,\n body=scan_body\n)\n# create inputs for batch-size 1, sequence-length 3, inner dimension 2\ninitial = np.array([0, 0]).astype(np.float32).reshape((1, 2))\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((1, 3, 2))\n# final state computed = [1 + 3 + 5, 2 + 4 + 6]\ny = np.array([9, 12]).astype(np.float32).reshape((1, 2))\n# scan-output computed\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((1, 3, 2))\n\nexpect(node, inputs=[initial, x], outputs=[y, z],\n name='test_scan_sum', opset_imports=[onnx.helper.make_opsetid(\"\", 8)])" + }, + { + "summary": "scan_9", + "code": "# Given an input sequence [x1, ..., xN], sum up its elements using a scan\n# returning the final state (x1+x2+...+xN) as well the scan_output\n# [x1, x1+x2, ..., x1+x2+...+xN]\n#\n# create graph to represent scan body\nsum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])\nnext = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])\nsum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['sum_in', 'next'],\n outputs=['sum_out']\n)\nid_node = onnx.helper.make_node(\n 'Identity',\n inputs=['sum_out'],\n outputs=['scan_out']\n)\nscan_body = onnx.helper.make_graph(\n [add_node, id_node],\n 'scan_body',\n [sum_in, next],\n [sum_out, scan_out]\n)\n# create scan op node\nnode = onnx.helper.make_node(\n 'Scan',\n inputs=['initial', 'x'],\n outputs=['y', 'z'],\n num_scan_inputs=1,\n body=scan_body\n)\n# create inputs for sequence-length 3, inner dimension 2\ninitial = np.array([0, 0]).astype(np.float32).reshape((2,))\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final state computed = [1 + 3 + 5, 2 + 4 + 6]\ny = np.array([9, 12]).astype(np.float32).reshape((2,))\n# scan-output computed\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(node, inputs=[initial, x], outputs=[y, z],\n name='test_scan9_sum', opset_imports=[onnx.helper.make_opsetid(\"\", 9)])" + } + ] + }, + { + "name": "Scan", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Scan can be used to iterate over one or more scan_input tensors,\nconstructing zero or more scan_output tensors. It combines ideas from general recurrences,\nfunctional programming constructs such as scan, fold, map, and zip, and is intended to enable\ngeneralizations of RNN-like constructs for sequence-to-sequence processing.\nOther tensors (referred to as state_variables here) can be used to carry a state\nwhen iterating from one element to another (similar to hidden-state in RNNs, also referred\nto as loop-carried dependences in the context of loops).\nMany common usages involve a single scan_input tensor (where functionality\nsimilar to scan, fold and map can be obtained). When more than one scan_input is used,\na behavior similar to zip is obtained.\n\nThe attribute body must be a graph, specifying the computation to be performed in\nevery iteration. It takes as input the current values of the state_variables and\nthe current iterated element of the scan_inputs. It must return the (updated) values\nof the state_variables and zero or more scan_output_element tensors. The values of the\nscan_output_element tensors are concatenated over all the iterations to produce the\nscan_output values of the scan construct (similar to the concatenated intermediate\nhidden-state values of RNN-like constructs). All the output tensors (state_variables as\nwell as scan_output_element tensors) are required to have the same shape in each iteration\nof the loop (a restriction imposed to enable efficient memory allocation).\n\nNote that the iterated element passed to the body subgraph does not have a sequence\naxis. It will have a rank one less than the rank of the corresponding scan_input.\n\nThe scan operation returns the final values of the state_variables as well as the\nscan_outputs.\n\nThe optional attribute scan_input_directions specifies the direction (forward or backward)\nfor each scan input. If this attribute is omitted, all sequences are scanned in the forward\ndirection. A bidirectional scan may be performed by specifying the same tensor input twice\nin the scan_inputs, once with a forward direction, and once with a backward direction.\n\nThe scan_output of the operation is produced by concatenating the scan_output_element\nvalues produced by the body in each iteration. The optional attribute scan_output_directions\nspecifies the direction in which scan_output is constructed (by appending or prepending the\nscan_output_element to scan_output in each iteration) for each scan_output. If this attribute\nis omitted, the scan_output_element is appended to the scan_output in each iteration.\n\nThe optional attribute scan_input_axes specifies the axis to be scanned for each scan_input.\nIf omitted, every scan_input will be scanned in axis 0. For example, if axis 0 is the\nbatch axis and axis 1 is the time axis (to be scanned), specify an axis value of 1.\nNote that scanning a non-zero axis may be less efficient than scanning axis zero.\n\nThe optional attribute scan_output_axes specifies the axis along which the scan_outputs\nare accumulated for each scan_output. For example, if axis 1 is the time axis (to be\nscanned) for both inputs and outputs, specify a scan_input axis and scan_output axis\nvalue of 1.\n\nNote that because of the ONNX restriction that only the last parameter of an operator can\nbe variadic, the initial-states and scan-inputs are listed together as one input parameter.\nSimilarly, the final-states and scan-outputs are listed together as one output parameter.\nThe attribute num_scan_inputs indicates the number M of scan-inputs.\n\nThe behavior of\n\n Scan <\n num_scan_inputs = m,\n body = loop-body,\n scan_input_axes = [axis_1, ..., axis_m]\n > (init_1, ..., init_n, scan_1, ..., scan_m)\n\nis equivalent to the following pseudo-code:\n\n // scan_i.shape[axis_i] denotes the (max) sequence-length of scan_i\n // scan_i.shape[axis_i] is required to be equal to scan_j.shape[axis_j] for all i,j.\n sequence_length = scan_1.shape[axis_1];\n\n // initialize state-variables\n st_1 = init_1; ... st_n = init_n;\n // initialize scan-output variables: [] denotes an empty tensor\n scan_out_1 = []; ...; scan_out_k = [];\n // identify number of iterations:\n\n // execute loop\n for (int t = 0; t < sequence_length; ++t) {\n // generate the scan-input elements: the notation T[t] indicates the sub-tensor\n // of rank one less than T obtained by indexing T at position t along axis k.\n si_1 = scan_1[t];\n ... ;\n si_m = scan_m[t];\n // execute loop-body\n st_1, ..., st_n, so_1, ..., so_k = loop-body(st_1, ..., st_n, si_1, ..., si_m)\n // accumulate the scan-output elements\n scan_out_1 = Concat(scan_out_1, so_1); ... ; scan_out_k = Concat(scan_out_k, so_k);\n }\n\n return st_1, ..., st_n, scan_out_1, ..., scan_out_k;\n\n*Sample usage: Encoding RNN using a Scan*\n\nThe following example shows how a simple RNN over an input tensor %X, with weight tensor %Wi,\nrecurrence weight tensor %Ri, bias tensors %Wbi and %Rbi, and initial hidden-state %H_0 can\nbe encoded as a ScanLoop. Note that the loop-body is a nested graph, and it directly computes\n%Wi, %Ri, %Wbi, and %Rbi (typically constants or initializers in the body graph). If these\nvalues are computed in the outer graph, they need to be passed in as extra state_variables.\n\n graph rnn-encoding {\n %H_0 = ...\n %X = ...\n %Y_h, %Y = Scan[body = , num_scan_inputs=1](%H_0, %X)\n return %Y, %Y_h\n }\n\n graph rnn-cell-1 (\n %H_tminus1[FLOAT, tensor]\n %X_t[FLOAT, tensor]\n ) {\n %Wi = ...\n %Ri = ...\n %Wbi = ...\n %Rbi = ...\n %t1 = X_t * (Wi^T)\n %t2 = H_tminus1*(Ri^T)\n %t3 = Add(%t1, %t2)\n %t4 = Add(%t3, %Wbi)\n %t5 = Add(%t4, %Rbi)\n %Ht = Tanh(%t5)\n %Accumulate = Identity(%Ht)\n return %Ht, %Accumulate\n }\n\n", + "attributes": [ + { + "name": "body", + "type": "graph", + "required": true, + "description": "The graph run each iteration. It has N+M inputs: (loop state variables..., scan_input_elts...). It has N+K outputs: (loop state variables..., scan_output_elts...). Each scan_output is created by concatenating the value of the specified scan_output_elt value at the end of each iteration of the loop. It is an error if the dimensions of these values change across loop iterations." + }, + { + "name": "num_scan_inputs", + "type": "int64", + "required": true, + "description": "An attribute specifying the number of scan_inputs M. " + }, + { + "name": "scan_input_axes", + "type": "int64[]", + "required": false, + "description": "An optional list of M flags. The i-th element of the list specifies the axis to be scanned (the sequence axis) for the i-th scan_input. If omitted, 0 will be used as the scan axis for every scan_input. Negative value for an axis means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + }, + { + "name": "scan_input_directions", + "type": "int64[]", + "required": false, + "description": "An optional list of M flags. The i-th element of the list specifies the direction to be scanned for the i-th scan_input tensor: 0 indicates forward direction and 1 indicates reverse direction. If omitted, all scan_input tensors will be scanned in the forward direction." + }, + { + "name": "scan_output_axes", + "type": "int64[]", + "required": false, + "description": "An optional list of K flags. The i-th element of the list specifies the axis for the i-th scan_output. The scan outputs are accumulated along the specified axis. If omitted, 0 will be used as the scan axis for every scan_output. Negative value for an axis means counting dimensions from the back. Accepted range is [-r, r-1]." + }, + { + "name": "scan_output_directions", + "type": "int64[]", + "required": false, + "description": "An optional list of K flags, one for each scan_output. The i-th element of the list specifies whether the i-th scan_output should be constructed by appending or prepending a new value in each iteration: 0 indicates appending and 1 indicates prepending. If omitted, all scan_output tensors will be produced by appending a value in each iteration." + } + ], + "inputs": [ + { + "name": "initial_state_and_scan_inputs", + "type": "V", + "list": true, + "description": "Initial values of the loop's N state variables followed by M scan_inputs" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "final_state_and_scan_outputs", + "type": "V", + "list": true, + "description": "Final values of the loop's N state variables followed by K scan_outputs" + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "1 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "scan_8", + "code": "# Given an input sequence [x1, ..., xN], sum up its elements using a scan\n# returning the final state (x1+x2+...+xN) as well the scan_output\n# [x1, x1+x2, ..., x1+x2+...+xN]\n#\n# create graph to represent scan body\nsum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])\nnext = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])\nsum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['sum_in', 'next'],\n outputs=['sum_out']\n)\nid_node = onnx.helper.make_node(\n 'Identity',\n inputs=['sum_out'],\n outputs=['scan_out']\n)\nscan_body = onnx.helper.make_graph(\n [add_node, id_node],\n 'scan_body',\n [sum_in, next],\n [sum_out, scan_out]\n)\n# create scan op node\nno_sequence_lens = '' # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Scan',\n inputs=[no_sequence_lens, 'initial', 'x'],\n outputs=['y', 'z'],\n num_scan_inputs=1,\n body=scan_body\n)\n# create inputs for batch-size 1, sequence-length 3, inner dimension 2\ninitial = np.array([0, 0]).astype(np.float32).reshape((1, 2))\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((1, 3, 2))\n# final state computed = [1 + 3 + 5, 2 + 4 + 6]\ny = np.array([9, 12]).astype(np.float32).reshape((1, 2))\n# scan-output computed\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((1, 3, 2))\n\nexpect(node, inputs=[initial, x], outputs=[y, z],\n name='test_scan_sum', opset_imports=[onnx.helper.make_opsetid(\"\", 8)])" + }, + { + "summary": "scan_9", + "code": "# Given an input sequence [x1, ..., xN], sum up its elements using a scan\n# returning the final state (x1+x2+...+xN) as well the scan_output\n# [x1, x1+x2, ..., x1+x2+...+xN]\n#\n# create graph to represent scan body\nsum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])\nnext = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])\nsum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['sum_in', 'next'],\n outputs=['sum_out']\n)\nid_node = onnx.helper.make_node(\n 'Identity',\n inputs=['sum_out'],\n outputs=['scan_out']\n)\nscan_body = onnx.helper.make_graph(\n [add_node, id_node],\n 'scan_body',\n [sum_in, next],\n [sum_out, scan_out]\n)\n# create scan op node\nnode = onnx.helper.make_node(\n 'Scan',\n inputs=['initial', 'x'],\n outputs=['y', 'z'],\n num_scan_inputs=1,\n body=scan_body\n)\n# create inputs for sequence-length 3, inner dimension 2\ninitial = np.array([0, 0]).astype(np.float32).reshape((2,))\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final state computed = [1 + 3 + 5, 2 + 4 + 6]\ny = np.array([9, 12]).astype(np.float32).reshape((2,))\n# scan-output computed\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(node, inputs=[initial, x], outputs=[y, z],\n name='test_scan9_sum', opset_imports=[onnx.helper.make_opsetid(\"\", 9)])" + } + ] + }, + { + "name": "Scan", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "Scan can be used to iterate over one or more scan_input tensors,\nconstructing zero or more scan_output tensors. It combines ideas from general recurrences,\nfunctional programming constructs such as scan, fold, map, and zip, and is intended to enable\ngeneralizations of RNN-like constructs for sequence-to-sequence processing.\nOther tensors (referred to as state_variables here) can be used to carry a state\nwhen iterating from one element to another (similar to hidden-state in RNNs, also referred\nto as loop-carried dependences in the context of loops).\nMany common usages involve a single scan_input tensor (where functionality\nsimilar to scan, fold and map can be obtained). When more than one scan_input is used,\na behavior similar to zip is obtained.\n\nThe attribute body must be a graph, specifying the computation to be performed in\nevery iteration. It takes as input the current values of the state_variables and\nthe current iterated element of the scan_inputs. It must return the (updated) values\nof the state_variables and zero or more scan_output_element tensors. The values of the\nscan_output_element tensors are concatenated over all the iterations to produce the\nscan_output values of the scan construct (similar to the concatenated intermediate\nhidden-state values of RNN-like constructs). All the output tensors (state_variables as\nwell as scan_output_element tensors) are required to have the same shape in each iteration\nof the loop (a restriction imposed to enable efficient memory allocation).\n\nNote that the iterated element passed to the body subgraph does not have a sequence\naxis. It will have a rank one less than the rank of the corresponding scan_input.\n\nThe scan operation returns the final values of the state_variables as well as the\nscan_outputs.\n\nThe optional attribute scan_input_directions specifies the direction (forward or backward)\nfor each scan input. If this attribute is omitted, all sequences are scanned in the forward\ndirection. A bidirectional scan may be performed by specifying the same tensor input twice\nin the scan_inputs, once with a forward direction, and once with a backward direction.\n\nThe scan_output of the operation is produced by concatenating the scan_output_element\nvalues produced by the body in each iteration. The optional attribute scan_output_directions\nspecifies the direction in which scan_output is constructed (by appending or prepending the\nscan_output_element to scan_output in each iteration) for each scan_output. If this attribute\nis omitted, the scan_output_element is appended to the scan_output in each iteration.\n\nThe optional attribute scan_input_axes specifies the axis to be scanned for each scan_input.\nIf omitted, every scan_input will be scanned in axis 0. For example, if axis 0 is the\nbatch axis and axis 1 is the time axis (to be scanned), specify an axis value of 1.\nNote that scanning a non-zero axis may be less efficient than scanning axis zero.\n\nThe optional attribute scan_output_axes specifies the axis along which the scan_outputs\nare accumulated for each scan_output. For example, if axis 1 is the time axis (to be\nscanned) for both inputs and outputs, specify a scan_input axis and scan_output axis\nvalue of 1.\n\nNote that because of the ONNX restriction that only the last parameter of an operator can\nbe variadic, the initial-states and scan-inputs are listed together as one input parameter.\nSimilarly, the final-states and scan-outputs are listed together as one output parameter.\nThe attribute num_scan_inputs indicates the number M of scan-inputs.\n\nThe behavior of\n\n Scan <\n num_scan_inputs = m,\n body = loop-body,\n scan_input_axes = [axis_1, ..., axis_m]\n > (init_1, ..., init_n, scan_1, ..., scan_m)\n\nis equivalent to the following pseudo-code:\n\n // scan_i.shape[axis_i] denotes the (max) sequence-length of scan_i\n // scan_i.shape[axis_i] is required to be equal to scan_j.shape[axis_j] for all i,j.\n sequence_length = scan_1.shape[axis_1];\n\n // initialize state-variables\n st_1 = init_1; ... st_n = init_n;\n // initialize scan-output variables: [] denotes an empty tensor\n scan_out_1 = []; ...; scan_out_k = [];\n // identify number of iterations:\n\n // execute loop\n for (int t = 0; t < sequence_length; ++t) {\n // generate the scan-input elements: the notation T[t] indicates the sub-tensor\n // of rank one less than T obtained by indexing T at position t along axis k.\n si_1 = scan_1[t];\n ... ;\n si_m = scan_m[t];\n // execute loop-body\n st_1, ..., st_n, so_1, ..., so_k = loop-body(st_1, ..., st_n, si_1, ..., si_m)\n // accumulate the scan-output elements\n scan_out_1 = Concat(scan_out_1, so_1); ... ; scan_out_k = Concat(scan_out_k, so_k);\n }\n\n return st_1, ..., st_n, scan_out_1, ..., scan_out_k;\n\n*Sample usage: Encoding RNN using a Scan*\n\nThe following example shows how a simple RNN over an input tensor %X, with weight tensor %Wi,\nrecurrence weight tensor %Ri, bias tensors %Wbi and %Rbi, and initial hidden-state %H_0 can\nbe encoded as a ScanLoop. Note that the loop-body is a nested graph, and it directly computes\n%Wi, %Ri, %Wbi, and %Rbi (typically constants or initializers in the body graph). If these\nvalues are computed in the outer graph, they need to be passed in as extra state_variables.\n\n graph rnn-encoding {\n %H_0 = ...\n %X = ...\n %Y_h, %Y = Scan[body = , num_scan_inputs=1](%H_0, %X)\n return %Y, %Y_h\n }\n\n graph rnn-cell-1 (\n %H_tminus1[FLOAT, tensor]\n %X_t[FLOAT, tensor]\n ) {\n %Wi = ...\n %Ri = ...\n %Wbi = ...\n %Rbi = ...\n %t1 = X_t * (Wi^T)\n %t2 = H_tminus1*(Ri^T)\n %t3 = Add(%t1, %t2)\n %t4 = Add(%t3, %Wbi)\n %t5 = Add(%t4, %Rbi)\n %Ht = Tanh(%t5)\n %Accumulate = Identity(%Ht)\n return %Ht, %Accumulate\n }\n\n", + "attributes": [ + { + "name": "body", + "type": "graph", + "required": true, + "description": "The graph run each iteration. It has N+M inputs: (loop state variables..., scan_input_elts...). It has N+K outputs: (loop state variables..., scan_output_elts...). Each scan_output is created by concatenating the value of the specified scan_output_elt value at the end of each iteration of the loop. It is an error if the dimensions of these values change across loop iterations." + }, + { + "name": "num_scan_inputs", + "type": "int64", + "required": true, + "description": "An attribute specifying the number of scan_inputs M. " + }, + { + "name": "scan_input_axes", + "type": "int64[]", + "required": false, + "description": "An optional list of M flags. The i-th element of the list specifies the axis to be scanned (the sequence axis) for the i-th scan_input. If omitted, 0 will be used as the scan axis for every scan_input. Negative value for an axis means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + }, + { + "name": "scan_input_directions", + "type": "int64[]", + "required": false, + "description": "An optional list of M flags. The i-th element of the list specifies the direction to be scanned for the i-th scan_input tensor: 0 indicates forward direction and 1 indicates reverse direction. If omitted, all scan_input tensors will be scanned in the forward direction." + }, + { + "name": "scan_output_axes", + "type": "int64[]", + "required": false, + "description": "An optional list of K flags. The i-th element of the list specifies the axis for the i-th scan_output. The scan outputs are accumulated along the specified axis. If omitted, 0 will be used as the scan axis for every scan_output. Negative value for an axis means counting dimensions from the back. Accepted range is [-r, r-1]." + }, + { + "name": "scan_output_directions", + "type": "int64[]", + "required": false, + "description": "An optional list of K flags, one for each scan_output. The i-th element of the list specifies whether the i-th scan_output should be constructed by appending or prepending a new value in each iteration: 0 indicates appending and 1 indicates prepending. If omitted, all scan_output tensors will be produced by appending a value in each iteration." + } + ], + "inputs": [ + { + "name": "initial_state_and_scan_inputs", + "type": "V", + "list": true, + "description": "Initial values of the loop's N state variables followed by M scan_inputs" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "final_state_and_scan_outputs", + "type": "V", + "list": true, + "description": "Final values of the loop's N state variables followed by K scan_outputs" + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "1 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "All Tensor types", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "scan_8", + "code": "# Given an input sequence [x1, ..., xN], sum up its elements using a scan\n# returning the final state (x1+x2+...+xN) as well the scan_output\n# [x1, x1+x2, ..., x1+x2+...+xN]\n#\n# create graph to represent scan body\nsum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])\nnext = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])\nsum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['sum_in', 'next'],\n outputs=['sum_out']\n)\nid_node = onnx.helper.make_node(\n 'Identity',\n inputs=['sum_out'],\n outputs=['scan_out']\n)\nscan_body = onnx.helper.make_graph(\n [add_node, id_node],\n 'scan_body',\n [sum_in, next],\n [sum_out, scan_out]\n)\n# create scan op node\nno_sequence_lens = '' # optional input, not supplied\nnode = onnx.helper.make_node(\n 'Scan',\n inputs=[no_sequence_lens, 'initial', 'x'],\n outputs=['y', 'z'],\n num_scan_inputs=1,\n body=scan_body\n)\n# create inputs for batch-size 1, sequence-length 3, inner dimension 2\ninitial = np.array([0, 0]).astype(np.float32).reshape((1, 2))\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((1, 3, 2))\n# final state computed = [1 + 3 + 5, 2 + 4 + 6]\ny = np.array([9, 12]).astype(np.float32).reshape((1, 2))\n# scan-output computed\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((1, 3, 2))\n\nexpect(node, inputs=[initial, x], outputs=[y, z],\n name='test_scan_sum', opset_imports=[onnx.helper.make_opsetid(\"\", 8)])" + }, + { + "summary": "scan_9", + "code": "# Given an input sequence [x1, ..., xN], sum up its elements using a scan\n# returning the final state (x1+x2+...+xN) as well the scan_output\n# [x1, x1+x2, ..., x1+x2+...+xN]\n#\n# create graph to represent scan body\nsum_in = onnx.helper.make_tensor_value_info('sum_in', onnx.TensorProto.FLOAT, [2])\nnext = onnx.helper.make_tensor_value_info('next', onnx.TensorProto.FLOAT, [2])\nsum_out = onnx.helper.make_tensor_value_info('sum_out', onnx.TensorProto.FLOAT, [2])\nscan_out = onnx.helper.make_tensor_value_info('scan_out', onnx.TensorProto.FLOAT, [2])\nadd_node = onnx.helper.make_node(\n 'Add',\n inputs=['sum_in', 'next'],\n outputs=['sum_out']\n)\nid_node = onnx.helper.make_node(\n 'Identity',\n inputs=['sum_out'],\n outputs=['scan_out']\n)\nscan_body = onnx.helper.make_graph(\n [add_node, id_node],\n 'scan_body',\n [sum_in, next],\n [sum_out, scan_out]\n)\n# create scan op node\nnode = onnx.helper.make_node(\n 'Scan',\n inputs=['initial', 'x'],\n outputs=['y', 'z'],\n num_scan_inputs=1,\n body=scan_body\n)\n# create inputs for sequence-length 3, inner dimension 2\ninitial = np.array([0, 0]).astype(np.float32).reshape((2,))\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final state computed = [1 + 3 + 5, 2 + 4 + 6]\ny = np.array([9, 12]).astype(np.float32).reshape((2,))\n# scan-output computed\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(node, inputs=[initial, x], outputs=[y, z],\n name='test_scan9_sum', opset_imports=[onnx.helper.make_opsetid(\"\", 9)])" + } + ] + }, + { + "name": "Scatter", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Given `data`, `updates` and `indices` input tensors of rank r >= 1, write the values provided by `updates`\ninto the first input, `data`, along `axis` dimension of `data` (by default outer-most one as axis=0) at corresponding `indices`.\nFor each entry in `updates`, the target index in `data` is specified by corresponding entry in `indices`\nfor dimension = axis, and index in source for dimension != axis. For instance, in a 2-D tensor case,\ndata[indices[i][j]][j] = updates[i][j] if axis = 0, or data[i][indices[i][j]] = updates[i][j] if axis = 1,\nwhere i and j are loop counters from 0 up to the respective size in `updates` - 1.\nExample 1:\n data = [\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n ]\n indices = [\n [1, 0, 2],\n [0, 2, 1],\n ]\n updates = [\n [1.0, 1.1, 1.2],\n [2.0, 2.1, 2.2],\n ]\n output = [\n [2.0, 1.1, 0.0]\n [1.0, 0.0, 2.2]\n [0.0, 2.1, 1.2]\n ]\nExample 2:\n data = [[1.0, 2.0, 3.0, 4.0, 5.0]]\n indices = [[1, 3]]\n updates = [[1.1, 2.1]]\n axis = 1\n output = [[1.0, 1.1, 3.0, 2.1, 5.0]]\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to scatter on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1]" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, of r >= 1 (same rank as input)." + }, + { + "name": "updates", + "type": "T", + "description": "Tensor of rank r >=1 (same rank and shape as indices)" + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r >= 1 (same rank as input)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input and output types can be of any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "scatter_with_axis", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'Scatter',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, 3]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter(data, indices, updates, axis=axis)\n# print(y) produces\n# [[1.0, 1.1, 3.0, 2.1, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_with_axis', opset_imports=[helper.make_opsetid(\"\", 10)])" + }, + { + "summary": "scatter_without_axis", + "code": "node = onnx.helper.make_node(\n 'Scatter',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n)\ndata = np.zeros((3, 3), dtype=np.float32)\nindices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)\nupdates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)\n\ny = scatter(data, indices, updates)\n# print(y) produces\n# [[2.0, 1.1, 0.0],\n# [1.0, 0.0, 2.2],\n# [0.0, 2.1, 1.2]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_without_axis', opset_imports=[helper.make_opsetid(\"\", 10)])" + } + ] + }, + { + "name": "Scatter", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "This operator is deprecated. Please use ScatterElements, which provides the same functionality.\n\nScatter takes three inputs `data`, `updates`, and `indices` of the same\nrank r >= 1 and an optional attribute axis that identifies an axis of `data`\n(by default, the outer-most axis, that is axis 0). The output of the operation\nis produced by creating a copy of the input `data`, and then updating its value\nto values specified by `updates` at specific index positions specified by\n`indices`. Its output shape is the same as the shape of `data`.\n\nFor each entry in `updates`, the target index in `data` is obtained by combining\nthe corresponding entry in `indices` with the index of the entry itself: the\nindex-value for dimension = axis is obtained from the value of the corresponding\nentry in `indices` and the index-value for dimension != axis is obtained from the\nindex of the entry itself.\n\nFor instance, in a 2-D tensor case, the update corresponding to the [i][j] entry\nis performed as below:\n```\n output[indices[i][j]][j] = updates[i][j] if axis = 0,\n output[i][indices[i][j]] = updates[i][j] if axis = 1,\n```\n\nThis operator is the inverse of GatherElements. It is similar to Torch's Scatter operation.\n\nExample 1:\n```\n data = [\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n ]\n indices = [\n [1, 0, 2],\n [0, 2, 1],\n ]\n updates = [\n [1.0, 1.1, 1.2],\n [2.0, 2.1, 2.2],\n ]\n output = [\n [2.0, 1.1, 0.0]\n [1.0, 0.0, 2.2]\n [0.0, 2.1, 1.2]\n ]\n```\nExample 2:\n```\n data = [[1.0, 2.0, 3.0, 4.0, 5.0]]\n indices = [[1, 3]]\n updates = [[1.1, 2.1]]\n axis = 1\n output = [[1.0, 1.1, 3.0, 2.1, 5.0]]\n```\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to scatter on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, of r >= 1 (same rank as input). All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + }, + { + "name": "updates", + "type": "T", + "description": "Tensor of rank r >=1 (same rank and shape as indices)" + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r >= 1 (same rank as input)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input and output types can be of any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "scatter_with_axis", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'Scatter',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, 3]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter(data, indices, updates, axis=axis)\n# print(y) produces\n# [[1.0, 1.1, 3.0, 2.1, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_with_axis', opset_imports=[helper.make_opsetid(\"\", 10)])" + }, + { + "summary": "scatter_without_axis", + "code": "node = onnx.helper.make_node(\n 'Scatter',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n)\ndata = np.zeros((3, 3), dtype=np.float32)\nindices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)\nupdates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)\n\ny = scatter(data, indices, updates)\n# print(y) produces\n# [[2.0, 1.1, 0.0],\n# [1.0, 0.0, 2.2],\n# [0.0, 2.1, 1.2]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_without_axis', opset_imports=[helper.make_opsetid(\"\", 10)])" + } + ] + }, + { + "name": "ScatterElements", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "ScatterElements takes three inputs `data`, `updates`, and `indices` of the same\nrank r >= 1 and an optional attribute axis that identifies an axis of `data`\n(by default, the outer-most axis, that is axis 0). The output of the operation\nis produced by creating a copy of the input `data`, and then updating its value\nto values specified by `updates` at specific index positions specified by\n`indices`. Its output shape is the same as the shape of `data`.\n\nFor each entry in `updates`, the target index in `data` is obtained by combining\nthe corresponding entry in `indices` with the index of the entry itself: the\nindex-value for dimension = axis is obtained from the value of the corresponding\nentry in `indices` and the index-value for dimension != axis is obtained from the\nindex of the entry itself.\n\nFor instance, in a 2-D tensor case, the update corresponding to the [i][j] entry\nis performed as below:\n```\n output[indices[i][j]][j] = updates[i][j] if axis = 0,\n output[i][indices[i][j]] = updates[i][j] if axis = 1,\n```\n\nThis operator is the inverse of GatherElements. It is similar to Torch's Scatter operation.\n\nExample 1:\n```\n data = [\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n ]\n indices = [\n [1, 0, 2],\n [0, 2, 1],\n ]\n updates = [\n [1.0, 1.1, 1.2],\n [2.0, 2.1, 2.2],\n ]\n output = [\n [2.0, 1.1, 0.0]\n [1.0, 0.0, 2.2]\n [0.0, 2.1, 1.2]\n ]\n```\nExample 2:\n```\n data = [[1.0, 2.0, 3.0, 4.0, 5.0]]\n indices = [[1, 3]]\n updates = [[1.1, 2.1]]\n axis = 1\n output = [[1.0, 1.1, 3.0, 2.1, 5.0]]\n```\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to scatter on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, of r >= 1 (same rank as input). All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + }, + { + "name": "updates", + "type": "T", + "description": "Tensor of rank r >=1 (same rank and shape as indices)" + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r >= 1 (same rank as input)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input and output types can be of any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "scatter_elements_with_axis", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, 3]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates, axis)\n# print(y) produces\n# [[1.0, 1.1, 3.0, 2.1, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_with_axis')" + }, + { + "summary": "scatter_elements_with_duplicate_indices", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n reduction='add',\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, 1]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates, axis, reduction='add')\n# print(y) produces\n# [[1.0, 5.2, 3.0, 4.0, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_with_duplicate_indices')" + }, + { + "summary": "scatter_elements_with_negative_indices", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, -3]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates, axis)\n# print(y) produces\n# [[1.0, 1.1, 2.1, 4.0, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_with_negative_indices')" + }, + { + "summary": "scatter_elements_without_axis", + "code": "node = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n)\ndata = np.zeros((3, 3), dtype=np.float32)\nindices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)\nupdates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates)\n# print(y) produces\n# [[2.0, 1.1, 0.0],\n# [1.0, 0.0, 2.2],\n# [0.0, 2.1, 1.2]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_without_axis')" + } + ] + }, + { + "name": "ScatterElements", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "ScatterElements takes three inputs `data`, `updates`, and `indices` of the same\nrank r >= 1 and an optional attribute axis that identifies an axis of `data`\n(by default, the outer-most axis, that is axis 0). The output of the operation\nis produced by creating a copy of the input `data`, and then updating its value\nto values specified by `updates` at specific index positions specified by\n`indices`. Its output shape is the same as the shape of `data`.\n\nFor each entry in `updates`, the target index in `data` is obtained by combining\nthe corresponding entry in `indices` with the index of the entry itself: the\nindex-value for dimension = axis is obtained from the value of the corresponding\nentry in `indices` and the index-value for dimension != axis is obtained from the\nindex of the entry itself.\n\nFor instance, in a 2-D tensor case, the update corresponding to the [i][j] entry\nis performed as below:\n```\n output[indices[i][j]][j] = updates[i][j] if axis = 0,\n output[i][indices[i][j]] = updates[i][j] if axis = 1,\n```\n\nThis operator is the inverse of GatherElements. It is similar to Torch's Scatter operation.\n\nExample 1:\n```\n data = [\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n ]\n indices = [\n [1, 0, 2],\n [0, 2, 1],\n ]\n updates = [\n [1.0, 1.1, 1.2],\n [2.0, 2.1, 2.2],\n ]\n output = [\n [2.0, 1.1, 0.0]\n [1.0, 0.0, 2.2]\n [0.0, 2.1, 1.2]\n ]\n```\nExample 2:\n```\n data = [[1.0, 2.0, 3.0, 4.0, 5.0]]\n indices = [[1, 3]]\n updates = [[1.1, 2.1]]\n axis = 1\n output = [[1.0, 1.1, 3.0, 2.1, 5.0]]\n```\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to scatter on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, of r >= 1 (same rank as input). All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + }, + { + "name": "updates", + "type": "T", + "description": "Tensor of rank r >=1 (same rank and shape as indices)" + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r >= 1 (same rank as input)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input and output types can be of any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "scatter_elements_with_axis", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, 3]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates, axis)\n# print(y) produces\n# [[1.0, 1.1, 3.0, 2.1, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_with_axis')" + }, + { + "summary": "scatter_elements_with_duplicate_indices", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n reduction='add',\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, 1]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates, axis, reduction='add')\n# print(y) produces\n# [[1.0, 5.2, 3.0, 4.0, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_with_duplicate_indices')" + }, + { + "summary": "scatter_elements_with_negative_indices", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, -3]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates, axis)\n# print(y) produces\n# [[1.0, 1.1, 2.1, 4.0, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_with_negative_indices')" + }, + { + "summary": "scatter_elements_without_axis", + "code": "node = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n)\ndata = np.zeros((3, 3), dtype=np.float32)\nindices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)\nupdates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates)\n# print(y) produces\n# [[2.0, 1.1, 0.0],\n# [1.0, 0.0, 2.2],\n# [0.0, 2.1, 1.2]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_without_axis')" + } + ] + }, + { + "name": "ScatterElements", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "ScatterElements takes three inputs `data`, `updates`, and `indices` of the same\nrank r >= 1 and an optional attribute axis that identifies an axis of `data`\n(by default, the outer-most axis, that is axis 0). The output of the operation\nis produced by creating a copy of the input `data`, and then updating its value\nto values specified by `updates` at specific index positions specified by\n`indices`. Its output shape is the same as the shape of `data`.\n\nFor each entry in `updates`, the target index in `data` is obtained by combining\nthe corresponding entry in `indices` with the index of the entry itself: the\nindex-value for dimension = axis is obtained from the value of the corresponding\nentry in `indices` and the index-value for dimension != axis is obtained from the\nindex of the entry itself.\n\n`reduction` allows specification of an optional reduction operation, which is applied to all values in `updates`\ntensor into `output` at the specified `indices`.\nIn cases where `reduction` is set to \"none\", indices should not have duplicate entries: that is, if idx1 != idx2, \nthen indices[idx1] != indices[idx2]. For instance, in a 2-D tensor case, the update \ncorresponding to the [i][j] entry is performed as below:\n```\n output[indices[i][j]][j] = updates[i][j] if axis = 0,\n output[i][indices[i][j]] = updates[i][j] if axis = 1,\n```\nWhen `reduction` is set to \"add\", the update corresponding to the [i][j] entry is performed as below:\n```\n output[indices[i][j]][j] += updates[i][j] if axis = 0,\n output[i][indices[i][j]] += updates[i][j] if axis = 1,\n```\nWhen `reduction` is set to \"mul\", the update corresponding to the [i][j] entry is performed as below:\n```\n output[indices[i][j]][j] *= updates[i][j] if axis = 0,\n output[i][indices[i][j]] *= updates[i][j] if axis = 1,\n```\n\nThis operator is the inverse of GatherElements. It is similar to Torch's Scatter operation.\n\nExample 1:\n```\n data = [\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n ]\n indices = [\n [1, 0, 2],\n [0, 2, 1],\n ]\n updates = [\n [1.0, 1.1, 1.2],\n [2.0, 2.1, 2.2],\n ]\n output = [\n [2.0, 1.1, 0.0]\n [1.0, 0.0, 2.2]\n [0.0, 2.1, 1.2]\n ]\n```\nExample 2:\n```\n data = [[1.0, 2.0, 3.0, 4.0, 5.0]]\n indices = [[1, 3]]\n updates = [[1.1, 2.1]]\n axis = 1\n output = [[1.0, 1.1, 3.0, 2.1, 5.0]]\n```\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to scatter on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "reduction", + "type": "string", + "required": false, + "default": "none", + "description": "Type of reduction to apply: none (default), add, mul. 'none': no reduction applied. 'add': reduction using the addition operation. 'mul': reduction using the multiplication operation." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "Tind", + "description": "Tensor of int32/int64 indices, of r >= 1 (same rank as input). All index values are expected to be within bounds [-s, s-1] along axis of size s. It is an error if any of the index values are out of bounds." + }, + { + "name": "updates", + "type": "T", + "description": "Tensor of rank r >=1 (same rank and shape as indices)" + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r >= 1 (same rank as input)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input and output types can be of any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "scatter_elements_with_axis", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, 3]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates, axis)\n# print(y) produces\n# [[1.0, 1.1, 3.0, 2.1, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_with_axis')" + }, + { + "summary": "scatter_elements_with_duplicate_indices", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n reduction='add',\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, 1]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates, axis, reduction='add')\n# print(y) produces\n# [[1.0, 5.2, 3.0, 4.0, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_with_duplicate_indices')" + }, + { + "summary": "scatter_elements_with_negative_indices", + "code": "axis = 1\nnode = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n axis=axis,\n)\ndata = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)\nindices = np.array([[1, -3]], dtype=np.int64)\nupdates = np.array([[1.1, 2.1]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates, axis)\n# print(y) produces\n# [[1.0, 1.1, 2.1, 4.0, 5.0]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_with_negative_indices')" + }, + { + "summary": "scatter_elements_without_axis", + "code": "node = onnx.helper.make_node(\n 'ScatterElements',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n)\ndata = np.zeros((3, 3), dtype=np.float32)\nindices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int64)\nupdates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)\n\ny = scatter_elements(data, indices, updates)\n# print(y) produces\n# [[2.0, 1.1, 0.0],\n# [1.0, 0.0, 2.2],\n# [0.0, 2.1, 1.2]]\n\nexpect(node, inputs=[data, indices, updates], outputs=[y],\n name='test_scatter_elements_without_axis')" + } + ] + }, + { + "name": "ScatterND", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "ScatterND takes three inputs `data` tensor of rank r >= 1, `indices` tensor of rank q >= 1,\nand `updates` tensor of rank q + r - indices.shape[-1] - 1. The output of the operation\nis produced by creating a copy of the input `data`, and then updating its value to values\nspecified by `updates` at specific index positions specified by `indices`. Its output shape\nis the same as the shape of `data`. Note that `indices` should not have duplicate entries.\nThat is, two or more `updates` for the same index-location is not supported.\n\n`indices` is an integer tensor. Let k denote indices.shape[-1], the last dimension in the shape of `indices`.\n `indices` is treated as a (q-1)-dimensional tensor of k-tuples, where each k-tuple is a partial-index into `data`.\nHence, k can be a value at most the rank of `data`. When k equals rank(data), each update entry specifies an\nupdate to a single element of the tensor. When k is less than rank(data) each update entry specifies an\nupdate to a slice of the tensor.\n\n`updates` is treated as a (q-1)-dimensional tensor of replacement-slice-values. Thus, the\nfirst (q-1) dimensions of updates.shape must match the first (q-1) dimensions of indices.shape.\nThe remaining dimensions of `updates` correspond to the dimensions of the\nreplacement-slice-values. Each replacement-slice-value is a (r-k) dimensional tensor,\ncorresponding to the trailing (r-k) dimensions of `data`. Thus, the shape of `updates`\nmust equal indices.shape[0:q-1] ++ data.shape[k:r-1], where ++ denotes the concatenation\nof shapes.\n\nThe `output` is calculated via the following equation:\n\n output = np.copy(data)\n update_indices = indices.shape[:-1]\n for idx in np.ndindex(update_indices):\n output[indices[idx]] = updates[idx]\n\nThe order of iteration in the above loop is not specified.\nIn particular, indices should not have duplicate entries: that is, if idx1 != idx2, then indices[idx1] != indices[idx2].\nThis ensures that the output value does not depend on the iteration order.\n\nThis operator is the inverse of GatherND.\n\nExample 1:\n```\n data = [1, 2, 3, 4, 5, 6, 7, 8]\n indices = [[4], [3], [1], [7]]\n updates = [9, 10, 11, 12]\n output = [1, 11, 3, 10, 9, 6, 7, 12]\n```\n\nExample 2:\n```\n data = [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]\n indices = [[0], [2]]\n updates = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]]\n output = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]\n```\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "tensor(int64)", + "description": "Tensor of rank q >= 1." + }, + { + "name": "updates", + "type": "T", + "description": "Tensor of rank q + r - indices_shape[-1] - 1." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r >= 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "scatternd", + "code": "node = onnx.helper.make_node(\n 'ScatterND',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n)\ndata = np.array(\n [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\nindices = np.array([[0], [2]], dtype=np.int64)\nupdates = np.array(\n [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)\n# Expecting output as np.array(\n# [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates)\nexpect(node, inputs=[data, indices, updates], outputs=[output],\n name='test_scatternd')" + }, + { + "summary": "scatternd_add", + "code": "node = onnx.helper.make_node(\n 'ScatterND',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n reduction='add',\n)\ndata = np.array(\n [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)\n# Expecting output as np.array(\n# [[[7, 8, 9, 10], [13, 14, 15, 16], [18, 17, 16, 15], [16, 15, 14, 13]],\n# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction='add')\nexpect(node, inputs=[data, indices, updates], outputs=[output],\n name='test_scatternd_add')" + }, + { + "summary": "scatternd_multiply", + "code": "node = onnx.helper.make_node(\n 'ScatterND',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n reduction='mul',\n)\ndata = np.array(\n [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)\n# Expecting output as np.array(\n# [[[5, 10, 15, 20], [60, 72, 84, 96], [168, 147, 126, 105], [128, 96, 64, 32]],\n# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction='mul')\nexpect(node, inputs=[data, indices, updates], outputs=[output],\n name='test_scatternd_multiply')" + } + ] + }, + { + "name": "ScatterND", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "ScatterND takes three inputs `data` tensor of rank r >= 1, `indices` tensor of rank q >= 1,\nand `updates` tensor of rank q + r - indices.shape[-1] - 1. The output of the operation\nis produced by creating a copy of the input `data`, and then updating its value to values\nspecified by `updates` at specific index positions specified by `indices`. Its output shape\nis the same as the shape of `data`. Note that `indices` should not have duplicate entries.\nThat is, two or more `updates` for the same index-location is not supported.\n\n`indices` is an integer tensor. Let k denote indices.shape[-1], the last dimension in the shape of `indices`.\n `indices` is treated as a (q-1)-dimensional tensor of k-tuples, where each k-tuple is a partial-index into `data`.\nHence, k can be a value at most the rank of `data`. When k equals rank(data), each update entry specifies an\nupdate to a single element of the tensor. When k is less than rank(data) each update entry specifies an\nupdate to a slice of the tensor.\n\n`updates` is treated as a (q-1)-dimensional tensor of replacement-slice-values. Thus, the\nfirst (q-1) dimensions of updates.shape must match the first (q-1) dimensions of indices.shape.\nThe remaining dimensions of `updates` correspond to the dimensions of the\nreplacement-slice-values. Each replacement-slice-value is a (r-k) dimensional tensor,\ncorresponding to the trailing (r-k) dimensions of `data`. Thus, the shape of `updates`\nmust equal indices.shape[0:q-1] ++ data.shape[k:r-1], where ++ denotes the concatenation\nof shapes.\n\nThe `output` is calculated via the following equation:\n\n output = np.copy(data)\n update_indices = indices.shape[:-1]\n for idx in np.ndindex(update_indices):\n output[indices[idx]] = updates[idx]\n\nThe order of iteration in the above loop is not specified.\nIn particular, indices should not have duplicate entries: that is, if idx1 != idx2, then indices[idx1] != indices[idx2].\nThis ensures that the output value does not depend on the iteration order.\n\nThis operator is the inverse of GatherND.\n\nExample 1:\n```\n data = [1, 2, 3, 4, 5, 6, 7, 8]\n indices = [[4], [3], [1], [7]]\n updates = [9, 10, 11, 12]\n output = [1, 11, 3, 10, 9, 6, 7, 12]\n```\n\nExample 2:\n```\n data = [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]\n indices = [[0], [2]]\n updates = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]]\n output = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]\n```\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "tensor(int64)", + "description": "Tensor of rank q >= 1." + }, + { + "name": "updates", + "type": "T", + "description": "Tensor of rank q + r - indices_shape[-1] - 1." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r >= 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "scatternd", + "code": "node = onnx.helper.make_node(\n 'ScatterND',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n)\ndata = np.array(\n [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\nindices = np.array([[0], [2]], dtype=np.int64)\nupdates = np.array(\n [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)\n# Expecting output as np.array(\n# [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates)\nexpect(node, inputs=[data, indices, updates], outputs=[output],\n name='test_scatternd')" + }, + { + "summary": "scatternd_add", + "code": "node = onnx.helper.make_node(\n 'ScatterND',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n reduction='add',\n)\ndata = np.array(\n [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)\n# Expecting output as np.array(\n# [[[7, 8, 9, 10], [13, 14, 15, 16], [18, 17, 16, 15], [16, 15, 14, 13]],\n# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction='add')\nexpect(node, inputs=[data, indices, updates], outputs=[output],\n name='test_scatternd_add')" + }, + { + "summary": "scatternd_multiply", + "code": "node = onnx.helper.make_node(\n 'ScatterND',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n reduction='mul',\n)\ndata = np.array(\n [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)\n# Expecting output as np.array(\n# [[[5, 10, 15, 20], [60, 72, 84, 96], [168, 147, 126, 105], [128, 96, 64, 32]],\n# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction='mul')\nexpect(node, inputs=[data, indices, updates], outputs=[output],\n name='test_scatternd_multiply')" + } + ] + }, + { + "name": "ScatterND", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "ScatterND takes three inputs `data` tensor of rank r >= 1, `indices` tensor of rank q >= 1,\nand `updates` tensor of rank q + r - indices.shape[-1] - 1. The output of the operation\nis produced by creating a copy of the input `data`, and then updating its value to values\nspecified by `updates` at specific index positions specified by `indices`. Its output shape\nis the same as the shape of `data`. Note that `indices` should not have duplicate entries.\nThat is, two or more `updates` for the same index-location is not supported.\n\n`indices` is an integer tensor. Let k denote indices.shape[-1], the last dimension in the shape of `indices`.\n `indices` is treated as a (q-1)-dimensional tensor of k-tuples, where each k-tuple is a partial-index into `data`.\nHence, k can be a value at most the rank of `data`. When k equals rank(data), each update entry specifies an\nupdate to a single element of the tensor. When k is less than rank(data) each update entry specifies an\nupdate to a slice of the tensor.\n\n`updates` is treated as a (q-1)-dimensional tensor of replacement-slice-values. Thus, the\nfirst (q-1) dimensions of updates.shape must match the first (q-1) dimensions of indices.shape.\nThe remaining dimensions of `updates` correspond to the dimensions of the\nreplacement-slice-values. Each replacement-slice-value is a (r-k) dimensional tensor,\ncorresponding to the trailing (r-k) dimensions of `data`. Thus, the shape of `updates`\nmust equal indices.shape[0:q-1] ++ data.shape[k:r-1], where ++ denotes the concatenation\nof shapes.\n\nThe `output` is calculated via the following equation:\n\n output = np.copy(data)\n update_indices = indices.shape[:-1]\n for idx in np.ndindex(update_indices):\n output[indices[idx]] = updates[idx]\n\nThe order of iteration in the above loop is not specified.\nIn particular, indices should not have duplicate entries: that is, if idx1 != idx2, then indices[idx1] != indices[idx2].\nThis ensures that the output value does not depend on the iteration order.\n\n`reduction` allows specification of an optional reduction operation, which is applied to all values in `updates`\ntensor into `output` at the specified `indices`.\nIn cases where `reduction` is set to \"none\", indices should not have duplicate entries: that is, if idx1 != idx2, \nthen indices[idx1] != indices[idx2]. This ensures that the output value does not depend on the iteration order.\nWhen `reduction` is set to \"add\", `output` is calculated as follows:\n\n output = np.copy(data)\n update_indices = indices.shape[:-1]\n for idx in np.ndindex(update_indices):\n output[indices[idx]] += updates[idx]\n\nWhen `reduction` is set to \"mul\", `output` is calculated as follows:\n\n output = np.copy(data)\n update_indices = indices.shape[:-1]\n for idx in np.ndindex(update_indices):\n output[indices[idx]] *= updates[idx]\n\nThis operator is the inverse of GatherND.\n\nExample 1:\n```\n data = [1, 2, 3, 4, 5, 6, 7, 8]\n indices = [[4], [3], [1], [7]]\n updates = [9, 10, 11, 12]\n output = [1, 11, 3, 10, 9, 6, 7, 12]\n```\n\nExample 2:\n```\n data = [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]\n indices = [[0], [2]]\n updates = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]]\n output = [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]]\n```\n", + "attributes": [ + { + "name": "reduction", + "type": "string", + "required": false, + "default": "none", + "description": "Type of reduction to apply: none (default), add, mul. 'none': no reduction applied. 'add': reduction using the addition operation. 'mul': reduction using the multiplication operation." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of rank r >= 1." + }, + { + "name": "indices", + "type": "tensor(int64)", + "description": "Tensor of rank q >= 1." + }, + { + "name": "updates", + "type": "T", + "description": "Tensor of rank q + r - indices_shape[-1] - 1." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of rank r >= 1." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "scatternd", + "code": "node = onnx.helper.make_node(\n 'ScatterND',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n)\ndata = np.array(\n [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\nindices = np.array([[0], [2]], dtype=np.int64)\nupdates = np.array(\n [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)\n# Expecting output as np.array(\n# [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates)\nexpect(node, inputs=[data, indices, updates], outputs=[output],\n name='test_scatternd')" + }, + { + "summary": "scatternd_add", + "code": "node = onnx.helper.make_node(\n 'ScatterND',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n reduction='add',\n)\ndata = np.array(\n [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)\n# Expecting output as np.array(\n# [[[7, 8, 9, 10], [13, 14, 15, 16], [18, 17, 16, 15], [16, 15, 14, 13]],\n# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction='add')\nexpect(node, inputs=[data, indices, updates], outputs=[output],\n name='test_scatternd_add')" + }, + { + "summary": "scatternd_multiply", + "code": "node = onnx.helper.make_node(\n 'ScatterND',\n inputs=['data', 'indices', 'updates'],\n outputs=['y'],\n reduction='mul',\n)\ndata = np.array(\n [[[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]],\n [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],\n [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]]], dtype=np.float32)\n# Expecting output as np.array(\n# [[[5, 10, 15, 20], [60, 72, 84, 96], [168, 147, 126, 105], [128, 96, 64, 32]],\n# [[1, 2, 3, 4], [5, 6, 7, 8], [8, 7, 6, 5], [4, 3, 2, 1]],\n# [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],\n# [[8, 7, 6, 5], [4, 3, 2, 1], [1, 2, 3, 4], [5, 6, 7, 8]]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction='mul')\nexpect(node, inputs=[data, indices, updates], outputs=[output],\n name='test_scatternd_multiply')" + } + ] + }, + { + "name": "Selu", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Selu takes one input data (Tensor) and produces one output data\n(Tensor) where the scaled exponential linear unit function,\n`y = gamma * (alpha * e^x - alpha) for x <= 0`, `y = gamma * x for x > 0`,\nis applied to the tensor elementwise.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.673200011253357, + "description": "Coefficient of SELU default to 1.6732." + }, + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + }, + { + "name": "gamma", + "type": "float32", + "required": false, + "default": 1.0506999492645264, + "description": "Coefficient of SELU default to 1.0507." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "selu", + "code": "node = onnx.helper.make_node(\n 'Selu',\n inputs=['x'],\n outputs=['y'],\n alpha=2.0,\n gamma=3.0\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-3.79272318, 0., 3.]\ny = np.clip(x, 0, np.inf) * 3.0 + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\nexpect(node, inputs=[x], outputs=[y],\n name='test_selu_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) * 3.0 + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\nexpect(node, inputs=[x], outputs=[y],\n name='test_selu')" + }, + { + "summary": "selu_default", + "code": "default_alpha = 1.67326319217681884765625\ndefault_gamma = 1.05070102214813232421875\nnode = onnx.helper.make_node(\n 'Selu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) * default_gamma + \\\n (np.exp(np.clip(x, -np.inf, 0)) - 1) * default_alpha * default_gamma\nexpect(node, inputs=[x], outputs=[y],\n name='test_selu_default')" + } + ], + "category": "Activation" + }, + { + "name": "Selu", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Selu takes one input data (Tensor) and produces one output data\n(Tensor) where the scaled exponential linear unit function,\n`y = gamma * (alpha * e^x - alpha) for x <= 0`, `y = gamma * x for x > 0`,\nis applied to the tensor elementwise.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.6732631921768188, + "description": "Coefficient of SELU default to 1.67326319217681884765625 (i.e., float32 approximation of 1.6732632423543772848170429916717)." + }, + { + "name": "gamma", + "type": "float32", + "required": false, + "default": 1.0507010221481323, + "description": "Coefficient of SELU default to 1.05070102214813232421875 (i.e., float32 approximation of 1.0507009873554804934193349852946)." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "selu", + "code": "node = onnx.helper.make_node(\n 'Selu',\n inputs=['x'],\n outputs=['y'],\n alpha=2.0,\n gamma=3.0\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-3.79272318, 0., 3.]\ny = np.clip(x, 0, np.inf) * 3.0 + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\nexpect(node, inputs=[x], outputs=[y],\n name='test_selu_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) * 3.0 + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\nexpect(node, inputs=[x], outputs=[y],\n name='test_selu')" + }, + { + "summary": "selu_default", + "code": "default_alpha = 1.67326319217681884765625\ndefault_gamma = 1.05070102214813232421875\nnode = onnx.helper.make_node(\n 'Selu',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, 0, np.inf) * default_gamma + \\\n (np.exp(np.clip(x, -np.inf, 0)) - 1) * default_alpha * default_gamma\nexpect(node, inputs=[x], outputs=[y],\n name='test_selu_default')" + } + ], + "category": "Activation" + }, + { + "name": "SequenceAt", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Outputs a tensor copy from the tensor at 'position' in 'input_sequence'.\nAccepted range for 'position' is in `[-n, n - 1]`, where `n` is the number of tensors in 'input_sequence'.\nNegative value means counting positions from the back.\n", + "inputs": [ + { + "name": "input_sequence", + "type": "S", + "description": "Input sequence." + }, + { + "name": "position", + "type": "I", + "description": "Position of the tensor in the sequence. Negative value means counting positions from the back. Accepted range in `[-n, n - 1]`, where `n` is the number of tensors in 'input_sequence'. It is an error if any of the index values are out of bounds. It must be a scalar(tensor of empty shape)." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "tensor", + "type": "T", + "description": "Output tensor at the specified position in the input sequence." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain to any tensor type.", + "type_param_str": "S", + "allowed_type_strs": [ + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + }, + { + "description": "Constrain to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain position to integral tensor. It must be a scalar(tensor of empty shape).", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "SequenceConstruct", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Construct a tensor sequence containing 'inputs' tensors.\nAll tensors in 'inputs' must have the same data type.\n", + "inputs": [ + { + "name": "inputs", + "type": "T", + "list": true, + "description": "Tensors." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "output_sequence", + "type": "S", + "description": "Sequence enclosing the input tensors." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input types to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain output types to any tensor type.", + "type_param_str": "S", + "allowed_type_strs": [ + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + } + ] + }, + { + "name": "SequenceEmpty", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Construct an empty tensor sequence, with given data type.\n", + "attributes": [ + { + "name": "dtype", + "type": "int64", + "required": false, + "description": "(Optional) The data type of the tensors in the output sequence. The default type is 'float'." + } + ], + "min_input": 0, + "max_input": 0, + "outputs": [ + { + "name": "output", + "type": "S", + "description": "Empty sequence." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain output types to any tensor type.", + "type_param_str": "S", + "allowed_type_strs": [ + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + } + ] + }, + { + "name": "SequenceErase", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Outputs a tensor sequence that removes the tensor at 'position' from 'input_sequence'.\nAccepted range for 'position' is in `[-n, n - 1]`, where `n` is the number of tensors in 'input_sequence'.\nNegative value means counting positions from the back.\n'position' is optional, by default it erases the last tensor from 'input_sequence'.\n", + "inputs": [ + { + "name": "input_sequence", + "type": "S", + "description": "Input sequence." + }, + { + "name": "position", + "type": "I", + "option": "optional", + "description": "Position of the tensor in the sequence. Negative value means counting positions from the back. Accepted range in `[-n, n - 1]`, where `n` is the number of tensors in 'input_sequence'. It is an error if any of the index values are out of bounds. It must be a scalar(tensor of empty shape)." + } + ], + "min_input": 1, + "max_input": 2, + "outputs": [ + { + "name": "output_sequence", + "type": "S", + "description": "Output sequence that has the tensor at the specified position removed." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain to any tensor type.", + "type_param_str": "S", + "allowed_type_strs": [ + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + }, + { + "description": "Constrain position to integral tensor. It must be a scalar(tensor of empty shape).", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "SequenceInsert", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Outputs a tensor sequence that inserts 'tensor' into 'input_sequence' at 'position'.\n'tensor' must have the same data type as 'input_sequence'.\nAccepted range for 'position' is in `[-n, n]`, where `n` is the number of tensors in 'input_sequence'.\nNegative value means counting positions from the back.\n'position' is optional, by default it inserts 'tensor' to the back of 'input_sequence'.\n", + "inputs": [ + { + "name": "input_sequence", + "type": "S", + "description": "Input sequence." + }, + { + "name": "tensor", + "type": "T", + "description": "Input tensor to be inserted into the input sequence." + }, + { + "name": "position", + "type": "I", + "option": "optional", + "description": "Position in the sequence where the new tensor is inserted. It is optional and default is to insert to the back of the sequence. Negative value means counting positions from the back. Accepted range in `[-n, n]`, where `n` is the number of tensors in 'input_sequence'. It is an error if any of the index values are out of bounds. It must be a scalar(tensor of empty shape)." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "output_sequence", + "type": "S", + "description": "Output sequence that contains the inserted tensor at given position." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "2 - 3", + "type_constraints": [ + { + "description": "Constrain to any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain to any tensor type.", + "type_param_str": "S", + "allowed_type_strs": [ + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + }, + { + "description": "Constrain position to integral tensor. It must be a scalar(tensor of empty shape).", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "sequenceinsert", + "code": "test_cases = {\n 'at_back': [np.array([10, 11, 12]).astype(np.int64)],\n 'at_front': [np.array([-2, -1, 0]), np.array([0]).astype(np.int64)]\n}\nsequence = [np.array([1, 2, 3, 4]).astype(np.int64), np.array([5, 6, 7]).astype(np.int64), np.array([8, 9]).astype(np.int64)]\n\nfor test_name, test_inputs in test_cases.items():\n tensor = test_inputs[0].astype(np.int64)\n\n if len(test_inputs) > 1:\n node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['sequence', 'tensor', 'position'],\n outputs=['output_sequence']\n )\n position = test_inputs[1]\n inserted = sequence_insert_reference_implementation(sequence, tensor, position)\n expect(node, inputs=[sequence, tensor, position], outputs=[inserted],\n name='test_sequence_insert_' + test_name)\n else:\n node = onnx.helper.make_node(\n 'SequenceInsert',\n inputs=['sequence', 'tensor'],\n outputs=['output_sequence']\n )\n inserted = sequence_insert_reference_implementation(sequence, tensor)\n expect(node, inputs=[sequence, tensor], outputs=[inserted],\n name='test_sequence_insert_' + test_name)" + } + ] + }, + { + "name": "SequenceLength", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Produces a scalar(tensor of empty shape) containing the number of tensors in 'input_sequence'.\n", + "inputs": [ + { + "name": "input_sequence", + "type": "S", + "description": "Input sequence." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "length", + "type": "I", + "description": "Length of input sequence. It must be a scalar(tensor of empty shape)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain to any tensor type.", + "type_param_str": "S", + "allowed_type_strs": [ + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + }, + { + "description": "Constrain output to integral tensor. It must be a scalar(tensor of empty shape).", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ] + }, + { + "name": "SequenceMap", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "Applies a sub-graph to each sample in the input sequence(s).\n\nInputs can be either tensors or sequences, with the exception of the first input which must\nbe a sequence. The length of the first input sequence will determine the number of samples in the\noutputs. Any other sequence inputs should have the same number of samples. The number of inputs\nand outputs, should match the one of the subgraph.\n\nFor each i-th element in the output, a sample will be extracted from the input sequence(s) at\nthe i-th position and the sub-graph will be applied to it.\nThe outputs will contain the outputs of the sub-graph for each sample, in the same order as in\nthe input.\n\nThis operator assumes that processing each sample is independent and could executed in parallel\nor in any order. Users cannot expect any specific ordering in which each subgraph is computed.", + "attributes": [ + { + "name": "body", + "type": "graph", + "required": true, + "description": "The graph to be run for each sample in the sequence(s). It should have as many inputs and outputs as inputs and outputs to the SequenceMap function." + } + ], + "inputs": [ + { + "name": "input_sequence", + "type": "S", + "description": "Input sequence." + }, + { + "name": "additional_inputs", + "type": "V", + "list": true, + "description": "Additional inputs to the graph" + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "out_sequence", + "type": "S", + "list": true, + "description": "Output sequence(s)" + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "1 - ∞", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input types to any sequence type.", + "type_param_str": "S", + "allowed_type_strs": [ + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + }, + { + "description": "Constrain to any tensor or sequence type.", + "type_param_str": "V", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)", + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + } + ], + "examples": [ + { + "summary": "sequence_map_add_1_sequence_1_tensor", + "code": "body = onnx.helper.make_graph(\n [onnx.helper.make_node('Add', ['in0', 'in1'], ['out0'])],\n 'seq_map_body',\n [onnx.helper.make_tensor_value_info('in0', onnx.TensorProto.FLOAT, ['N']),\n onnx.helper.make_tensor_value_info('in1', onnx.TensorProto.FLOAT, ['N'])],\n [onnx.helper.make_tensor_value_info(\n 'out0', onnx.TensorProto.FLOAT, ['N'])]\n)\n\nnode = onnx.helper.make_node(\n 'SequenceMap',\n inputs=['x0', 'x1'],\n outputs=['y0'],\n body=body\n)\n\nx0 = [np.random.uniform(0.0, 1.0, 10).astype(np.float32) for k in range(3)]\nx1 = np.random.uniform(0.0, 1.0, 10).astype(np.float32)\ny0 = [x0[i] + x1 for i in range(3)]\ninput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N']),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n]\nexpect(node, inputs=[x0, x1], outputs=[y0],\n input_type_protos=input_type_protos,\n output_type_protos=output_type_protos,\n name='test_sequence_map_add_1_sequence_1_tensor')" + }, + { + "summary": "sequence_map_add_2_sequences", + "code": "body = onnx.helper.make_graph(\n [onnx.helper.make_node('Add', ['in0', 'in1'], ['out0'])],\n 'seq_map_body',\n [onnx.helper.make_tensor_value_info('in0', onnx.TensorProto.FLOAT, ['N']),\n onnx.helper.make_tensor_value_info('in1', onnx.TensorProto.FLOAT, ['N'])],\n [onnx.helper.make_tensor_value_info(\n 'out0', onnx.TensorProto.FLOAT, ['N'])]\n)\n\nnode = onnx.helper.make_node(\n 'SequenceMap',\n inputs=['x0', 'x1'],\n outputs=['y0'],\n body=body\n)\n\nN = [np.random.randint(1, 10) for _ in range(3)]\nx0 = [np.random.uniform(0.0, 1.0, N[k]).astype(np.float32)\n for k in range(3)]\nx1 = [np.random.uniform(0.0, 1.0, N[k]).astype(np.float32)\n for k in range(3)]\ny0 = [x0[k] + x1[k] for k in range(3)]\ninput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n]\nexpect(node, inputs=[x0, x1], outputs=[y0],\n input_type_protos=input_type_protos,\n output_type_protos=output_type_protos,\n name='test_sequence_map_add_2_sequences')" + }, + { + "summary": "sequence_map_extract_shapes", + "code": "body = onnx.helper.make_graph(\n [onnx.helper.make_node('Shape', ['x'], ['shape'])],\n 'seq_map_body',\n [onnx.helper.make_tensor_value_info('x', onnx.TensorProto.FLOAT, ['H', 'W', 'C'])],\n [onnx.helper.make_tensor_value_info('shape', onnx.TensorProto.INT64, [3])]\n)\n\nnode = onnx.helper.make_node(\n 'SequenceMap',\n inputs=['in_seq'],\n outputs=['shapes'],\n body=body\n)\n\nshapes = [\n np.array([40, 30, 3], dtype=np.int64),\n np.array([20, 10, 3], dtype=np.int64),\n np.array([10, 5, 3], dtype=np.int64),\n]\nx0 = [np.zeros(shape, dtype=np.float32) for shape in shapes]\ninput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['H', 'W', 'C'])),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, [3])),\n]\nexpect(node, inputs=[x0], outputs=[shapes],\n input_type_protos=input_type_protos,\n output_type_protos=output_type_protos,\n name='test_sequence_map_extract_shapes')" + }, + { + "summary": "sequence_map_identity_1_sequence", + "code": "body = onnx.helper.make_graph(\n [onnx.helper.make_node('Identity', ['in0'], ['out0'])],\n 'seq_map_body',\n [onnx.helper.make_tensor_value_info(\n 'in0', onnx.TensorProto.FLOAT, ['N'])],\n [onnx.helper.make_tensor_value_info(\n 'out0', onnx.TensorProto.FLOAT, ['M'])]\n)\n\nnode = onnx.helper.make_node(\n 'SequenceMap',\n inputs=['x'],\n outputs=['y'],\n body=body\n)\n\nx = [np.random.uniform(0.0, 1.0, 10).astype(np.float32)\n for _ in range(3)]\ny = x\ninput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n]\nexpect(node, inputs=[x], outputs=[y],\n input_type_protos=input_type_protos,\n output_type_protos=output_type_protos,\n name='test_sequence_map_identity_1_sequence')" + }, + { + "summary": "sequence_map_identity_1_sequence_1_tensor", + "code": "body = onnx.helper.make_graph(\n [onnx.helper.make_node('Identity', ['in0'], ['out0']),\n onnx.helper.make_node('Identity', ['in1'], ['out1'])],\n 'seq_map_body',\n [onnx.helper.make_tensor_value_info('in0', onnx.TensorProto.FLOAT, ['N']),\n onnx.helper.make_tensor_value_info('in1', onnx.TensorProto.FLOAT, ['M'])],\n [onnx.helper.make_tensor_value_info(\n 'out0', onnx.TensorProto.FLOAT, ['N']),\n onnx.helper.make_tensor_value_info(\n 'out1', onnx.TensorProto.FLOAT, ['M'])]\n)\n\nnode = onnx.helper.make_node(\n 'SequenceMap',\n inputs=['x0', 'x1'],\n outputs=['y0', 'y1'],\n body=body\n)\n\nx0 = [np.random.uniform(0.0, 1.0, np.random.randint(\n 1, 10)).astype(np.float32) for _ in range(3)]\nx1 = np.random.uniform(0.0, 1.0, np.random.randint(\n 1, 10)).astype(np.float32)\ny0 = x0\ny1 = [x1 for _ in range(3)]\ninput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['M']),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['M'])),\n]\nexpect(node, inputs=[x0, x1], outputs=[y0, y1],\n input_type_protos=input_type_protos,\n output_type_protos=output_type_protos,\n name='test_sequence_map_identity_1_sequence_1_tensor')" + }, + { + "summary": "sequence_map_identity_2_sequences", + "code": "body = onnx.helper.make_graph(\n [onnx.helper.make_node('Identity', ['in0'], ['out0']),\n onnx.helper.make_node('Identity', ['in1'], ['out1'])],\n 'seq_map_body',\n [onnx.helper.make_tensor_value_info('in0', onnx.TensorProto.FLOAT, ['N']),\n onnx.helper.make_tensor_value_info('in1', onnx.TensorProto.FLOAT, ['M'])],\n [onnx.helper.make_tensor_value_info('out0', onnx.TensorProto.FLOAT, ['N']),\n onnx.helper.make_tensor_value_info('out1', onnx.TensorProto.FLOAT, ['M'])]\n)\n\nnode = onnx.helper.make_node(\n 'SequenceMap',\n inputs=['x0', 'x1'],\n outputs=['y0', 'y1'],\n body=body\n)\n\nx0 = [np.random.uniform(0.0, 1.0, np.random.randint(\n 1, 10)).astype(np.float32) for _ in range(3)]\nx1 = [np.random.uniform(0.0, 1.0, np.random.randint(\n 1, 10)).astype(np.float32) for _ in range(3)]\ny0 = x0\ny1 = x1\ninput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['M'])),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['N'])),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, ['M'])),\n]\nexpect(node, inputs=[x0, x1], outputs=[y0, y1],\n input_type_protos=input_type_protos,\n output_type_protos=output_type_protos,\n name='test_sequence_map_identity_2_sequences')" + } + ] + }, + { + "name": "Shape", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Takes a tensor as input and outputs an 1D int64 tensor containing the shape of the input tensor.\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "shape", + "type": "T1", + "description": "Shape of the input tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input tensor can be of arbitrary type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain output to int64 tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "shape", + "code": "x = np.array([\n [1, 2, 3],\n [4, 5, 6],\n]).astype(np.float32)\ntest_shape('_example', x) # preserve names of original test cases\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\n\ntest_shape('', x) # preserve names of original test cases\n\ntest_shape('_start_1', x, start=1)\n\ntest_shape('_end_1', x, end=1)\n\ntest_shape('_start_negative_1', x, start=-1)\n\ntest_shape('_end_negative_1', x, end=-1)\n\ntest_shape('_start_1_end_negative_1', x, start=1, end=-1)\n\ntest_shape('_start_1_end_2', x, start=1, end=2)\n\ntest_shape('_clip_start', x, start=-10)\n\ntest_shape('_clip_end', x, end=10)" + } + ] + }, + { + "name": "Shape", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Takes a tensor as input and outputs an 1D int64 tensor containing the shape of the input tensor.\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "shape", + "type": "T1", + "description": "Shape of the input tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input tensor can be of arbitrary type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain output to int64 tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "shape", + "code": "x = np.array([\n [1, 2, 3],\n [4, 5, 6],\n]).astype(np.float32)\ntest_shape('_example', x) # preserve names of original test cases\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\n\ntest_shape('', x) # preserve names of original test cases\n\ntest_shape('_start_1', x, start=1)\n\ntest_shape('_end_1', x, end=1)\n\ntest_shape('_start_negative_1', x, start=-1)\n\ntest_shape('_end_negative_1', x, end=-1)\n\ntest_shape('_start_1_end_negative_1', x, start=1, end=-1)\n\ntest_shape('_start_1_end_2', x, start=1, end=2)\n\ntest_shape('_clip_start', x, start=-10)\n\ntest_shape('_clip_end', x, end=10)" + } + ] + }, + { + "name": "Shape", + "module": "ai.onnx", + "version": 15, + "support_level": "common", + "description": "Takes a tensor as input and outputs an 1D int64 tensor containing the shape of the input tensor.\nOptional attributes start and end can be used to compute a slice of the input tensor's shape.\nIf start axis is omitted, the slice starts from axis 0.\nThe end axis, if specified, is exclusive (and the returned value will not include the size of that axis).\nIf the end axis is omitted, the axes upto the last one will be included.\nNegative axes indicate counting back from the last axis.\nNote that axes will be clamped to the range [0, r-1], where r is the\nrank of the input tensor if they are out-of-range (after adding r in the case of\nnegative axis). Thus, specifying any end value > r is equivalent to specifying an end\nvalue of r, and specifying any start value < -r is equivalent to specifying a start\nvalue of 0.\n\nFor example:\nInput tensor with shape: [2, 3, 4] \nNo attributes specified.\nOutput: [2, 3, 4] \n\nInput tensor with shape: [2, 3, 4] \nstart: -1\nOutput: [4] \n\nInput tensor with shape: [2, 3, 4] \nend: -1\nOutput: [2, 3]\n\nInput tensor with shape: [2, 3, 4] \nstart: 1\nend: 2\nOutput: [3] \n", + "attributes": [ + { + "name": "end", + "type": "int64", + "required": false, + "description": "(Optional) Ending axis for slicing the shape. Negative value means counting dimensions from the back. If omitted, sizes of all axes upto (including) the last one will be included." + }, + { + "name": "start", + "type": "int64", + "required": false, + "description": "(Optional) Starting axis for slicing the shape. Default value is 0.Negative value means counting dimensions from the back." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "shape", + "type": "T1", + "description": "Shape of the input tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input tensor can be of arbitrary type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain output to int64 tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "shape", + "code": "x = np.array([\n [1, 2, 3],\n [4, 5, 6],\n]).astype(np.float32)\ntest_shape('_example', x) # preserve names of original test cases\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\n\ntest_shape('', x) # preserve names of original test cases\n\ntest_shape('_start_1', x, start=1)\n\ntest_shape('_end_1', x, end=1)\n\ntest_shape('_start_negative_1', x, start=-1)\n\ntest_shape('_end_negative_1', x, end=-1)\n\ntest_shape('_start_1_end_negative_1', x, start=1, end=-1)\n\ntest_shape('_start_1_end_2', x, start=1, end=2)\n\ntest_shape('_clip_start', x, start=-10)\n\ntest_shape('_clip_end', x, end=10)" + } + ] + }, + { + "name": "Shrink", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Shrink takes one input data (Tensor) and produces one Tensor output,\nhaving same datatype and shape with input. It has two attributes, lambd and\nbias. The formula of this operator is: If x < -lambd, y = x + bias;\nIf x > lambd, y = x - bias; Otherwise, y = 0.\n", + "attributes": [ + { + "name": "bias", + "type": "float32", + "required": false, + "description": "The bias value added to output. Default is 0." + }, + { + "name": "lambd", + "type": "float32", + "required": false, + "default": 0.5, + "description": "The lambd value for the Shrink formulation. Default is 0.5." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input data as Tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to only numeric types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "hard_shrink", + "code": "node = onnx.helper.make_node(\n 'Shrink',\n inputs=['x'],\n outputs=['y'],\n lambd=1.5,\n)\nX = np.arange(-2.0, 2.1, dtype=np.float32)\nY = np.array([-2, 0, 0, 0, 2], dtype=np.float32)\nexpect(node, inputs=[X], outputs=[Y],\n name='test_shrink_hard')" + }, + { + "summary": "soft_shrink", + "code": "node = onnx.helper.make_node(\n 'Shrink',\n inputs=['x'],\n outputs=['y'],\n lambd=1.5,\n bias=1.5,\n)\nX = np.arange(-2.0, 2.1, dtype=np.float32)\nY = np.array([-0.5, 0, 0, 0, 0.5], dtype=np.float32)\nexpect(node, inputs=[X], outputs=[Y],\n name='test_shrink_soft')" + } + ] + }, + { + "name": "Sigmoid", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Sigmoid takes one input data (Tensor) and produces one output data\n(Tensor) where the sigmoid function, y = 1 / (1 + exp(-x)), is applied to the\ntensor elementwise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sigmoid", + "code": "node = onnx.helper.make_node(\n 'Sigmoid',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = 1.0 / (1.0 + np.exp(np.negative(x))) # expected output [0.26894143, 0.5, 0.7310586]\nexpect(node, inputs=[x], outputs=[y],\n name='test_sigmoid_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = 1.0 / (1.0 + np.exp(np.negative(x)))\nexpect(node, inputs=[x], outputs=[y],\n name='test_sigmoid')" + } + ], + "category": "Activation" + }, + { + "name": "Sigmoid", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Sigmoid takes one input data (Tensor) and produces one output data\n(Tensor) where the sigmoid function, y = 1 / (1 + exp(-x)), is applied to the\ntensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sigmoid", + "code": "node = onnx.helper.make_node(\n 'Sigmoid',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = 1.0 / (1.0 + np.exp(np.negative(x))) # expected output [0.26894143, 0.5, 0.7310586]\nexpect(node, inputs=[x], outputs=[y],\n name='test_sigmoid_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = 1.0 / (1.0 + np.exp(np.negative(x)))\nexpect(node, inputs=[x], outputs=[y],\n name='test_sigmoid')" + } + ], + "category": "Activation" + }, + { + "name": "Sigmoid", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Sigmoid takes one input data (Tensor) and produces one output data\n(Tensor) where the sigmoid function, y = 1 / (1 + exp(-x)), is applied to the\ntensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "sigmoid", + "code": "node = onnx.helper.make_node(\n 'Sigmoid',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = 1.0 / (1.0 + np.exp(np.negative(x))) # expected output [0.26894143, 0.5, 0.7310586]\nexpect(node, inputs=[x], outputs=[y],\n name='test_sigmoid_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = 1.0 / (1.0 + np.exp(np.negative(x)))\nexpect(node, inputs=[x], outputs=[y],\n name='test_sigmoid')" + } + ], + "category": "Activation" + }, + { + "name": "Sign", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Calculate the sign of the given input tensor element-wise.\nIf input > 0, output 1. if input < 0, output -1. if input == 0, output 0.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The sign of the input tensor computed element-wise. It has the same shape and type of the input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sign", + "code": "node = onnx.helper.make_node(\n 'Sign',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array(range(-5, 6)).astype(np.float32)\ny = np.sign(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_sign')" + } + ] + }, + { + "name": "Sign", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Calculate the sign of the given input tensor element-wise.\nIf input > 0, output 1. if input < 0, output -1. if input == 0, output 0.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The sign of the input tensor computed element-wise. It has the same shape and type of the input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "sign", + "code": "node = onnx.helper.make_node(\n 'Sign',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array(range(-5, 6)).astype(np.float32)\ny = np.sign(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_sign')" + } + ] + }, + { + "name": "Sin", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Calculates the sine of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The sine of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sin", + "code": "node = onnx.helper.make_node(\n 'Sin',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.sin(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_sin_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.sin(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_sin')" + } + ] + }, + { + "name": "Sinh", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Calculates the hyperbolic sine of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The hyperbolic sine values of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sinh", + "code": "node = onnx.helper.make_node(\n 'Sinh',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.sinh(x) # expected output [-1.17520118, 0., 1.17520118]\nexpect(node, inputs=[x], outputs=[y],\n name='test_sinh_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.sinh(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_sinh')" + } + ] + }, + { + "name": "Size", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Takes a tensor as input and outputs a int64 scalar that equals to the total number of elements of the input tensor.\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "size", + "type": "T1", + "description": "Total number of elements of the input tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input tensor can be of arbitrary type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain output to int64 tensor, which should be a scalar though.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "size", + "code": "node = onnx.helper.make_node(\n 'Size',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([\n [1, 2, 3],\n [4, 5, 6],\n]).astype(np.float32)\ny = np.array(6).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_size_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.array(x.size).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_size')" + } + ] + }, + { + "name": "Size", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Takes a tensor as input and outputs a int64 scalar that equals to the total number of elements of the input tensor.\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "size", + "type": "T1", + "description": "Total number of elements of the input tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input tensor can be of arbitrary type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain output to int64 tensor, which should be a scalar though.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "size", + "code": "node = onnx.helper.make_node(\n 'Size',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([\n [1, 2, 3],\n [4, 5, 6],\n]).astype(np.float32)\ny = np.array(6).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_size_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.array(x.size).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_size')" + } + ] + }, + { + "name": "Slice", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Produces a slice of the input tensor along multiple axes. Similar to numpy:\nhttps://docs.scipy.org/doc/numpy/reference/arrays.indexing.html\nSlices uses `axes`, `starts` and `ends` attributes to specify the start and end\ndimension for each axis in the list of axes, it uses this information to\nslice the input `data` tensor. If a negative value is passed for any of the\nstart or end indices, it represent number of elements before the end of that\ndimension. If the value passed to start or end is larger than the `n` (the\nnumber of elements in this dimension), it represents `n`. For slicing to the\nend of a dimension with unknown size, it is recommended to pass in `INT_MAX`.\nIf `axes` are omitted, they are set to `[0, ..., ndim-1]`.\nExample 1:\n data = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n axes = [0, 1]\n starts = [1, 0]\n ends = [2, 3]\n result = [\n [5, 6, 7],\n ]\nExample 2:\n data = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n starts = [0, 1]\n ends = [-1, 1000]\n result = [\n [2, 3, 4],\n ]\n", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "Axes that `starts` and `ends` apply to. It's optional. If not present, will be treated as [0, 1, ..., len(`starts`) - 1]." + }, + { + "name": "ends", + "type": "int64[]", + "required": true, + "description": "Ending indices (exclusive) of corresponding axis in axes`" + }, + { + "name": "starts", + "type": "int64[]", + "required": true, + "description": "Starting indices of corresponding axis in `axes`" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of data to extract slices from." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Sliced data tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "slice", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\ny = x[0:3, 0:10]\nstarts = np.array([0, 0], dtype=np.int64)\nends = np.array([3, 10], dtype=np.int64)\naxes = np.array([0, 1], dtype=np.int64)\nsteps = np.array([1, 1], dtype=np.int64)\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice')" + }, + { + "summary": "slice_default_axes", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends], outputs=[y],\n name='test_slice_default_axes')" + }, + { + "summary": "slice_default_steps", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\naxes = np.array([0, 1, 2], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends, axes], outputs=[y],\n name='test_slice_default_steps')" + }, + { + "summary": "slice_end_out_of_bounds", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([1], dtype=np.int64)\nends = np.array([1000], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 1:1000]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_end_out_of_bounds')" + }, + { + "summary": "slice_neg", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0], dtype=np.int64)\nends = np.array([-1], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 0:-1]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_neg')" + }, + { + "summary": "slice_neg_steps", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([20, 10, 4], dtype=np.int64)\nends = np.array([0, 0, 1], dtype=np.int64)\naxes = np.array([0, 1, 2], dtype=np.int64)\nsteps = np.array([-1, -3, -2]).astype(np.int64)\ny = x[20:0:-1, 10:0:-3, 4:1:-2]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_neg_steps')" + }, + { + "summary": "slice_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\naxes = np.array([0, -2, -1], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends, axes], outputs=[y],\n name='test_slice_negative_axes')" + }, + { + "summary": "slice_start_out_of_bounds", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([1000], dtype=np.int64)\nends = np.array([1000], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 1000:1000]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_start_out_of_bounds')" + } + ], + "category": "Tensor" + }, + { + "name": "Slice", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Produces a slice of the input tensor along multiple axes. Similar to numpy:\nhttps://docs.scipy.org/doc/numpy/reference/arrays.indexing.html\nSlices uses `starts`, `ends`, `axes` and `steps` inputs to specify the start and end\ndimension and step for each axis in the list of axes, it uses this information to\nslice the input `data` tensor. If a negative value is passed for any of the\nstart or end indices, it represent number of elements before the end of that\ndimension. If the value passed to start or end is larger than the `n` (the\nnumber of elements in this dimension), it represents `n`. For slicing to the\nend of a dimension with unknown size, it is recommended to pass in `INT_MAX`.\nIf a negative value is passed for step, it represents slicing backward.\nIf `axes` are omitted, they are set to `[0, ..., ndim-1]`.\nIf `steps` are omitted, they are set to `[1, ..., 1]` of length `len(starts)`\nExample 1:\n data = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n axes = [0, 1]\n starts = [1, 0]\n ends = [2, 3]\n steps = [1, 2]\n result = [\n [5, 7],\n ]\nExample 2:\n data = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n starts = [0, 1]\n ends = [-1, 1000]\n result = [\n [2, 3, 4],\n ]\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of data to extract slices from." + }, + { + "name": "starts", + "type": "Tind", + "description": "1-D tensor of starting indices of corresponding axis in `axes`" + }, + { + "name": "ends", + "type": "Tind", + "description": "1-D tensor of ending indices (exclusive) of corresponding axis in `axes`" + }, + { + "name": "axes", + "type": "Tind", + "option": "optional", + "description": "1-D tensor of axes that `starts` and `ends` apply to." + }, + { + "name": "steps", + "type": "Tind", + "option": "optional", + "description": "1-D tensor of slice step of corresponding axis in `axes`. Default to 1. " + } + ], + "min_input": 3, + "max_input": 5, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Sliced data tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "3 - 5", + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "slice", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\ny = x[0:3, 0:10]\nstarts = np.array([0, 0], dtype=np.int64)\nends = np.array([3, 10], dtype=np.int64)\naxes = np.array([0, 1], dtype=np.int64)\nsteps = np.array([1, 1], dtype=np.int64)\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice')" + }, + { + "summary": "slice_default_axes", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends], outputs=[y],\n name='test_slice_default_axes')" + }, + { + "summary": "slice_default_steps", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\naxes = np.array([0, 1, 2], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends, axes], outputs=[y],\n name='test_slice_default_steps')" + }, + { + "summary": "slice_end_out_of_bounds", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([1], dtype=np.int64)\nends = np.array([1000], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 1:1000]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_end_out_of_bounds')" + }, + { + "summary": "slice_neg", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0], dtype=np.int64)\nends = np.array([-1], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 0:-1]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_neg')" + }, + { + "summary": "slice_neg_steps", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([20, 10, 4], dtype=np.int64)\nends = np.array([0, 0, 1], dtype=np.int64)\naxes = np.array([0, 1, 2], dtype=np.int64)\nsteps = np.array([-1, -3, -2]).astype(np.int64)\ny = x[20:0:-1, 10:0:-3, 4:1:-2]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_neg_steps')" + }, + { + "summary": "slice_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\naxes = np.array([0, -2, -1], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends, axes], outputs=[y],\n name='test_slice_negative_axes')" + }, + { + "summary": "slice_start_out_of_bounds", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([1000], dtype=np.int64)\nends = np.array([1000], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 1000:1000]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_start_out_of_bounds')" + } + ], + "category": "Tensor" + }, + { + "name": "Slice", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Produces a slice of the input tensor along multiple axes. Similar to numpy:\nhttps://docs.scipy.org/doc/numpy/reference/arrays.indexing.html\nSlices uses `starts`, `ends`, `axes` and `steps` inputs to specify the start and end\ndimension and step for each axis in the list of axes, it uses this information to\nslice the input `data` tensor. If a negative value is passed for any of the\nstart or end indices, it represents number of elements before the end of that\ndimension. If the value passed to start or end is larger than the `n` (the\nnumber of elements in this dimension), it represents `n`. For slicing to the\nend of a dimension with unknown size, it is recommended to pass in `INT_MAX`\nwhen sclicing forward and 'INT_MIN' when slicing backward.\nIf a negative value is passed for step, it represents slicing backward.\nHowever step value cannot be 0.\nIf `axes` are omitted, they are set to `[0, ..., ndim-1]`.\nIf `steps` are omitted, they are set to `[1, ..., 1]` of length `len(starts)`\nExample 1:\n data = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n axes = [0, 1]\n starts = [1, 0]\n ends = [2, 3]\n steps = [1, 2]\n result = [\n [5, 7],\n ]\nExample 2:\n data = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n starts = [0, 1]\n ends = [-1, 1000]\n result = [\n [2, 3, 4],\n ]\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of data to extract slices from." + }, + { + "name": "starts", + "type": "Tind", + "description": "1-D tensor of starting indices of corresponding axis in `axes`" + }, + { + "name": "ends", + "type": "Tind", + "description": "1-D tensor of ending indices (exclusive) of corresponding axis in `axes`" + }, + { + "name": "axes", + "type": "Tind", + "option": "optional", + "description": "1-D tensor of axes that `starts` and `ends` apply to. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + }, + { + "name": "steps", + "type": "Tind", + "option": "optional", + "description": "1-D tensor of slice step of corresponding axis in `axes`. Negative value means slicing backward. 'steps' cannot be 0. Defaults to 1." + } + ], + "min_input": 3, + "max_input": 5, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Sliced data tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "3 - 5", + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "slice", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\ny = x[0:3, 0:10]\nstarts = np.array([0, 0], dtype=np.int64)\nends = np.array([3, 10], dtype=np.int64)\naxes = np.array([0, 1], dtype=np.int64)\nsteps = np.array([1, 1], dtype=np.int64)\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice')" + }, + { + "summary": "slice_default_axes", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends], outputs=[y],\n name='test_slice_default_axes')" + }, + { + "summary": "slice_default_steps", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\naxes = np.array([0, 1, 2], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends, axes], outputs=[y],\n name='test_slice_default_steps')" + }, + { + "summary": "slice_end_out_of_bounds", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([1], dtype=np.int64)\nends = np.array([1000], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 1:1000]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_end_out_of_bounds')" + }, + { + "summary": "slice_neg", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0], dtype=np.int64)\nends = np.array([-1], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 0:-1]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_neg')" + }, + { + "summary": "slice_neg_steps", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([20, 10, 4], dtype=np.int64)\nends = np.array([0, 0, 1], dtype=np.int64)\naxes = np.array([0, 1, 2], dtype=np.int64)\nsteps = np.array([-1, -3, -2]).astype(np.int64)\ny = x[20:0:-1, 10:0:-3, 4:1:-2]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_neg_steps')" + }, + { + "summary": "slice_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\naxes = np.array([0, -2, -1], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends, axes], outputs=[y],\n name='test_slice_negative_axes')" + }, + { + "summary": "slice_start_out_of_bounds", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([1000], dtype=np.int64)\nends = np.array([1000], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 1000:1000]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_start_out_of_bounds')" + } + ], + "category": "Tensor" + }, + { + "name": "Slice", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Produces a slice of the input tensor along multiple axes. Similar to numpy:\nhttps://numpy.org/doc/stable/user/basics.indexing.html?highlight=slice#slicing-and-striding\n\nSlice uses the `starts`, `ends`, `axes` and `steps` inputs to select a sub-tensor\nof its input `data` tensor.\n\nAn effective `start[i]`, `end[i]`, and `step[i]` must be computed for each `i`\nin `[0, ... r-1]` where `r = rank(input)` as follows:\n\nIf `axes` are omitted, they are set to `[0, ..., r-1]`.\nIf `steps` are omitted, they are set to `[1, ..., 1]` of length `len(starts)`\n\nThe effective values are initialized as `start[i] = 0`, `end[i] = dims[i]` where\n`dims` are the dimensions of `input` and `step[i] = `1.\n\nAll negative elements of `axes` are made non-negatve by adding `r` to them, where\n`r =rank(input)`.\n\nAll negative values in `starts[i]` and `ends[i]` have `dims[axes[i]]` added to them,\nwhere `dims` are the dimensions of `input`. Then `start[axes[i]]` is the adjusted\n`starts[i]` is clamped into the range `[0, dims[axes[i]]]` for positive stepping\nand `[0, dims[axes[i]]-1]` for negative stepping.\n\nThe clamping for the adjusted `ends[i]` depends on the sign of `steps[i]` and must\naccommodate copying 0 through `dims[axes[i]]` elements, so for positive stepping\n`end[axes[i]]` is clamped to `[0, dims[axes[i]]]`, while for negative stepping it\nis clamped to `[-1, dims[axes[i]]-1]`.\n\nFinally, `step[axes[i]] = steps[i]`.\n\nFor slicing to the end of a dimension with unknown size, it is recommended to pass\nin `INT_MAX` when slicing forward and 'INT_MIN' when slicing backward.\n\nExample 1:\n data = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n axes = [0, 1]\n starts = [1, 0]\n ends = [2, 3]\n steps = [1, 2]\n result = [\n [5, 7],\n ]\nExample 2:\n data = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n starts = [0, 1]\n ends = [-1, 1000]\n result = [\n [2, 3, 4],\n ]\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensor of data to extract slices from." + }, + { + "name": "starts", + "type": "Tind", + "description": "1-D tensor of starting indices of corresponding axis in `axes`" + }, + { + "name": "ends", + "type": "Tind", + "description": "1-D tensor of ending indices (exclusive) of corresponding axis in `axes`" + }, + { + "name": "axes", + "type": "Tind", + "option": "optional", + "description": "1-D tensor of axes that `starts` and `ends` apply to. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data). Behavior is undefined if an axis is repeated." + }, + { + "name": "steps", + "type": "Tind", + "option": "optional", + "description": "1-D tensor of slice step of corresponding axis in `axes`. Negative value means slicing backward. 'steps' cannot be 0. Defaults to 1s." + } + ], + "min_input": 3, + "max_input": 5, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Sliced data tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "3 - 5", + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain indices to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "slice", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\ny = x[0:3, 0:10]\nstarts = np.array([0, 0], dtype=np.int64)\nends = np.array([3, 10], dtype=np.int64)\naxes = np.array([0, 1], dtype=np.int64)\nsteps = np.array([1, 1], dtype=np.int64)\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice')" + }, + { + "summary": "slice_default_axes", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends], outputs=[y],\n name='test_slice_default_axes')" + }, + { + "summary": "slice_default_steps", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\naxes = np.array([0, 1, 2], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends, axes], outputs=[y],\n name='test_slice_default_steps')" + }, + { + "summary": "slice_end_out_of_bounds", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([1], dtype=np.int64)\nends = np.array([1000], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 1:1000]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_end_out_of_bounds')" + }, + { + "summary": "slice_neg", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0], dtype=np.int64)\nends = np.array([-1], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 0:-1]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_neg')" + }, + { + "summary": "slice_neg_steps", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([20, 10, 4], dtype=np.int64)\nends = np.array([0, 0, 1], dtype=np.int64)\naxes = np.array([0, 1, 2], dtype=np.int64)\nsteps = np.array([-1, -3, -2]).astype(np.int64)\ny = x[20:0:-1, 10:0:-3, 4:1:-2]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_neg_steps')" + }, + { + "summary": "slice_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([0, 0, 3], dtype=np.int64)\nends = np.array([20, 10, 4], dtype=np.int64)\naxes = np.array([0, -2, -1], dtype=np.int64)\ny = x[:, :, 3:4]\n\nexpect(node, inputs=[x, starts, ends, axes], outputs=[y],\n name='test_slice_negative_axes')" + }, + { + "summary": "slice_start_out_of_bounds", + "code": "node = onnx.helper.make_node(\n 'Slice',\n inputs=['x', 'starts', 'ends', 'axes', 'steps'],\n outputs=['y'],\n)\n\nx = np.random.randn(20, 10, 5).astype(np.float32)\nstarts = np.array([1000], dtype=np.int64)\nends = np.array([1000], dtype=np.int64)\naxes = np.array([1], dtype=np.int64)\nsteps = np.array([1], dtype=np.int64)\ny = x[:, 1000:1000]\n\nexpect(node, inputs=[x, starts, ends, axes, steps], outputs=[y],\n name='test_slice_start_out_of_bounds')" + } + ], + "category": "Tensor" + }, + { + "name": "Softmax", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "The operator computes the softmax (normalized exponential) values for each layer in the batch\n of the given input. The input is a 2-D tensor (Tensor) of size\n(batch_size x input_feature_dimensions). The output tensor has the same shape\nand contains the softmax values of the corresponding input.\n\nInput does not need to explicitly be a 2D vector; rather, it will be\ncoerced into one. For an arbitrary n-dimensional tensor\ninput \\in [a_0, a_1, ..., a_{k-1}, a_k, ..., a_{n-1}] and k is\nthe axis provided, then input will be coerced into a 2-dimensional tensor with\ndimensions [a_0 * ... * a_{k-1}, a_k * ... * a_{n-1}]. For the default\ncase where axis=1, this means the input tensor will be coerced into a 2D tensor\nof dimensions [a_0, a_1 * ... * a_{n-1}], where a_0 is often the batch size.\nIn this situation, we must have a_0 = N and a_1 * ... * a_{n-1} = D.\nEach of these dimensions must be matched correctly, or else the operator\nwill throw errors.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Describes the axis of the inputs when coerced to 2D; defaults to one because the 0th axis most likely describes the batch_size" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input tensor that's coerced into a 2D matrix of size (NxD) as described above." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output values with the same shape as input tensor (the original size without coercion)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "softmax", + "code": "node = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.array([[-1, 0, 1]]).astype(np.float32)\n# expected output [[0.09003058, 0.24472848, 0.66524094]]\ny = softmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_example')" + }, + { + "summary": "softmax_axis", + "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]\n ).astype(np.float32)\n# expected output\n# [[0.032058604 0.08714432 0.23688284 0.6439143 ]\n# [0.032058604 0.08714432 0.23688284 0.6439143 ]]\ny = softmax(x)\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_large_number')\n\nx = np.abs(np.random.randn(3, 4, 5).astype(np.float32))\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=0,\n)\ny = softmax(x, axis=0)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_axis_0')\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=1,\n)\ny = softmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_axis_1')\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=2,\n)\ny = softmax(x, axis=2)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_axis_2')\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=-1,\n)\ny = softmax(x, axis=-1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_negative_axis')\n\n# default axis is -1\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_default_axis')" + } + ], + "category": "Activation" + }, + { + "name": "Softmax", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "The operator computes the softmax (normalized exponential) values for each layer in the batch\n of the given input.\n\nThe input does not need to explicitly be a 2D vector; rather, it will be\ncoerced into one. For an arbitrary n-dimensional tensor\ninput \\in [a_0, a_1, ..., a_{k-1}, a_k, ..., a_{n-1}] and k is\nthe axis provided, then input will be coerced into a 2-dimensional tensor with\ndimensions [a_0 * ... * a_{k-1}, a_k * ... * a_{n-1}]. For the default\ncase where axis=1, this means the input tensor will be coerced into a 2D tensor\nof dimensions [a_0, a_1 * ... * a_{n-1}], where a_0 is often the batch size.\nIn this situation, we must have a_0 = N and a_1 * ... * a_{n-1} = D.\nEach of these dimensions must be matched correctly, or else the operator\nwill throw errors. The output tensor has the same shape\nand contains the softmax values of the corresponding input.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": 1, + "description": "Describes the axis of the inputs when coerced to 2D; defaults to one because the 0th axis most likely describes the batch_size. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input tensor that's coerced into a 2D matrix of size (NxD) as described above." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output values with the same shape as input tensor (the original size without coercion)." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "softmax", + "code": "node = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.array([[-1, 0, 1]]).astype(np.float32)\n# expected output [[0.09003058, 0.24472848, 0.66524094]]\ny = softmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_example')" + }, + { + "summary": "softmax_axis", + "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]\n ).astype(np.float32)\n# expected output\n# [[0.032058604 0.08714432 0.23688284 0.6439143 ]\n# [0.032058604 0.08714432 0.23688284 0.6439143 ]]\ny = softmax(x)\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_large_number')\n\nx = np.abs(np.random.randn(3, 4, 5).astype(np.float32))\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=0,\n)\ny = softmax(x, axis=0)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_axis_0')\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=1,\n)\ny = softmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_axis_1')\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=2,\n)\ny = softmax(x, axis=2)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_axis_2')\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=-1,\n)\ny = softmax(x, axis=-1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_negative_axis')\n\n# default axis is -1\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_default_axis')" + } + ], + "category": "Activation" + }, + { + "name": "Softmax", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "The operator computes the normalized exponential values for the given input:\n\n Softmax(input, axis) = Exp(input) / ReduceSum(Exp(input), axis=axis, keepdims=1) \n\nThe \"axis\" attribute indicates the dimension along which Softmax\nwill be performed. The output tensor has the same shape\nand contains the Softmax values of the corresponding input.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": -1, + "description": "\nDescribes the dimension Softmax will be performed on.\nNegative value means counting dimensions\nfrom the back. Accepted range is [-r, r-1] where r = rank(input).\n" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The input tensor of rank >= axis." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The output values with the same shape as the input tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "softmax", + "code": "node = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n)\nx = np.array([[-1, 0, 1]]).astype(np.float32)\n# expected output [[0.09003058, 0.24472848, 0.66524094]]\ny = softmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_example')" + }, + { + "summary": "softmax_axis", + "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]\n ).astype(np.float32)\n# expected output\n# [[0.032058604 0.08714432 0.23688284 0.6439143 ]\n# [0.032058604 0.08714432 0.23688284 0.6439143 ]]\ny = softmax(x)\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_large_number')\n\nx = np.abs(np.random.randn(3, 4, 5).astype(np.float32))\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=0,\n)\ny = softmax(x, axis=0)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_axis_0')\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=1,\n)\ny = softmax(x, axis=1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_axis_1')\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=2,\n)\ny = softmax(x, axis=2)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_axis_2')\n\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n axis=-1,\n)\ny = softmax(x, axis=-1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_negative_axis')\n\n# default axis is -1\nnode = onnx.helper.make_node(\n 'Softmax',\n inputs=['x'],\n outputs=['y'],\n)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softmax_default_axis')" + } + ], + "category": "Activation" + }, + { + "name": "SoftmaxCrossEntropyLoss", + "module": "ai.onnx", + "version": 12, + "support_level": "common", + "description": "Loss function that measures the softmax cross entropy\nbetween 'scores' and 'labels'.\nThis operator first computes a loss tensor whose shape is identical to the labels input.\nIf the input is 2-D with shape (N, C), the loss tensor may be a N-element vector L = (l_1, l_2, ..., l_N).\nIf the input is N-D tensor with shape (N, C, D1, D2, ..., Dk),\nthe loss tensor L may have (N, D1, D2, ..., Dk) as its shape and L[i,][j_1][j_2]...[j_k] denotes a scalar element in L.\nAfter L is available, this operator can optionally do a reduction operator.\n\nshape(scores): (N, C) where C is the number of classes, or (N, C, D1, D2,..., Dk),\n with K >= 1 in case of K-dimensional loss.\nshape(labels): (N) where each value is 0 <= labels[i] <= C-1, or (N, D1, D2,..., Dk),\n with K >= 1 in case of K-dimensional loss.\n\nThe loss for one sample, l_i, can caculated as follows:\n l[i][d1][d2]...[dk] = -y[i][c][d1][d2]..[dk], where i is the index of classes.\nor\n l[i][d1][d2]...[dk] = -y[i][c][d1][d2]..[dk] * weights[c], if 'weights' is provided.\n\nloss is zero for the case when label-value equals ignore_index.\n l[i][d1][d2]...[dk] = 0, when labels[n][d1][d2]...[dk] = ignore_index\n\nwhere:\n p = Softmax(scores)\n y = Log(p)\n c = labels[i][d1][d2]...[dk]\n\nFinally, L is optionally reduced:\nIf reduction = 'none', the output is L with shape (N, D1, D2, ..., Dk).\nIf reduction = 'sum', the output is scalar: Sum(L).\nIf reduction = 'mean', the output is scalar: ReduceMean(L), or if weight is provided: ReduceSum(L) / ReduceSum(W),\nwhere tensor W is of shape (N, D1, D2, ..., Dk) and W[n][d1][d2]...[dk] = weights[labels[i][d1][d2]...[dk]].\n", + "attributes": [ + { + "name": "ignore_index", + "type": "int64", + "required": false, + "description": "Specifies a target value that is ignored and does not contribute to the input gradient. It's an optional value." + }, + { + "name": "reduction", + "type": "string", + "required": false, + "default": "mean", + "description": "Type of reduction to apply to loss: none, sum, mean(default). 'none': no reduction will be applied, 'sum': the output will be summed. 'mean': the sum of the output will be divided by the number of elements in the output." + } + ], + "inputs": [ + { + "name": "scores", + "type": "T", + "description": "The predicted outputs with shape [batch_size, class_size], or [batch_size, class_size, D1, D2 , ..., Dk], where K is the number of dimensions." + }, + { + "name": "labels", + "type": "Tind", + "description": "The ground truth output tensor, with shape [batch_size], or [batch_size, D1, D2, ..., Dk], where K is the number of dimensions. Labels element value shall be in range of [0, C). If ignore_index is specified, it may have a value outside [0, C) and the label values should either be in the range [0, C) or have the value ignore_index." + }, + { + "name": "weights", + "type": "T", + "option": "optional", + "description": "A manual rescaling weight given to each class. If given, it has to be a 1D Tensor assigning weight to each of the classes. Otherwise, it is treated as if having all ones." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Weighted loss float Tensor. If reduction is 'none', this has the shape of [batch_size], or [batch_size, D1, D2, ..., Dk] in case of K-dimensional loss. Otherwise, it is a scalar." + }, + { + "name": "log_prob", + "type": "T", + "option": "optional", + "description": "Log probability tensor. If the output of softmax is prob, its value is log(prob)." + } + ], + "min_output": 1, + "max_output": 2, + "inputs_range": "2 - 3", + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain target to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "input_shape_is_NCd1_mean_weight_negative_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(-1)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1 = 3, 5, 6\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1)).astype(np.int64)\nlabels[0][0] = -1\nweight = np.random.rand(C).astype(np.float32)\n\nsce = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[x, labels, weight], outputs=[sce], name='test_sce_NCd1_mean_weight_negative_ii')" + }, + { + "summary": "input_shape_is_NCd1_mean_weight_negative_ii_log_prob", + "code": "reduction = 'mean'\nignore_index = np.int64(-1)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1 = 3, 5, 6\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1)).astype(np.int64)\nlabels[0][0] = -1\nweight = np.random.rand(C).astype(np.float32)\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels, weight], outputs=[loss, log_prob], name='test_sce_NCd1_mean_weight_negative_ii_log_prob')" + }, + { + "summary": "input_shape_is_NCd1d2d3_none_no_weight_negative_ii", + "code": "reduction = 'none'\nignore_index = np.int64(-5)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1, dim2, dim3 = 3, 5, 6, 6, 5\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3)).astype(np.int64)\nlabels[0][0][0][0] = -5\n\nsce = softmaxcrossentropy(x,\n labels,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_NCd1d2d3_none_no_weight_negative_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3_none_no_weight_negative_ii_log_prob", + "code": "reduction = 'none'\nignore_index = np.int64(-5)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1, dim2, dim3 = 3, 5, 6, 6, 5\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3)).astype(np.int64)\nlabels[0][0][0][0] = -5\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_NCd1d2d3_none_no_weight_negative_ii_log_prob')" + }, + { + "summary": "input_shape_is_NCd1d2d3_sum_weight_high_ii", + "code": "reduction = 'sum'\nignore_index = np.int64(10)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C = 3, 5\nnp.random.seed(0)\nx = np.random.rand(N, C).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N)).astype(np.int64)\nlabels[0] = 10\nweight = np.random.rand(C).astype(np.float32)\n\nsce = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[x, labels, weight], outputs=[sce], name='test_sce_NCd1d2d3_sum_weight_high_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3_sum_weight_high_ii_log_prob", + "code": "reduction = 'sum'\nignore_index = np.int64(10)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C = 3, 5\nnp.random.seed(0)\nx = np.random.rand(N, C).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N)).astype(np.int64)\nlabels[0] = 10\nweight = np.random.rand(C).astype(np.float32)\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels, weight], outputs=[loss, log_prob], name='test_sce_NCd1d2d3_sum_weight_high_ii_log_prob')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight", + "code": "reduction = 'mean'\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nsce = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction)\n\nexpect(node, inputs=[x, labels, weight], outputs=[sce], name='test_sce_NCd1d2d3d4d5_mean_weight')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight_log_prob", + "code": "reduction = 'mean'\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels, weight], outputs=[loss, log_prob], name='test_sce_NCd1d2d3d4d5_mean_weight_log_prob')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight", + "code": "reduction = 'none'\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\n\nsce = softmaxcrossentropy(x,\n labels,\n reduction=reduction)\n\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_NCd1d2d3d4d5_none_no_weight')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight_log_prob", + "code": "reduction = 'none'\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n reduction=reduction,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_NCd1d2d3d4d5_none_no_weight_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_mean')" + }, + { + "summary": "softmaxcrossentropy_mean_3d", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\ny = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, y)\n\n# Check results\nexpect(node, inputs=[x, y], outputs=[sce], name='test_sce_mean_3d')" + }, + { + "summary": "softmaxcrossentropy_mean_3d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\ny = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, y, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, y], outputs=[loss, log_prob], name='test_sce_mean_3d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_mean_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nlabels[0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_mean_no_weight_ii')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_3d", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\nlabels[0][0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_mean_no_weight_ii_3d')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_3d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\nlabels[0][0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_mean_no_weight_ii_3d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_4d", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2, 7).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2, 7)).astype(np.int64)\nlabels[0][0][0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, reduction=reduction, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_mean_no_weight_ii_4d')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_4d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2, 7).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2, 7)).astype(np.int64)\nlabels[0][0][0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, reduction=reduction, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_mean_no_weight_ii_4d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nlabels[0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_mean_no_weight_ii_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_weights", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, weight=weights)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_mean_weight')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(0)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nlabels[0] = np.int64(0)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, weight=weights, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_mean_weight_ii')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_3d", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(1)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\nlabels[0][0] = np.int64(1)\nweights = np.array([0.2, 0.3, 0.6, 0.1, 0.5], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, weight=weights, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_mean_weight_ii_3d')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_3d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(1)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\nlabels[0][0] = np.int64(1)\nweights = np.array([0.2, 0.3, 0.6, 0.1, 0.5], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, weight=weights, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_mean_weight_ii_3d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_4d", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2, 7).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2, 7)).astype(np.int64)\nlabels[0][0][0] = np.int64(2)\nweights = np.array([0.2, 0.3, 0.6, 0.1, 0.5], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, reduction=reduction, weight=weights, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_mean_weight_ii_4d')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_4d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2, 7).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2, 7)).astype(np.int64)\nlabels[0][0][0] = np.int64(2)\nweights = np.array([0.2, 0.3, 0.6, 0.1, 0.5], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, reduction=reduction, weight=weights, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_mean_weight_ii_4d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(0)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nlabels[0] = np.int64(0)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, weight=weights, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_mean_weight_ii_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, weight=weights, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_mean_weight_log_prob')" + }, + { + "summary": "softmaxcrossentropy_none", + "code": "# Define operator attributes.\nreduction = 'none'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, reduction='none')\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_none')" + }, + { + "summary": "softmaxcrossentropy_none_log_prob", + "code": "# Define operator attributes.\nreduction = 'none'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, reduction='none', get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_none_log_prob')" + }, + { + "summary": "softmaxcrossentropy_none_weights", + "code": "# Define operator attributes.\nreduction = 'none'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, weight=weights, reduction='none')\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_none_weights')" + }, + { + "summary": "softmaxcrossentropy_none_weights_log_prob", + "code": "# Define operator attributes.\nreduction = 'none'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, weight=weights, reduction='none', get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_none_weights_log_prob')" + }, + { + "summary": "softmaxcrossentropy_sum", + "code": "# Define operator attributes.\nreduction = 'sum'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, reduction='sum')\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_sum')" + }, + { + "summary": "softmaxcrossentropy_sum_log_prob", + "code": "# Define operator attributes.\nreduction = 'sum'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, reduction='sum', get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_sum_log_prob')" + } + ] + }, + { + "name": "SoftmaxCrossEntropyLoss", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Loss function that measures the softmax cross entropy\nbetween 'scores' and 'labels'.\nThis operator first computes a loss tensor whose shape is identical to the labels input.\nIf the input is 2-D with shape (N, C), the loss tensor may be a N-element vector L = (l_1, l_2, ..., l_N).\nIf the input is N-D tensor with shape (N, C, D1, D2, ..., Dk),\nthe loss tensor L may have (N, D1, D2, ..., Dk) as its shape and L[i,][j_1][j_2]...[j_k] denotes a scalar element in L.\nAfter L is available, this operator can optionally do a reduction operator.\n\nshape(scores): (N, C) where C is the number of classes, or (N, C, D1, D2,..., Dk),\n with K >= 1 in case of K-dimensional loss.\nshape(labels): (N) where each value is 0 <= labels[i] <= C-1, or (N, D1, D2,..., Dk),\n with K >= 1 in case of K-dimensional loss.\n\nThe loss for one sample, l_i, can caculated as follows:\n l[i][d1][d2]...[dk] = -y[i][c][d1][d2]..[dk], where i is the index of classes.\nor\n l[i][d1][d2]...[dk] = -y[i][c][d1][d2]..[dk] * weights[c], if 'weights' is provided.\n\nloss is zero for the case when label-value equals ignore_index.\n l[i][d1][d2]...[dk] = 0, when labels[n][d1][d2]...[dk] = ignore_index\n\nwhere:\n p = Softmax(scores)\n y = Log(p)\n c = labels[i][d1][d2]...[dk]\n\nFinally, L is optionally reduced:\nIf reduction = 'none', the output is L with shape (N, D1, D2, ..., Dk).\nIf reduction = 'sum', the output is scalar: Sum(L).\nIf reduction = 'mean', the output is scalar: ReduceMean(L), or if weight is provided: ReduceSum(L) / ReduceSum(W),\nwhere tensor W is of shape (N, D1, D2, ..., Dk) and W[n][d1][d2]...[dk] = weights[labels[i][d1][d2]...[dk]].\n", + "attributes": [ + { + "name": "ignore_index", + "type": "int64", + "required": false, + "description": "Specifies a target value that is ignored and does not contribute to the input gradient. It's an optional value." + }, + { + "name": "reduction", + "type": "string", + "required": false, + "default": "mean", + "description": "Type of reduction to apply to loss: none, sum, mean(default). 'none': no reduction will be applied, 'sum': the output will be summed. 'mean': the sum of the output will be divided by the number of elements in the output." + } + ], + "inputs": [ + { + "name": "scores", + "type": "T", + "description": "The predicted outputs with shape [batch_size, class_size], or [batch_size, class_size, D1, D2 , ..., Dk], where K is the number of dimensions." + }, + { + "name": "labels", + "type": "Tind", + "description": "The ground truth output tensor, with shape [batch_size], or [batch_size, D1, D2, ..., Dk], where K is the number of dimensions. Labels element value shall be in range of [0, C). If ignore_index is specified, it may have a value outside [0, C) and the label values should either be in the range [0, C) or have the value ignore_index." + }, + { + "name": "weights", + "type": "T", + "option": "optional", + "description": "A manual rescaling weight given to each class. If given, it has to be a 1D Tensor assigning weight to each of the classes. Otherwise, it is treated as if having all ones." + } + ], + "min_input": 2, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Weighted loss float Tensor. If reduction is 'none', this has the shape of [batch_size], or [batch_size, D1, D2, ..., Dk] in case of K-dimensional loss. Otherwise, it is a scalar." + }, + { + "name": "log_prob", + "type": "T", + "option": "optional", + "description": "Log probability tensor. If the output of softmax is prob, its value is log(prob)." + } + ], + "min_output": 1, + "max_output": 2, + "inputs_range": "2 - 3", + "outputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + }, + { + "description": "Constrain target to integer types", + "type_param_str": "Tind", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "input_shape_is_NCd1_mean_weight_negative_ii", + "code": "reduction = 'mean'\nignore_index = np.int64(-1)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1 = 3, 5, 6\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1)).astype(np.int64)\nlabels[0][0] = -1\nweight = np.random.rand(C).astype(np.float32)\n\nsce = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[x, labels, weight], outputs=[sce], name='test_sce_NCd1_mean_weight_negative_ii')" + }, + { + "summary": "input_shape_is_NCd1_mean_weight_negative_ii_log_prob", + "code": "reduction = 'mean'\nignore_index = np.int64(-1)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1 = 3, 5, 6\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1)).astype(np.int64)\nlabels[0][0] = -1\nweight = np.random.rand(C).astype(np.float32)\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels, weight], outputs=[loss, log_prob], name='test_sce_NCd1_mean_weight_negative_ii_log_prob')" + }, + { + "summary": "input_shape_is_NCd1d2d3_none_no_weight_negative_ii", + "code": "reduction = 'none'\nignore_index = np.int64(-5)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1, dim2, dim3 = 3, 5, 6, 6, 5\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3)).astype(np.int64)\nlabels[0][0][0][0] = -5\n\nsce = softmaxcrossentropy(x,\n labels,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_NCd1d2d3_none_no_weight_negative_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3_none_no_weight_negative_ii_log_prob", + "code": "reduction = 'none'\nignore_index = np.int64(-5)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C, dim1, dim2, dim3 = 3, 5, 6, 6, 5\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3)).astype(np.int64)\nlabels[0][0][0][0] = -5\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_NCd1d2d3_none_no_weight_negative_ii_log_prob')" + }, + { + "summary": "input_shape_is_NCd1d2d3_sum_weight_high_ii", + "code": "reduction = 'sum'\nignore_index = np.int64(10)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C = 3, 5\nnp.random.seed(0)\nx = np.random.rand(N, C).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N)).astype(np.int64)\nlabels[0] = 10\nweight = np.random.rand(C).astype(np.float32)\n\nsce = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index)\n\nexpect(node, inputs=[x, labels, weight], outputs=[sce], name='test_sce_NCd1d2d3_sum_weight_high_ii')" + }, + { + "summary": "input_shape_is_NCd1d2d3_sum_weight_high_ii_log_prob", + "code": "reduction = 'sum'\nignore_index = np.int64(10)\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\nN, C = 3, 5\nnp.random.seed(0)\nx = np.random.rand(N, C).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N)).astype(np.int64)\nlabels[0] = 10\nweight = np.random.rand(C).astype(np.float32)\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels, weight], outputs=[loss, log_prob], name='test_sce_NCd1d2d3_sum_weight_high_ii_log_prob')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight", + "code": "reduction = 'mean'\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nsce = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction)\n\nexpect(node, inputs=[x, labels, weight], outputs=[sce], name='test_sce_NCd1d2d3d4d5_mean_weight')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight_log_prob", + "code": "reduction = 'mean'\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n weight=weight,\n reduction=reduction,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels, weight], outputs=[loss, log_prob], name='test_sce_NCd1d2d3d4d5_mean_weight_log_prob')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight", + "code": "reduction = 'none'\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\n\nsce = softmaxcrossentropy(x,\n labels,\n reduction=reduction)\n\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_NCd1d2d3d4d5_none_no_weight')" + }, + { + "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight_log_prob", + "code": "reduction = 'none'\n\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\nN, C, dim1, dim2, dim3, dim4, dim5 = 3, 5, 6, 6, 5, 3, 4\nnp.random.seed(0)\nx = np.random.rand(N, C, dim1, dim2, dim3, dim4, dim5).astype(np.float32)\nlabels = np.random.randint(0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)).astype(np.int64)\n\nloss, log_prob = softmaxcrossentropy(x,\n labels,\n reduction=reduction,\n get_log_prob=True)\n\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_NCd1d2d3d4d5_none_no_weight_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_mean')" + }, + { + "summary": "softmaxcrossentropy_mean_3d", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\ny = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, y)\n\n# Check results\nexpect(node, inputs=[x, y], outputs=[sce], name='test_sce_mean_3d')" + }, + { + "summary": "softmaxcrossentropy_mean_3d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\ny = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, y, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, y], outputs=[loss, log_prob], name='test_sce_mean_3d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_mean_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nlabels[0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_mean_no_weight_ii')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_3d", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\nlabels[0][0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_mean_no_weight_ii_3d')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_3d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\nlabels[0][0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_mean_no_weight_ii_3d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_4d", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2, 7).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2, 7)).astype(np.int64)\nlabels[0][0][0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, reduction=reduction, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_mean_no_weight_ii_4d')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_4d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2, 7).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2, 7)).astype(np.int64)\nlabels[0][0][0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, reduction=reduction, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_mean_no_weight_ii_4d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_no_weights_ii_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nlabels[0] = np.int64(2)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_mean_no_weight_ii_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_weights", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, weight=weights)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_mean_weight')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(0)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nlabels[0] = np.int64(0)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, weight=weights, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_mean_weight_ii')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_3d", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(1)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\nlabels[0][0] = np.int64(1)\nweights = np.array([0.2, 0.3, 0.6, 0.1, 0.5], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, weight=weights, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_mean_weight_ii_3d')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_3d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(1)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2)).astype(np.int64)\nlabels[0][0] = np.int64(1)\nweights = np.array([0.2, 0.3, 0.6, 0.1, 0.5], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, weight=weights, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_mean_weight_ii_3d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_4d", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2, 7).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2, 7)).astype(np.int64)\nlabels[0][0][0] = np.int64(2)\nweights = np.array([0.2, 0.3, 0.6, 0.1, 0.5], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, reduction=reduction, weight=weights, ignore_index=ignore_index)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_mean_weight_ii_4d')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_4d_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(2)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5, 2, 7).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, 2, 7)).astype(np.int64)\nlabels[0][0][0] = np.int64(2)\nweights = np.array([0.2, 0.3, 0.6, 0.1, 0.5], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, reduction=reduction, weight=weights, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_mean_weight_ii_4d_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_ii_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\nignore_index = np.int64(0)\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction,\n ignore_index=ignore_index)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nlabels[0] = np.int64(0)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, weight=weights, ignore_index=ignore_index, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_mean_weight_ii_log_prob')" + }, + { + "summary": "softmaxcrossentropy_mean_weights_log_prob", + "code": "# Define operator attributes.\nreduction = 'mean'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, weight=weights, get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_mean_weight_log_prob')" + }, + { + "summary": "softmaxcrossentropy_none", + "code": "# Define operator attributes.\nreduction = 'none'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, reduction='none')\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_none')" + }, + { + "summary": "softmaxcrossentropy_none_log_prob", + "code": "# Define operator attributes.\nreduction = 'none'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, reduction='none', get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_none_log_prob')" + }, + { + "summary": "softmaxcrossentropy_none_weights", + "code": "# Define operator attributes.\nreduction = 'none'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, weight=weights, reduction='none')\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[sce], name='test_sce_none_weights')" + }, + { + "summary": "softmaxcrossentropy_none_weights_log_prob", + "code": "# Define operator attributes.\nreduction = 'none'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y', 'w'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\nweights = np.array([0.9, 0.7, 0.8, 0.9, 0.9], dtype=np.float32)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, weight=weights, reduction='none', get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels, weights], outputs=[loss, log_prob], name='test_sce_none_weights_log_prob')" + }, + { + "summary": "softmaxcrossentropy_sum", + "code": "# Define operator attributes.\nreduction = 'sum'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nsce = softmaxcrossentropy(x, labels, reduction='sum')\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[sce], name='test_sce_sum')" + }, + { + "summary": "softmaxcrossentropy_sum_log_prob", + "code": "# Define operator attributes.\nreduction = 'sum'\n\n# Create operator.\nnode = onnx.helper.make_node('SoftmaxCrossEntropyLoss',\n inputs=['x', 'y'],\n outputs=['z', 'log_prob'],\n reduction=reduction)\n\n# Define operator inputs.\nnp.random.seed(0)\nx = np.random.rand(3, 5).astype(np.float32)\nlabels = np.random.randint(0, high=5, size=(3, )).astype(np.int64)\n\n# Compute SoftmaxCrossEntropyLoss\nloss, log_prob = softmaxcrossentropy(x, labels, reduction='sum', get_log_prob=True)\n\n# Check results\nexpect(node, inputs=[x, labels], outputs=[loss, log_prob], name='test_sce_sum_log_prob')" + } + ] + }, + { + "name": "Softplus", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Softplus takes one input data (Tensor) and produces one output data\n(Tensor) where the softplus function, y = ln(exp(x) + 1), is applied to\nthe tensor elementwise.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "1D input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "1D input tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "softplus", + "code": "node = onnx.helper.make_node(\n 'Softplus',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.log(np.exp(x) + 1) # expected output [0.31326166, 0.69314718, 1.31326163]\nexpect(node, inputs=[x], outputs=[y],\n name='test_softplus_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.log(np.exp(x) + 1)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softplus')" + } + ], + "category": "Activation" + }, + { + "name": "Softsign", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Calculates the softsign (x/(1+|x|)) of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The softsign (x/(1+|x|)) values of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "softsign", + "code": "node = onnx.helper.make_node(\n 'Softsign',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.array([-0.5, 0, 0.5]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_softsign_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x / (1 + np.abs(x))\nexpect(node, inputs=[x], outputs=[y],\n name='test_softsign')" + } + ], + "category": "Activation" + }, + { + "name": "SpaceToDepth", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "SpaceToDepth rearranges blocks of spatial data into depth. More specifically,\nthis op outputs a copy of the input tensor where values from the height and width dimensions\nare moved to the depth dimension.\n", + "attributes": [ + { + "name": "blocksize", + "type": "int64", + "required": true, + "description": "Blocks of [blocksize, blocksize] are moved." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of [N,C,H,W], where N is the batch axis, C is the channel or depth, H is the height and W is the width." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of [N, C * blocksize * blocksize, H/blocksize, W/blocksize]." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "example", + "code": "node = onnx.helper.make_node(\n 'SpaceToDepth',\n inputs=['x'],\n outputs=['y'],\n blocksize=2,\n)\n\n# (1, 1, 4, 6) input tensor\nx = np.array([[[[0, 6, 1, 7, 2, 8],\n [12, 18, 13, 19, 14, 20],\n [3, 9, 4, 10, 5, 11],\n [15, 21, 16, 22, 17, 23]]]]).astype(np.float32)\n\n# (1, 4, 2, 3) output tensor\ny = np.array([[[[0, 1, 2],\n [3, 4, 5]],\n [[6, 7, 8],\n [9, 10, 11]],\n [[12, 13, 14],\n [15, 16, 17]],\n [[18, 19, 20],\n [21, 22, 23]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_spacetodepth_example')" + }, + { + "summary": "spacetodepth", + "code": "b, c, h, w = shape = (2, 2, 6, 6)\nblocksize = 2\nnode = onnx.helper.make_node(\n 'SpaceToDepth',\n inputs=['x'],\n outputs=['y'],\n blocksize=blocksize,\n)\nx = np.random.random_sample(shape).astype(np.float32)\ntmp = np.reshape(x, [b, c,\n h // blocksize, blocksize,\n w // blocksize, blocksize])\ntmp = np.transpose(tmp, [0, 3, 5, 1, 2, 4])\ny = np.reshape(tmp, [b, c * (blocksize**2),\n h // blocksize,\n w // blocksize])\nexpect(node, inputs=[x], outputs=[y],\n name='test_spacetodepth')" + } + ] + }, + { + "name": "SpaceToDepth", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "SpaceToDepth rearranges blocks of spatial data into depth. More specifically,\nthis op outputs a copy of the input tensor where values from the height and width dimensions\nare moved to the depth dimension.\n", + "attributes": [ + { + "name": "blocksize", + "type": "int64", + "required": true, + "description": "Blocks of [blocksize, blocksize] are moved." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of [N,C,H,W], where N is the batch axis, C is the channel or depth, H is the height and W is the width." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of [N, C * blocksize * blocksize, H/blocksize, W/blocksize]." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "example", + "code": "node = onnx.helper.make_node(\n 'SpaceToDepth',\n inputs=['x'],\n outputs=['y'],\n blocksize=2,\n)\n\n# (1, 1, 4, 6) input tensor\nx = np.array([[[[0, 6, 1, 7, 2, 8],\n [12, 18, 13, 19, 14, 20],\n [3, 9, 4, 10, 5, 11],\n [15, 21, 16, 22, 17, 23]]]]).astype(np.float32)\n\n# (1, 4, 2, 3) output tensor\ny = np.array([[[[0, 1, 2],\n [3, 4, 5]],\n [[6, 7, 8],\n [9, 10, 11]],\n [[12, 13, 14],\n [15, 16, 17]],\n [[18, 19, 20],\n [21, 22, 23]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y],\n name='test_spacetodepth_example')" + }, + { + "summary": "spacetodepth", + "code": "b, c, h, w = shape = (2, 2, 6, 6)\nblocksize = 2\nnode = onnx.helper.make_node(\n 'SpaceToDepth',\n inputs=['x'],\n outputs=['y'],\n blocksize=blocksize,\n)\nx = np.random.random_sample(shape).astype(np.float32)\ntmp = np.reshape(x, [b, c,\n h // blocksize, blocksize,\n w // blocksize, blocksize])\ntmp = np.transpose(tmp, [0, 3, 5, 1, 2, 4])\ny = np.reshape(tmp, [b, c * (blocksize**2),\n h // blocksize,\n w // blocksize])\nexpect(node, inputs=[x], outputs=[y],\n name='test_spacetodepth')" + } + ] + }, + { + "name": "Split", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Split a tensor into a list of tensors, along the specified\n'axis'. The lengths of the split can be specified using argument 'axis' or\noptional second input blob to the operator. Otherwise, the tensor is split\nto equal sized parts.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to split on" + }, + { + "name": "split", + "type": "int64[]", + "required": false, + "description": "length of each output" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The tensor to split" + }, + { + "name": "split", + "type": "T", + "option": "optional", + "description": "Optional list of output lengths (see also arg 'split')" + } + ], + "min_input": 1, + "max_input": 2, + "outputs": [ + { + "name": "outputs...", + "type": "T", + "list": true, + "description": "One or more outputs forming list of tensors after splitting" + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "1 - 2", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "1d", + "code": "input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2', 'output_3'],\n axis=0\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_1d')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2'],\n axis=0,\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_1d')" + }, + { + "summary": "2d", + "code": "input = np.array([[1., 2., 3., 4., 5., 6.],\n [7., 8., 9., 10., 11., 12.]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2'],\n axis=1\n)\n\nexpected_outputs = [np.array([[1., 2., 3.], [7., 8., 9.]]).astype(np.float32),\n np.array([[4., 5., 6.], [10., 11., 12.]]).astype(np.float32)]\n\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_2d')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2'],\n axis=1,\n)\n\nexpected_outputs = [np.array([[1., 2.], [7., 8.]]).astype(np.float32),\n np.array([[3., 4., 5., 6.], [9., 10., 11., 12.]]).astype(np.float32)]\n\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_2d')" + }, + { + "summary": "default_values", + "code": "input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)\n\n# If axis is not specified, split is applied on default axis 0\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2', 'output_3']\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_default_axis')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2']\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_default_axis')" + }, + { + "summary": "zero_size_splits", + "code": "input = np.array([]).astype(np.float32)\n\n# Split emtpy tensor to tensors of size zero\nsplit = np.array([0, 0, 0]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2', 'output_3']\n)\n\nexpected_outputs = [np.array([]).astype(np.float32), np.array([]).astype(np.float32), np.array([]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_zero_size_splits')" + } + ], + "category": "Tensor" + }, + { + "name": "Split", + "module": "ai.onnx", + "version": 2, + "support_level": "common", + "description": "Split a tensor into a list of tensors, along the specified\n'axis'. Lengths of the parts can be specified using argument 'split'.\nOtherwise, the tensor is split to equal sized parts.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to split on. " + }, + { + "name": "split", + "type": "int64[]", + "required": false, + "description": "length of each output" + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The tensor to split" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "outputs", + "type": "T", + "list": true, + "description": "One or more outputs forming list of tensors after splitting" + } + ], + "min_output": 1, + "max_output": 2147483647, + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "1d", + "code": "input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2', 'output_3'],\n axis=0\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_1d')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2'],\n axis=0,\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_1d')" + }, + { + "summary": "2d", + "code": "input = np.array([[1., 2., 3., 4., 5., 6.],\n [7., 8., 9., 10., 11., 12.]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2'],\n axis=1\n)\n\nexpected_outputs = [np.array([[1., 2., 3.], [7., 8., 9.]]).astype(np.float32),\n np.array([[4., 5., 6.], [10., 11., 12.]]).astype(np.float32)]\n\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_2d')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2'],\n axis=1,\n)\n\nexpected_outputs = [np.array([[1., 2.], [7., 8.]]).astype(np.float32),\n np.array([[3., 4., 5., 6.], [9., 10., 11., 12.]]).astype(np.float32)]\n\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_2d')" + }, + { + "summary": "default_values", + "code": "input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)\n\n# If axis is not specified, split is applied on default axis 0\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2', 'output_3']\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_default_axis')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2']\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_default_axis')" + }, + { + "summary": "zero_size_splits", + "code": "input = np.array([]).astype(np.float32)\n\n# Split emtpy tensor to tensors of size zero\nsplit = np.array([0, 0, 0]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2', 'output_3']\n)\n\nexpected_outputs = [np.array([]).astype(np.float32), np.array([]).astype(np.float32), np.array([]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_zero_size_splits')" + } + ], + "category": "Tensor" + }, + { + "name": "Split", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Split a tensor into a list of tensors, along the specified\n'axis'. Lengths of the parts can be specified using argument 'split'.\nOtherwise, the tensor is split to equal sized parts.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to split on. A negative value means counting dimensions from the back. Accepted range is [-rank, rank-1] where r = rank(input)." + }, + { + "name": "split", + "type": "int64[]", + "required": false, + "description": "length of each output. Values should be >= 0." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The tensor to split" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "outputs", + "type": "T", + "list": true, + "description": "One or more outputs forming list of tensors after splitting" + } + ], + "min_output": 1, + "max_output": 2147483647, + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "1d", + "code": "input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2', 'output_3'],\n axis=0\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_1d')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2'],\n axis=0,\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_1d')" + }, + { + "summary": "2d", + "code": "input = np.array([[1., 2., 3., 4., 5., 6.],\n [7., 8., 9., 10., 11., 12.]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2'],\n axis=1\n)\n\nexpected_outputs = [np.array([[1., 2., 3.], [7., 8., 9.]]).astype(np.float32),\n np.array([[4., 5., 6.], [10., 11., 12.]]).astype(np.float32)]\n\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_2d')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2'],\n axis=1,\n)\n\nexpected_outputs = [np.array([[1., 2.], [7., 8.]]).astype(np.float32),\n np.array([[3., 4., 5., 6.], [9., 10., 11., 12.]]).astype(np.float32)]\n\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_2d')" + }, + { + "summary": "default_values", + "code": "input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)\n\n# If axis is not specified, split is applied on default axis 0\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2', 'output_3']\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_default_axis')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2']\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_default_axis')" + }, + { + "summary": "zero_size_splits", + "code": "input = np.array([]).astype(np.float32)\n\n# Split emtpy tensor to tensors of size zero\nsplit = np.array([0, 0, 0]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2', 'output_3']\n)\n\nexpected_outputs = [np.array([]).astype(np.float32), np.array([]).astype(np.float32), np.array([]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_zero_size_splits')" + } + ], + "category": "Tensor" + }, + { + "name": "Split", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Split a tensor into a list of tensors, along the specified\n'axis'. Lengths of the parts can be specified using input 'split'.\nOtherwise, the tensor is split to equal sized parts.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to split on. A negative value means counting dimensions from the back. Accepted range is [-rank, rank-1] where r = rank(input)." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The tensor to split" + }, + { + "name": "split", + "type": "tensor(int64)", + "option": "optional", + "description": "Optional length of each output. Values should be >= 0.Sum of the values must be equal to the dim value at 'axis' specified." + } + ], + "min_input": 1, + "max_input": 2, + "outputs": [ + { + "name": "outputs", + "type": "T", + "list": true, + "description": "One or more outputs forming list of tensors after splitting" + } + ], + "min_output": 1, + "max_output": 2147483647, + "inputs_range": "1 - 2", + "outputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "1d", + "code": "input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2', 'output_3'],\n axis=0\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_1d')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2'],\n axis=0,\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_1d')" + }, + { + "summary": "2d", + "code": "input = np.array([[1., 2., 3., 4., 5., 6.],\n [7., 8., 9., 10., 11., 12.]]).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2'],\n axis=1\n)\n\nexpected_outputs = [np.array([[1., 2., 3.], [7., 8., 9.]]).astype(np.float32),\n np.array([[4., 5., 6.], [10., 11., 12.]]).astype(np.float32)]\n\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_2d')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2'],\n axis=1,\n)\n\nexpected_outputs = [np.array([[1., 2.], [7., 8.]]).astype(np.float32),\n np.array([[3., 4., 5., 6.], [9., 10., 11., 12.]]).astype(np.float32)]\n\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_2d')" + }, + { + "summary": "default_values", + "code": "input = np.array([1., 2., 3., 4., 5., 6.]).astype(np.float32)\n\n# If axis is not specified, split is applied on default axis 0\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input'],\n outputs=['output_1', 'output_2', 'output_3']\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4.]).astype(np.float32), np.array([5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input], outputs=[y for y in expected_outputs], name='test_split_equal_parts_default_axis')\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2']\n)\n\nexpected_outputs = [np.array([1., 2.]).astype(np.float32), np.array([3., 4., 5., 6.]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_variable_parts_default_axis')" + }, + { + "summary": "zero_size_splits", + "code": "input = np.array([]).astype(np.float32)\n\n# Split emtpy tensor to tensors of size zero\nsplit = np.array([0, 0, 0]).astype(np.int64)\nnode = onnx.helper.make_node(\n 'Split',\n inputs=['input', 'split'],\n outputs=['output_1', 'output_2', 'output_3']\n)\n\nexpected_outputs = [np.array([]).astype(np.float32), np.array([]).astype(np.float32), np.array([]).astype(np.float32)]\nexpect(node, inputs=[input, split], outputs=[y for y in expected_outputs], name='test_split_zero_size_splits')" + } + ], + "category": "Tensor" + }, + { + "name": "SplitToSequence", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Split a tensor into a sequence of tensors, along the specified\n'axis'. Lengths of the parts can be specified using argument 'split'.\n'split' must contain only positive numbers.\n'split' is either a scalar (tensor of empty shape), or a 1-D tensor.\nIf 'split' is a scalar, then 'input' will be split into equally sized chunks(if possible).\nLast chunk will be smaller if the 'input' size along the given axis 'axis' is not divisible\nby 'split'.\nOtherwise, the tensor is split into 'size(split)' chunks, with lengths of the parts on 'axis'\nspecified in 'split'. In this scenario, the sum of entries in 'split' must be equal to the\ndimension size of input tensor on 'axis'.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "Which axis to split on. A negative value means counting dimensions from the back. Accepted range is [-rank, rank-1]." + }, + { + "name": "keepdims", + "type": "int64", + "required": false, + "default": 1, + "description": "Keep the split dimension or not. Default 1, which means we keep split dimension. If input 'split' is specified, this attribute is ignored." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "The tensor to split" + }, + { + "name": "split", + "type": "I", + "option": "optional", + "description": "Length of each output. It can be either a scalar(tensor of empty shape), or a 1-D tensor. All values must be >= 0. " + } + ], + "min_input": 1, + "max_input": 2, + "outputs": [ + { + "name": "output_sequence", + "type": "S", + "description": "One or more outputs forming a sequence of tensors after splitting" + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain split size to integral tensor.", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int32)", + "tensor(int64)" + ] + }, + { + "description": "Constrain output types to all tensor types.", + "type_param_str": "S", + "allowed_type_strs": [ + "seq(tensor(uint8))", + "seq(tensor(uint16))", + "seq(tensor(uint32))", + "seq(tensor(uint64))", + "seq(tensor(int8))", + "seq(tensor(int16))", + "seq(tensor(int32))", + "seq(tensor(int64))", + "seq(tensor(float16))", + "seq(tensor(float))", + "seq(tensor(double))", + "seq(tensor(string))", + "seq(tensor(bool))", + "seq(tensor(complex64))", + "seq(tensor(complex128))" + ] + } + ] + }, + { + "name": "Sqrt", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Square root takes one input data (Tensor) and produces one output data\n(Tensor) where the square root is, y = x^0.5, is applied to\nthe tensor elementwise. If x is negative, then it will return NaN.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sqrt", + "code": "node = onnx.helper.make_node(\n 'Sqrt',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([1, 4, 9]).astype(np.float32)\ny = np.sqrt(x) # expected output [1., 2., 3.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_sqrt_example')\n\nx = np.abs(np.random.randn(3, 4, 5).astype(np.float32))\ny = np.sqrt(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_sqrt')" + } + ] + }, + { + "name": "Sqrt", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Square root takes one input data (Tensor) and produces one output data\n(Tensor) where the square root is, y = x^0.5, is applied to\nthe tensor elementwise. If x is negative, then it will return NaN.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sqrt", + "code": "node = onnx.helper.make_node(\n 'Sqrt',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([1, 4, 9]).astype(np.float32)\ny = np.sqrt(x) # expected output [1., 2., 3.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_sqrt_example')\n\nx = np.abs(np.random.randn(3, 4, 5).astype(np.float32))\ny = np.sqrt(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_sqrt')" + } + ] + }, + { + "name": "Sqrt", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Square root takes one input data (Tensor) and produces one output data\n(Tensor) where the square root is, y = x^0.5, is applied to\nthe tensor elementwise. If x is negative, then it will return NaN.\n", + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "sqrt", + "code": "node = onnx.helper.make_node(\n 'Sqrt',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([1, 4, 9]).astype(np.float32)\ny = np.sqrt(x) # expected output [1., 2., 3.]\nexpect(node, inputs=[x], outputs=[y],\n name='test_sqrt_example')\n\nx = np.abs(np.random.randn(3, 4, 5).astype(np.float32))\ny = np.sqrt(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_sqrt')" + } + ] + }, + { + "name": "Squeeze", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Remove single-dimensional entries from the shape of a tensor.\nTakes a parameter `axes` with a list of axes to squeeze.\nIf `axes` is not provided, all the single dimensions will be removed from\nthe shape. If an axis is selected with shape entry not equal to one, an error is raised.\n", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "List of non-negative integers, indicate the dimensions to squeeze." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensors with at least max(dims) dimensions." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "squeezed", + "type": "T", + "description": "Reshaped tensor with same data as input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "squeeze", + "code": "node = onnx.helper.make_node(\n 'Squeeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\naxes = np.array([0], dtype=np.int64)\ny = np.squeeze(x, axis=0)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_squeeze')" + }, + { + "summary": "squeeze_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Squeeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 1, 5).astype(np.float32)\naxes = np.array([-2], dtype=np.int64)\ny = np.squeeze(x, axis=-2)\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_squeeze_negative_axes')" + } + ], + "category": "Transform" + }, + { + "name": "Squeeze", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Remove single-dimensional entries from the shape of a tensor.\nTakes a parameter `axes` with a list of axes to squeeze.\nIf `axes` is not provided, all the single dimensions will be removed from\nthe shape. If an axis is selected with shape entry not equal to one, an error is raised.\n", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": false, + "description": "List of integers indicating the dimensions to squeeze. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensors with at least max(dims) dimensions." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "squeezed", + "type": "T", + "description": "Reshaped tensor with same data as input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "squeeze", + "code": "node = onnx.helper.make_node(\n 'Squeeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\naxes = np.array([0], dtype=np.int64)\ny = np.squeeze(x, axis=0)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_squeeze')" + }, + { + "summary": "squeeze_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Squeeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 1, 5).astype(np.float32)\naxes = np.array([-2], dtype=np.int64)\ny = np.squeeze(x, axis=-2)\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_squeeze_negative_axes')" + } + ], + "category": "Transform" + }, + { + "name": "Squeeze", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Remove single-dimensional entries from the shape of a tensor.\nTakes an input `axes` with a list of axes to squeeze.\nIf `axes` is not provided, all the single dimensions will be removed from\nthe shape. If an axis is selected with shape entry not equal to one, an error is raised.\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Tensors with at least max(dims) dimensions." + }, + { + "name": "axes", + "type": "tensor(int64)", + "option": "optional", + "description": "List of integers indicating the dimensions to squeeze. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." + } + ], + "min_input": 1, + "max_input": 2, + "outputs": [ + { + "name": "squeezed", + "type": "T", + "description": "Reshaped tensor with same data as input." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "squeeze", + "code": "node = onnx.helper.make_node(\n 'Squeeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\naxes = np.array([0], dtype=np.int64)\ny = np.squeeze(x, axis=0)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_squeeze')" + }, + { + "summary": "squeeze_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Squeeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 1, 5).astype(np.float32)\naxes = np.array([-2], dtype=np.int64)\ny = np.squeeze(x, axis=-2)\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_squeeze_negative_axes')" + } + ], + "category": "Transform" + }, + { + "name": "StringNormalizer", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "StringNormalization performs string operations for basic cleaning.\nThis operator has only one input (denoted by X) and only one output\n(denoted by Y). This operator first examines the elements in the X,\nand removes elements specified in \"stopwords\" attribute.\nAfter removing stop words, the intermediate result can be further lowercased,\nuppercased, or just returned depending the \"case_change_action\" attribute.\nThis operator only accepts [C]- and [1, C]-tensor.\nIf all elements in X are dropped, the output will be the empty value of string tensor with shape [1]\nif input shape is [C] and shape [1, 1] if input shape is [1, C].\n", + "attributes": [ + { + "name": "case_change_action", + "type": "string", + "required": false, + "default": "NONE", + "description": "string enum that cases output to be lowercased/uppercases/unchanged. Valid values are \"LOWER\", \"UPPER\", \"NONE\". Default is \"NONE\"" + }, + { + "name": "is_case_sensitive", + "type": "int64", + "required": false, + "description": "Boolean. Whether the identification of stop words in X is case-sensitive. Default is false" + }, + { + "name": "locale", + "type": "string", + "required": false, + "description": "Environment dependent string that denotes the locale according to which output strings needs to be upper/lowercased.Default en_US or platform specific equivalent as decided by the implementation." + }, + { + "name": "stopwords", + "type": "string[]", + "required": false, + "description": "List of stop words. If not set, no word would be removed from X." + } + ], + "inputs": [ + { + "name": "X", + "type": "tensor(string)", + "description": "UTF-8 strings to normalize" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(string)", + "description": "UTF-8 Normalized strings" + } + ], + "min_output": 1, + "max_output": 1, + "examples": [ + { + "summary": "monday_casesensintive_lower", + "code": "input = np.array([u'monday', u'tuesday', u'wednesday', u'thursday']).astype(object)\noutput = np.array([u'tuesday', u'wednesday', u'thursday']).astype(object)\nstopwords = [u'monday']\n\nnode = onnx.helper.make_node(\n 'StringNormalizer',\n inputs=['x'],\n outputs=['y'],\n case_change_action='LOWER',\n is_case_sensitive=1,\n stopwords=stopwords\n)\nexpect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_casesensintive_lower')" + }, + { + "summary": "monday_casesensintive_nochangecase", + "code": "input = np.array([u'monday', u'tuesday', u'wednesday', u'thursday']).astype(object)\noutput = np.array([u'tuesday', u'wednesday', u'thursday']).astype(object)\nstopwords = [u'monday']\n\nnode = onnx.helper.make_node(\n 'StringNormalizer',\n inputs=['x'],\n outputs=['y'],\n is_case_sensitive=1,\n stopwords=stopwords\n)\nexpect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_casesensintive_nochangecase')" + }, + { + "summary": "monday_casesensintive_upper", + "code": "input = np.array([u'monday', u'tuesday', u'wednesday', u'thursday']).astype(object)\noutput = np.array([u'TUESDAY', u'WEDNESDAY', u'THURSDAY']).astype(object)\nstopwords = [u'monday']\n\nnode = onnx.helper.make_node(\n 'StringNormalizer',\n inputs=['x'],\n outputs=['y'],\n case_change_action='UPPER',\n is_case_sensitive=1,\n stopwords=stopwords\n)\nexpect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_casesensintive_upper')" + }, + { + "summary": "monday_empty_output", + "code": "input = np.array([u'monday', u'monday']).astype(object)\noutput = np.array([u'']).astype(object)\nstopwords = [u'monday']\n\nnode = onnx.helper.make_node(\n 'StringNormalizer',\n inputs=['x'],\n outputs=['y'],\n case_change_action='UPPER',\n is_case_sensitive=1,\n stopwords=stopwords\n)\nexpect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_empty_output')" + }, + { + "summary": "monday_insensintive_upper_twodim", + "code": "input = np.array([u'Monday', u'tuesday', u'wednesday', u'Monday', u'tuesday', u'wednesday']).astype(object).reshape([1, 6])\n\n# It does upper case cecedille, accented E\n# and german umlaut but fails\n# with german eszett\noutput = np.array([u'TUESDAY', u'WEDNESDAY', u'TUESDAY', u'WEDNESDAY']).astype(object).reshape([1, 4])\nstopwords = [u'monday']\n\nnode = onnx.helper.make_node(\n 'StringNormalizer',\n inputs=['x'],\n outputs=['y'],\n case_change_action='UPPER',\n stopwords=stopwords\n)\nexpect(node, inputs=[input], outputs=[output], name='test_strnormalizer_export_monday_insensintive_upper_twodim')" + }, + { + "summary": "nostopwords_nochangecase", + "code": "input = np.array([u'monday', u'tuesday']).astype(object)\noutput = input\n\n# No stopwords. This is a NOOP\nnode = onnx.helper.make_node(\n 'StringNormalizer',\n inputs=['x'],\n outputs=['y'],\n is_case_sensitive=1,\n)\nexpect(node, inputs=[input], outputs=[output], name='test_strnormalizer_nostopwords_nochangecase')" + } + ] + }, + { + "name": "Sub", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Performs element-wise binary subtraction (with limited broadcast support).\n\nIf necessary the right-hand-side argument will be broadcasted to match the\nshape of left-hand-side argument. When broadcasting is specified, the second\ntensor can either be of element size 1 (including a scalar tensor and any\ntensor with rank equal to or smaller than the first tensor), or having its\nshape as a contiguous subset of the first tensor's shape. The starting of the\nmutually equal shape is specified by the argument \"axis\", and if it is not set,\nsuffix matching is assumed. 1-dim expansion doesn't work yet.\n\nFor example, the following tensor shapes are supported (with broadcast=1):\n\n shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (5,)\n shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)\n shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1\n shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0\n\nAttribute `broadcast=1` needs to be passed to enable broadcasting.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions. See doc for details." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Pass 1 to enable broadcasting" + }, + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand, should share the type with the second operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same dimensions and type as A" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sub", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([3, 2, 1]).astype(np.float32)\nz = x - y # expected output [-2., 0., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub')\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_uint8')" + }, + { + "summary": "sub_broadcast", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_bcast')" + } + ] + }, + { + "name": "Sub", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Performs element-wise binary subtraction (with limited broadcast support).\n\nIf necessary the right-hand-side argument will be broadcasted to match the\nshape of left-hand-side argument. When broadcasting is specified, the second\ntensor can either be of element size 1 (including a scalar tensor and any\ntensor with rank equal to or smaller than the first tensor), or having its\nshape as a contiguous subset of the first tensor's shape. The starting of the\nmutually equal shape is specified by the argument \"axis\", and if it is not set,\nsuffix matching is assumed. 1-dim expansion doesn't work yet.\n\nFor example, the following tensor shapes are supported (with broadcast=1):\n\n shape(A) = (2, 3, 4, 5), shape(B) = (,), i.e. B is a scalar tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (1, 1), i.e. B is an 1-element tensor\n shape(A) = (2, 3, 4, 5), shape(B) = (5,)\n shape(A) = (2, 3, 4, 5), shape(B) = (4, 5)\n shape(A) = (2, 3, 4, 5), shape(B) = (3, 4), with axis=1\n shape(A) = (2, 3, 4, 5), shape(B) = (2), with axis=0\n\nAttribute `broadcast=1` needs to be passed to enable broadcasting.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions. See doc for details." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Pass 1 to enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand, should share the type with the second operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand. With broadcasting can be of smaller size than A. If broadcasting is disabled it should be of the same size." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same dimensions and type as A" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sub", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([3, 2, 1]).astype(np.float32)\nz = x - y # expected output [-2., 0., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub')\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_uint8')" + }, + { + "summary": "sub_broadcast", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_bcast')" + } + ] + }, + { + "name": "Sub", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Performs element-wise binary subtraction (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sub", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([3, 2, 1]).astype(np.float32)\nz = x - y # expected output [-2., 0., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub')\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_uint8')" + }, + { + "summary": "sub_broadcast", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_bcast')" + } + ] + }, + { + "name": "Sub", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Performs element-wise binary subtraction (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to high-precision numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint32)", + "tensor(uint64)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "sub", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([3, 2, 1]).astype(np.float32)\nz = x - y # expected output [-2., 0., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub')\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_uint8')" + }, + { + "summary": "sub_broadcast", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_bcast')" + } + ] + }, + { + "name": "Sub", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Performs element-wise binary subtraction (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n\n(Opset 14 change): Extend supported types to include uint8, int8, uint16, and int16.\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First operand." + }, + { + "name": "B", + "type": "T", + "description": "Second operand." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T", + "description": "Result, has same element type as two inputs" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "sub", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.array([1, 2, 3]).astype(np.float32)\ny = np.array([3, 2, 1]).astype(np.float32)\nz = x - y # expected output [-2., 0., 2.]\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(3, 4, 5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub')\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_uint8')" + }, + { + "summary": "sub_broadcast", + "code": "node = onnx.helper.make_node(\n 'Sub',\n inputs=['x', 'y'],\n outputs=['z'],\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.random.randn(5).astype(np.float32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_sub_bcast')" + } + ] + }, + { + "name": "Sum", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Element-wise sum of each of the input tensors. All inputs and outputs must\nhave the same shape and data type.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for Sum." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "sum", + "type": "T", + "description": "Output tensor. Same dimension as inputs." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sum", + "code": "data_0 = np.array([3, 0, 2]).astype(np.float32)\ndata_1 = np.array([1, 3, 4]).astype(np.float32)\ndata_2 = np.array([2, 6, 6]).astype(np.float32)\nresult = np.array([6, 9, 12]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_sum_example')\n\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_sum_one_input')\n\nresult = np.add(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_sum_two_inputs')" + } + ] + }, + { + "name": "Sum", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Element-wise sum of each of the input tensors. All inputs and outputs must\nhave the same shape and data type.\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for Sum." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "sum", + "type": "T", + "description": "Output tensor. Same dimension as inputs." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sum", + "code": "data_0 = np.array([3, 0, 2]).astype(np.float32)\ndata_1 = np.array([1, 3, 4]).astype(np.float32)\ndata_2 = np.array([2, 6, 6]).astype(np.float32)\nresult = np.array([6, 9, 12]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_sum_example')\n\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_sum_one_input')\n\nresult = np.add(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_sum_two_inputs')" + } + ] + }, + { + "name": "Sum", + "module": "ai.onnx", + "version": 8, + "support_level": "common", + "description": "Element-wise sum of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for sum." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "sum", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "sum", + "code": "data_0 = np.array([3, 0, 2]).astype(np.float32)\ndata_1 = np.array([1, 3, 4]).astype(np.float32)\ndata_2 = np.array([2, 6, 6]).astype(np.float32)\nresult = np.array([6, 9, 12]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_sum_example')\n\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_sum_one_input')\n\nresult = np.add(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_sum_two_inputs')" + } + ] + }, + { + "name": "Sum", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Element-wise sum of each of the input tensors (with Numpy-style broadcasting support).\nAll inputs and outputs must have the same data type.\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "data_0", + "type": "T", + "list": true, + "description": "List of tensors for sum." + } + ], + "min_input": 1, + "max_input": 2147483647, + "outputs": [ + { + "name": "sum", + "type": "T", + "description": "Output tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - ∞", + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "sum", + "code": "data_0 = np.array([3, 0, 2]).astype(np.float32)\ndata_1 = np.array([1, 3, 4]).astype(np.float32)\ndata_2 = np.array([2, 6, 6]).astype(np.float32)\nresult = np.array([6, 9, 12]).astype(np.float32)\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0', 'data_1', 'data_2'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1, data_2], outputs=[result],\n name='test_sum_example')\n\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0], outputs=[data_0],\n name='test_sum_one_input')\n\nresult = np.add(data_0, data_1)\nnode = onnx.helper.make_node(\n 'Sum',\n inputs=['data_0', 'data_1'],\n outputs=['result'],\n)\nexpect(node, inputs=[data_0, data_1], outputs=[result],\n name='test_sum_two_inputs')" + } + ] + }, + { + "name": "Tan", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Calculates the tangent of the given input tensor, element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The tangent of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "tan", + "code": "node = onnx.helper.make_node(\n 'Tan',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.tan(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_tan_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.tan(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_tan')" + } + ] + }, + { + "name": "Tanh", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Calculates the hyperbolic tangent of the given input tensor element-wise.\n", + "attributes": [ + { + "name": "consumed_inputs", + "type": "int64[]", + "required": false, + "description": "legacy optimization attribute." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "1-D input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The hyperbolic tangent values of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "tanh", + "code": "node = onnx.helper.make_node(\n 'Tanh',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.tanh(x) # expected output [-0.76159418, 0., 0.76159418]\nexpect(node, inputs=[x], outputs=[y],\n name='test_tanh_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.tanh(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_tanh')" + } + ], + "category": "Activation" + }, + { + "name": "Tanh", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Calculates the hyperbolic tangent of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The hyperbolic tangent values of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "tanh", + "code": "node = onnx.helper.make_node(\n 'Tanh',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.tanh(x) # expected output [-0.76159418, 0., 0.76159418]\nexpect(node, inputs=[x], outputs=[y],\n name='test_tanh_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.tanh(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_tanh')" + } + ], + "category": "Activation" + }, + { + "name": "Tanh", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Calculates the hyperbolic tangent of the given input tensor element-wise.\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "The hyperbolic tangent values of the input tensor computed element-wise" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(bfloat16)" + ] + } + ], + "examples": [ + { + "summary": "tanh", + "code": "node = onnx.helper.make_node(\n 'Tanh',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\ny = np.tanh(x) # expected output [-0.76159418, 0., 0.76159418]\nexpect(node, inputs=[x], outputs=[y],\n name='test_tanh_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.tanh(x)\nexpect(node, inputs=[x], outputs=[y],\n name='test_tanh')" + } + ], + "category": "Activation" + }, + { + "name": "TfIdfVectorizer", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "This transform extracts n-grams from the input sequence and save them as a vector. Input can\nbe either a 1-D or 2-D tensor. For 1-D input, output is the n-gram representation of that input.\nFor 2-D input, the output is also a 2-D tensor whose i-th row is the n-gram representation of the i-th input row.\nMore specifically, if input shape is [C], the corresponding output shape would be [max(ngram_indexes) + 1].\nIf input shape is [N, C], this operator produces a [N, max(ngram_indexes) + 1]-tensor.\n\nIn contrast to standard n-gram extraction, here, the indexes of extracting an n-gram from the original\nsequence are not necessarily consecutive numbers. The discontinuity between indexes are controlled by the number of skips.\nIf the number of skips is 2, we should skip two tokens when scanning through the original sequence.\nLet's consider an example. Assume that input sequence is [94, 17, 36, 12, 28] and the number of skips is 2.\nThe associated 2-grams are [94, 12] and [17, 28] respectively indexed by [0, 3] and [1, 4].\nIf the number of skips becomes 0, the 2-grams generated are [94, 17], [17, 36], [36, 12], [12, 28]\nindexed by [0, 1], [1, 2], [2, 3], [3, 4], respectively.\n\nThe output vector (denoted by Y) stores the count of each n-gram;\nY[ngram_indexes[i]] indicates the times that the i-th n-gram is found. The attribute ngram_indexes is used to determine the mapping\nbetween index i and the corresponding n-gram's output coordinate. If pool_int64s is [94, 17, 17, 36], ngram_indexes is [1, 0],\nngram_counts=[0, 0], then the Y[0] (first element in Y) and Y[1] (second element in Y) are the counts of [17, 36] and [94, 17],\nrespectively. An n-gram which cannot be found in pool_strings/pool_int64s should be ignored and has no effect on the output.\nNote that we may consider all skips up to S when generating the n-grams.\n\nThe examples used above are true if mode is \"TF\". If mode is \"IDF\", all the counts larger than 1 would be truncated to 1 and\nthe i-th element in weights would be used to scale (by multiplication) the count of the i-th n-gram in pool. If mode is \"TFIDF\",\nthis operator first computes the counts of all n-grams and then scale them by the associated values in the weights attribute.\n\nOnly one of pool_strings and pool_int64s can be set. If pool_int64s is set, the input should be an integer tensor.\nIf pool_strings is set, the input must be a string tensor.\n", + "attributes": [ + { + "name": "max_gram_length", + "type": "int64", + "required": true, + "description": "Maximum n-gram length. If this value is 3, 3-grams will be used to generate the output." + }, + { + "name": "max_skip_count", + "type": "int64", + "required": true, + "description": "Maximum number of items (integers/strings) to be skipped when constructing an n-gram from X. If max_skip_count=1, min_gram_length=2, max_gram_length=3, this operator may generate 2-grams with skip_count=0 and skip_count=1, and 3-grams with skip_count=0 and skip_count=1" + }, + { + "name": "min_gram_length", + "type": "int64", + "required": true, + "description": "Minimum n-gram length. If this value is 2 and max_gram_length is 3, output may contain counts of 2-grams and 3-grams." + }, + { + "name": "mode", + "type": "string", + "required": true, + "description": "The weighting criteria. It can be one of \"TF\" (term frequency), \"IDF\" (inverse document frequency), and \"TFIDF\" (the combination of TF and IDF)" + }, + { + "name": "ngram_counts", + "type": "int64[]", + "required": true, + "description": "The starting indexes of 1-grams, 2-grams, and so on in pool. It is useful when determining the boundary between two consecutive collections of n-grams. For example, if ngram_counts is [0, 17, 36], the first index (zero-based) of 1-gram/2-gram/3-gram in pool are 0/17/36. This format is essentially identical to CSR (or CSC) sparse matrix format, and we choose to use this due to its popularity." + }, + { + "name": "ngram_indexes", + "type": "int64[]", + "required": true, + "description": "list of int64s (type: AttributeProto::INTS). This list is parallel to the specified 'pool_*' attribute. The i-th element in ngram_indexes indicate the coordinate of the i-th n-gram in the output tensor." + }, + { + "name": "pool_int64s", + "type": "int64[]", + "required": false, + "description": "List of int64 n-grams learned from the training set. Either this or pool_strings attributes must be present but not both. It's an 1-D tensor starting with the collections of all 1-grams and ending with the collections of n-grams. The i-th element in pool stores the n-gram that should be mapped to coordinate ngram_indexes[i] in the output vector." + }, + { + "name": "pool_strings", + "type": "string[]", + "required": false, + "description": "List of strings n-grams learned from the training set. Either this or pool_int64s attributes must be present but not both. It's an 1-D tensor starting with the collections of all 1-grams and ending with the collections of n-grams. The i-th element in pool stores the n-gram that should be mapped to coordinate ngram_indexes[i] in the output vector." + }, + { + "name": "weights", + "type": "float32[]", + "required": false, + "description": "list of floats. This attribute stores the weight of each n-gram in pool. The i-th element in weights is the weight of the i-th n-gram in pool. Its length equals to the size of ngram_indexes. By default, weights is an all-one tensor.This attribute is used when mode is \"IDF\" or \"TFIDF\" to scale the associated word counts." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input for n-gram extraction" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T1", + "description": "Ngram results" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Input is ether string UTF-8 or int32/int64", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int32)", + "tensor(int64)" + ] + }, + { + "description": "1-D tensor of floats", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)" + ] + } + ], + "examples": [ + { + "summary": "tf_batch_onlybigrams_skip0", + "code": "input = np.array([[1, 1, 3, 3, 3, 7], [8, 6, 7, 5, 6, 8]]).astype(np.int32)\noutput = np.array([[0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 1.]]).astype(np.float32)\n\nngram_counts = np.array([0, 4]).astype(np.int64)\nngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)\npool_int64s = np.array([2, 3, 5, 4, # unigrams\n 5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams\n\nhelper = TfIdfVectorizerHelper(\n mode='TF',\n min_gram_length=2,\n max_gram_length=2,\n max_skip_count=0,\n ngram_counts=ngram_counts,\n ngram_indexes=ngram_indexes,\n pool_int64s=pool_int64s\n)\nnode = helper.make_node_noweights()\nexpect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_batch_onlybigrams_skip0')" + }, + { + "summary": "tf_batch_onlybigrams_skip5", + "code": "input = np.array([[1, 1, 3, 3, 3, 7], [8, 6, 7, 5, 6, 8]]).astype(np.int32)\noutput = np.array([[0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 1., 1.]]).astype(np.float32)\n\nngram_counts = np.array([0, 4]).astype(np.int64)\nngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)\npool_int64s = np.array([2, 3, 5, 4, # unigrams\n 5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams\n\nhelper = TfIdfVectorizerHelper(\n mode='TF',\n min_gram_length=2,\n max_gram_length=2,\n max_skip_count=5,\n ngram_counts=ngram_counts,\n ngram_indexes=ngram_indexes,\n pool_int64s=pool_int64s\n)\nnode = helper.make_node_noweights()\nexpect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_batch_onlybigrams_skip5')" + }, + { + "summary": "tf_batch_uniandbigrams_skip5", + "code": "input = np.array([[1, 1, 3, 3, 3, 7], [8, 6, 7, 5, 6, 8]]).astype(np.int32)\noutput = np.array([[0., 3., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 1., 1., 1.]]).astype(np.float32)\n\nngram_counts = np.array([0, 4]).astype(np.int64)\nngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)\npool_int64s = np.array([2, 3, 5, 4, # unigrams\n 5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams\n\nhelper = TfIdfVectorizerHelper(\n mode='TF',\n min_gram_length=1,\n max_gram_length=2,\n max_skip_count=5,\n ngram_counts=ngram_counts,\n ngram_indexes=ngram_indexes,\n pool_int64s=pool_int64s\n)\nnode = helper.make_node_noweights()\nexpect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_batch_uniandbigrams_skip5')" + }, + { + "summary": "tf_only_bigrams_skip0", + "code": "input = np.array([1, 1, 3, 3, 3, 7, 8, 6, 7, 5, 6, 8]).astype(np.int32)\noutput = np.array([0., 0., 0., 0., 1., 1., 1.]).astype(np.float32)\n\nngram_counts = np.array([0, 4]).astype(np.int64)\nngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)\npool_int64s = np.array([2, 3, 5, 4, # unigrams\n 5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams\n\nhelper = TfIdfVectorizerHelper(\n mode='TF',\n min_gram_length=2,\n max_gram_length=2,\n max_skip_count=0,\n ngram_counts=ngram_counts,\n ngram_indexes=ngram_indexes,\n pool_int64s=pool_int64s\n)\nnode = helper.make_node_noweights()\nexpect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_only_bigrams_skip0')" + }, + { + "summary": "tf_onlybigrams_levelempty", + "code": "input = np.array([1, 1, 3, 3, 3, 7, 8, 6, 7, 5, 6, 8]).astype(np.int32)\noutput = np.array([1., 1., 1.]).astype(np.float32)\n\nngram_counts = np.array([0, 0]).astype(np.int64)\nngram_indexes = np.array([0, 1, 2]).astype(np.int64)\npool_int64s = np.array([ # unigrams none\n 5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams\n\nhelper = TfIdfVectorizerHelper(\n mode='TF',\n min_gram_length=2,\n max_gram_length=2,\n max_skip_count=0,\n ngram_counts=ngram_counts,\n ngram_indexes=ngram_indexes,\n pool_int64s=pool_int64s\n)\nnode = helper.make_node_noweights()\nexpect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_onlybigrams_levelempty')" + }, + { + "summary": "tf_onlybigrams_skip5", + "code": "input = np.array([1, 1, 3, 3, 3, 7, 8, 6, 7, 5, 6, 8]).astype(np.int32)\noutput = np.array([0., 0., 0., 0., 1., 3., 1.]).astype(np.float32)\n\nngram_counts = np.array([0, 4]).astype(np.int64)\nngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)\npool_int64s = np.array([2, 3, 5, 4, # unigrams\n 5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams\n\nhelper = TfIdfVectorizerHelper(\n mode='TF',\n min_gram_length=2,\n max_gram_length=2,\n max_skip_count=5,\n ngram_counts=ngram_counts,\n ngram_indexes=ngram_indexes,\n pool_int64s=pool_int64s\n)\nnode = helper.make_node_noweights()\nexpect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_onlybigrams_skip5')" + }, + { + "summary": "tf_uniandbigrams_skip5", + "code": "input = np.array([1, 1, 3, 3, 3, 7, 8, 6, 7, 5, 6, 8]).astype(np.int32)\noutput = np.array([0., 3., 1., 0., 1., 3., 1.]).astype(np.float32)\n\nngram_counts = np.array([0, 4]).astype(np.int64)\nngram_indexes = np.array([0, 1, 2, 3, 4, 5, 6]).astype(np.int64)\npool_int64s = np.array([2, 3, 5, 4, # unigrams\n 5, 6, 7, 8, 6, 7]).astype(np.int64) # bigrams\n\nhelper = TfIdfVectorizerHelper(\n mode='TF',\n min_gram_length=1,\n max_gram_length=2,\n max_skip_count=5,\n ngram_counts=ngram_counts,\n ngram_indexes=ngram_indexes,\n pool_int64s=pool_int64s\n)\nnode = helper.make_node_noweights()\nexpect(node, inputs=[input], outputs=[output], name='test_tfidfvectorizer_tf_uniandbigrams_skip5')" + } + ] + }, + { + "name": "ThresholdedRelu", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "ThresholdedRelu takes one input data (Tensor) and produces one output data\n(Tensor) where the rectified linear function, y = x for x > alpha, y = 0 otherwise,\nis applied to the tensor elementwise.\n", + "attributes": [ + { + "name": "alpha", + "type": "float32", + "required": false, + "default": 1.0, + "description": "Threshold value" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "Output tensor" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "default", + "code": "default_alpha = 1.0\nnode = onnx.helper.make_node(\n 'ThresholdedRelu',\n inputs=['x'],\n outputs=['y']\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, default_alpha, np.inf)\ny[y == default_alpha] = 0\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_thresholdedrelu_default')" + }, + { + "summary": "thresholdedrelu", + "code": "alpha = 2.0\nnode = onnx.helper.make_node(\n 'ThresholdedRelu',\n inputs=['x'],\n outputs=['y'],\n alpha=alpha\n)\n\nx = np.array([-1.5, 0., 1.2, 2.0, 2.2]).astype(np.float32)\ny = np.clip(x, alpha, np.inf) # expected output [0., 0., 0., 0., 2.2]\ny[y == alpha] = 0\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_thresholdedrelu_example')\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = np.clip(x, alpha, np.inf)\ny[y == alpha] = 0\n\nexpect(node, inputs=[x], outputs=[y],\n name='test_thresholdedrelu')" + } + ], + "category": "Activation" + }, + { + "name": "Tile", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Repeat the elements of a tensor along an axis.", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of any shape." + }, + { + "name": "tiles", + "type": "T", + "description": "Number of repeated copies to make of the input tensor." + }, + { + "name": "axis", + "type": "T", + "description": "Axis along which to repeat." + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of same shape and type as input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain tiles and axis's type to int64 tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "tile", + "code": "node = onnx.helper.make_node(\n 'Tile',\n inputs=['x', 'y'],\n outputs=['z']\n)\n\nx = np.random.rand(2, 3, 4, 5).astype(np.float32)\n\nrepeats = np.random.randint(low=1, high=10, size=(np.ndim(x),)).astype(np.int64)\n\nz = np.tile(x, repeats)\n\nexpect(node,\n inputs=[x, repeats],\n outputs=[z],\n name='test_tile')" + }, + { + "summary": "tile_precomputed", + "code": "node = onnx.helper.make_node(\n 'Tile',\n inputs=['x', 'y'],\n outputs=['z']\n)\n\nx = np.array([\n [0, 1],\n [2, 3]\n], dtype=np.float32)\n\nrepeats = np.array([2, 2], dtype=np.int64)\n\nz = np.array([\n [0, 1, 0, 1],\n [2, 3, 2, 3],\n [0, 1, 0, 1],\n [2, 3, 2, 3]\n], dtype=np.float32)\n\nexpect(node,\n inputs=[x, repeats],\n outputs=[z],\n name='test_tile_precomputed')" + } + ], + "category": "Shape" + }, + { + "name": "Tile", + "module": "ai.onnx", + "version": 6, + "support_level": "common", + "description": "Constructs a tensor by tiling a given tensor.\nThis is the same as function `tile` in Numpy, but no broadcast.\nFor example A = [[1, 2], [3, 4]], B = [1, 2], tile(A, B) = [[1, 2, 1, 2], [3, 4, 3, 4]]\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of any shape." + }, + { + "name": "repeats", + "type": "T1", + "description": "1D int64 tensor of the same length as input's dimension number, includes numbers of repeated copies along input's dimensions." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of the same dimensions and type as tensor input. output_dim[i] = input_dim[i] * repeats[i]" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain repeat's type to int64 tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "tile", + "code": "node = onnx.helper.make_node(\n 'Tile',\n inputs=['x', 'y'],\n outputs=['z']\n)\n\nx = np.random.rand(2, 3, 4, 5).astype(np.float32)\n\nrepeats = np.random.randint(low=1, high=10, size=(np.ndim(x),)).astype(np.int64)\n\nz = np.tile(x, repeats)\n\nexpect(node,\n inputs=[x, repeats],\n outputs=[z],\n name='test_tile')" + }, + { + "summary": "tile_precomputed", + "code": "node = onnx.helper.make_node(\n 'Tile',\n inputs=['x', 'y'],\n outputs=['z']\n)\n\nx = np.array([\n [0, 1],\n [2, 3]\n], dtype=np.float32)\n\nrepeats = np.array([2, 2], dtype=np.int64)\n\nz = np.array([\n [0, 1, 0, 1],\n [2, 3, 2, 3],\n [0, 1, 0, 1],\n [2, 3, 2, 3]\n], dtype=np.float32)\n\nexpect(node,\n inputs=[x, repeats],\n outputs=[z],\n name='test_tile_precomputed')" + } + ], + "category": "Shape" + }, + { + "name": "Tile", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Constructs a tensor by tiling a given tensor.\nThis is the same as function `tile` in Numpy, but no broadcast.\nFor example A = [[1, 2], [3, 4]], B = [1, 2], tile(A, B) = [[1, 2, 1, 2], [3, 4, 3, 4]]\n", + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of any shape." + }, + { + "name": "repeats", + "type": "T1", + "description": "1D int64 tensor of the same length as input's dimension number, includes numbers of repeated copies along input's dimensions." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of the same dimensions and type as tensor input. output_dim[i] = input_dim[i] * repeats[i]" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + }, + { + "description": "Constrain repeat's type to int64 tensors.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "tile", + "code": "node = onnx.helper.make_node(\n 'Tile',\n inputs=['x', 'y'],\n outputs=['z']\n)\n\nx = np.random.rand(2, 3, 4, 5).astype(np.float32)\n\nrepeats = np.random.randint(low=1, high=10, size=(np.ndim(x),)).astype(np.int64)\n\nz = np.tile(x, repeats)\n\nexpect(node,\n inputs=[x, repeats],\n outputs=[z],\n name='test_tile')" + }, + { + "summary": "tile_precomputed", + "code": "node = onnx.helper.make_node(\n 'Tile',\n inputs=['x', 'y'],\n outputs=['z']\n)\n\nx = np.array([\n [0, 1],\n [2, 3]\n], dtype=np.float32)\n\nrepeats = np.array([2, 2], dtype=np.int64)\n\nz = np.array([\n [0, 1, 0, 1],\n [2, 3, 2, 3],\n [0, 1, 0, 1],\n [2, 3, 2, 3]\n], dtype=np.float32)\n\nexpect(node,\n inputs=[x, repeats],\n outputs=[z],\n name='test_tile_precomputed')" + } + ], + "category": "Shape" + }, + { + "name": "TopK", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Retrieve the top-K elements along a specified axis. Given an input tensor of\nshape [a_1, a_2, ..., a_n, r] and integer argument k, return two outputs:\n -Value tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n]\n which contains the values of the top k elements along the specified axis\n -Index tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n] which\n contains the indices of the top k elements (original indices from the input\n tensor).\nGiven two equivalent values, this operator uses the indices along the axis as\n a tiebreaker. That is, the element with the lower index will appear first.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": -1, + "description": "Dimension on which to do the sort." + }, + { + "name": "k", + "type": "int64", + "required": true, + "description": "Number of top elements to retrieve" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Tensor of shape [a_1, a_2, ..., a_n, r]" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Values", + "type": "T", + "description": "Tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n] containing top K values from the input tensor" + }, + { + "name": "Indices", + "type": "I", + "description": "Tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n] containing the corresponding input tensor indices for the top K values." + } + ], + "min_output": 2, + "max_output": 2, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain index tensor to int64", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "top_k", + "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n 'TopK',\n inputs=['x', 'k'],\n outputs=['values', 'indices'],\n axis=axis\n)\nX = np.array([\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n], dtype=np.float32)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n#print(values_ref)\n#[[ 3. 2. 1.]\n# [ 7. 6. 5.]\n# [11. 10. 9.]]\n#print(indices_ref)\n#[[3 2 1]\n# [3 2 1]\n# [3 2 1]]\n\nexpect(node, inputs=[X, K], outputs=[values_ref, indices_ref],\n name='test_top_k')" + }, + { + "summary": "top_k_negative_axis", + "code": "axis = -1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n 'TopK',\n inputs=['x', 'k'],\n outputs=['values', 'indices'],\n axis=axis\n)\nX = np.array([\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n], dtype=np.float32)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n# print(values_ref)\n#[[ 3. 2. 1.]\n# [ 7. 6. 5.]\n# [11. 10. 9.]]\n# print(indices_ref)\n#[[3 2 1]\n# [3 2 1]\n# [3 2 1]]\n\nexpect(node, inputs=[X, K], outputs=[values_ref, indices_ref],\n name='test_top_k_negative_axis')" + }, + { + "summary": "top_k_smallest", + "code": "axis = 1\nlargest = 0\nsorted = 1\nk = 3\n\nnode = onnx.helper.make_node(\n 'TopK',\n inputs=['x', 'k'],\n outputs=['values', 'indices'],\n axis=axis,\n largest=largest,\n sorted=sorted\n)\n\nX = np.array([\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [11, 10, 9, 8],\n], dtype=np.float32)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n#print(values_ref)\n#[[ 0. 1. 2.]\n# [ 4. 5. 6.]\n# [ 8. 9. 10.]]\n#print(indices_ref)\n#[[0 1 2]\n# [0 1 2]\n# [3 2 1]]\n\nexpect(node, inputs=[X, K], outputs=[values_ref, indices_ref],\n name='test_top_k_smallest')" + } + ] + }, + { + "name": "TopK", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Retrieve the top-K elements along a specified axis. Given an input tensor of\nshape [a_1, a_2, ..., a_n, r] and integer argument k, return two outputs:\n -Value tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n]\n which contains the values of the top k elements along the specified axis\n -Index tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n] which\n contains the indices of the top k elements (original indices from the input\n tensor).\n\nGiven two equivalent values, this operator uses the indices along the axis as\n a tiebreaker. That is, the element with the lower index will appear first.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": -1, + "description": "Dimension on which to do the sort." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Tensor of shape [a_1, a_2, ..., a_n, r]" + }, + { + "name": "K", + "type": "tensor(int64)", + "description": "A 1-D tensor containing a single positive value corresponding to the number of top elements to retrieve" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Values", + "type": "T", + "description": "Tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n] containing top K values from the input tensor" + }, + { + "name": "Indices", + "type": "I", + "description": "Tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n] containing the corresponding input tensor indices for the top K values." + } + ], + "min_output": 2, + "max_output": 2, + "type_constraints": [ + { + "description": "Constrain input and output types to float tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain index tensor to int64", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "top_k", + "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n 'TopK',\n inputs=['x', 'k'],\n outputs=['values', 'indices'],\n axis=axis\n)\nX = np.array([\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n], dtype=np.float32)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n#print(values_ref)\n#[[ 3. 2. 1.]\n# [ 7. 6. 5.]\n# [11. 10. 9.]]\n#print(indices_ref)\n#[[3 2 1]\n# [3 2 1]\n# [3 2 1]]\n\nexpect(node, inputs=[X, K], outputs=[values_ref, indices_ref],\n name='test_top_k')" + }, + { + "summary": "top_k_negative_axis", + "code": "axis = -1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n 'TopK',\n inputs=['x', 'k'],\n outputs=['values', 'indices'],\n axis=axis\n)\nX = np.array([\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n], dtype=np.float32)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n# print(values_ref)\n#[[ 3. 2. 1.]\n# [ 7. 6. 5.]\n# [11. 10. 9.]]\n# print(indices_ref)\n#[[3 2 1]\n# [3 2 1]\n# [3 2 1]]\n\nexpect(node, inputs=[X, K], outputs=[values_ref, indices_ref],\n name='test_top_k_negative_axis')" + }, + { + "summary": "top_k_smallest", + "code": "axis = 1\nlargest = 0\nsorted = 1\nk = 3\n\nnode = onnx.helper.make_node(\n 'TopK',\n inputs=['x', 'k'],\n outputs=['values', 'indices'],\n axis=axis,\n largest=largest,\n sorted=sorted\n)\n\nX = np.array([\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [11, 10, 9, 8],\n], dtype=np.float32)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n#print(values_ref)\n#[[ 0. 1. 2.]\n# [ 4. 5. 6.]\n# [ 8. 9. 10.]]\n#print(indices_ref)\n#[[0 1 2]\n# [0 1 2]\n# [3 2 1]]\n\nexpect(node, inputs=[X, K], outputs=[values_ref, indices_ref],\n name='test_top_k_smallest')" + } + ] + }, + { + "name": "TopK", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Retrieve the top-K largest or smallest elements along a specified axis. Given an input tensor of\nshape [a_1, a_2, ..., a_n, r] and integer argument k, return two outputs:\n -Value tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n]\n which contains the values of the top k elements along the specified axis\n -Index tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n] which\n contains the indices of the top k elements (original indices from the input\n tensor).\n\nIf \"largest\" is 1 (the default value) then the k largest elements are returned.\nIf \"sorted\" is 1 (the default value) then the resulting k elements will be sorted.\nIf \"sorted\" is 0, order of returned 'Values' and 'Indices' are undefined.\n\nGiven two equivalent values, this operator uses the indices along the axis as\n a tiebreaker. That is, the element with the lower index will appear first.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "default": -1, + "description": "Dimension on which to do the sort. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + }, + { + "name": "largest", + "type": "int64", + "required": false, + "default": 1, + "description": "Whether to return the top-K largest or smallest elements." + }, + { + "name": "sorted", + "type": "int64", + "required": false, + "default": 1, + "description": "Whether to return the elements in sorted order." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Tensor of shape [a_1, a_2, ..., a_n, r]" + }, + { + "name": "K", + "type": "tensor(int64)", + "description": "A 1-D tensor containing a single positive value corresponding to the number of top elements to retrieve" + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Values", + "type": "T", + "description": "Tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n] containing top K values from the input tensor" + }, + { + "name": "Indices", + "type": "I", + "description": "Tensor of shape [a_1, a_2, ..., a_{axis-1}, k, a_{axis+1}, ... a_n] containing the corresponding input tensor indices for the top K values." + } + ], + "min_output": 2, + "max_output": 2, + "type_constraints": [ + { + "description": "Constrain input and output types to numeric tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + }, + { + "description": "Constrain index tensor to int64", + "type_param_str": "I", + "allowed_type_strs": [ + "tensor(int64)" + ] + } + ], + "examples": [ + { + "summary": "top_k", + "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n 'TopK',\n inputs=['x', 'k'],\n outputs=['values', 'indices'],\n axis=axis\n)\nX = np.array([\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n], dtype=np.float32)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n#print(values_ref)\n#[[ 3. 2. 1.]\n# [ 7. 6. 5.]\n# [11. 10. 9.]]\n#print(indices_ref)\n#[[3 2 1]\n# [3 2 1]\n# [3 2 1]]\n\nexpect(node, inputs=[X, K], outputs=[values_ref, indices_ref],\n name='test_top_k')" + }, + { + "summary": "top_k_negative_axis", + "code": "axis = -1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n 'TopK',\n inputs=['x', 'k'],\n outputs=['values', 'indices'],\n axis=axis\n)\nX = np.array([\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n], dtype=np.float32)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n# print(values_ref)\n#[[ 3. 2. 1.]\n# [ 7. 6. 5.]\n# [11. 10. 9.]]\n# print(indices_ref)\n#[[3 2 1]\n# [3 2 1]\n# [3 2 1]]\n\nexpect(node, inputs=[X, K], outputs=[values_ref, indices_ref],\n name='test_top_k_negative_axis')" + }, + { + "summary": "top_k_smallest", + "code": "axis = 1\nlargest = 0\nsorted = 1\nk = 3\n\nnode = onnx.helper.make_node(\n 'TopK',\n inputs=['x', 'k'],\n outputs=['values', 'indices'],\n axis=axis,\n largest=largest,\n sorted=sorted\n)\n\nX = np.array([\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [11, 10, 9, 8],\n], dtype=np.float32)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n#print(values_ref)\n#[[ 0. 1. 2.]\n# [ 4. 5. 6.]\n# [ 8. 9. 10.]]\n#print(indices_ref)\n#[[0 1 2]\n# [0 1 2]\n# [3 2 1]]\n\nexpect(node, inputs=[X, K], outputs=[values_ref, indices_ref],\n name='test_top_k_smallest')" + } + ] + }, + { + "name": "Transpose", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Transpose the input tensor similar to numpy.transpose. For example, when\nperm=(1, 0, 2), given an input tensor of shape (1, 2, 3), the output shape\nwill be (2, 1, 3).\n", + "attributes": [ + { + "name": "perm", + "type": "int64[]", + "required": false, + "description": "A list of integers. By default, reverse the dimensions, otherwise permute the axes according to the values given." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "transposed", + "type": "T", + "description": "Transposed output." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "all_permutations", + "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\npermutations = list(itertools.permutations(np.arange(len(shape))))\n\nfor i in range(len(permutations)):\n node = onnx.helper.make_node(\n 'Transpose',\n inputs=['data'],\n outputs=['transposed'],\n perm=permutations[i]\n )\n transposed = np.transpose(data, permutations[i])\n expect(node, inputs=[data], outputs=[transposed],\n name='test_transpose_all_permutations_' + str(i))" + }, + { + "summary": "default", + "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Transpose',\n inputs=['data'],\n outputs=['transposed']\n)\n\ntransposed = np.transpose(data)\nexpect(node, inputs=[data], outputs=[transposed],\n name='test_transpose_default')" + } + ], + "category": "Transform" + }, + { + "name": "Transpose", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Transpose the input tensor similar to numpy.transpose. For example, when\nperm=(1, 0, 2), given an input tensor of shape (1, 2, 3), the output shape\nwill be (2, 1, 3).\n", + "attributes": [ + { + "name": "perm", + "type": "int64[]", + "required": false, + "description": "A list of integers. By default, reverse the dimensions, otherwise permute the axes according to the values given." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "An input tensor." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "transposed", + "type": "T", + "description": "Transposed output." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "all_permutations", + "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\npermutations = list(itertools.permutations(np.arange(len(shape))))\n\nfor i in range(len(permutations)):\n node = onnx.helper.make_node(\n 'Transpose',\n inputs=['data'],\n outputs=['transposed'],\n perm=permutations[i]\n )\n transposed = np.transpose(data, permutations[i])\n expect(node, inputs=[data], outputs=[transposed],\n name='test_transpose_all_permutations_' + str(i))" + }, + { + "summary": "default", + "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\n\nnode = onnx.helper.make_node(\n 'Transpose',\n inputs=['data'],\n outputs=['transposed']\n)\n\ntransposed = np.transpose(data)\nexpect(node, inputs=[data], outputs=[transposed],\n name='test_transpose_default')" + } + ], + "category": "Transform" + }, + { + "name": "TreeEnsembleClassifier", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Tree Ensemble classifier. Returns the top class for each of N inputs.
\n The attributes named 'nodes_X' form a sequence of tuples, associated by\n index into the sequences, which must all be of equal length. These tuples\n define the nodes.
\n Similarly, all fields prefixed with 'class_' are tuples of votes at the leaves.\n A leaf may have multiple votes, where each vote is weighted by\n the associated class_weights index.
\n One and only one of classlabels_strings or classlabels_int64s\n will be defined. The class_ids are indices into this list.\n", + "attributes": [ + { + "name": "base_values", + "type": "float32[]", + "required": false, + "description": "Base values for classification, added to final class score; the size must be the same as the classes or can be left unassigned (assumed 0)" + }, + { + "name": "class_ids", + "type": "int64[]", + "required": false, + "description": "The index of the class list that each weight is for." + }, + { + "name": "class_nodeids", + "type": "int64[]", + "required": false, + "description": "node id that this weight is for." + }, + { + "name": "class_treeids", + "type": "int64[]", + "required": false, + "description": "The id of the tree that this node is in." + }, + { + "name": "class_weights", + "type": "float32[]", + "required": false, + "description": "The weight for the class in class_id." + }, + { + "name": "classlabels_int64s", + "type": "int64[]", + "required": false, + "description": "Class labels if using integer labels.
One and only one of the 'classlabels_*' attributes must be defined." + }, + { + "name": "classlabels_strings", + "type": "string[]", + "required": false, + "description": "Class labels if using string labels.
One and only one of the 'classlabels_*' attributes must be defined." + }, + { + "name": "nodes_falsenodeids", + "type": "int64[]", + "required": false, + "description": "Child node if expression is false." + }, + { + "name": "nodes_featureids", + "type": "int64[]", + "required": false, + "description": "Feature id for each node." + }, + { + "name": "nodes_hitrates", + "type": "float32[]", + "required": false, + "description": "Popularity of each node, used for performance and may be omitted." + }, + { + "name": "nodes_missing_value_tracks_true", + "type": "int64[]", + "required": false, + "description": "For each node, define what to do in the presence of a missing value: if a value is missing (NaN), use the 'true' or 'false' branch based on the value in this array.
This attribute may be left undefined, and the defalt value is false (0) for all nodes." + }, + { + "name": "nodes_modes", + "type": "string[]", + "required": false, + "description": "The node kind, that is, the comparison to make at the node. There is no comparison to make at a leaf node.
One of 'BRANCH_LEQ', 'BRANCH_LT', 'BRANCH_GTE', 'BRANCH_GT', 'BRANCH_EQ', 'BRANCH_NEQ', 'LEAF'" + }, + { + "name": "nodes_nodeids", + "type": "int64[]", + "required": false, + "description": "Node id for each node. Ids may restart at zero for each tree, but it not required to." + }, + { + "name": "nodes_treeids", + "type": "int64[]", + "required": false, + "description": "Tree id for each node." + }, + { + "name": "nodes_truenodeids", + "type": "int64[]", + "required": false, + "description": "Child node if expression is true." + }, + { + "name": "nodes_values", + "type": "float32[]", + "required": false, + "description": "Thresholds to do the splitting on for each node." + }, + { + "name": "post_transform", + "type": "string", + "required": false, + "default": "NONE", + "description": "Indicates the transform to apply to the score.
One of 'NONE,' 'SOFTMAX,' 'LOGISTIC,' 'SOFTMAX_ZERO,' or 'PROBIT.'" + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Input of shape [N,F]" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "N, Top class for each point" + }, + { + "name": "Z", + "type": "tensor(float)", + "description": "The class score for each class, for each point, a tensor of shape [N,E]." + } + ], + "min_output": 2, + "max_output": 2, + "type_constraints": [ + { + "description": "The input type must be a tensor of a numeric type.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + }, + { + "description": "The output type will be a tensor of strings or integers, depending on which of the the classlabels_* attributes is used.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "TreeEnsembleClassifier", + "module": "ai.onnx.ml", + "version": 3, + "support_level": "common", + "description": "Tree Ensemble classifier. Returns the top class for each of N inputs.
\n The attributes named 'nodes_X' form a sequence of tuples, associated by\n index into the sequences, which must all be of equal length. These tuples\n define the nodes.
\n Similarly, all fields prefixed with 'class_' are tuples of votes at the leaves.\n A leaf may have multiple votes, where each vote is weighted by\n the associated class_weights index.
\n One and only one of classlabels_strings or classlabels_int64s\n will be defined. The class_ids are indices into this list.\n All fields ending with _as_tensor can be used instead of the\n same parameter without the suffix if the element type is double and not float.\n", + "attributes": [ + { + "name": "base_values", + "type": "float32[]", + "required": false, + "description": "Base values for classification, added to final class score; the size must be the same as the classes or can be left unassigned (assumed 0)" + }, + { + "name": "base_values_as_tensor", + "type": "tensor", + "required": false, + "description": "Base values for classification, added to final class score; the size must be the same as the classes or can be left unassigned (assumed 0)" + }, + { + "name": "class_ids", + "type": "int64[]", + "required": false, + "description": "The index of the class list that each weight is for." + }, + { + "name": "class_nodeids", + "type": "int64[]", + "required": false, + "description": "node id that this weight is for." + }, + { + "name": "class_treeids", + "type": "int64[]", + "required": false, + "description": "The id of the tree that this node is in." + }, + { + "name": "class_weights", + "type": "float32[]", + "required": false, + "description": "The weight for the class in class_id." + }, + { + "name": "class_weights_as_tensor", + "type": "tensor", + "required": false, + "description": "The weight for the class in class_id." + }, + { + "name": "classlabels_int64s", + "type": "int64[]", + "required": false, + "description": "Class labels if using integer labels.
One and only one of the 'classlabels_*' attributes must be defined." + }, + { + "name": "classlabels_strings", + "type": "string[]", + "required": false, + "description": "Class labels if using string labels.
One and only one of the 'classlabels_*' attributes must be defined." + }, + { + "name": "nodes_falsenodeids", + "type": "int64[]", + "required": false, + "description": "Child node if expression is false." + }, + { + "name": "nodes_featureids", + "type": "int64[]", + "required": false, + "description": "Feature id for each node." + }, + { + "name": "nodes_hitrates", + "type": "float32[]", + "required": false, + "description": "Popularity of each node, used for performance and may be omitted." + }, + { + "name": "nodes_hitrates_as_tensor", + "type": "tensor", + "required": false, + "description": "Popularity of each node, used for performance and may be omitted." + }, + { + "name": "nodes_missing_value_tracks_true", + "type": "int64[]", + "required": false, + "description": "For each node, define what to do in the presence of a missing value: if a value is missing (NaN), use the 'true' or 'false' branch based on the value in this array.
This attribute may be left undefined, and the defalt value is false (0) for all nodes." + }, + { + "name": "nodes_modes", + "type": "string[]", + "required": false, + "description": "The node kind, that is, the comparison to make at the node. There is no comparison to make at a leaf node.
One of 'BRANCH_LEQ', 'BRANCH_LT', 'BRANCH_GTE', 'BRANCH_GT', 'BRANCH_EQ', 'BRANCH_NEQ', 'LEAF'" + }, + { + "name": "nodes_nodeids", + "type": "int64[]", + "required": false, + "description": "Node id for each node. Ids may restart at zero for each tree, but it not required to." + }, + { + "name": "nodes_treeids", + "type": "int64[]", + "required": false, + "description": "Tree id for each node." + }, + { + "name": "nodes_truenodeids", + "type": "int64[]", + "required": false, + "description": "Child node if expression is true." + }, + { + "name": "nodes_values", + "type": "float32[]", + "required": false, + "description": "Thresholds to do the splitting on for each node." + }, + { + "name": "nodes_values_as_tensor", + "type": "tensor", + "required": false, + "description": "Thresholds to do the splitting on for each node." + }, + { + "name": "post_transform", + "type": "string", + "required": false, + "default": "NONE", + "description": "Indicates the transform to apply to the score.
One of 'NONE,' 'SOFTMAX,' 'LOGISTIC,' 'SOFTMAX_ZERO,' or 'PROBIT.'" + } + ], + "inputs": [ + { + "name": "X", + "type": "T1", + "description": "Input of shape [N,F]" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T2", + "description": "N, Top class for each point" + }, + { + "name": "Z", + "type": "tensor(float)", + "description": "The class score for each class, for each point, a tensor of shape [N,E]." + } + ], + "min_output": 2, + "max_output": 2, + "type_constraints": [ + { + "description": "The input type must be a tensor of a numeric type.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + }, + { + "description": "The output type will be a tensor of strings or integers, depending on which of the the classlabels_* attributes is used.", + "type_param_str": "T2", + "allowed_type_strs": [ + "tensor(string)", + "tensor(int64)" + ] + } + ] + }, + { + "name": "TreeEnsembleRegressor", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Tree Ensemble regressor. Returns the regressed values for each input in N.
\n All args with nodes_ are fields of a tuple of tree nodes, and\n it is assumed they are the same length, and an index i will decode the\n tuple across these inputs. Each node id can appear only once\n for each tree id.
\n All fields prefixed with target_ are tuples of votes at the leaves.
\n A leaf may have multiple votes, where each vote is weighted by\n the associated target_weights index.
\n All trees must have their node ids start at 0 and increment by 1.
\n Mode enum is BRANCH_LEQ, BRANCH_LT, BRANCH_GTE, BRANCH_GT, BRANCH_EQ, BRANCH_NEQ, LEAF\n", + "attributes": [ + { + "name": "aggregate_function", + "type": "string", + "required": false, + "default": "SUM", + "description": "Defines how to aggregate leaf values within a target.
One of 'AVERAGE,' 'SUM,' 'MIN,' 'MAX.'" + }, + { + "name": "base_values", + "type": "float32[]", + "required": false, + "description": "Base values for classification, added to final class score; the size must be the same as the classes or can be left unassigned (assumed 0)" + }, + { + "name": "n_targets", + "type": "int64", + "required": false, + "description": "The total number of targets." + }, + { + "name": "nodes_falsenodeids", + "type": "int64[]", + "required": false, + "description": "Child node if expression is false" + }, + { + "name": "nodes_featureids", + "type": "int64[]", + "required": false, + "description": "Feature id for each node." + }, + { + "name": "nodes_hitrates", + "type": "float32[]", + "required": false, + "description": "Popularity of each node, used for performance and may be omitted." + }, + { + "name": "nodes_missing_value_tracks_true", + "type": "int64[]", + "required": false, + "description": "For each node, define what to do in the presence of a NaN: use the 'true' (if the attribute value is 1) or 'false' (if the attribute value is 0) branch based on the value in this array.
This attribute may be left undefined and the defalt value is false (0) for all nodes." + }, + { + "name": "nodes_modes", + "type": "string[]", + "required": false, + "description": "The node kind, that is, the comparison to make at the node. There is no comparison to make at a leaf node.
One of 'BRANCH_LEQ', 'BRANCH_LT', 'BRANCH_GTE', 'BRANCH_GT', 'BRANCH_EQ', 'BRANCH_NEQ', 'LEAF'" + }, + { + "name": "nodes_nodeids", + "type": "int64[]", + "required": false, + "description": "Node id for each node. Node ids must restart at zero for each tree and increase sequentially." + }, + { + "name": "nodes_treeids", + "type": "int64[]", + "required": false, + "description": "Tree id for each node." + }, + { + "name": "nodes_truenodeids", + "type": "int64[]", + "required": false, + "description": "Child node if expression is true" + }, + { + "name": "nodes_values", + "type": "float32[]", + "required": false, + "description": "Thresholds to do the splitting on for each node." + }, + { + "name": "post_transform", + "type": "string", + "required": false, + "default": "NONE", + "description": "Indicates the transform to apply to the score.
One of 'NONE,' 'SOFTMAX,' 'LOGISTIC,' 'SOFTMAX_ZERO,' or 'PROBIT'" + }, + { + "name": "target_ids", + "type": "int64[]", + "required": false, + "description": "The index of the target that each weight is for" + }, + { + "name": "target_nodeids", + "type": "int64[]", + "required": false, + "description": "The node id of each weight" + }, + { + "name": "target_treeids", + "type": "int64[]", + "required": false, + "description": "The id of the tree that each node is in." + }, + { + "name": "target_weights", + "type": "float32[]", + "required": false, + "description": "The weight for each target" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input of shape [N,F]" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(float)", + "description": "N classes" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input type must be a tensor of a numeric type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + } + ] + }, + { + "name": "TreeEnsembleRegressor", + "module": "ai.onnx.ml", + "version": 3, + "support_level": "common", + "description": "Tree Ensemble regressor. Returns the regressed values for each input in N.
\n All args with nodes_ are fields of a tuple of tree nodes, and\n it is assumed they are the same length, and an index i will decode the\n tuple across these inputs. Each node id can appear only once\n for each tree id.
\n All fields prefixed with target_ are tuples of votes at the leaves.
\n A leaf may have multiple votes, where each vote is weighted by\n the associated target_weights index.
\n All fields ending with _as_tensor can be used instead of the\n same parameter without the suffix if the element type is double and not float.\n All trees must have their node ids start at 0 and increment by 1.
\n Mode enum is BRANCH_LEQ, BRANCH_LT, BRANCH_GTE, BRANCH_GT, BRANCH_EQ, BRANCH_NEQ, LEAF\n", + "attributes": [ + { + "name": "aggregate_function", + "type": "string", + "required": false, + "default": "SUM", + "description": "Defines how to aggregate leaf values within a target.
One of 'AVERAGE,' 'SUM,' 'MIN,' 'MAX.'" + }, + { + "name": "base_values", + "type": "float32[]", + "required": false, + "description": "Base values for classification, added to final class score; the size must be the same as the classes or can be left unassigned (assumed 0)" + }, + { + "name": "base_values_as_tensor", + "type": "tensor", + "required": false, + "description": "Base values for classification, added to final class score; the size must be the same as the classes or can be left unassigned (assumed 0)" + }, + { + "name": "n_targets", + "type": "int64", + "required": false, + "description": "The total number of targets." + }, + { + "name": "nodes_falsenodeids", + "type": "int64[]", + "required": false, + "description": "Child node if expression is false" + }, + { + "name": "nodes_featureids", + "type": "int64[]", + "required": false, + "description": "Feature id for each node." + }, + { + "name": "nodes_hitrates", + "type": "float32[]", + "required": false, + "description": "Popularity of each node, used for performance and may be omitted." + }, + { + "name": "nodes_hitrates_as_tensor", + "type": "tensor", + "required": false, + "description": "Popularity of each node, used for performance and may be omitted." + }, + { + "name": "nodes_missing_value_tracks_true", + "type": "int64[]", + "required": false, + "description": "For each node, define what to do in the presence of a NaN: use the 'true' (if the attribute value is 1) or 'false' (if the attribute value is 0) branch based on the value in this array.
This attribute may be left undefined and the defalt value is false (0) for all nodes." + }, + { + "name": "nodes_modes", + "type": "string[]", + "required": false, + "description": "The node kind, that is, the comparison to make at the node. There is no comparison to make at a leaf node.
One of 'BRANCH_LEQ', 'BRANCH_LT', 'BRANCH_GTE', 'BRANCH_GT', 'BRANCH_EQ', 'BRANCH_NEQ', 'LEAF'" + }, + { + "name": "nodes_nodeids", + "type": "int64[]", + "required": false, + "description": "Node id for each node. Node ids must restart at zero for each tree and increase sequentially." + }, + { + "name": "nodes_treeids", + "type": "int64[]", + "required": false, + "description": "Tree id for each node." + }, + { + "name": "nodes_truenodeids", + "type": "int64[]", + "required": false, + "description": "Child node if expression is true" + }, + { + "name": "nodes_values", + "type": "float32[]", + "required": false, + "description": "Thresholds to do the splitting on for each node." + }, + { + "name": "nodes_values_as_tensor", + "type": "tensor", + "required": false, + "description": "Thresholds to do the splitting on for each node." + }, + { + "name": "post_transform", + "type": "string", + "required": false, + "default": "NONE", + "description": "Indicates the transform to apply to the score.
One of 'NONE,' 'SOFTMAX,' 'LOGISTIC,' 'SOFTMAX_ZERO,' or 'PROBIT'" + }, + { + "name": "target_ids", + "type": "int64[]", + "required": false, + "description": "The index of the target that each weight is for" + }, + { + "name": "target_nodeids", + "type": "int64[]", + "required": false, + "description": "The node id of each weight" + }, + { + "name": "target_treeids", + "type": "int64[]", + "required": false, + "description": "The id of the tree that each node is in." + }, + { + "name": "target_weights", + "type": "float32[]", + "required": false, + "description": "The weight for each target" + }, + { + "name": "target_weights_as_tensor", + "type": "tensor", + "required": false, + "description": "The weight for each target" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "Input of shape [N,F]" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "tensor(float)", + "description": "N classes" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The input type must be a tensor of a numeric type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(float)", + "tensor(double)", + "tensor(int64)", + "tensor(int32)" + ] + } + ] + }, + { + "name": "Trilu", + "module": "ai.onnx", + "version": 14, + "support_level": "common", + "description": "Given a 2-D matrix or batches of 2-D matrices, returns the upper or lower triangular part of the tensor(s).\nThe attribute \"upper\" determines whether the upper or lower part is retained. If set to true,\nthe upper triangular matrix is retained. Lower triangular matrix is retained otherwise.\nDefault value for the \"upper\" attribute is true.\nTrilu takes one input tensor of shape [*, N, M], where * is zero or more batch dimensions. The upper triangular part consists\nof the elements on and above the given diagonal (k). The lower triangular part consists of elements on and below the diagonal.\nAll other elements in the matrix are set to zero.\nIf k = 0, the triangular part on and above/below the main diagonal is retained.\nIf upper is set to true, a positive k retains the upper triangular matrix excluding the main diagonal and (k-1) diagonals above it.\nA negative k value retains the main diagonal and |k| diagonals below it.\nIf upper is set to false, a positive k retains the lower triangular matrix including the main diagonal and k diagonals above it.\nA negative k value excludes the main diagonal and (|k|-1) diagonals below it.\n", + "attributes": [ + { + "name": "upper", + "type": "int64", + "required": false, + "default": 1, + "description": "Boolean. Indicates whether upper or lower part of matrix is retained. Default is true." + } + ], + "inputs": [ + { + "name": "input", + "type": "T", + "description": "Input tensor of rank 2 or higher." + }, + { + "name": "k", + "type": "tensor(int64)", + "option": "optional", + "description": "A 0-D tensor containing a single value corresponding to the number diagonals above or below the main diagonal to exclude or include. Default value is 0 if it's not specified." + } + ], + "min_input": 1, + "max_input": 2, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Output tensor of the same type and shape as the input tensor." + } + ], + "min_output": 1, + "max_output": 1, + "inputs_range": "1 - 2", + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "tril", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x'],\n outputs=['y'],\n upper=0,\n)\n\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 1, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[4, 0, 0, 0, 0],\n# [1, 2, 0, 0, 0],\n# [9, 4, 1, 0, 0],\n# [4, 3, 4, 2, 0]]\ny = tril_reference_implementation(x)\nexpect(node, inputs=[x], outputs=[y], name='test_tril')" + }, + { + "summary": "tril_neg", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n upper=0,\n)\n\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\nk = np.array(-1).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 1, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[0, 0, 0, 0, 0],\n# [1, 0, 0, 0, 0],\n# [9, 4, 0, 0, 0],\n# [4, 3, 4, 0, 0]]\ny = tril_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_tril_neg')" + }, + { + "summary": "tril_one_row", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x'],\n outputs=['y'],\n upper=0,\n)\n\nx = np.random.randint(10, size=(3, 1, 5)).astype(np.int64)\n# X:\n# [[[6, 2, 4, 1, 6]],\n#\n# [[8, 3, 8, 7, 0]],\n#\n# [[2, 2, 9, 5, 9]]]\n# expect result:\n# [[[6, 0, 0, 0, 0]],\n#\n# [[8, 0, 0, 0, 0]],\n#\n# [[2, 0, 0, 0, 0]]]\ny = tril_reference_implementation(x)\nexpect(node, inputs=[x], outputs=[y], name='test_tril_one_row_neg')" + }, + { + "summary": "tril_out_neg", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n upper=0,\n)\n\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\nk = np.array(-7).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 1, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[0, 0, 0, 0, 0],\n# [0, 0, 0, 0, 0],\n# [0, 0, 0, 0, 0],\n# [0, 0, 0, 0, 0]]\ny = tril_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_tril_out_neg')" + }, + { + "summary": "tril_out_pos", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n upper=0,\n)\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\nk = np.array(6).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 1, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 1, 8, 7],\n# [4, 3, 4, 2, 4]]\ny = tril_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_tril_out_pos')" + }, + { + "summary": "tril_pos", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n upper=0,\n)\n\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\nk = np.array(2).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 1, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[4, 7, 3, 0, 0],\n# [1, 2, 8, 6, 0],\n# [9, 4, 1, 8, 7],\n# [4, 3, 4, 2, 4]]\ny = tril_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_tril_pos')" + }, + { + "summary": "tril_square", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x'],\n outputs=['y'],\n upper=0,\n)\n\nx = np.random.randint(10, size=(2, 3, 3)).astype(np.int64)\n# X:\n# [[[0, 4, 3],\n# [2, 0, 9],\n# [8, 2, 5]],\n#\n# [[2, 7, 2],\n# [2, 6, 0],\n# [2, 6, 5]]]\n# expect result:\n# [[[0, 0, 0],\n# [2, 0, 0],\n# [8, 2, 5]],\n#\n# [[2, 0, 0],\n# [2, 6, 0],\n# [2, 6, 5]]]\ny = tril_reference_implementation(x)\nexpect(node, inputs=[x], outputs=[y], name='test_tril_square')" + }, + { + "summary": "tril_square_neg", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n upper=0,\n)\n\nx = np.random.randint(10, size=(2, 3, 3)).astype(np.int64)\nk = np.array(-1).astype(np.int64)\n# X:\n# [[[0, 4, 3],\n# [2, 0, 9],\n# [8, 2, 5]],\n#\n# [[2, 7, 2],\n# [2, 6, 0],\n# [2, 6, 5]]]\n# expect result:\n# [[[0, 0, 0],\n# [2, 0, 0],\n# [8, 2, 0]],\n#\n# [[0, 0, 0],\n# [2, 0, 0],\n# [2, 6, 0]]]\ny = tril_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_tril_square_neg')" + }, + { + "summary": "tril_zero", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n upper=0,\n)\n\nx = np.random.randint(10, size=(3, 0, 5)).astype(np.int64)\nk = np.array(6).astype(np.int64)\n# X:\n# []\n# expect result:\n# []\ny = tril_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_tril_zero')" + }, + { + "summary": "triu", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 0, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[4, 7, 3, 7, 9],\n# [0, 2, 8, 6, 9],\n# [0, 0, 0, 8, 7],\n# [0, 0, 0, 2, 4]]\ny = triu_reference_implementation(x)\nexpect(node, inputs=[x], outputs=[y], name='test_triu')" + }, + { + "summary": "triu_neg", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n)\n\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\nk = np.array(-1).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 0, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [0, 4, 0, 8, 7],\n# [0, 0, 4, 2, 4]]\ny = triu_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_triu_neg')" + }, + { + "summary": "triu_one_row", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n)\n\nx = np.random.randint(10, size=(3, 1, 5)).astype(np.int64)\nk = np.array(1).astype(np.int64)\n# X:\n# [[[1, 4, 9, 7, 1]],\n#\n# [[9, 2, 8, 8, 4]],\n#\n# [[3, 9, 7, 4, 2]]]\n# expect result:\n# [[[0, 4, 9, 7, 1]],\n#\n# [[0, 2, 8, 8, 4]],\n#\n# [[0, 9, 7, 4, 2]]]\ny = triu_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_triu_one_row')" + }, + { + "summary": "triu_out_neg_out", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n)\n\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\nk = np.array(-7).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 0, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 0, 8, 7],\n# [4, 3, 4, 2, 4]]\ny = triu_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_triu_out_neg_out')" + }, + { + "summary": "triu_out_pos", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n)\n\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\nk = np.array(6).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 0, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[0, 0, 0, 0, 0],\n# [0, 0, 0, 0, 0],\n# [0, 0, 0, 0, 0],\n# [0, 0, 0, 0, 0]]\ny = triu_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_triu_out_pos')" + }, + { + "summary": "triu_pos", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n)\n\nx = np.random.randint(10, size=(4, 5)).astype(np.int64)\nk = np.array(2).astype(np.int64)\n# X:\n# [[4, 7, 3, 7, 9],\n# [1, 2, 8, 6, 9],\n# [9, 4, 0, 8, 7],\n# [4, 3, 4, 2, 4]]\n# expect result:\n# [[0, 0, 3, 7, 9],\n# [0, 0, 0, 6, 9],\n# [0, 0, 0, 0, 7],\n# [0, 0, 0, 0, 0]]\ny = triu_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_triu_pos')" + }, + { + "summary": "triu_square", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x'],\n outputs=['y'],\n)\n\nx = np.random.randint(10, size=(2, 3, 3)).astype(np.int64)\ny = triu_reference_implementation(x)\n# X:\n# [[[4, 6, 9],\n# [7, 5, 4],\n# [8, 1, 2]],\n#\n# [[1, 4, 9],\n# [9, 6, 3],\n# [8, 9, 8]]]\n# expect result:\n# [[[4, 6, 9],\n# [0, 5, 4],\n# [0, 0, 2]],\n#\n# [[1, 4, 9],\n# [0, 6, 3],\n# [0, 0, 8]]]\nexpect(node, inputs=[x], outputs=[y], name='test_triu_square')" + }, + { + "summary": "triu_square_neg", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n)\n\nx = np.random.randint(10, size=(2, 3, 3)).astype(np.int64)\nk = np.array(-1).astype(np.int64)\n# X:\n# [[[4, 6, 9],\n# [7, 5, 4],\n# [8, 1, 2]],\n#\n# [[1, 4, 9],\n# [9, 6, 3],\n# [8, 9, 8]]]\n# expect result:\n# [[[4, 6, 9],\n# [7, 5, 4],\n# [0, 1, 2]],\n#\n# [[1, 4, 9],\n# [9, 6, 3],\n# [0, 9, 8]]]\ny = triu_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_triu_square_neg')" + }, + { + "summary": "triu_zero", + "code": "node = onnx.helper.make_node(\n 'Trilu',\n inputs=['x', 'k'],\n outputs=['y'],\n)\n\nx = np.random.randint(10, size=(0, 5)).astype(np.int64)\nk = np.array(6).astype(np.int64)\n# X:\n# []\n# expect result:\n# []\ny = triu_reference_implementation(x, int(k))\nexpect(node, inputs=[x, k], outputs=[y], name='test_triu_zero')" + } + ] + }, + { + "name": "Unique", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Find the unique elements of a tensor. When an optional attribute 'axis' is provided, unique subtensors sliced along the 'axis' are returned.\nOtherwise the input tensor is flattened and unique values of the flattened tensor are returned.\n\nThis operator returns the unique values or sliced unique subtensors of the input tensor and three optional outputs.\nThe first output tensor 'Y' contains all unique values or subtensors of the input.\nThe second optional output tensor 'indices' contains indices of 'Y' elements' first occurance in 'X'..\nThe third optional output tensor 'inverse_indices' contains, for elements of 'X', its corresponding indices in 'Y'. \".\nThe fourth optional output tensor 'counts' contains the count of each element of 'Y' in the input.\n\nOutputs are either sorted in ascending order or optionally in the order of the first occurrence of the values in the input.\n\nhttps://docs.scipy.org/doc/numpy/reference/generated/numpy.unique.html\n\nExample 1:\n input_X = [2, 1, 1, 3, 4, 3]\n attribute_sorted = 0\n attribute_axis = None\n output_Y = [2, 1, 3, 4]\n output_indices = [0, 1, 3, 4]\n output_inverse_indices = [0, 1, 1, 2, 3, 2]\n output_counts = [1, 2, 2, 1]\n\nExample 2:\n input_X = [[1, 3], [2, 3]]\n attribute_sorted = 1\n attribute_axis = None\n output_Y = [1, 2, 3]\n output_indices = [0, 2, 1]\n output_inverse_indices = [0, 2, 1, 2]\n output_counts = [1, 1, 2]\n\nExample 3:\n input_X = [[1, 0, 0], [1, 0, 0], [2, 3, 4]]\n attribute_sorted = 1\n attribute_axis = 0\n output_Y = [[1, 0, 0], [2, 3, 4]]\n output_indices = [0, 2]\n output_inverse_indices = [0, 0, 1]\n output_counts = [2, 1]\n\nExample 4:\n input_x = [[[1., 1.], [0., 1.], [2., 1.], [0., 1.]],\n [[1., 1.], [0., 1.], [2., 1.], [0., 1.]]]\n attribute_sorted = 1\n attribute_axis = 1\n\n intermediate data are presented below for better understanding:\n\n there are 4 subtensors sliced along axis 1 of input_x (shape = (2, 4, 2)):\n A: [[1, 1], [1, 1]],\n [[0, 1], [0, 1]],\n [[2, 1], [2, 1]],\n [[0, 1], [0, 1]].\n\n there are 3 unique subtensors:\n [[1, 1], [1, 1]],\n [[0, 1], [0, 1]],\n [[2, 1], [2, 1]].\n\n sorted unique subtensors:\n B: [[0, 1], [0, 1]],\n [[1, 1], [1, 1]],\n [[2, 1], [2, 1]].\n\n output_Y is constructed from B:\n [[[0. 1.], [1. 1.], [2. 1.]],\n [[0. 1.], [1. 1.], [2. 1.]]]\n\n output_indices is to map from B to A:\n [1, 0, 2]\n\n output_inverse_indices is to map from A to B:\n [1, 0, 2, 0]\n\n output_counts = [2 1 1]\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "(Optional) The dimension to apply unique. If not specified, the unique elements of the flattened input are returned. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(input)." + }, + { + "name": "sorted", + "type": "int64", + "required": false, + "default": 1, + "description": "(Optional) Whether to sort the unique elements in ascending order before returning as output. Must be one of 0, or 1 (default)." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "A N-D input tensor that is to be processed." + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "A tensor of the same type as 'X' containing all the unique values or subtensors sliced along a provided 'axis' in 'X', either sorted or maintained in the same order they occur in input 'X'" + }, + { + "name": "indices", + "type": "tensor(int64)", + "option": "optional", + "description": "A 1-D INT64 tensor containing indices of 'Y' elements' first occurance in 'X'. When 'axis' is provided, it contains indices to subtensors in input 'X' on the 'axis'. When 'axis' is not provided, it contains indices to values in the flattened input tensor. " + }, + { + "name": "inverse_indices", + "type": "tensor(int64)", + "option": "optional", + "description": "A 1-D INT64 tensor containing, for elements of 'X', its corresponding indices in 'Y'. When 'axis' is provided, it contains indices to subtensors in output 'Y' on the 'axis'. When 'axis' is not provided, it contains indices to values in output 'Y'. " + }, + { + "name": "counts", + "type": "tensor(int64)", + "option": "optional", + "description": "A 1-D INT64 tensor containing the count of each element of 'Y' in input 'X'" + } + ], + "min_output": 1, + "max_output": 4, + "outputs_range": "1 - 4", + "type_constraints": [ + { + "description": "Input can be of any tensor type.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "not_sorted_without_axis", + "code": "node_not_sorted = onnx.helper.make_node(\n 'Unique',\n inputs=['X'],\n outputs=['Y', 'indices', 'inverse_indices', 'counts'],\n sorted=0\n)\n# numpy unique does not retain original order (it sorts the output unique values)\n# https://github.com/numpy/numpy/issues/8621\n# we need to recover unsorted output and indices\nx = np.array([2.0, 1.0, 1.0, 3.0, 4.0, 3.0], dtype=np.float32)\ny, indices, inverse_indices, counts = np.unique(x, True, True, True)\n\n# prepare index mapping from sorted to unsorted\nargsorted_indices = np.argsort(indices)\ninverse_indices_map = {i: si for i, si in zip(argsorted_indices, np.arange(len(argsorted_indices)))}\n\nindices = indices[argsorted_indices]\ny = np.take(x, indices, axis=0)\ninverse_indices = np.asarray([inverse_indices_map[i] for i in inverse_indices], dtype=np.int64)\ncounts = counts[argsorted_indices]\nindices, inverse_indices, counts = specify_int64(indices, inverse_indices, counts)\n# print(y)\n# [2.0, 1.0, 3.0, 4.0]\n# print(indices)\n# [0 1 3 4]\n# print(inverse_indices)\n# [0, 1, 1, 2, 3, 2]\n# print(counts)\n# [1, 2, 2, 1]\n\nexpect(node_not_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_not_sorted_without_axis')" + }, + { + "summary": "sorted_with_axis", + "code": "node_sorted = onnx.helper.make_node(\n 'Unique',\n inputs=['X'],\n outputs=['Y', 'indices', 'inverse_indices', 'counts'],\n sorted=1,\n axis=0\n)\n\nx = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]], dtype=np.float32)\ny, indices, inverse_indices, counts = np.unique(x, True, True, True, axis=0)\nindices, inverse_indices, counts = specify_int64(indices, inverse_indices, counts)\n# print(y)\n# [[1. 0. 0.]\n# [2. 3. 4.]]\n# print(indices)\n# [0 2]\n# print(inverse_indices)\n# [0 0 1]\n# print(counts)\n# [2 1]\n\nexpect(node_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_sorted_with_axis')" + }, + { + "summary": "sorted_with_axis_3d", + "code": "node_sorted = onnx.helper.make_node(\n 'Unique',\n inputs=['X'],\n outputs=['Y', 'indices', 'inverse_indices', 'counts'],\n sorted=1,\n axis=1\n)\n\nx = np.array([[[1., 1.], [0., 1.], [2., 1.], [0., 1.]],\n [[1., 1.], [0., 1.], [2., 1.], [0., 1.]]], dtype=np.float32)\ny, indices, inverse_indices, counts = np.unique(x, True, True, True, axis=1)\nindices, inverse_indices, counts = specify_int64(indices, inverse_indices, counts)\n# print(y)\n# [[[0. 1.]\n# [1. 1.]\n# [2. 1.]]\n# [[0. 1.]\n# [1. 1.]\n# [2. 1.]]]\n# print(indices)\n# [1 0 2]\n# print(inverse_indices)\n# [1 0 2 0]\n# print(counts)\n# [2 1 1]\nexpect(node_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_sorted_with_axis_3d')" + }, + { + "summary": "sorted_with_negative_axis", + "code": "node_sorted = onnx.helper.make_node(\n 'Unique',\n inputs=['X'],\n outputs=['Y', 'indices', 'inverse_indices', 'counts'],\n sorted=1,\n axis=-1\n)\n\nx = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 3]], dtype=np.float32)\ny, indices, inverse_indices, counts = np.unique(x, True, True, True, axis=-1)\nindices, inverse_indices, counts = specify_int64(indices, inverse_indices, counts)\n# print(y)\n# [[0. 1.]\n# [0. 1.]\n# [3. 2.]]\n# print(indices)\n# [1 0]\n# print(inverse_indices)\n# [1 0 0]\n# print(counts)\n# [2 1]\n\nexpect(node_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_sorted_with_negative_axis')" + }, + { + "summary": "sorted_without_axis", + "code": "node_sorted = onnx.helper.make_node(\n 'Unique',\n inputs=['X'],\n outputs=['Y', 'indices', 'inverse_indices', 'counts']\n)\n\nx = np.array([2.0, 1.0, 1.0, 3.0, 4.0, 3.0], dtype=np.float32)\ny, indices, inverse_indices, counts = np.unique(x, True, True, True)\nindices, inverse_indices, counts = specify_int64(indices, inverse_indices, counts)\nexpect(node_sorted, inputs=[x], outputs=[y, indices, inverse_indices, counts], name='test_unique_sorted_without_axis')" + } + ] + }, + { + "name": "Unsqueeze", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Insert single-dimensional entries to the shape of a tensor.\nTakes one required argument `axes`, a list of dimensions that will be inserted.\nDimension indices in `axes` are as seen in the output tensor. For example:\n Given a tensor such that tensor with shape [3, 4, 5], then\n Unsqueeze(tensor, axes=[0, 4]) has shape [1, 3, 4, 5, 1]\n", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": true, + "description": "List of non-negative integers, indicate the dimensions to be inserted" + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Original tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "expanded", + "type": "T", + "description": "Reshaped tensor with same data as input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "unsqueeze_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 1, 5).astype(np.float32)\naxes = np.array([-2]).astype(np.int64)\ny = np.expand_dims(x, axis=-2)\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_negative_axes')" + }, + { + "summary": "unsqueeze_one_axis", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\n\nfor i in range(x.ndim):\n axes = np.array([i]).astype(np.int64)\n node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n )\n y = np.expand_dims(x, axis=i)\n\n expect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_axis_' + str(i))" + }, + { + "summary": "unsqueeze_three_axes", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\naxes = np.array([2, 4, 5]).astype(np.int64)\n\nnode = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\ny = np.expand_dims(x, axis=2)\ny = np.expand_dims(y, axis=4)\ny = np.expand_dims(y, axis=5)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_three_axes')" + }, + { + "summary": "unsqueeze_two_axes", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\naxes = np.array([1, 4]).astype(np.int64)\n\nnode = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\ny = np.expand_dims(x, axis=1)\ny = np.expand_dims(y, axis=4)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_two_axes')" + }, + { + "summary": "unsqueeze_unsorted_axes", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\naxes = np.array([5, 4, 2]).astype(np.int64)\n\nnode = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\ny = np.expand_dims(x, axis=2)\ny = np.expand_dims(y, axis=4)\ny = np.expand_dims(y, axis=5)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_unsorted_axes')" + } + ], + "category": "Transform" + }, + { + "name": "Unsqueeze", + "module": "ai.onnx", + "version": 11, + "support_level": "common", + "description": "Insert single-dimensional entries to the shape of an input tensor (`data`).\nTakes one required argument `axes` - which contains a list of dimension indices and this operator will insert a dimension of value `1` into the corresponding index of the output tensor (`expanded`).\n\nFor example:\n Given an input tensor (`data`) of shape [3, 4, 5], then\n Unsqueeze(data, axes=[0, 4]) outputs a tensor (`expanded`) containing same data as `data` but with shape [1, 3, 4, 5, 1].\n\nThe attribute `axes` should not contain any duplicate entries. It is an error if it contains duplicates.\nThe rank of the output tensor (`output_rank`) is the rank of the input tensor (`data`) plus the number of values in `axes`.\nEach value in `axes` should be within the (inclusive) range [-output_rank , output_rank - 1].\nThe order of values in `axes` does not matter and can come in any order.\n\n", + "attributes": [ + { + "name": "axes", + "type": "int64[]", + "required": true, + "description": "List of integers indicating the dimensions to be inserted. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(expanded)." + } + ], + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Original tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "expanded", + "type": "T", + "description": "Reshaped tensor with same data as input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "unsqueeze_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 1, 5).astype(np.float32)\naxes = np.array([-2]).astype(np.int64)\ny = np.expand_dims(x, axis=-2)\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_negative_axes')" + }, + { + "summary": "unsqueeze_one_axis", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\n\nfor i in range(x.ndim):\n axes = np.array([i]).astype(np.int64)\n node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n )\n y = np.expand_dims(x, axis=i)\n\n expect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_axis_' + str(i))" + }, + { + "summary": "unsqueeze_three_axes", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\naxes = np.array([2, 4, 5]).astype(np.int64)\n\nnode = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\ny = np.expand_dims(x, axis=2)\ny = np.expand_dims(y, axis=4)\ny = np.expand_dims(y, axis=5)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_three_axes')" + }, + { + "summary": "unsqueeze_two_axes", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\naxes = np.array([1, 4]).astype(np.int64)\n\nnode = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\ny = np.expand_dims(x, axis=1)\ny = np.expand_dims(y, axis=4)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_two_axes')" + }, + { + "summary": "unsqueeze_unsorted_axes", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\naxes = np.array([5, 4, 2]).astype(np.int64)\n\nnode = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\ny = np.expand_dims(x, axis=2)\ny = np.expand_dims(y, axis=4)\ny = np.expand_dims(y, axis=5)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_unsorted_axes')" + } + ], + "category": "Transform" + }, + { + "name": "Unsqueeze", + "module": "ai.onnx", + "version": 13, + "support_level": "common", + "description": "Insert single-dimensional entries to the shape of an input tensor (`data`).\nTakes one required input `axes` - which contains a list of dimension indices and this operator will insert a dimension of value `1` into the corresponding index of the output tensor (`expanded`).\n\nFor example:\n Given an input tensor (`data`) of shape [3, 4, 5], then\n Unsqueeze(data, axes=[0, 4]) outputs a tensor (`expanded`) containing same data as `data` but with shape [1, 3, 4, 5, 1].\n\nThe input `axes` should not contain any duplicate entries. It is an error if it contains duplicates.\nThe rank of the output tensor (`output_rank`) is the rank of the input tensor (`data`) plus the number of values in `axes`.\nEach value in `axes` should be within the (inclusive) range [-output_rank , output_rank - 1].\nThe order of values in `axes` does not matter and can come in any order.\n\n", + "inputs": [ + { + "name": "data", + "type": "T", + "description": "Original tensor" + }, + { + "name": "axes", + "type": "tensor(int64)", + "description": "List of integers indicating the dimensions to be inserted. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(expanded)." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "expanded", + "type": "T", + "description": "Reshaped tensor with same data as input." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "unsqueeze_negative_axes", + "code": "node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\nx = np.random.randn(1, 3, 1, 5).astype(np.float32)\naxes = np.array([-2]).astype(np.int64)\ny = np.expand_dims(x, axis=-2)\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_negative_axes')" + }, + { + "summary": "unsqueeze_one_axis", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\n\nfor i in range(x.ndim):\n axes = np.array([i]).astype(np.int64)\n node = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n )\n y = np.expand_dims(x, axis=i)\n\n expect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_axis_' + str(i))" + }, + { + "summary": "unsqueeze_three_axes", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\naxes = np.array([2, 4, 5]).astype(np.int64)\n\nnode = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\ny = np.expand_dims(x, axis=2)\ny = np.expand_dims(y, axis=4)\ny = np.expand_dims(y, axis=5)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_three_axes')" + }, + { + "summary": "unsqueeze_two_axes", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\naxes = np.array([1, 4]).astype(np.int64)\n\nnode = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\ny = np.expand_dims(x, axis=1)\ny = np.expand_dims(y, axis=4)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_two_axes')" + }, + { + "summary": "unsqueeze_unsorted_axes", + "code": "x = np.random.randn(3, 4, 5).astype(np.float32)\naxes = np.array([5, 4, 2]).astype(np.int64)\n\nnode = onnx.helper.make_node(\n 'Unsqueeze',\n inputs=['x', 'axes'],\n outputs=['y'],\n)\ny = np.expand_dims(x, axis=2)\ny = np.expand_dims(y, axis=4)\ny = np.expand_dims(y, axis=5)\n\nexpect(node, inputs=[x, axes], outputs=[y],\n name='test_unsqueeze_unsorted_axes')" + } + ], + "category": "Transform" + }, + { + "name": "Upsample", + "module": "ai.onnx", + "version": 1, + "support_level": "experimental", + "description": "Upsample the input tensor.\nThe width and height of the output tensor are:\n output_width = floor(input_width * width_scale),\n output_height = floor(input_height * height_scale).\nExample:\n Given `data` tensor, width_scale, height_scale, mode,\n Upsample the input 4-D tensor in nearest mode:\n data = [[[\n [1, 2],\n [3, 4]\n ]]]\n width_scale = 2\n height_scale = 2\n mode = \"nearest\"\n output = [[[\n [1, 1, 2, 2],\n [1, 1, 2, 2],\n [3, 3, 4, 4],\n [3, 3, 4, 4]\n ]]]\n", + "attributes": [ + { + "name": "height_scale", + "type": "float32", + "required": true, + "description": "The scale along height dimension. It takes value greater than or equal to 1." + }, + { + "name": "mode", + "type": "string", + "required": false, + "default": "nearest", + "description": "Two interpolation modes: nearest(default), bilinear" + }, + { + "name": "width_scale", + "type": "float32", + "required": true, + "description": "The scale along width dimension. It takes value greater than or equal to 1." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "4-D tensor, [N,C,H,W]" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "4-D tensor after resizing, [N,C,H,W]" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain output types to bool, int32, int64, float16, float, double tensors.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)" + ] + } + ], + "examples": [ + { + "summary": "nearest", + "code": "node = onnx.helper.make_node(\n 'Upsample',\n inputs=['X', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\noutput = np.array([[[\n [1, 1, 1, 2, 2, 2],\n [1, 1, 1, 2, 2, 2],\n [3, 3, 3, 4, 4, 4],\n [3, 3, 3, 4, 4, 4],\n]]], dtype=np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_upsample_nearest', opset_imports=[helper.make_opsetid(\"\", 9)])" + } + ], + "category": "Data" + }, + { + "name": "Upsample", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Upsample the input tensor.\nEach dimension value of the output tensor is:\n output_dimension = floor(input_dimension * scale).\n", + "attributes": [ + { + "name": "mode", + "type": "string", + "required": false, + "default": "nearest", + "description": "Two interpolation modes: nearest (default), and linear (including bilinear, trilinear, etc)" + }, + { + "name": "scales", + "type": "float32[]", + "required": true, + "description": "The scale array along each dimension. It takes value greater than or equal to 1. The number of elements of 'scales' should be the same as the rank of input 'X'." + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "N-D tensor" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "N-D tensor after resizing" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "nearest", + "code": "node = onnx.helper.make_node(\n 'Upsample',\n inputs=['X', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\noutput = np.array([[[\n [1, 1, 1, 2, 2, 2],\n [1, 1, 1, 2, 2, 2],\n [3, 3, 3, 4, 4, 4],\n [3, 3, 3, 4, 4, 4],\n]]], dtype=np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_upsample_nearest', opset_imports=[helper.make_opsetid(\"\", 9)])" + } + ], + "category": "Data" + }, + { + "name": "Upsample", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Upsample the input tensor.\nEach dimension value of the output tensor is:\n output_dimension = floor(input_dimension * scale).\n", + "attributes": [ + { + "name": "mode", + "type": "string", + "required": false, + "default": "nearest", + "description": "Two interpolation modes: nearest (default), and linear (including bilinear, trilinear, etc)" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "N-D tensor" + }, + { + "name": "scales", + "type": "tensor(float)", + "description": "The scale array along each dimension. It takes value greater than or equal to 1. The number of elements of 'scales' should be the same as the rank of input 'X'." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "N-D tensor after resizing" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input 'X' and output 'Y' to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "nearest", + "code": "node = onnx.helper.make_node(\n 'Upsample',\n inputs=['X', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\noutput = np.array([[[\n [1, 1, 1, 2, 2, 2],\n [1, 1, 1, 2, 2, 2],\n [3, 3, 3, 4, 4, 4],\n [3, 3, 3, 4, 4, 4],\n]]], dtype=np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_upsample_nearest', opset_imports=[helper.make_opsetid(\"\", 9)])" + } + ], + "category": "Data" + }, + { + "name": "Upsample", + "module": "ai.onnx", + "version": 10, + "support_level": "common", + "description": "Upsample the input tensor.\nEach dimension value of the output tensor is:\n output_dimension = floor(input_dimension * scale).\n", + "attributes": [ + { + "name": "mode", + "type": "string", + "required": false, + "default": "nearest", + "description": "Two interpolation modes: nearest (default), and linear (including bilinear, trilinear, etc)" + } + ], + "inputs": [ + { + "name": "X", + "type": "T", + "description": "N-D tensor" + }, + { + "name": "scales", + "type": "tensor(float)", + "description": "The scale array along each dimension. It takes value greater than or equal to 1. The number of elements of 'scales' should be the same as the rank of input 'X'." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "Y", + "type": "T", + "description": "N-D tensor after resizing" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input 'X' and output 'Y' to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "nearest", + "code": "node = onnx.helper.make_node(\n 'Upsample',\n inputs=['X', 'scales'],\n outputs=['Y'],\n mode='nearest',\n)\n\ndata = np.array([[[\n [1, 2],\n [3, 4],\n]]], dtype=np.float32)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\noutput = np.array([[[\n [1, 1, 1, 2, 2, 2],\n [1, 1, 1, 2, 2, 2],\n [3, 3, 3, 4, 4, 4],\n [3, 3, 3, 4, 4, 4],\n]]], dtype=np.float32)\n\nexpect(node, inputs=[data, scales], outputs=[output],\n name='test_upsample_nearest', opset_imports=[helper.make_opsetid(\"\", 9)])" + } + ], + "category": "Data" + }, + { + "name": "Where", + "module": "ai.onnx", + "version": 9, + "support_level": "common", + "description": "Return elements, either from X or Y, depending on condition.\nWhere behaves like\n[numpy.where](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html)\nwith three parameters.\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).", + "inputs": [ + { + "name": "condition", + "type": "B", + "description": "When True (nonzero), yield X, otherwise yield Y" + }, + { + "name": "X", + "type": "T", + "description": "values selected at indices where condition is True" + }, + { + "name": "Y", + "type": "T", + "description": "values selected at indices where condition is False" + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of shape equal to the broadcasted shape of condition, X, and Y." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain to boolean tensors.", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + }, + { + "description": "Constrain input and output types to all tensor types.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "long", + "code": "node = onnx.helper.make_node(\n 'Where',\n inputs=['condition', 'x', 'y'],\n outputs=['z'],\n)\n\ncondition = np.array([[1, 0], [1, 1]], dtype=bool)\nx = np.array([[1, 2], [3, 4]], dtype=np.int64)\ny = np.array([[9, 8], [7, 6]], dtype=np.int64)\nz = np.where(condition, x, y) # expected output [[1, 8], [3, 4]]\nexpect(node, inputs=[condition, x, y], outputs=[z],\n name='test_where_long_example')" + }, + { + "summary": "where", + "code": "node = onnx.helper.make_node(\n 'Where',\n inputs=['condition', 'x', 'y'],\n outputs=['z'],\n)\n\ncondition = np.array([[1, 0], [1, 1]], dtype=bool)\nx = np.array([[1, 2], [3, 4]], dtype=np.float32)\ny = np.array([[9, 8], [7, 6]], dtype=np.float32)\nz = np.where(condition, x, y) # expected output [[1, 8], [3, 4]]\nexpect(node, inputs=[condition, x, y], outputs=[z],\n name='test_where_example')" + } + ] + }, + { + "name": "Where", + "module": "ai.onnx", + "version": 16, + "support_level": "common", + "description": "Return elements, either from X or Y, depending on condition.\nWhere behaves like\n[numpy.where](https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html)\nwith three parameters.\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n\n**History**\n- Version 16 adds bfloat16 to the types allowed (for the second and third parameter).\n", + "inputs": [ + { + "name": "condition", + "type": "B", + "description": "When True (nonzero), yield X, otherwise yield Y" + }, + { + "name": "X", + "type": "T", + "description": "values selected at indices where condition is True" + }, + { + "name": "Y", + "type": "T", + "description": "values selected at indices where condition is False" + } + ], + "min_input": 3, + "max_input": 3, + "outputs": [ + { + "name": "output", + "type": "T", + "description": "Tensor of shape equal to the broadcasted shape of condition, X, and Y." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain to boolean tensors.", + "type_param_str": "B", + "allowed_type_strs": [ + "tensor(bool)" + ] + }, + { + "description": "Constrain input and output types to all tensor types (including bfloat).", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(uint8)", + "tensor(uint16)", + "tensor(uint32)", + "tensor(uint64)", + "tensor(int8)", + "tensor(int16)", + "tensor(int32)", + "tensor(int64)", + "tensor(bfloat16)", + "tensor(float16)", + "tensor(float)", + "tensor(double)", + "tensor(string)", + "tensor(bool)", + "tensor(complex64)", + "tensor(complex128)" + ] + } + ], + "examples": [ + { + "summary": "long", + "code": "node = onnx.helper.make_node(\n 'Where',\n inputs=['condition', 'x', 'y'],\n outputs=['z'],\n)\n\ncondition = np.array([[1, 0], [1, 1]], dtype=bool)\nx = np.array([[1, 2], [3, 4]], dtype=np.int64)\ny = np.array([[9, 8], [7, 6]], dtype=np.int64)\nz = np.where(condition, x, y) # expected output [[1, 8], [3, 4]]\nexpect(node, inputs=[condition, x, y], outputs=[z],\n name='test_where_long_example')" + }, + { + "summary": "where", + "code": "node = onnx.helper.make_node(\n 'Where',\n inputs=['condition', 'x', 'y'],\n outputs=['z'],\n)\n\ncondition = np.array([[1, 0], [1, 1]], dtype=bool)\nx = np.array([[1, 2], [3, 4]], dtype=np.float32)\ny = np.array([[9, 8], [7, 6]], dtype=np.float32)\nz = np.where(condition, x, y) # expected output [[1, 8], [3, 4]]\nexpect(node, inputs=[condition, x, y], outputs=[z],\n name='test_where_example')" + } + ] + }, + { + "name": "Xor", + "module": "ai.onnx", + "version": 1, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `xor` logical operation\nelementwise on the input tensors `A` and `B`.\n\nIf broadcasting is enabled, the right-hand-side argument will be broadcasted\nto match the shape of left-hand-side argument. See the doc of `Add` for a\ndetailed description of the broadcasting rules.\n", + "attributes": [ + { + "name": "axis", + "type": "int64", + "required": false, + "description": "If set, defines the broadcast dimensions." + }, + { + "name": "broadcast", + "type": "int64", + "required": false, + "description": "Enable broadcasting" + } + ], + "inputs": [ + { + "name": "A", + "type": "T", + "description": "Left input tensor for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Right input tensor for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to boolean tensor.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "xor", + "code": "node = onnx.helper.make_node(\n 'Xor',\n inputs=['x', 'y'],\n outputs=['xor'],\n)\n\n# 2d\nx = (np.random.randn(3, 4) > 0).astype(bool)\ny = (np.random.randn(3, 4) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor2d')\n\n# 3d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor3d')\n\n# 4d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor4d')" + }, + { + "summary": "xor_broadcast", + "code": "node = onnx.helper.make_node(\n 'Xor',\n inputs=['x', 'y'],\n outputs=['xor'],\n)\n\n# 3d vs 1d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(5) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast3v1d')\n\n# 3d vs 2d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(4, 5) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast3v2d')\n\n# 4d vs 2d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(5, 6) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast4v2d')\n\n# 4d vs 3d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(4, 5, 6) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast4v3d')\n\n# 4d vs 4d\nx = (np.random.randn(1, 4, 1, 6) > 0).astype(bool)\ny = (np.random.randn(3, 1, 5, 6) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast4v4d')" + } + ], + "category": "Logic" + }, + { + "name": "Xor", + "module": "ai.onnx", + "version": 7, + "support_level": "common", + "description": "Returns the tensor resulted from performing the `xor` logical operation\nelementwise on the input tensors `A` and `B` (with Numpy-style broadcasting support).\n\nThis operator supports **multidirectional (i.e., Numpy-style) broadcasting**; for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", + "inputs": [ + { + "name": "A", + "type": "T", + "description": "First input operand for the logical operator." + }, + { + "name": "B", + "type": "T", + "description": "Second input operand for the logical operator." + } + ], + "min_input": 2, + "max_input": 2, + "outputs": [ + { + "name": "C", + "type": "T1", + "description": "Result tensor." + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "Constrain input to boolean tensor.", + "type_param_str": "T", + "allowed_type_strs": [ + "tensor(bool)" + ] + }, + { + "description": "Constrain output to boolean tensor.", + "type_param_str": "T1", + "allowed_type_strs": [ + "tensor(bool)" + ] + } + ], + "examples": [ + { + "summary": "xor", + "code": "node = onnx.helper.make_node(\n 'Xor',\n inputs=['x', 'y'],\n outputs=['xor'],\n)\n\n# 2d\nx = (np.random.randn(3, 4) > 0).astype(bool)\ny = (np.random.randn(3, 4) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor2d')\n\n# 3d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor3d')\n\n# 4d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor4d')" + }, + { + "summary": "xor_broadcast", + "code": "node = onnx.helper.make_node(\n 'Xor',\n inputs=['x', 'y'],\n outputs=['xor'],\n)\n\n# 3d vs 1d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(5) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast3v1d')\n\n# 3d vs 2d\nx = (np.random.randn(3, 4, 5) > 0).astype(bool)\ny = (np.random.randn(4, 5) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast3v2d')\n\n# 4d vs 2d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(5, 6) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast4v2d')\n\n# 4d vs 3d\nx = (np.random.randn(3, 4, 5, 6) > 0).astype(bool)\ny = (np.random.randn(4, 5, 6) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast4v3d')\n\n# 4d vs 4d\nx = (np.random.randn(1, 4, 1, 6) > 0).astype(bool)\ny = (np.random.randn(3, 1, 5, 6) > 0).astype(bool)\nz = np.logical_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z],\n name='test_xor_bcast4v4d')" + } + ], + "category": "Logic" + }, + { + "name": "ZipMap", + "module": "ai.onnx.ml", + "version": 1, + "support_level": "common", + "description": "Creates a map from the input and the attributes.
\n The values are provided by the input tensor, while the keys are specified by the attributes.\n Must provide keys in either classlabels_strings or classlabels_int64s (but not both).
\n The columns of the tensor correspond one-by-one to the keys specified by the attributes. There must be as many columns as keys.
\n", + "attributes": [ + { + "name": "classlabels_int64s", + "type": "int64[]", + "required": false, + "description": "The keys when using int keys.
One and only one of the 'classlabels_*' attributes must be defined." + }, + { + "name": "classlabels_strings", + "type": "string[]", + "required": false, + "description": "The keys when using string keys.
One and only one of the 'classlabels_*' attributes must be defined." + } + ], + "inputs": [ + { + "name": "X", + "type": "tensor(float)", + "description": "The input values" + } + ], + "min_input": 1, + "max_input": 1, + "outputs": [ + { + "name": "Z", + "type": "T", + "description": "The output map" + } + ], + "min_output": 1, + "max_output": 1, + "type_constraints": [ + { + "description": "The output will be a sequence of string or integer maps to float.", + "type_param_str": "T", + "allowed_type_strs": [ + "seq(map(string, float))", + "seq(map(int64, float))" + ] + } + ] + }, + { + "name": "FusedConv", + "module": "com.microsoft", + "version": 1, + "inputs": [ + { + "name": "input", + "type": "T" + }, + { + "name": "weights", + "type": "T" + }, + { + "name": "bias", + "type": "T" + } + ], + "outputs": [ + { + "name": "output", + "type": "T" + } + ], + "category": "Layer" + } +] diff --git a/onnx-proto.js b/onnx-proto.js new file mode 100644 index 0000000..1824af3 --- /dev/null +++ b/onnx-proto.js @@ -0,0 +1,1737 @@ +var $root = protobuf.get('onnx'); + +$root.onnx = {}; + +$root.onnx.Version = { + "_START_VERSION": 0, + "IR_VERSION_2017_10_10": 1, + "IR_VERSION_2017_10_30": 2, + "IR_VERSION_2017_11_3": 3, + "IR_VERSION_2019_1_22": 4, + "IR_VERSION_2019_3_18": 5, + "IR_VERSION_2019_9_19": 6, + "IR_VERSION_2020_5_8": 7, + "IR_VERSION": 8 +}; + +$root.onnx.AttributeProto = class AttributeProto { + + constructor() { + this.floats = []; + this.ints = []; + this.strings = []; + this.tensors = []; + this.graphs = []; + this.sparse_tensors = []; + this.type_protos = []; + } + + static decode(reader, length) { + const message = new $root.onnx.AttributeProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = reader.string(); + break; + case 21: + message.ref_attr_name = reader.string(); + break; + case 13: + message.doc_string = reader.string(); + break; + case 20: + message.type = reader.int32(); + break; + case 2: + message.f = reader.float(); + break; + case 3: + message.i = reader.int64(); + break; + case 4: + message.s = reader.bytes(); + break; + case 5: + message.t = $root.onnx.TensorProto.decode(reader, reader.uint32()); + break; + case 6: + message.g = $root.onnx.GraphProto.decode(reader, reader.uint32()); + break; + case 22: + message.sparse_tensor = $root.onnx.SparseTensorProto.decode(reader, reader.uint32()); + break; + case 14: + message.tp = $root.onnx.TypeProto.decode(reader, reader.uint32()); + break; + case 7: + message.floats = reader.floats(message.floats, tag); + break; + case 8: + message.ints = reader.array(message.ints, () => reader.int64(), tag); + break; + case 9: + message.strings.push(reader.bytes()); + break; + case 10: + message.tensors.push($root.onnx.TensorProto.decode(reader, reader.uint32())); + break; + case 11: + message.graphs.push($root.onnx.GraphProto.decode(reader, reader.uint32())); + break; + case 23: + message.sparse_tensors.push($root.onnx.SparseTensorProto.decode(reader, reader.uint32())); + break; + case 15: + message.type_protos.push($root.onnx.TypeProto.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.AttributeProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "name": + message.name = reader.string(); + break; + case "ref_attr_name": + message.ref_attr_name = reader.string(); + break; + case "doc_string": + message.doc_string = reader.string(); + break; + case "type": + message.type = reader.enum($root.onnx.AttributeProto.AttributeType); + break; + case "f": + message.f = reader.float(); + break; + case "i": + message.i = reader.int64(); + break; + case "s": + message.s = reader.bytes(); + break; + case "t": + message.t = $root.onnx.TensorProto.decodeText(reader); + break; + case "g": + message.g = $root.onnx.GraphProto.decodeText(reader); + break; + case "sparse_tensor": + message.sparse_tensor = $root.onnx.SparseTensorProto.decodeText(reader); + break; + case "tp": + message.tp = $root.onnx.TypeProto.decodeText(reader); + break; + case "floats": + reader.array(message.floats, () => reader.float()); + break; + case "ints": + reader.array(message.ints, () => reader.int64()); + break; + case "strings": + reader.array(message.strings, () => reader.bytes()); + break; + case "tensors": + message.tensors.push($root.onnx.TensorProto.decodeText(reader)); + break; + case "graphs": + message.graphs.push($root.onnx.GraphProto.decodeText(reader)); + break; + case "sparse_tensors": + message.sparse_tensors.push($root.onnx.SparseTensorProto.decodeText(reader)); + break; + case "type_protos": + message.type_protos.push($root.onnx.TypeProto.decodeText(reader)); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.AttributeProto.prototype.name = ""; +$root.onnx.AttributeProto.prototype.ref_attr_name = ""; +$root.onnx.AttributeProto.prototype.doc_string = ""; +$root.onnx.AttributeProto.prototype.type = 0; +$root.onnx.AttributeProto.prototype.f = 0; +$root.onnx.AttributeProto.prototype.i = protobuf.Int64.create(0); +$root.onnx.AttributeProto.prototype.s = new Uint8Array([]); +$root.onnx.AttributeProto.prototype.t = null; +$root.onnx.AttributeProto.prototype.g = null; +$root.onnx.AttributeProto.prototype.sparse_tensor = null; +$root.onnx.AttributeProto.prototype.tp = null; + +$root.onnx.AttributeProto.AttributeType = { + "UNDEFINED": 0, + "FLOAT": 1, + "INT": 2, + "STRING": 3, + "TENSOR": 4, + "GRAPH": 5, + "SPARSE_TENSOR": 11, + "TYPE_PROTO": 13, + "FLOATS": 6, + "INTS": 7, + "STRINGS": 8, + "TENSORS": 9, + "GRAPHS": 10, + "SPARSE_TENSORS": 12, + "TYPE_PROTOS": 14 +}; + +$root.onnx.ValueInfoProto = class ValueInfoProto { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.ValueInfoProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = reader.string(); + break; + case 2: + message.type = $root.onnx.TypeProto.decode(reader, reader.uint32()); + break; + case 3: + message.doc_string = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.ValueInfoProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "name": + message.name = reader.string(); + break; + case "type": + message.type = $root.onnx.TypeProto.decodeText(reader); + break; + case "doc_string": + message.doc_string = reader.string(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.ValueInfoProto.prototype.name = ""; +$root.onnx.ValueInfoProto.prototype.type = null; +$root.onnx.ValueInfoProto.prototype.doc_string = ""; + +$root.onnx.NodeProto = class NodeProto { + + constructor() { + this.input = []; + this.output = []; + this.attribute = []; + } + + static decode(reader, length) { + const message = new $root.onnx.NodeProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.input.push(reader.string()); + break; + case 2: + message.output.push(reader.string()); + break; + case 3: + message.name = reader.string(); + break; + case 4: + message.op_type = reader.string(); + break; + case 7: + message.domain = reader.string(); + break; + case 5: + message.attribute.push($root.onnx.AttributeProto.decode(reader, reader.uint32())); + break; + case 6: + message.doc_string = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.NodeProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "input": + reader.array(message.input, () => reader.string()); + break; + case "output": + reader.array(message.output, () => reader.string()); + break; + case "name": + message.name = reader.string(); + break; + case "op_type": + message.op_type = reader.string(); + break; + case "domain": + message.domain = reader.string(); + break; + case "attribute": + message.attribute.push($root.onnx.AttributeProto.decodeText(reader)); + break; + case "doc_string": + message.doc_string = reader.string(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.NodeProto.prototype.name = ""; +$root.onnx.NodeProto.prototype.op_type = ""; +$root.onnx.NodeProto.prototype.domain = ""; +$root.onnx.NodeProto.prototype.doc_string = ""; + +$root.onnx.TrainingInfoProto = class TrainingInfoProto { + + constructor() { + this.initialization_binding = []; + this.update_binding = []; + } + + static decode(reader, length) { + const message = new $root.onnx.TrainingInfoProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.initialization = $root.onnx.GraphProto.decode(reader, reader.uint32()); + break; + case 2: + message.algorithm = $root.onnx.GraphProto.decode(reader, reader.uint32()); + break; + case 3: + message.initialization_binding.push($root.onnx.StringStringEntryProto.decode(reader, reader.uint32())); + break; + case 4: + message.update_binding.push($root.onnx.StringStringEntryProto.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TrainingInfoProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "initialization": + message.initialization = $root.onnx.GraphProto.decodeText(reader); + break; + case "algorithm": + message.algorithm = $root.onnx.GraphProto.decodeText(reader); + break; + case "initialization_binding": + message.initialization_binding.push($root.onnx.StringStringEntryProto.decodeText(reader)); + break; + case "update_binding": + message.update_binding.push($root.onnx.StringStringEntryProto.decodeText(reader)); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TrainingInfoProto.prototype.initialization = null; +$root.onnx.TrainingInfoProto.prototype.algorithm = null; + +$root.onnx.ModelProto = class ModelProto { + + constructor() { + this.opset_import = []; + this.metadata_props = []; + this.training_info = []; + this.functions = []; + } + + static decode(reader, length) { + const message = new $root.onnx.ModelProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.ir_version = reader.int64(); + break; + case 8: + message.opset_import.push($root.onnx.OperatorSetIdProto.decode(reader, reader.uint32())); + break; + case 2: + message.producer_name = reader.string(); + break; + case 3: + message.producer_version = reader.string(); + break; + case 4: + message.domain = reader.string(); + break; + case 5: + message.model_version = reader.int64(); + break; + case 6: + message.doc_string = reader.string(); + break; + case 7: + message.graph = $root.onnx.GraphProto.decode(reader, reader.uint32()); + break; + case 14: + message.metadata_props.push($root.onnx.StringStringEntryProto.decode(reader, reader.uint32())); + break; + case 20: + message.training_info.push($root.onnx.TrainingInfoProto.decode(reader, reader.uint32())); + break; + case 25: + message.functions.push($root.onnx.FunctionProto.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.ModelProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "ir_version": + message.ir_version = reader.int64(); + break; + case "opset_import": + message.opset_import.push($root.onnx.OperatorSetIdProto.decodeText(reader)); + break; + case "producer_name": + message.producer_name = reader.string(); + break; + case "producer_version": + message.producer_version = reader.string(); + break; + case "domain": + message.domain = reader.string(); + break; + case "model_version": + message.model_version = reader.int64(); + break; + case "doc_string": + message.doc_string = reader.string(); + break; + case "graph": + message.graph = $root.onnx.GraphProto.decodeText(reader); + break; + case "metadata_props": + message.metadata_props.push($root.onnx.StringStringEntryProto.decodeText(reader)); + break; + case "training_info": + message.training_info.push($root.onnx.TrainingInfoProto.decodeText(reader)); + break; + case "functions": + message.functions.push($root.onnx.FunctionProto.decodeText(reader)); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.ModelProto.prototype.ir_version = protobuf.Int64.create(0); +$root.onnx.ModelProto.prototype.producer_name = ""; +$root.onnx.ModelProto.prototype.producer_version = ""; +$root.onnx.ModelProto.prototype.domain = ""; +$root.onnx.ModelProto.prototype.model_version = protobuf.Int64.create(0); +$root.onnx.ModelProto.prototype.doc_string = ""; +$root.onnx.ModelProto.prototype.graph = null; + +$root.onnx.StringStringEntryProto = class StringStringEntryProto { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.StringStringEntryProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key = reader.string(); + break; + case 2: + message.value = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.StringStringEntryProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "key": + message.key = reader.string(); + break; + case "value": + message.value = reader.string(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.StringStringEntryProto.prototype.key = ""; +$root.onnx.StringStringEntryProto.prototype.value = ""; + +$root.onnx.TensorAnnotation = class TensorAnnotation { + + constructor() { + this.quant_parameter_tensor_names = []; + } + + static decode(reader, length) { + const message = new $root.onnx.TensorAnnotation(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.tensor_name = reader.string(); + break; + case 2: + message.quant_parameter_tensor_names.push($root.onnx.StringStringEntryProto.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TensorAnnotation(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "tensor_name": + message.tensor_name = reader.string(); + break; + case "quant_parameter_tensor_names": + message.quant_parameter_tensor_names.push($root.onnx.StringStringEntryProto.decodeText(reader)); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TensorAnnotation.prototype.tensor_name = ""; + +$root.onnx.GraphProto = class GraphProto { + + constructor() { + this.node = []; + this.initializer = []; + this.sparse_initializer = []; + this.input = []; + this.output = []; + this.value_info = []; + this.quantization_annotation = []; + } + + static decode(reader, length) { + const message = new $root.onnx.GraphProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.node.push($root.onnx.NodeProto.decode(reader, reader.uint32())); + break; + case 2: + message.name = reader.string(); + break; + case 5: + message.initializer.push($root.onnx.TensorProto.decode(reader, reader.uint32())); + break; + case 15: + message.sparse_initializer.push($root.onnx.SparseTensorProto.decode(reader, reader.uint32())); + break; + case 10: + message.doc_string = reader.string(); + break; + case 11: + message.input.push($root.onnx.ValueInfoProto.decode(reader, reader.uint32())); + break; + case 12: + message.output.push($root.onnx.ValueInfoProto.decode(reader, reader.uint32())); + break; + case 13: + message.value_info.push($root.onnx.ValueInfoProto.decode(reader, reader.uint32())); + break; + case 14: + message.quantization_annotation.push($root.onnx.TensorAnnotation.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.GraphProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "node": + message.node.push($root.onnx.NodeProto.decodeText(reader)); + break; + case "name": + message.name = reader.string(); + break; + case "initializer": + message.initializer.push($root.onnx.TensorProto.decodeText(reader)); + break; + case "sparse_initializer": + message.sparse_initializer.push($root.onnx.SparseTensorProto.decodeText(reader)); + break; + case "doc_string": + message.doc_string = reader.string(); + break; + case "input": + message.input.push($root.onnx.ValueInfoProto.decodeText(reader)); + break; + case "output": + message.output.push($root.onnx.ValueInfoProto.decodeText(reader)); + break; + case "value_info": + message.value_info.push($root.onnx.ValueInfoProto.decodeText(reader)); + break; + case "quantization_annotation": + message.quantization_annotation.push($root.onnx.TensorAnnotation.decodeText(reader)); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.GraphProto.prototype.name = ""; +$root.onnx.GraphProto.prototype.doc_string = ""; + +$root.onnx.TensorProto = class TensorProto { + + constructor() { + this.dims = []; + this.float_data = []; + this.int32_data = []; + this.string_data = []; + this.int64_data = []; + this.external_data = []; + this.double_data = []; + this.uint64_data = []; + } + + static decode(reader, length) { + const message = new $root.onnx.TensorProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.dims = reader.array(message.dims, () => reader.int64(), tag); + break; + case 2: + message.data_type = reader.int32(); + break; + case 3: + message.segment = $root.onnx.TensorProto.Segment.decode(reader, reader.uint32()); + break; + case 4: + message.float_data = reader.floats(message.float_data, tag); + break; + case 5: + message.int32_data = reader.array(message.int32_data, () => reader.int32(), tag); + break; + case 6: + message.string_data.push(reader.bytes()); + break; + case 7: + message.int64_data = reader.array(message.int64_data, () => reader.int64(), tag); + break; + case 8: + message.name = reader.string(); + break; + case 12: + message.doc_string = reader.string(); + break; + case 9: + message.raw_data = reader.bytes(); + break; + case 13: + message.external_data.push($root.onnx.StringStringEntryProto.decode(reader, reader.uint32())); + break; + case 14: + message.data_location = reader.int32(); + break; + case 10: + message.double_data = reader.doubles(message.double_data, tag); + break; + case 11: + message.uint64_data = reader.array(message.uint64_data, () => reader.uint64(), tag); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TensorProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "dims": + reader.array(message.dims, () => reader.int64()); + break; + case "data_type": + message.data_type = reader.int32(); + break; + case "segment": + message.segment = $root.onnx.TensorProto.Segment.decodeText(reader); + break; + case "float_data": + reader.array(message.float_data, () => reader.float()); + break; + case "int32_data": + reader.array(message.int32_data, () => reader.int32()); + break; + case "string_data": + reader.array(message.string_data, () => reader.bytes()); + break; + case "int64_data": + reader.array(message.int64_data, () => reader.int64()); + break; + case "name": + message.name = reader.string(); + break; + case "doc_string": + message.doc_string = reader.string(); + break; + case "raw_data": + message.raw_data = reader.bytes(); + break; + case "external_data": + message.external_data.push($root.onnx.StringStringEntryProto.decodeText(reader)); + break; + case "data_location": + message.data_location = reader.enum($root.onnx.TensorProto.DataLocation); + break; + case "double_data": + reader.array(message.double_data, () => reader.double()); + break; + case "uint64_data": + reader.array(message.uint64_data, () => reader.uint64()); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TensorProto.prototype.data_type = 0; +$root.onnx.TensorProto.prototype.segment = null; +$root.onnx.TensorProto.prototype.name = ""; +$root.onnx.TensorProto.prototype.doc_string = ""; +$root.onnx.TensorProto.prototype.raw_data = new Uint8Array([]); +$root.onnx.TensorProto.prototype.data_location = 0; + +$root.onnx.TensorProto.DataType = { + "UNDEFINED": 0, + "FLOAT": 1, + "UINT8": 2, + "INT8": 3, + "UINT16": 4, + "INT16": 5, + "INT32": 6, + "INT64": 7, + "STRING": 8, + "BOOL": 9, + "FLOAT16": 10, + "DOUBLE": 11, + "UINT32": 12, + "UINT64": 13, + "COMPLEX64": 14, + "COMPLEX128": 15, + "BFLOAT16": 16 +}; + +$root.onnx.TensorProto.Segment = class Segment { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.TensorProto.Segment(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.begin = reader.int64(); + break; + case 2: + message.end = reader.int64(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TensorProto.Segment(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "begin": + message.begin = reader.int64(); + break; + case "end": + message.end = reader.int64(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TensorProto.Segment.prototype.begin = protobuf.Int64.create(0); +$root.onnx.TensorProto.Segment.prototype.end = protobuf.Int64.create(0); + +$root.onnx.TensorProto.DataLocation = { + "DEFAULT": 0, + "EXTERNAL": 1 +}; + +$root.onnx.SparseTensorProto = class SparseTensorProto { + + constructor() { + this.dims = []; + } + + static decode(reader, length) { + const message = new $root.onnx.SparseTensorProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.values = $root.onnx.TensorProto.decode(reader, reader.uint32()); + break; + case 2: + message.indices = $root.onnx.TensorProto.decode(reader, reader.uint32()); + break; + case 3: + message.dims = reader.array(message.dims, () => reader.int64(), tag); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.SparseTensorProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "values": + message.values = $root.onnx.TensorProto.decodeText(reader); + break; + case "indices": + message.indices = $root.onnx.TensorProto.decodeText(reader); + break; + case "dims": + reader.array(message.dims, () => reader.int64()); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.SparseTensorProto.prototype.values = null; +$root.onnx.SparseTensorProto.prototype.indices = null; + +$root.onnx.TensorShapeProto = class TensorShapeProto { + + constructor() { + this.dim = []; + } + + static decode(reader, length) { + const message = new $root.onnx.TensorShapeProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.dim.push($root.onnx.TensorShapeProto.Dimension.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TensorShapeProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "dim": + message.dim.push($root.onnx.TensorShapeProto.Dimension.decodeText(reader)); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TensorShapeProto.Dimension = class Dimension { + + constructor() { + } + + get value() { + $root.onnx.TensorShapeProto.Dimension.valueSet = $root.onnx.TensorShapeProto.Dimension.valueSet || new Set([ "dim_value", "dim_param"]); + return Object.keys(this).find((key) => $root.onnx.TensorShapeProto.Dimension.valueSet.has(key) && this[key] != null); + } + + static decode(reader, length) { + const message = new $root.onnx.TensorShapeProto.Dimension(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.dim_value = reader.int64(); + break; + case 2: + message.dim_param = reader.string(); + break; + case 3: + message.denotation = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TensorShapeProto.Dimension(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "dim_value": + message.dim_value = reader.int64(); + break; + case "dim_param": + message.dim_param = reader.string(); + break; + case "denotation": + message.denotation = reader.string(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TensorShapeProto.Dimension.prototype.denotation = ""; + +$root.onnx.TypeProto = class TypeProto { + + constructor() { + } + + get value() { + $root.onnx.TypeProto.valueSet = $root.onnx.TypeProto.valueSet || new Set([ "tensor_type", "sequence_type", "map_type", "optional_type", "sparse_tensor_type", "opaque_type"]); + return Object.keys(this).find((key) => $root.onnx.TypeProto.valueSet.has(key) && this[key] != null); + } + + static decode(reader, length) { + const message = new $root.onnx.TypeProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.tensor_type = $root.onnx.TypeProto.Tensor.decode(reader, reader.uint32()); + break; + case 4: + message.sequence_type = $root.onnx.TypeProto.Sequence.decode(reader, reader.uint32()); + break; + case 5: + message.map_type = $root.onnx.TypeProto.Map.decode(reader, reader.uint32()); + break; + case 9: + message.optional_type = $root.onnx.TypeProto.Optional.decode(reader, reader.uint32()); + break; + case 8: + message.sparse_tensor_type = $root.onnx.TypeProto.SparseTensor.decode(reader, reader.uint32()); + break; + case 7: + message.opaque_type = $root.onnx.TypeProto.Opaque.decode(reader, reader.uint32()); + break; + case 6: + message.denotation = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TypeProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "tensor_type": + message.tensor_type = $root.onnx.TypeProto.Tensor.decodeText(reader); + break; + case "sequence_type": + message.sequence_type = $root.onnx.TypeProto.Sequence.decodeText(reader); + break; + case "map_type": + message.map_type = $root.onnx.TypeProto.Map.decodeText(reader); + break; + case "optional_type": + message.optional_type = $root.onnx.TypeProto.Optional.decodeText(reader); + break; + case "sparse_tensor_type": + message.sparse_tensor_type = $root.onnx.TypeProto.SparseTensor.decodeText(reader); + break; + case "opaque_type": + message.opaque_type = $root.onnx.TypeProto.Opaque.decodeText(reader); + break; + case "denotation": + message.denotation = reader.string(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TypeProto.prototype.denotation = ""; + +$root.onnx.TypeProto.Tensor = class Tensor { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.TypeProto.Tensor(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.elem_type = reader.int32(); + break; + case 2: + message.shape = $root.onnx.TensorShapeProto.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TypeProto.Tensor(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "elem_type": + message.elem_type = reader.int32(); + break; + case "shape": + message.shape = $root.onnx.TensorShapeProto.decodeText(reader); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TypeProto.Tensor.prototype.elem_type = 0; +$root.onnx.TypeProto.Tensor.prototype.shape = null; + +$root.onnx.TypeProto.Sequence = class Sequence { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.TypeProto.Sequence(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.elem_type = $root.onnx.TypeProto.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TypeProto.Sequence(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "elem_type": + message.elem_type = $root.onnx.TypeProto.decodeText(reader); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TypeProto.Sequence.prototype.elem_type = null; + +$root.onnx.TypeProto.Map = class Map { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.TypeProto.Map(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.key_type = reader.int32(); + break; + case 2: + message.value_type = $root.onnx.TypeProto.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TypeProto.Map(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "key_type": + message.key_type = reader.int32(); + break; + case "value_type": + message.value_type = $root.onnx.TypeProto.decodeText(reader); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TypeProto.Map.prototype.key_type = 0; +$root.onnx.TypeProto.Map.prototype.value_type = null; + +$root.onnx.TypeProto.Optional = class Optional { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.TypeProto.Optional(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.elem_type = $root.onnx.TypeProto.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TypeProto.Optional(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "elem_type": + message.elem_type = $root.onnx.TypeProto.decodeText(reader); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TypeProto.Optional.prototype.elem_type = null; + +$root.onnx.TypeProto.SparseTensor = class SparseTensor { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.TypeProto.SparseTensor(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.elem_type = reader.int32(); + break; + case 2: + message.shape = $root.onnx.TensorShapeProto.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TypeProto.SparseTensor(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "elem_type": + message.elem_type = reader.int32(); + break; + case "shape": + message.shape = $root.onnx.TensorShapeProto.decodeText(reader); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TypeProto.SparseTensor.prototype.elem_type = 0; +$root.onnx.TypeProto.SparseTensor.prototype.shape = null; + +$root.onnx.TypeProto.Opaque = class Opaque { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.TypeProto.Opaque(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.domain = reader.string(); + break; + case 2: + message.name = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.TypeProto.Opaque(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "domain": + message.domain = reader.string(); + break; + case "name": + message.name = reader.string(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.TypeProto.Opaque.prototype.domain = ""; +$root.onnx.TypeProto.Opaque.prototype.name = ""; + +$root.onnx.OperatorSetIdProto = class OperatorSetIdProto { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.OperatorSetIdProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.domain = reader.string(); + break; + case 2: + message.version = reader.int64(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.OperatorSetIdProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "domain": + message.domain = reader.string(); + break; + case "version": + message.version = reader.int64(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.OperatorSetIdProto.prototype.domain = ""; +$root.onnx.OperatorSetIdProto.prototype.version = protobuf.Int64.create(0); + +$root.onnx.OperatorStatus = { + "EXPERIMENTAL": 0, + "STABLE": 1 +}; + +$root.onnx.FunctionProto = class FunctionProto { + + constructor() { + this.input = []; + this.output = []; + this.attribute = []; + this.node = []; + this.opset_import = []; + } + + static decode(reader, length) { + const message = new $root.onnx.FunctionProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.name = reader.string(); + break; + case 4: + message.input.push(reader.string()); + break; + case 5: + message.output.push(reader.string()); + break; + case 6: + message.attribute.push(reader.string()); + break; + case 7: + message.node.push($root.onnx.NodeProto.decode(reader, reader.uint32())); + break; + case 8: + message.doc_string = reader.string(); + break; + case 9: + message.opset_import.push($root.onnx.OperatorSetIdProto.decode(reader, reader.uint32())); + break; + case 10: + message.domain = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.FunctionProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "name": + message.name = reader.string(); + break; + case "input": + reader.array(message.input, () => reader.string()); + break; + case "output": + reader.array(message.output, () => reader.string()); + break; + case "attribute": + reader.array(message.attribute, () => reader.string()); + break; + case "node": + message.node.push($root.onnx.NodeProto.decodeText(reader)); + break; + case "doc_string": + message.doc_string = reader.string(); + break; + case "opset_import": + message.opset_import.push($root.onnx.OperatorSetIdProto.decodeText(reader)); + break; + case "domain": + message.domain = reader.string(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.FunctionProto.prototype.name = ""; +$root.onnx.FunctionProto.prototype.doc_string = ""; +$root.onnx.FunctionProto.prototype.domain = ""; + +$root.onnx.OperatorProto = class OperatorProto { + + constructor() { + } + + static decode(reader, length) { + const message = new $root.onnx.OperatorProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.op_type = reader.string(); + break; + case 2: + message.since_version = reader.int64(); + break; + case 3: + message.status = reader.int32(); + break; + case 10: + message.doc_string = reader.string(); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.OperatorProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "op_type": + message.op_type = reader.string(); + break; + case "since_version": + message.since_version = reader.int64(); + break; + case "status": + message.status = reader.enum($root.onnx.OperatorStatus); + break; + case "doc_string": + message.doc_string = reader.string(); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.OperatorProto.prototype.op_type = ""; +$root.onnx.OperatorProto.prototype.since_version = protobuf.Int64.create(0); +$root.onnx.OperatorProto.prototype.status = 0; +$root.onnx.OperatorProto.prototype.doc_string = ""; + +$root.onnx.OperatorSetProto = class OperatorSetProto { + + constructor() { + this.operator = []; + this.functions = []; + } + + static decode(reader, length) { + const message = new $root.onnx.OperatorSetProto(); + const end = length !== undefined ? reader.position + length : reader.length; + while (reader.position < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + message.magic = reader.string(); + break; + case 2: + message.ir_version = reader.int64(); + break; + case 3: + message.ir_version_prerelease = reader.string(); + break; + case 7: + message.ir_build_metadata = reader.string(); + break; + case 4: + message.domain = reader.string(); + break; + case 5: + message.opset_version = reader.int64(); + break; + case 6: + message.doc_string = reader.string(); + break; + case 8: + message.operator.push($root.onnx.OperatorProto.decode(reader, reader.uint32())); + break; + case 9: + message.functions.push($root.onnx.FunctionProto.decode(reader, reader.uint32())); + break; + default: + reader.skipType(tag & 7); + break; + } + } + return message; + } + + static decodeText(reader) { + const message = new $root.onnx.OperatorSetProto(); + reader.start(); + while (!reader.end()) { + const tag = reader.tag(); + switch (tag) { + case "magic": + message.magic = reader.string(); + break; + case "ir_version": + message.ir_version = reader.int64(); + break; + case "ir_version_prerelease": + message.ir_version_prerelease = reader.string(); + break; + case "ir_build_metadata": + message.ir_build_metadata = reader.string(); + break; + case "domain": + message.domain = reader.string(); + break; + case "opset_version": + message.opset_version = reader.int64(); + break; + case "doc_string": + message.doc_string = reader.string(); + break; + case "operator": + message.operator.push($root.onnx.OperatorProto.decodeText(reader)); + break; + case "functions": + message.functions.push($root.onnx.FunctionProto.decodeText(reader)); + break; + default: + reader.field(tag, message); + break; + } + } + return message; + } +}; + +$root.onnx.OperatorSetProto.prototype.magic = ""; +$root.onnx.OperatorSetProto.prototype.ir_version = protobuf.Int64.create(0); +$root.onnx.OperatorSetProto.prototype.ir_version_prerelease = ""; +$root.onnx.OperatorSetProto.prototype.ir_build_metadata = ""; +$root.onnx.OperatorSetProto.prototype.domain = ""; +$root.onnx.OperatorSetProto.prototype.opset_version = protobuf.Int64.create(0); +$root.onnx.OperatorSetProto.prototype.doc_string = ""; diff --git a/onnx-schema.js b/onnx-schema.js new file mode 100644 index 0000000..17146e8 --- /dev/null +++ b/onnx-schema.js @@ -0,0 +1,401 @@ +var $root = flatbuffers.get('ort'); + +$root.onnxruntime = $root.onnxruntime || {}; + +$root.onnxruntime.fbs = $root.onnxruntime.fbs || {}; + +$root.onnxruntime.fbs.AttributeType = { + UNDEFINED: 0, + FLOAT: 1, + INT: 2, + STRING: 3, + TENSOR: 4, + GRAPH: 5, + FLOATS: 6, + INTS: 7, + STRINGS: 8, + TENSORS: 9, + GRAPHS: 10, + SPARSE_TENSOR: 11, + SPARSE_TENSORS: 12 +}; + +$root.onnxruntime.fbs.Shape = class Shape { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.Shape(); + $.dim = reader.tableArray(position, 4, $root.onnxruntime.fbs.Dimension.decode); + return $; + } +}; + +$root.onnxruntime.fbs.Dimension = class Dimension { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.Dimension(); + $.value = reader.table(position, 4, $root.onnxruntime.fbs.DimensionValue.decode); + $.denotation = reader.string_(position, 6, null); + return $; + } +}; + +$root.onnxruntime.fbs.DimensionValueType = { + UNKNOWN: 0, + VALUE: 1, + PARAM: 2 +}; + +$root.onnxruntime.fbs.DimensionValue = class DimensionValue { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.DimensionValue(); + $.dim_type = reader.int8_(position, 4, 0); + $.dim_value = reader.int64_(position, 6, 0); + $.dim_param = reader.string_(position, 8, null); + return $; + } +}; + +$root.onnxruntime.fbs.TensorDataType = { + UNDEFINED: 0, + FLOAT: 1, + UINT8: 2, + INT8: 3, + UINT16: 4, + INT16: 5, + INT32: 6, + INT64: 7, + STRING: 8, + BOOL: 9, + FLOAT16: 10, + DOUBLE: 11, + UINT32: 12, + UINT64: 13, + COMPLEX64: 14, + COMPLEX128: 15, + BFLOAT16: 16 +}; + +$root.onnxruntime.fbs.TensorTypeAndShape = class TensorTypeAndShape { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.TensorTypeAndShape(); + $.elem_type = reader.int32_(position, 4, 0); + $.shape = reader.table(position, 6, $root.onnxruntime.fbs.Shape.decode); + return $; + } +}; + +$root.onnxruntime.fbs.MapType = class MapType { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.MapType(); + $.key_type = reader.int32_(position, 4, 0); + $.value_type = reader.table(position, 6, $root.onnxruntime.fbs.TypeInfo.decode); + return $; + } +}; + +$root.onnxruntime.fbs.SequenceType = class SequenceType { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.SequenceType(); + $.elem_type = reader.table(position, 4, $root.onnxruntime.fbs.TypeInfo.decode); + return $; + } +}; + +$root.onnxruntime.fbs.NodeType = { + Primitive: 0, + Fused: 1 +}; + +$root.onnxruntime.fbs.EdgeEnd = class EdgeEnd { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.EdgeEnd(); + $.node_index = reader.uint32(position + 0); + $.src_arg_index = reader.int32(position + 4); + $.dst_arg_index = reader.int32(position + 8); + return $; + } +}; + +$root.onnxruntime.fbs.NodeEdge = class NodeEdge { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.NodeEdge(); + $.node_index = reader.uint32_(position, 4, 0); + $.input_edges = reader.structArray(position, 6, undefined,$root.onnxruntime.fbs.EdgeEnd.decode); + $.output_edges = reader.structArray(position, 8, undefined,$root.onnxruntime.fbs.EdgeEnd.decode); + return $; + } +}; + +$root.onnxruntime.fbs.Node = class Node { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.Node(); + $.name = reader.string_(position, 4, null); + $.doc_string = reader.string_(position, 6, null); + $.domain = reader.string_(position, 8, null); + $.since_version = reader.int32_(position, 10, 0); + $.index = reader.uint32_(position, 12, 0); + $.op_type = reader.string_(position, 14, null); + $.type = reader.int32_(position, 16, 0); + $.execution_provider_type = reader.string_(position, 18, null); + $.inputs = reader.strings_(position, 20); + $.outputs = reader.strings_(position, 22); + $.attributes = reader.tableArray(position, 24, $root.onnxruntime.fbs.Attribute.decode); + $.input_arg_counts = reader.typedArray(position, 26, Int32Array); + $.implicit_inputs = reader.strings_(position, 28); + return $; + } +}; + +$root.onnxruntime.fbs.ValueInfo = class ValueInfo { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.ValueInfo(); + $.name = reader.string_(position, 4, null); + $.doc_string = reader.string_(position, 6, null); + $.type = reader.table(position, 8, $root.onnxruntime.fbs.TypeInfo.decode); + return $; + } +}; + +$root.onnxruntime.fbs.TypeInfoValue = class { + + static decode(reader, position, type) { + switch (type) { + case 1: return $root.onnxruntime.fbs.TensorTypeAndShape.decode(reader, position); + case 2: return $root.onnxruntime.fbs.SequenceType.decode(reader, position); + case 3: return $root.onnxruntime.fbs.MapType.decode(reader, position); + } + return undefined; + } + + static decodeText(reader, json, type) { + switch (type) { + case 'TensorTypeAndShape': return $root.onnxruntime.fbs.TensorTypeAndShape.decodeText(reader, json); + case 'SequenceType': return $root.onnxruntime.fbs.SequenceType.decodeText(reader, json); + case 'MapType': return $root.onnxruntime.fbs.MapType.decodeText(reader, json); + } + return undefined; + } +}; + +$root.onnxruntime.fbs.TypeInfo = class TypeInfo { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.TypeInfo(); + $.denotation = reader.string_(position, 4, null); + $.value = reader.union(position, 6, $root.onnxruntime.fbs.TypeInfoValue.decode); + return $; + } +}; + +$root.onnxruntime.fbs.OperatorSetId = class OperatorSetId { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.OperatorSetId(); + $.domain = reader.string_(position, 4, null); + $.version = reader.int64_(position, 6, 0); + return $; + } +}; + +$root.onnxruntime.fbs.Tensor = class Tensor { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.Tensor(); + $.name = reader.string_(position, 4, null); + $.doc_string = reader.string_(position, 6, null); + $.dims = reader.int64s_(position, 8); + $.data_type = reader.int32_(position, 10, 0); + $.raw_data = reader.typedArray(position, 12, Uint8Array); + $.string_data = reader.strings_(position, 14); + return $; + } +}; + +$root.onnxruntime.fbs.SparseTensor = class SparseTensor { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.SparseTensor(); + $.values = reader.table(position, 4, $root.onnxruntime.fbs.Tensor.decode); + $.indices = reader.table(position, 6, $root.onnxruntime.fbs.Tensor.decode); + $.dims = reader.int64s_(position, 8); + return $; + } +}; + +$root.onnxruntime.fbs.Attribute = class Attribute { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.Attribute(); + $.name = reader.string_(position, 4, null); + $.doc_string = reader.string_(position, 6, null); + $.type = reader.int32_(position, 8, 0); + $.f = reader.float32_(position, 10, 0); + $.i = reader.int64_(position, 12, 0); + $.s = reader.string_(position, 14, null); + $.t = reader.table(position, 16, $root.onnxruntime.fbs.Tensor.decode); + $.g = reader.table(position, 18, $root.onnxruntime.fbs.Graph.decode); + $.floats = reader.typedArray(position, 20, Float32Array); + $.ints = reader.int64s_(position, 22); + $.strings = reader.strings_(position, 24); + $.tensors = reader.tableArray(position, 26, $root.onnxruntime.fbs.Tensor.decode); + $.graphs = reader.tableArray(position, 28, $root.onnxruntime.fbs.Graph.decode); + return $; + } +}; + +$root.onnxruntime.fbs.NodesToOptimizeIndices = class NodesToOptimizeIndices { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.NodesToOptimizeIndices(); + $.node_indices = reader.typedArray(position, 4, Uint32Array); + $.num_inputs = reader.uint32_(position, 6, 0); + $.num_outputs = reader.uint32_(position, 8, 0); + $.has_variadic_input = reader.bool_(position, 10, false); + $.has_variadic_output = reader.bool_(position, 12, false); + $.num_variadic_inputs = reader.uint32_(position, 14, 0); + $.num_variadic_outputs = reader.uint32_(position, 16, 0); + return $; + } +}; + +$root.onnxruntime.fbs.NodeIndexAndKernelDefHash = class NodeIndexAndKernelDefHash { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.NodeIndexAndKernelDefHash(); + $.node_index = reader.uint32_(position, 4, 0); + $.kernel_def_hash = reader.uint64_(position, 6, 0); + return $; + } +}; + +$root.onnxruntime.fbs.RuntimeOptimizationRecord = class RuntimeOptimizationRecord { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.RuntimeOptimizationRecord(); + $.action_id = reader.string_(position, 4, null); + $.nodes_to_optimize_indices = reader.table(position, 6, $root.onnxruntime.fbs.NodesToOptimizeIndices.decode); + $.produced_nodes = reader.tableArray(position, 8, $root.onnxruntime.fbs.NodeIndexAndKernelDefHash.decode); + return $; + } +}; + +$root.onnxruntime.fbs.RuntimeOptimizationRecordContainerEntry = class RuntimeOptimizationRecordContainerEntry { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.RuntimeOptimizationRecordContainerEntry(); + $.optimizer_name = reader.string_(position, 4, null); + $.runtime_optimization_records = reader.tableArray(position, 6, $root.onnxruntime.fbs.RuntimeOptimizationRecord.decode); + return $; + } +}; + +$root.onnxruntime.fbs.RuntimeOptimizations = class RuntimeOptimizations { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.RuntimeOptimizations(); + $.records = reader.tableArray(position, 4, $root.onnxruntime.fbs.RuntimeOptimizationRecordContainerEntry.decode); + return $; + } +}; + +$root.onnxruntime.fbs.Graph = class Graph { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.Graph(); + $.initializers = reader.tableArray(position, 4, $root.onnxruntime.fbs.Tensor.decode); + $.node_args = reader.tableArray(position, 6, $root.onnxruntime.fbs.ValueInfo.decode); + $.nodes = reader.tableArray(position, 8, $root.onnxruntime.fbs.Node.decode); + $.max_node_index = reader.uint32_(position, 10, 0); + $.node_edges = reader.tableArray(position, 12, $root.onnxruntime.fbs.NodeEdge.decode); + $.inputs = reader.strings_(position, 14); + $.outputs = reader.strings_(position, 16); + $.sparse_initializers = reader.tableArray(position, 18, $root.onnxruntime.fbs.SparseTensor.decode); + $.runtime_optimizations = reader.table(position, 20, $root.onnxruntime.fbs.RuntimeOptimizations.decode); + return $; + } +}; + +$root.onnxruntime.fbs.StringStringEntry = class StringStringEntry { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.StringStringEntry(); + $.key = reader.string_(position, 4, null); + $.value = reader.string_(position, 6, null); + return $; + } +}; + +$root.onnxruntime.fbs.Model = class Model { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.Model(); + $.ir_version = reader.int64_(position, 4, 0); + $.opset_import = reader.tableArray(position, 6, $root.onnxruntime.fbs.OperatorSetId.decode); + $.producer_name = reader.string_(position, 8, null); + $.producer_version = reader.string_(position, 10, null); + $.domain = reader.string_(position, 12, null); + $.model_version = reader.int64_(position, 14, 0); + $.doc_string = reader.string_(position, 16, null); + $.graph = reader.table(position, 18, $root.onnxruntime.fbs.Graph.decode); + $.graph_doc_string = reader.string_(position, 20, null); + $.metadata_props = reader.tableArray(position, 22, $root.onnxruntime.fbs.StringStringEntry.decode); + return $; + } +}; + +$root.onnxruntime.fbs.KernelCreateInfos = class KernelCreateInfos { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.KernelCreateInfos(); + $.node_indices = reader.typedArray(position, 4, Uint32Array); + $.kernel_def_hashes = reader.uint64s_(position, 6); + return $; + } +}; + +$root.onnxruntime.fbs.SubGraphSessionState = class SubGraphSessionState { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.SubGraphSessionState(); + $.graph_id = reader.string_(position, 4, null); + $.session_state = reader.table(position, 6, $root.onnxruntime.fbs.SessionState.decode); + return $; + } +}; + +$root.onnxruntime.fbs.SessionState = class SessionState { + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.SessionState(); + $.kernels = reader.table(position, 4, $root.onnxruntime.fbs.KernelCreateInfos.decode); + $.sub_graph_session_states = reader.tableArray(position, 6, $root.onnxruntime.fbs.SubGraphSessionState.decode); + return $; + } +}; + +$root.onnxruntime.fbs.InferenceSession = class InferenceSession { + + static identifier(reader) { + return reader.identifier === 'ORTM'; + } + + static create(reader) { + return $root.onnxruntime.fbs.InferenceSession.decode(reader, reader.root); + } + + static decode(reader, position) { + const $ = new $root.onnxruntime.fbs.InferenceSession(); + $.ort_version = reader.string_(position, 4, null); + $.model = reader.table(position, 6, $root.onnxruntime.fbs.Model.decode); + $.session_state = reader.table(position, 8, $root.onnxruntime.fbs.SessionState.decode); + return $; + } +}; diff --git a/onnx.js b/onnx.js new file mode 100644 index 0000000..133f315 --- /dev/null +++ b/onnx.js @@ -0,0 +1,2490 @@ + +var onnx = onnx || {}; +var protobuf = protobuf || require('./protobuf'); +var flatbuffers = flatbuffers || require('./flatbuffers'); +var text = text || require('./text'); + +onnx.ModelFactory = class { + + match(context) { + const identifier = context.identifier; + const extension = identifier.split('.').pop().toLowerCase(); + if (identifier.endsWith('saved_model.pb') || identifier.endsWith('predict_net.pb') || identifier.endsWith('init_net.pb')) { + return undefined; + } + if (identifier.endsWith('predict_net.pbtxt') || identifier.endsWith('predict_net.prototxt') || + identifier.endsWith('init_net.pbtxt') || identifier.endsWith('init_net.prototxt')) { + return undefined; + } + let tags = context.tags('pb'); + if (tags.size > 0) { + if (tags.size === 1 && tags.get(1) === 2) { + const tags = context.tags('pb+'); + const match = (tags, schema) => { + for (const pair of schema) { + const key = pair[0]; + const inner = pair[1]; + const value = tags[key]; + if (value === undefined) { + continue; + } + if (inner === false) { + return false; + } + if (Array.isArray(inner)) { + if (typeof value !== 'object' || !match(value, inner)) { + return false; + } + } + else if (inner !== value) { + if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) { + return true; + } + return false; + } + } + return true; + }; + // mediapipe.BoxDetectorIndex + if (match(tags, [[1,[[1,[[1,[[1,5],[2,5],[3,5],[4,5],[6,0],[7,5],[8,5],[10,5],[11,0],[12,0]]],[2,5],[3,[]]]],[2,false],[3,false],[4,false],[5,false]]],[2,false],[3,false]] )) { + return undefined; + } + // third_party.tensorflow.python.keras.protobuf.SavedMetadata + if (match(tags, [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]])) { + return undefined; + } + } + if (Array.from(tags.keys()).every((tag) => tag <= 100) && + Array.from(tags.values()).every((type) => type < 5)) { + // TensorProto + if (tags.get(1) === 0 && tags.get(2) === 0) { + const schema = [[1,0],[2,0],[4,2],[5,2],[7,2],[8,2],[9,2]]; + if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) { + return 'onnx.pb.TensorProto'; + } + } + // GraphProto + if (tags.get(1) === 2) { + const schema = [[1,2],[2,2],[3,2],[4,2],[5,2],[6,0],[7,0],[8,2],[9,2],[10,2],[11,2],[12,2],[13,2],[14,2]]; + if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) { + const decode = (buffer, value) => { + const reader = protobuf.BinaryReader.open(buffer); + const length = reader.length; + while (reader.position < length) { + const tag = reader.uint32(); + const number = tag >>> 3; + const type = tag & 7; + if (value === number) { + return type === 2 ? reader.bytes() : null; + } + else { + reader.skipType(type); + } + } + return null; + }; + const stream = context.stream; + const buffer = stream.peek(); + const nodeBuffer = decode(buffer, 1); + if (nodeBuffer) { + const nameBuffer = decode(nodeBuffer, 4); + if (nameBuffer && nameBuffer.every((c) => c > 0x20 && c < 0x7f)) { + return 'onnx.pb.GraphProto'; + } + } + } + } + // ModelProto + if (tags.get(7) === 2) { + const schema = [[1,0],[2,2],[3,2],[4,2][5,0],[6,2],[7,2],[8,2],[14,2],[20,2]]; + if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) { + return 'onnx.pb.ModelProto'; + } + } + } + } + const stream = context.stream; + if (stream.length > 5) { + const buffer = stream.peek(Math.min(stream.length, 32)); + if (buffer[0] === 0x08 && buffer[1] < 0x0A && buffer[2] === 0x12) { + const producers = [ + 'backend-test', 'BrainwaveCompiler', + 'CNTK', + 'keras2onnx', 'Kneron', 'kneron_formatter', 'kneron_kl530_test_case', + 'darknet to ONNX example', + 'htshinichi', + 'MATLAB Deep Learning Toolbox Converter for ONNX Model Format', 'ML.NET', 'MVTec Software', + 'onnx-caffe2', 'onnx-example', 'onnx.quantize', 'onnx.utils.extract_model', 'OnnxMLTools', 'onnx_test', 'onnxruntime-tools', 'onnxruntime.transformers', + 'PaddlePaddle', 'pytorch', + 'sclblonnx', 'skl2onnx', + 'Tencent YouTu', 'tf2onnx', 'tflite2onnx', + 'WinMLTools' + ]; + if (producers.some((producer) => Array.from(producer).every((ch, index) => index + 4 < buffer.length && ch.charCodeAt(0) === buffer[index + 4]))) { + return 'onnx.pb.ModelProto'; + } + } + } + if (onnx.Text.Reader.open(stream)) { + return 'onnx.text'; + } + if (onnx.Runtime.Reader.open(stream, extension)) { + return 'onnx.flatbuffers'; + } + tags = context.tags('pbtxt'); + if (tags.has('ir_version')) { + return 'onnx.pbtxt.ModelProto'; + } + if (tags.has('graph') && extension !== 'model') { + return 'onnx.pbtxt.ModelProto'; + } + return undefined; + } + + open(context, match) { + const open = (model, format) => { + return onnx.Metadata.open(context).then((metadata) => { + return new onnx.Model(metadata, model, format); + }); + }; + switch (match) { + case 'onnx.pbtxt.ModelProto': + return context.require('./onnx-proto').then(() => { + try { + onnx.proto = protobuf.get('onnx').onnx; + const stream = context.stream; + const reader = protobuf.TextReader.open(stream); + const model = onnx.proto.ModelProto.decodeText(reader); + const format = 'ONNX' + (model.ir_version ? ' v' + model.ir_version.toString() : ''); + return open(model, format); + } + catch (error) { + const message = error && error.message ? error.message : error.toString(); + throw new onnx.Error('File text format is not onnx.ModelProto (' + message.replace(/\.$/, '') + ').'); + } + }); + case 'onnx.pb.TensorProto': + return context.require('./onnx-proto').then(() => { + // TensorProto + // input_0.pb, output_0.pb + try { + onnx.proto = protobuf.get('onnx').onnx; + const stream = context.stream; + const reader = protobuf.BinaryReader.open(stream); + const tensor = onnx.proto.TensorProto.decode(reader); + tensor.name = tensor.name || context.identifier; + const model = new onnx.proto.ModelProto(); + model.graph = new onnx.proto.GraphProto(); + model.graph.initializer = [ tensor ]; + model.graph.value_info = [ new onnx.proto.ValueInfoProto() ]; + model.graph.value_info[0].name = tensor.name; + model.graph.node = [ new onnx.proto.NodeProto() ]; + model.graph.node[0].op_type = 'Constant'; + model.graph.node[0].attribute = [ new onnx.proto.AttributeProto() ]; + model.graph.node[0].attribute[0].name = 'value'; + model.graph.node[0].attribute[0].type = onnx.AttributeType.TENSOR; + model.graph.node[0].attribute[0].t = tensor; + const format = 'ONNX Tensor'; + return open(model, format); + } + catch (error) { + const message = error && error.message ? error.message : error.toString(); + throw new onnx.Error('File format is not onnx.TensorProto (' + message.replace(/\.$/, '') + ').'); + } + }); + case 'onnx.pb.GraphProto': + return context.require('./onnx-proto').then(() => { + // GraphProto + try { + onnx.proto = protobuf.get('onnx').onnx; + const stream = context.stream; + const reader = protobuf.BinaryReader.open(stream); + const model = new onnx.proto.ModelProto(); + model.graph = onnx.proto.GraphProto.decode(reader); + const format = 'ONNX'; + return open(model, format); + } + catch (error) { + const message = error && error.message ? error.message : error.toString(); + throw new onnx.Error('File format is not onnx.GraphProto (' + message.replace(/\.$/, '') + ').'); + } + }); + case 'onnx.pb.ModelProto': + return context.require('./onnx-proto').then(() => { + // ModelProto + try { + onnx.proto = protobuf.get('onnx').onnx; + const stream = context.stream; + const reader = protobuf.BinaryReader.open(stream); + const model = onnx.proto.ModelProto.decode(reader); + const format = 'ONNX' + (model.ir_version ? ' v' + model.ir_version.toString() : ''); + // console.log(format) // ONNX v7 + return open(model, format); + } + catch (error) { + const message = error && error.message ? error.message : error.toString(); + throw new onnx.Error('File format is not onnx.ModelProto (' + message.replace(/\.$/, '') + ').'); + } + }); + case 'onnx.flatbuffers': { + return context.require('./onnx-schema').then((/* schema */) => { + try { + onnx.schema = flatbuffers.get('ort').onnxruntime.fbs; + const stream = context.stream; + const reader = onnx.Runtime.Reader.open(stream, 'ort'); + const model = reader.read(); + const format = 'ONNX Runtime' + (model.ir_version ? ' v' + model.ir_version.toString() : ''); + return open(model, format); + } + catch (error) { + const message = error && error.message ? error.message : error.toString(); + throw new onnx.Error('File format is not ort.Model (' + message.replace(/\.$/, '') + ').'); + } + }); + } + case 'onnx.text': { + return context.require('./onnx-proto').then(() => { + try { + onnx.proto = protobuf.get('onnx').onnx; + const stream = context.stream; + const reader = onnx.Text.Reader.open(stream); + const model = reader.read(); + const format = 'ONNX Text' + (model.ir_version ? ' v' + model.ir_version.toString() : ''); + return open(model, format); + } + catch (error) { + const message = error && error.message ? error.message : error.toString(); + throw new onnx.Error('File format is not onnx.ModelProto (' + message.replace(/\.$/, '') + ').'); + } + }); + } + default: { + throw new onnx.Error("Unknown ONNX format '" + match + "'."); + } + } + } +}; + +onnx.Model = class { + + constructor(metadata, model, format) { + this._graphs = []; + this._format = format; + this._producer = model.producer_name && model.producer_name.length > 0 ? model.producer_name + (model.producer_version && model.producer_version.length > 0 ? ' ' + model.producer_version : '') : null; + this._domain = model.domain; + this._modelVersion = model.model_version; + this._description = model.doc_string; + this._metadata = []; + this._imports = null; + + const imports = new Map(); + if (model.opset_import && model.opset_import.length > 0) { + for (const opset_import of model.opset_import) { + const domain = opset_import.domain || 'ai.onnx'; + const version = opset_import.version ? typeof opset_import.version === 'number' ? opset_import.version: opset_import.version.toNumber() : 0; + if (!imports.has(domain) || imports.get(domain) > version) { + imports.set(domain, version); + } + } + this._imports = Array.from(imports).map((pair) => pair[0] + ' v' + pair[1].toString()); + } + if (imports.size == 0) { + imports.set('ai.onnx', 1); + imports.set('ai.onnx.ml', 1); + } + + let imageFormat = ''; + if (model.metadata_props) { + const imageMetadata = {}; + for (const metadata_prop of model.metadata_props) { + switch (metadata_prop.key) { + case 'author': + this._author = metadata_prop.value; + break; + case 'company': + this._company = metadata_prop.value; + break; + case 'converted_from': + this._converted_from = metadata_prop.value; + break; + case 'license': + this._license = metadata_prop.value; + break; + case 'license_url': + this._licenseUrl = metadata_prop.value; + break; + case 'Image.BitmapPixelFormat': + case 'Image.ColorSpaceGamma': + case 'Image.NominalPixelRange': + imageMetadata[metadata_prop.key] = metadata_prop.value; + break; + default: + this._metadata.push({ name: metadata_prop.key, value: metadata_prop.value}); + break; + } + } + imageFormat = [ imageMetadata['Image.BitmapPixelFormat'], imageMetadata['Image.ColorSpaceGamma'], imageMetadata['Image.NominalPixelRange'] ].filter((item) => item); + } + this._graphs = []; + if (model && model.graph) { + const graphMetadata = new onnx.GraphMetadata(metadata, imports); + const context = new onnx.ModelContext(graphMetadata, imageFormat); + for (const func of model.functions || []) { + context.metadata.add(new onnx.Function(context, func)); + } + const graphs = [ model.graph ]; + while (graphs.length > 0) { + const graph = graphs.shift(); + this._graphs.push(context.graph(graph)); + for (const node of graph.node || []) { + for (const attribute of node.attribute || []) { + if (attribute.g) { + graphs.push(attribute.g); + } + else if (attribute.graphs && attribute.graphs.length > 0) { + graphs.push(...attribute.graphs); + } + } + } + } + } + } + + get format() { + return this._format; + } + + get imports() { + return this._imports; + } + + get producer() { + return this._producer; + } + + get domain() { + return this._domain || null; + } + + get description() { + return this._description || null; + } + + get author() { + return this._author || null; + } + + get company() { + return this._company || null; + } + + get source() { + return this._converted_from || null; + } + + get license() { + const license = []; + if (this._license && this._license.length > 0) { + license.push(this._license); + } + if (this._licenseUrl && this._licenseUrl.length > 0) { + license.push('' + this._licenseUrl + ''); + } + if (license.length > 0) { + return license; + } + return null; + } + + get metadata() { + return this._metadata; + } + + get graphs() { + return this._graphs; + } +}; + +onnx.Graph = class { + + constructor(context, graph) { + this._node = ''; + this._description = ''; + this._nodes = []; + this._inputs = []; + this._outputs = []; + this._name = graph.name || null; + this._description = graph.doc_string || ''; + + context = new onnx.GraphContext(context, graph.node); + + for (const initializer of graph.initializer) { + const tensor = context.tensor(initializer.name); + tensor.initializer = new onnx.Tensor(context, initializer, 'Initializer'); + } + for (const sparse_initializer of graph.sparse_initializer) { + const tensor = context.tensor(sparse_initializer.values.name); + tensor.initializer = new onnx.Tensor(context, sparse_initializer, 'Sparse Initializer'); + } + for (const tensor_annotation of graph.quantization_annotation || []) { + const tensor = context.tensor(tensor_annotation.tensor_name); + const annotation = {}; + for (const pair of tensor_annotation.quant_parameter_tensor_names) { + annotation[pair.key] = pair.value; + } + tensor.annotation = annotation; + } + for (const valueInfo of graph.value_info) { + const tensor = context.tensor(valueInfo.name); + tensor.type = context.createType(valueInfo.type); + tensor.description = valueInfo.doc_string; + } + graph.input = graph.input.map((valueInfo) => { + const tensor = context.tensor(valueInfo.name); + tensor.type = context.createType(valueInfo.type); + tensor.description = valueInfo.doc_string; + return tensor; + }); + graph.output = graph.output.map((valueInfo) => { + const tensor = context.tensor(valueInfo.name); + tensor.type = context.createType(valueInfo.type); + tensor.description = valueInfo.doc_string; + return tensor; + }); + new onnx.Inference(graph.node, graph.output); + context.push(graph.node, graph.input, graph.output); + this._nodes = context.pop(); + for (const input of graph.input) { + const argument = context.argument(input.name); + if (!argument.initializer) { + this._inputs.push(new onnx.Parameter(input.name, [ argument ])); + } + } + for (const output of graph.output) { + const argument = context.argument(output.name); + if (!argument.initializer) { + this._outputs.push(new onnx.Parameter(output.name, [ argument ])); + } + } + } + + get name() { + return this._name; + } + + get description() { + return this._description; + } + + get inputs() { + return this._inputs; + } + + get outputs() { + return this._outputs; + } + + get nodes() { + return this._nodes; + } + + toString() { + return 'graph(' + this.name + ')'; + } +}; + +onnx.Parameter = class { + + constructor(name, args) { + this._name = name; + this._arguments = args; + } + + get name() { + return this._name; + } + + get visible() { + return true; + } + + get arguments() { + return this._arguments; + } +}; + +onnx.Argument = class { + + constructor(name, type, initializer, annotation, description) { + if (typeof name !== 'string') { + throw new onnx.Error("Invalid argument identifier '" + JSON.stringify(name) + "'."); + } + this._name = name; + this._type = type || null; + this._initializer = initializer || null; + this._annotation = annotation; + this._description = description || ''; + } + + get name() { + return this._name; + } + + get type() { + return this._type; + } + + get description() { + return this._description; + } + + get quantization() { + if (this._annotation) { + return Object.keys(this._annotation).map((key) => key + ': ' + this._annotation[key]).join(', '); + } + return null; + } + + get initializer() { + return this._initializer; + } +}; + +onnx.Node = class { + + constructor(context, op_type, domain, name, description, attributes, inputs, outputs) { + attributes = attributes || []; + this._type = context.metadata.type(op_type, domain) || { name: op_type, module: domain }; + if (this.type.module !== domain && !(this._type instanceof onnx.Function)) { + this._type = Object.assign({}, this.type); + this._type.name = op_type; + this._type.module = domain; + } + this._name = name || ''; + this._description = description || ''; + this._inputs = inputs; + this._outputs = outputs; + this._attributes = attributes.map((attribute) => new onnx.Attribute(context, op_type, domain, attribute)); + this._chain = []; + const identifier = domain ? domain + '.' + op_type : op_type; + switch (identifier) { + case 'com.microsoft.FusedConv': { + const activation = attributes.find((attribute) => attribute.name === 'activation'); + if (activation) { + const type = context.decodeText(activation.s); + this._chain.push(new onnx.Node(context, type, '', '', '', [], [], [])); + } + break; + } + } + } + + get type() { + return this._type; + } + + get name() { + return this._name; + } + + get description() { + return this._description; + } + + get attributes() { + return this._attributes; + } + + get inputs() { + return this._inputs; + } + + get outputs() { + return this._outputs; + } + + get chain() { + return this._chain; + } +}; + +onnx.Attribute = class { + + constructor(context, op_type, domain, attribute) { + this._name = attribute.name; + this._description = attribute.doc_string || ''; + this._type = null; + this._value = null; + switch (attribute.type) { + case onnx.AttributeType.FLOAT: + this._value = attribute.f; + this._type = 'float32'; + break; + case onnx.AttributeType.INT: + this._value = attribute.i; + this._type = 'int64'; + break; + case onnx.AttributeType.STRING: + switch (op_type) { + case 'Int8GivenTensorFill': + this._value = Array.from(attribute.s); + break; + default: + this._value = context.decodeText(attribute.s); + break; + } + this._type = 'string'; + break; + case onnx.AttributeType.TENSOR: + this._value = new onnx.Tensor(context, attribute.t); + this._type = 'tensor'; + break; + case onnx.AttributeType.GRAPH: + this._value = context.graph(attribute.g); + this._type = 'graph'; + break; + case onnx.AttributeType.FLOATS: + this._value = ArrayBuffer.isView(attribute.floats) ? Array.from(attribute.floats) : attribute.floats; + this._type = 'float32[]'; + break; + case onnx.AttributeType.INTS: + this._value = ArrayBuffer.isView(attribute.ints) ? Array.from(attribute.ints) : attribute.ints; + this._type = 'int64[]'; + break; + case onnx.AttributeType.STRINGS: + this._value = attribute.strings.map((s) => context.decodeText(s)); + this._type = 'string[]'; + break; + case onnx.AttributeType.TENSORS: + this._value = attribute.tensors.map((tensor) => new onnx.Tensor(context, tensor)); + this._type = 'tensor[]'; + break; + case onnx.AttributeType.GRAPHS: + this._value = attribute.graphs.map((graph) => context.graph(graph)); + this._type = 'graph[]'; + break; + case onnx.AttributeType.SPARSE_TENSOR: + this._value = new onnx.Tensor(context, attribute.sparse_tensor); + this._type = 'tensor'; + break; + case onnx.AttributeType.SPARSE_TENSORS: + this._value = attribute.sparse_tensors.map((tensor) => new onnx.Tensor(context, tensor)); + this._type = 'tensor[]'; + break; + case onnx.AttributeType.TYPE_PROTO: + this._value = context.createType(attribute.tp); + this._type = 'type'; + break; + case onnx.AttributeType.TYPE_PROTOS: + this._value = attribute.type_protos.map((type) => context.createType(type)); + this._type = 'type[]'; + break; + default: + throw new onnx.Error("Unknown attribute type '" + attribute.type + "'."); + } + + const metadata = context.metadata.attribute(op_type, domain, attribute.name); + if (metadata) { + if (Object.prototype.hasOwnProperty.call(metadata, 'default') && this._value == metadata.default) { + this._visible = false; + } + if (metadata.type === 'DataType') { + this._type = metadata.type; + const value = this._value ? parseInt(this._value.toString(), 10) : this._value; + this._value = Number.isInteger(value) ? context.createDataType(value) : value; + } + } + } + + get name() { + return this._name; + } + + get type() { + return this._type; + } + + get value() { + return this._value; + } + + get description() { + return this._description; + } + + get visible() { + return this._visible == false ? false : true; + } +}; + +onnx.Group = class { + + constructor(name, groups) { + this._type = { name: 'Scope' }; + this._name = name; + this._nodes = []; + for (const entry of groups) { + const key = entry[0]; + if (key === '') { + for (const node of entry[1]) { + this._nodes.push(node); + } + } + else { + this._nodes.push(new onnx.Group(name === '' ? key : name + '/' + key, entry[1])); + } + } + const set = new Set(); + const inputs = new Array(); + const outputs = new Array(); + for (const node of this._nodes) { + if (node instanceof onnx.Group) { + node.freeze(); + } + for (const parameter of node.outputs) { + for (const argument of parameter.arguments) { + if (!argument.initializer) { + outputs.push(argument); + set.add(argument.name); + } + } + } + } + for (const node of this._nodes) { + for (const parameter of node.inputs) { + for (const argument of parameter.arguments) { + if (!set.has(argument.name) && !argument.initializer) { + inputs.push(argument); + } + } + } + } + this._inputs = [ new onnx.Parameter('inputs', inputs) ]; + this._outputs = [ new onnx.Parameter('outputs', outputs) ]; + this._attributes = []; + } + + get name() { + return this._name; + } + + get type() { + return this._type; + } + + get inputs() { + return this._inputs; + } + + get outputs() { + return this._outputs; + } + + get attributes() { + return this._attributes; + } + + get nodes() { + return this._nodes; + } +}; + +onnx.Tensor = class { + + constructor(context, tensor, kind) { + this._kind = kind || null; + const data = (tensor) => { + let data = undefined; + if (tensor.data_location === onnx.DataLocation.DEFAULT) { + switch (tensor.data_type) { + case onnx.DataType.FLOAT16: + if (tensor.int32_data && tensor.int32_data.length > 0) { + const buffer = new Uint8Array(tensor.int32_data.length << 1); + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + const array = tensor.int32_data; + for (let i = 0; i < array.length; i++) { + view.setUint16(i << 1, array[i], true); + } + data = { + type: tensor.data_type, + buffer: buffer + }; + } + break; + case onnx.DataType.FLOAT: + data = new Float32Array(tensor.float_data); + break; + case onnx.DataType.DOUBLE: + data = new Float64Array(tensor.double_data); + break; + case onnx.DataType.BOOL: + if (tensor.int32_data && tensor.int32_data.length > 0) { + const array = tensor.int32_data; + data = new Array(array.length); + for (let i = 0; i < data.length; i++) { + data[i] = array[i] === 0 ? false : true; + } + } + break; + case onnx.DataType.INT8: + data = new Int8Array(tensor.int32_data); + break; + case onnx.DataType.UINT8: + data = new Uint8Array(tensor.int32_data); + break; + case onnx.DataType.INT16: + data = new Int32Array(tensor.int32_data); + break; + case onnx.DataType.UINT16: + data = new Int32Array(tensor.int32_data); + break; + case onnx.DataType.INT32: + data = new Int32Array(tensor.int32_data); + break; + case onnx.DataType.UINT32: + case onnx.DataType.UINT64: + data = tensor.uint64_data; + break; + case onnx.DataType.INT64: + data = tensor.int64_data; + break; + case onnx.DataType.STRING: + data = tensor.string_data; + break; + } + if (data && (Array.isArray(data) || ArrayBuffer.isView(data)) && data.length === 0) { + data = undefined; + } + if (!data && tensor.raw_data && tensor.raw_data.length > 0) { + data = { + type: tensor.data_type, + buffer: tensor.raw_data + }; + } + } + return data; + }; + if ((onnx.proto && tensor instanceof onnx.proto.SparseTensorProto) || + (onnx.schema && tensor instanceof onnx.schema.SparseTensor)) { + this._name = tensor.values.name || ''; + this._type = context.createTensorType(tensor.values.data_type, tensor.dims.map((dim) => dim), null); + this._location = Array.from(new Set([ context.createLocation(tensor.values.data_location), context.createLocation(tensor.indices.data_location) ])).join(':'); + this._values = data(tensor.values); + this._indices = data(tensor.indices); + } + else { + this._name = tensor.name || ''; + this._type = context.createTensorType(tensor.data_type, tensor.dims.map((dim) => dim), null); + this._location = context.createLocation(tensor.data_location); + this._values = data(tensor); + } + } + + get name() { + return this._name; + } + + get kind() { + return this._kind; + } + + get type() { + return this._type; + } + + get state() { + return this._context().state || null; + } + + get value() { + const context = this._context(); + if (context.state) { + return null; + } + context.limit = Number.MAX_SAFE_INTEGER; + return this._decode(context, 0); + } + + toString() { + const context = this._context(); + if (context.state) { + return ''; + } + context.limit = 10000; + const value = this._decode(context, 0); + return onnx.Tensor._stringify(value, '', ' '); + } + + _context() { + const context = {}; + context.state = null; + if (this._sparse) { + context.state = 'Sparse data not implemented.'; + return context; + } + if (this._location !== 'default') { + context.state = "Data '" + this._location + "' location not implemented."; + return context; + } + const decode = (data) => { + if (!data || Array.isArray(data) || ArrayBuffer.isView(data)) { + return data; + } + const buffer = data.buffer; + const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + const type = data.type; + data = undefined; + switch (type) { + case onnx.DataType.BOOL: + data = new Array(buffer.length); + for (let i = 0; i < buffer.length; i++) { + data[i] = view.getUint8(i) === 0 ? false : true; + } + break; + case onnx.DataType.FLOAT16: + data = new Float32Array(buffer.length >> 1); + for (let i = 0; i < data.length; i++) { + data[i] = view.getFloat16(i << 1, true); + } + break; + case onnx.DataType.FLOAT: + data = new Float32Array(buffer.length >> 2); + for (let i = 0; i < data.length; i++) { + data[i] = view.getFloat32(i << 2, true); + } + break; + case onnx.DataType.DOUBLE: + data = new Float64Array(buffer.length >> 3); + for (let i = 0; i < data.length; i++) { + data[i] = view.getFloat64(i << 3, true); + } + break; + case onnx.DataType.INT8: + data = new Int8Array(buffer.length); + for (let i = 0; i < data.length; i++) { + data[i] = view.getInt8(i, true); + } + break; + case onnx.DataType.UINT8: + data = new Uint8Array(buffer.length); + for (let i = 0; i < data.length; i++) { + data[i] = view.getUint8(i, true); + } + break; + case onnx.DataType.INT16: + data = new Int16Array(buffer.length >> 1); + for (let i = 0; i < data.length; i++) { + data[i] = view.getInt16(i << 1, true); + } + break; + case onnx.DataType.UINT16: + data = new Uint16Array(buffer.length >> 1); + for (let i = 0; i < data.length; i++) { + data[i] = view.getUint16(i << 1, true); + } + break; + case onnx.DataType.INT32: + data = new Int32Array(buffer.length >> 2); + for (let i = 0; i < data.length; i++) { + data[i] = view.getInt32(i << 2, true); + } + break; + case onnx.DataType.UINT32: + data = new Uint32Array(buffer.length >> 2); + for (let i = 0; i < data.length; i++) { + data[i] = view.getUint32(i << 2, true); + } + break; + case onnx.DataType.INT64: + data = new Array(buffer.length >> 3); + for (let i = 0; i < data.length; i++) { + data[i] = view.getInt64(i << 3, true); + } + break; + case onnx.DataType.UINT64: + data = new Array(buffer.length >> 3); + for (let i = 0; i < data.length; i++) { + data[i] = view.getUint64(i << 3, true); + } + break; + } + return data; + }; + this._values = decode(this._values); + if (!this._values) { + context.state = 'Tensor data is empty.'; + return context; + } + this._indices = decode(this._indices); + context.values = this._values; + context.indices = this._indices; + context.index = 0; + context.dataType = this.type.dataType; + context.shape = this.type.shape.dimensions; + context.data = function() { + if (!this._data) { + if (this.indices && this.values && this.indices.length === this.values.length) { + const size = context.shape.reduce((a, b) => a * b, 1); + const indices = this.indices; + const values = this.values; + const array = new values.constructor(size); + switch (this.dataType) { + case 'boolean': + array.fill(false); + break; + case 'int64': + case 'uint64': + break; + } + if (indices.length > 0) { + if (Object.prototype.hasOwnProperty.call(indices[0], 'low')) { + for (let i = 0; i < indices.length; i++) { + const index = indices[i]; + array[index.high === 0 ? index.low : index.toNumber()] = values[i]; + } + } + else { + for (let i = 0; i < indices.length; i++) { + array[indices[i]] = values[i]; + } + } + } + this._data = array; + } + else { + this._data = this.values; + } + } + return this._data; + }; + return context; + } + + _decode(context, dimension) { + const shape = context.shape.length !== 0 ? context.shape : [ 1 ]; + const results = []; + const size = shape[dimension]; + const data = context.data(); + if (dimension == shape.length - 1) { + for (let i = 0; i < size; i++) { + if (context.index > context.limit) { + results.push('...'); + return results; + } + results.push(data[context.index++]); + } + } + else { + for (let j = 0; j < size; j++) { + if (context.index > context.limit) { + results.push('...'); + return results; + } + results.push(this._decode(context, dimension + 1)); + } + } + if (context.shape.length == 0) { + return results[0]; + } + return results; + } + + static _stringify(value, indentation, indent) { + if (Array.isArray(value)) { + const result = []; + result.push(indentation + '['); + const items = value.map((item) => onnx.Tensor._stringify(item, indentation + indent, indent)); + if (items.length > 0) { + result.push(items.join(',\n')); + } + result.push(indentation + ']'); + return result.join('\n'); + } + if (typeof value == 'string') { + return indentation + value; + } + if (value == Infinity) { + return indentation + 'Infinity'; + } + if (value == -Infinity) { + return indentation + '-Infinity'; + } + if (isNaN(value)) { + return indentation + 'NaN'; + } + return indentation + value.toString(); + } +}; + +onnx.TensorType = class { + + constructor(dataType, shape, denotation) { + this._dataType = dataType; + this._shape = shape; + this._denotation = denotation || null; + } + + get dataType() { + return this._dataType; + } + + get shape() { + return this._shape; + } + + get denotation() { + return this._denotation; + } + + toString() { + return this.dataType + this._shape.toString(); + } +}; + +onnx.TensorShape = class { + + constructor(dimensions) { + this._dimensions = dimensions; + } + + get dimensions() { + return this._dimensions; + } + + toString() { + if (!this._dimensions || this._dimensions.length == 0) { + return ''; + } + return '[' + this._dimensions.map((dim) => dim ? dim.toString() : '?').join(',') + ']'; + } +}; + +onnx.SequenceType = class { + + constructor(elementType, denotation) { + this._elementType = elementType; + this._denotation = denotation; + } + + get elementType() { + return this._elementType; + } + + get dennotation() { + return this._dennotation; + } + + toString() { + return 'sequence<' + this._elementType.toString() + '>'; + } +}; + +onnx.MapType = class { + + constructor(keyType, valueType, denotation) { + this._keyType = keyType; + this._valueType = valueType; + this._denotation = denotation; + } + + get keyType() { + return this._keyType; + } + + get valueType() { + return this._valueType; + } + + get denotation() { + return this._denotation; + } + + toString() { + return 'map<' + this._keyType + ',' + this._valueType.toString() + '>'; + } +}; + +onnx.OpaqueType = class { + + constructor(domain, name) { + this._domain = domain; + this._name = name; + } + + toString() { + const name = (this._domain ? (this._domain + '.') : '') + this._name; + return 'opaque<' + name + '>'; + } +}; + +onnx.Function = class { + + constructor(context, func) { + this._name = func.name; + this._domain = func.domain; + this._description = func.doc_string; + this._inputs = []; + this._outputs = []; + this._attributes = func.attribute.map((attribtue) => { return { name: attribtue }; }); + context = new onnx.GraphContext(context, func.node); + func.input = func.input.map((input) => context.tensor(input)); + func.output = func.output.map((output) => context.tensor(output)); + context.push(func.node, func.input, func.output); + this._nodes = context.pop(); + for (const input of func.input) { + const argument = context.argument(input.name); + if (!argument.initializer) { + this._inputs.push(new onnx.Parameter(input.name, [ argument ])); + } + } + for (const output of func.output) { + const argument = context.argument(output.name); + if (!argument.initializer) { + this._outputs.push(new onnx.Parameter(output.name, [ argument ])); + } + } + } + + get type() { + return 'function'; + } + + get name() { + return this._name; + } + + get module() { + return this._domain; + } + + get description() { + return this._description; + } + + get inputs() { + return this._inputs; + } + + get outputs() { + return this._outputs; + } + + get attributes() { + return this._attributes; + } + + get nodes() { + return this._nodes; + } +}; + +onnx.GraphMetadata = class { + + constructor(metadata, imports) { + this._metadata = metadata; + this._imports = imports; + this._cache = new Map(); + this._attributes = new Map(); + this._functions = new Map(); + } + + add(func) { + if (!this._functions.has(func.module)) { + this._functions.set(func.module, new Map()); + } + const map = this._functions.get(func.module); + if (map.has(func.name)) { + throw new onnx.Error("Duplicate function identifier '" + func.module + '.' + func.name + "'."); + } + map.set(func.name, func); + } + + type(name, domain) { + domain = domain || 'ai.onnx'; + const key = domain + ':' + name; + if (!this._cache.has(key)) { + let value = this._metadata.type(name, domain, this._imports); + if (!value) { + if (this._functions.has(domain)) { + const map = this._functions.get(domain); + if (map.has(name)) { + value = map.get(name); + } + } + } + this._cache.set(key, value); + } + return this._cache.get(key); + } + + attribute(type, domain, name) { + const key = domain + ':' + type + ':' + name; + if (!this._attributes.has(key)) { + const schema = this.type(type, domain); + if (schema && schema.attributes && schema.attributes.length > 0) { + for (const attribute of schema.attributes) { + this._attributes.set(key, attribute); + } + } + if (!this._attributes.has(key)) { + this._attributes.set(key, null); + } + } + return this._attributes.get(key); + } +}; + +onnx.Metadata = class { + + static open(context) { + if (onnx.Metadata._metadata) { + return Promise.resolve(onnx.Metadata._metadata); + } + return context.request('onnx-metadata.json', 'utf-8', null).then((data) => { + onnx.Metadata._metadata = new onnx.Metadata(data); + return onnx.Metadata._metadata; + }).catch(() => { + onnx.Metadata._metadata = new onnx.Metadata(null); + return onnx.Metadata._metadata; + }); + } + + constructor(data) { + this._map = new Map(); + if (data) { + const metadata = JSON.parse(data); + for (const item of metadata) { + if (!this._map.has(item.module)) { + this._map.set(item.module, new Map()); + } + const map = this._map.get(item.module); + if (!map.has(item.name)) { + map.set(item.name, []); + } + map.get(item.name).push(item); + } + } + } + + type(name, domain, imports) { + domain = domain || 'ai.onnx'; + let current = null; + if (this._map.has(domain)) { + const map = this._map.get(domain); + if (map.has(name)) { + for (const metadata of map.get(name)) { + const matchVersion = current ? current.version : -1; + const importVersion = imports.get(metadata.module) || 0; + if (importVersion >= metadata.version && matchVersion < metadata.version) { + current = metadata; + } + } + } + } + return current; + } +}; + +onnx.Inference = class { + + constructor(nodes, outputs) { + this._outputs = new Map(); + + for (const node of nodes) { + for (const output of node.output) { + this._outputs.set(output.name, node); + } + } + + for (const output of outputs) { + this._infer(output.name); + } + } + + _infer(output) { + if (this._outputs.has(output)) { + let hasInputShapes = true; + const node = this._outputs.get(output); + for (const input of node.input) { + if (!input.type) { + this._infer(input); + if (!input.type) { + hasInputShapes = false; + break; + } + } + } + if (hasInputShapes) { + // continue + } + } + } +}; + +onnx.DataLocation = { + DEFAULT: 0, + EXTERNAL: 1 +}; + +onnx.DataType = { + UNDEFINED: 0, + FLOAT: 1, + UINT8: 2, + INT8: 3, + UINT16: 4, + INT16: 5, + INT32: 6, + INT64: 7, + STRING: 8, + BOOL: 9, + FLOAT16: 10, + DOUBLE: 11, + UINT32: 12, + UINT64: 13, + COMPLEX64: 14, + COMPLEX128: 15, + BFLOAT16: 16 +}; + +onnx.AttributeType = { + UNDEFINED: 0, + FLOAT: 1, + INT: 2, + STRING: 3, + TENSOR: 4, + GRAPH: 5, + FLOATS: 6, + INTS: 7, + STRINGS: 8, + TENSORS: 9, + GRAPHS: 10, + SPARSE_TENSOR: 11, + SPARSE_TENSORS: 12, + TYPE_PROTO: 13, + TYPE_PROTOS: 14 +}; + +onnx.ModelContext = class { + + constructor(metadata, imageFormat) { + this._metadata = metadata; + this._imageFormat = imageFormat; + this._graphs = new Map(); + } + + get metadata() { + return this._metadata; + } + + get imageFormat() { + return this._imageFormat; + } + + graph(value) { + if (!this._graphs.has(value)) { + this._graphs.set(value, new onnx.Graph(this, value)); + } + return this._graphs.get(value); + } +}; + +onnx.GraphContext = class { + + constructor(context, nodes) { + this._context = context; + this._decoder = new TextDecoder('utf-8'); + this._dataTypes = new Map(Object.entries(onnx.DataType).map((entry) => [ entry[1], entry[0].toLowerCase() ])); + this._dataTypes.set(onnx.DataType.UNDEFINED, 'UNDEFINED'); + this._dataTypes.set(onnx.DataType.BOOL, 'boolean'); + this._dataTypes.set(onnx.DataType.FLOAT, 'float32'); + this._dataTypes.set(onnx.DataType.DOUBLE, 'float64'); + this._tensors = new Map(); + this._arguments = new Map(); + this._groups = new Map(); + this._nodes = []; + for (const node of nodes) { + node.input = node.input.map((name) => this.tensor(name)); + node.output = node.output.map((name) => this.tensor(name)); + node.param = {}; + for (const attribute of node.attribute) { + if (attribute.type) { + continue; + } + if (attribute.ints && attribute.ints.length > 0) { + attribute.type = onnx.AttributeType.INTS; + } + else if (attribute.floats && attribute.floats.length > 0) { + attribute.type = onnx.AttributeType.FLOATS; + } + else if (attribute.strings && attribute.strings.length > 0) { + attribute.type = onnx.AttributeType.STRINGS; + } + else if (attribute.graphs && attribute.graphs.length > 0) { + attribute.type = onnx.AttributeType.GRAPHS; + } + else if (attribute.s && attribute.s.length > 0) { + attribute.type = onnx.AttributeType.STRING; + } + else if (Object.prototype.hasOwnProperty.call(attribute, 'f')) { + attribute.type = onnx.AttributeType.FLOAT; + } + else if (Object.prototype.hasOwnProperty.call(attribute, 'i')) { + attribute.type = onnx.AttributeType.INT; + } + else if (Object.prototype.hasOwnProperty.call(attribute, 't')) { + attribute.type = onnx.AttributeType.TENSOR; + } + else if (Object.prototype.hasOwnProperty.call(attribute, 'g')) { + attribute.type = onnx.AttributeType.GRAPH; + } + else if (Object.prototype.hasOwnProperty.call(attribute, 'sparse_tensor')) { + attribute.type =onnx.AttributeType.SPARSE_TENSOR; + } + else { + attribute.type = onnx.AttributeType.UNDEFINED; + } + } + } + } + + get metadata() { + return this._context.metadata; + } + + graph(name) { + return this._context.graph(name); + } + + tensor(name) { + if (!this._tensors.has(name)) { + this._tensors.set(name, { name: name }); + } + return this._tensors.get(name); + } + + group(name) { + if (!this._groups.has(name)) { + const path = name.split('/'); + if (path.length > 1) { + path.pop(); + return this.group(path.join('/')); + } + this._groups.set(name, new Map([ [ '', [] ]])); + } + return this._groups.get(name); + } + + argument(name) { + if (!this._arguments.has(name)) { + const tensor = this.tensor(name); + const type = tensor.initializer ? tensor.initializer.type : tensor.type || null; + this._arguments.set(name, new onnx.Argument(name, type, tensor.initializer, tensor.annotation, tensor.description)); + } + return this._arguments.get(name); + } + + createType(type) { + if (!type) { + return null; + } + let denotation = ''; + switch (type.denotation) { + case 'TENSOR': + denotation = 'Tensor'; + break; + case 'IMAGE': + denotation = 'Image' + (this._context.imageFormat ? '(' + this._context.imageFormat.join(',') + ')' : ''); + break; + case 'AUDIO': + denotation = 'Audio'; + break; + case 'TEXT': + denotation = 'Text'; + break; + } + switch (type.value) { + case 'tensor_type': { + const tensor_type = type.tensor_type; + let shape = []; + if (tensor_type.shape && tensor_type.shape.dim) { + shape = tensor_type.shape.dim.map((dim) => dim.dim_param ? dim.dim_param : dim.dim_value ? dim.dim_value : null); + } + return this.createTensorType(tensor_type.elem_type, shape, denotation); + } + case 'sparse_tensor_type': { + const tensor_type = type.sparse_tensor_type; + let shape = []; + if (tensor_type.shape && tensor_type.shape.dim) { + shape = tensor_type.shape.dim.map((dim) => dim.dim_param ? dim.dim_param : dim.dim_value); + } + return this.createTensorType(tensor_type.elem_type, shape, denotation); + } + case 'map_type': { + return this.createMapType(type.map_type.key_type, this.createType(type.map_type.value_type), denotation); + } + case 'sequence_type': { + return new onnx.SequenceType(this.createType(type.sequence_type.elem_type), denotation); + } + case 'opaque_type': { + return new onnx.OpaqueType(type.opaque_type.domain, type.opaque_type.name); + } + } + return null; + } + + createTensorType(dataType, shape, denotation) { + dataType = this.createDataType(dataType); + return new onnx.TensorType(dataType, new onnx.TensorShape(shape), denotation); + } + + createMapType(keyType, valueType, denotation) { + keyType = this.createDataType(keyType); + return new onnx.MapType(keyType, valueType, denotation); + } + + createDataType(value) { + return this._dataTypes.has(value) ? this._dataTypes.get(value) : this._dataTypes.get(onnx.DataType.UNDEFINED); + } + + createLocation(value) { + switch (value) { + case onnx.DataLocation.DEFAULT: return 'default'; + case onnx.DataLocation.EXTERNAL: return 'external'; + } + return 'UNDEFINED'; + } + + decodeText(value) { + if (typeof value === 'string') { + return value; + } + return this._decoder.decode(value); + } + + push(nodes, inputs, outputs) { + const inputMap = new Map(); + const outputMap = new Map(); + for (const node of nodes) { + node.input.every((input) => inputMap.set(input.name, (inputMap.get(input) || 0) + 1)); + node.output.every((output) => outputMap.set(output.name, (outputMap.get(output) || 0) + 1)); + } + inputs.every((input) => inputMap.delete(input.name)); + outputs.every((output) => outputMap.delete(output.name)); + nodes = nodes.filter((node) => { + const constant = node && + node.op_type === 'Constant' && + node.attribute.length === 1 && node.attribute[0] && + node.input.length === 0 && + node.output.length === 1 && node.output[0] && inputMap.get(node.output[0].name) === 1 && outputMap.get(node.output[0].name) === 1; + const attribute = constant ? node.attribute[0] : null; + if (attribute && attribute.name === 'value' && attribute.type === onnx.AttributeType.TENSOR && attribute.t) { + const tensor = this.tensor(node.output[0].name); + tensor.initializer = new onnx.Tensor(this, attribute.t, 'Constant'); + return false; + } + else if (attribute && attribute.name === 'sparse_value' && attribute.type === onnx.AttributeType.SPARSE_TENSOR && attribute.sparse_tensor) { + const tensor = this.tensor(node.output[0].name); + tensor.initializer = new onnx.Tensor(this, attribute.sparse_tensor, 'Sparse Constant'); + return false; + } + return true; + }); + for (let node of nodes) { + const schema = this._context.metadata.type(node.op_type, node.domain); + const inputs = []; + node.input = node.input || []; + for (let i = 0; i < node.input.length; ) { + const input = schema && schema.inputs && i < schema.inputs.length ? schema.inputs[i] : { name: i.toString() }; + const count = input.list ? node.input.length - i : 1; + const list = node.input.slice(i, i + count).map((input) => this.argument(input.name)); + inputs.push(new onnx.Parameter(input.name, list)); + i += count; + } + const outputs = []; + node.output = node.output || []; + for (let i = 0; i < node.output.length; ) { + const output = schema && schema.outputs && i < schema.outputs.length ? schema.outputs[i] : { name: i.toString() }; + const count = output.list ? node.output.length - i : 1; + const list = node.output.slice(i, i + count).map((output) => this.argument(output.name)); + outputs.push(new onnx.Parameter(output.name, list)); + i += count; + } + node = new onnx.Node(this, node.op_type, node.domain, node.name, node.doc_string, node.attribute, inputs, outputs); + this._nodes.push(node); + + // const path = (node.name || '').split('/'); + // path.pop(); + // this.group(path.join('/')).get('').push(node); + } + } + + pop() { + /* + const nodes = []; + for (const entry of this._groups) { + if (entry[0] === '') { + for (const node of entry[1].get('')) { + nodes.push(node); + } + continue; + } + nodes.push(new onnx.Group(entry[0], entry[1])); + } + return nodes; + */ + return this._nodes; + } +}; + +onnx.Runtime = {}; + +onnx.Runtime.Reader = class { + + static open(stream, extension) { + if (stream.length >= 8) { + const buffer = stream.peek(Math.min(32, stream.length)); + const reader = flatbuffers.BinaryReader.open(buffer); + const identifier = reader.identifier; + if (identifier === 'ORTM') { + return new onnx.Runtime.Reader(stream); + } + if (extension === 'ort') { + const signature = [ 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]; + if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) { + return new onnx.Runtime.Reader(stream); + } + } + } + return null; + } + + constructor(stream) { + this._stream = stream; + } + + read() { + this._graphs = new Set(); + const reader = flatbuffers.BinaryReader.open(this._stream); + const session = onnx.schema.InferenceSession.create(reader); + const model = session.model; + const graph = model.graph; + graph.doc_string = model.graph_doc_string; + delete model.graph_doc_string; + this._graph(graph); + return model; + } + + _graph(graph) { + if (this._graphs.has(graph)) { + return; + } + this._graphs.add(graph); + graph.name = this._graphs.size.toString(); + graph.node = graph.nodes.map((node) => { + this._node(node); + return node; + }); + delete graph.nodes; + graph.input = graph.inputs.map((input) => { + return { name: input }; + }); + delete graph.inputs; + graph.output = graph.outputs.map((output) => { + return { name: output }; + }); + delete graph.outputs; + graph.value_info = graph.node_args; + delete graph.node_args; + graph.initializer = graph.initializers.map((tensor) => { + tensor.data_location = onnx.DataLocation.DEFAULT; + return tensor; + }); + delete graph.initializers; + graph.sparse_initializer = graph.sparse_initializers.map((tensor) => { + tensor.values.data_location = onnx.DataLocation.DEFAULT; + tensor.indices.data_location = onnx.DataLocation.DEFAULT; + return tensor; + }); + delete graph.sparse_initializers; + } + + _node(node) { + node.input = node.inputs; + node.output = node.outputs; + node.attribute = node.attributes.map((attribute) => { + switch (attribute.type) { + case onnx.AttributeType.GRAPH: + this._graph(attribute.g); + break; + case onnx.AttributeType.GRAPHS: + for (const graph of attribute.graphs) { + this._graph(graph); + } + break; + } + return attribute; + }); + delete node.inputs; + delete node.outputs; + delete node.attributes; + } +}; + +onnx.Text = {}; + +onnx.Text.Reader = class { + + static open(stream) { + try { + if (stream.length > 0 && stream.peek(1)[0] < 0x80 || stream.peek(1)[0] >= 0xFE) { + const reader = text.Reader.open(stream); + const lines = []; + for (let i = 0; i < 32; i++) { + const line = reader.read(); + if (line === undefined) { + break; + } + lines.push(line); + } + const content = lines.join('\n'); + if (/^\s*<\s*ir_version\s*:/m.exec(content) || + /^\s*[a-zA-Z][a-zA-Z0-9]*\s*\(.*\)\s=>\s\(/m.exec(content)) { + return new onnx.Text.Reader(stream); + } + } + } + catch (err) { + // continue regardless of error + } + return null; + } + + constructor(stream) { + this._stream = stream; + this._dataTypes = new Map([ + [ 'float', 1 ], [ 'uint8', 2 ], [ 'int8', 3 ], [ 'uint16', 4 ], + [ 'int16', 5 ], [ 'int32', 6 ], [ 'int64', 7 ], [ 'string', 8 ], + [ 'bool', 9 ], [ 'float16', 10 ], [ 'double', 11 ], [ 'uint32', 12 ], + [ 'uint64', 13 ], [ 'complex64', 14 ], [ 'complex128', 15 ], [ 'bfloat16', 16 ] + ]); + this._attributeTypes = new Map([ + [ 'float', 1 ], [ 'int', 2 ], [ 'string', 3 ], + [ 'tensor', 4 ], [ 'graph', 5 ], [ 'sparse_tensor', 11 ], [ 'type_proto', 13 ], + [ 'floats', 6 ], [ 'ints', 7 ], [ 'strings', 8 ], + [ 'tensors', 9 ], [ 'graphs', 10 ], [ 'sparse_tensors', 12 ], [ 'type_protos', 14 ] + ]); + } + + read() { + const decoder = text.Decoder.open(this._stream); + this._decoder = decoder; + this._position = 0; + this._char = decoder.decode(); + return this._model(); + } + + _seek(position) { + this._decoder.position = position; + this._char = ''; + this._next(); + } + + _model() { + this._whitespace(); + const model = new onnx.proto.ModelProto(); + if (this._match('<')) { + do { + const keyword = this._identifier(); + this._expect(':'); + switch (keyword) { + case 'ir_version': + case 'model_version': + model[keyword] = this._integer(); + break; + case 'opset_import': + model[keyword] = this._operatorSetId(); + break; + case 'producer_name': + case 'producer_version': + case 'domain': + case 'doc_string': + model[keyword] = this._string(); + break; + case 'metadata_props': + this._expect('['); + if (!this._match(']')) { + do { + const entry = new onnx.proto.StringStringEntryProto(); + entry.key = this._string(); + this._expect(':'); + entry.value = this._string(); + model.metadata_props.push(entry); + } while (this._match(',')); + this._expect(']'); + } + break; + default: + this._throw("Unknown keyword '" + keyword + "'."); + break; + } + } while (this._match(',')); + this._expect('>'); + } + model.graph = this._graph(); + this._whitespace(); + while (this._char !== undefined) { + const func = this._function(); + if (func) { + model.functions.push(func); + } + this._whitespace(); + } + return model; + } + + _graph() { + const graph = new onnx.proto.GraphProto(); + graph.name = this._identifier(); + if (this._match('(')) { + if (!this._match(')')) { + do { + const valueInfo = this._valueInfo(); + if (this._match('=')) { + const tensor = this._tensor(valueInfo.type); + tensor.name = valueInfo.name; + graph.initializer.push(tensor); + } + graph.input.push(valueInfo); + } + while (this._match(',')); + this._expect(')'); + } + } + this._expect('=>'); + graph.output = this._valueInfoList(); + if (this._match('<')) { + if (!this._match('>')) { + do { + const valueInfo = this._valueInfo(); + if (this._match('=')) { + const tensor = this._tensor(valueInfo.type); + tensor.name = valueInfo.name; + graph.initializer.push(tensor); + } + else { + graph.value_info.push(valueInfo); + } + } + while (this._match(',')); + this._expect('>'); + } + } + graph.node = this._nodeList(); + return graph; + } + + _nodeList() { + const list = []; + this._expect('{'); + while (!this._match('}')) { + list.push(this._node()); + } + return list; + } + + _node() { + const node = new onnx.proto.NodeProto(); + node.output = this._identifierList(); + this._expect('='); + let identifier = this._identifier(); + let domain = ''; + while (this._match('.')) { + if (domain) { + domain += '.'; + } + domain += identifier; + identifier = this._identifier(); + } + node.domain = domain; + node.op_type = identifier; + node.attribute = this._attributeList(); + this._expect('('); + node.input = this._identifierList(); + this._expect(')'); + if (!node.attribute || node.attribute.length === 0) { + node.attribute = this._attributeList(); + } + return node; + } + + _attributeList() { + const list = []; + if (this._match('<')) { + do { + list.push(this._attribute()); + } + while (this._match(',')); + this._expect('>'); + } + return list; + } + + _attribute() { + const attribute = new onnx.proto.AttributeProto(); + attribute.name = this._identifier(); + if (this._match(':')) { + const type = this._identifier(); + if (!this._attributeTypes.has(type)) { + this._throw("Unexpected attribute type '" + type + "'."); + } + attribute.type = this._attributeTypes.get(type); + } + this._expect('='); + if (this._match('[')) { + const list = []; + do { + list.push(this._literal()); + } + while (this._match(',')); + this._expect(']'); + if (list.every((value) => typeof value === 'string')) { + attribute.type = onnx.AttributeType.STRINGS; + attribute.strings = list; + } + else if (list.every((value) => typeof value === 'number' && Number.isInteger(value))) { + attribute.type = onnx.AttributeType.INTS; + attribute.ints = list; + } + else if (list.every((value) => typeof value === 'number')) { + attribute.type = onnx.AttributeType.FLOATS; + attribute.floats = list; + } + else { + this._throw("Unexpected value '" + JSON.stringify(list) + "'."); + } + } + else { + if ((this._char >= 'a' && this._char <= 'z') || (this._char >= 'A' && this._char <= 'Z') || this._char === '_') { + const identifier = this._identifier(); + if (this._dataTypes.has(identifier)) { + attribute.type = onnx.AttributeType.TENSOR; + if (!this._dataTypes.has(identifier)) { + this._throw("Unexpected type '" + identifier + "'."); + } + const type = this._type(this._dataTypes.get(identifier)); + if (!type.tensor_type.elem_type) { + this._throw('Expected tensor data type.'); + } + if (!type.tensor_type.shape || !type.tensor_type.shape.dim) { + this._throw('Expected tensor shape.'); + } + attribute.t = this._tensor(type); + } + else { + attribute.type = onnx.AttributeType.GRAPH; + attribute.g = this._graph(); + } + } + else if (this._match('@')) { + attribute.ref_attr_name = this._identifier(); + } + else { + const value = this._literal(); + switch (typeof value) { + case 'number': + if (Number.isInteger(value)) { + attribute.type = onnx.AttributeType.INT; + attribute.i = value; + } + else { + attribute.type = onnx.AttributeType.FLOAT; + attribute.f = value; + } + break; + case 'string': + attribute.type = onnx.AttributeType.STRING; + attribute.s = value; + break; + default: { + this._throw("Unexpected value '" + JSON.stringify(value) + "'."); + } + } + } + } + return attribute; + } + + _valueInfoList() { + const list = []; + this._expect('('); + if (!this._match(')')) { + do { + list.push(this._valueInfo()); + } while (this._match(',')); + this._expect(')'); + } + return list; + } + + _valueInfo() { + const valueInfo = new onnx.proto.ValueInfoProto(); + let identifier = this._identifier(); + if (this._dataTypes.has(identifier)) { + valueInfo.type = this._type(this._dataTypes.get(identifier)); + identifier = this._identifier(); + } + valueInfo.name = identifier; + return valueInfo; + } + + _type(elem_type) { + const type = new onnx.proto.TypeProto(); + type.tensor_type = new onnx.proto.TypeProto.Tensor(); + type.tensor_type.elem_type = elem_type; + if (this._match('[')) { + if (!this._match(']')) { + type.tensor_type.shape = this._shape(); + this._expect(']'); + } + } + else { + type.tensor_type.shape = new onnx.proto.TensorShapeProto(); + } + return type; + } + + _shape() { + const shape = new onnx.proto.TensorShapeProto(); + do { + const dimension = new onnx.proto.TensorShapeProto.Dimension(); + if (!this._match('?')) { + const identifier = this._identifier(true); + if (identifier) { + dimension.dim_param = identifier; + } + else { + dimension.dim_value = this._integer(); + } + } + shape.dim.push(dimension); + } + while (this._match(',')); + return shape; + } + + _tensor(type) { + const tensor = new onnx.proto.TensorProto(); + if (!type.tensor_type || !type.tensor_type.elem_type) { + this._throw('Expected tensor type.'); + } + if (!type.tensor_type.shape || !type.tensor_type.shape.dim || !type.tensor_type.shape.dim.every((dim) => dim.dim_value)) { + this._throw('Expected numeric tensor shape.'); + } + const elem_type = type.tensor_type.elem_type; + tensor.data_type = elem_type; + tensor.dims = type.tensor_type.shape.dim.map((dim) => dim.dim_value); + this._match('='); + this._expect('{'); + if (!this._match('}')) { + do { + switch (elem_type) { + case onnx.DataType.INT8: + case onnx.DataType.INT16: + case onnx.DataType.INT32: + case onnx.DataType.UINT8: + case onnx.DataType.UINT16: + case onnx.DataType.BOOL: + tensor.int32_data.push(this._integer()); + break; + case onnx.DataType.INT64: + tensor.int64_data.push(this._integer()); + break; + case onnx.DataType.UINT32: + case onnx.DataType.UINT64: + tensor.uint64_data.push(this._integer()); + break; + case onnx.DataType.FLOAT: + tensor.float_data.push(this._float()); + break; + case onnx.DataType.DOUBLE: + tensor.double_data.push(this._float()); + break; + case onnx.DataType.STRING: + tensor.string_data.push(this.string()); + break; + default: + return this._throw("Unsupported tensor element type '" + elem_type.toString() + "'."); + } + } while (this._match(',')); + this._expect('}'); + } + return tensor; + } + + _function() { + const func = new onnx.proto.FunctionProto(); + if (this._match('<')) { + do { + const keyword = this._identifier(); + this._expect(':'); + switch (keyword) { + case 'opset_import': + func[keyword] = this._operatorSetId(); + break; + case 'domain': + case 'doc_string': + func[keyword] = this._string(); + break; + default: + this._throw("Unknown keyword '" + keyword + "'."); + break; + } + } + while (this._match(',')); + this._expect('>'); + } + func.name = this._identifier(); + if (this._match('<')) { + func.attribute = this._identifierList(); + this._expect('>'); + } + if (this._match('(')) { + func.input = this._identifierList(); + this._expect(')'); + } + this._expect('=>'); + if (this._match('(')) { + func.output = this._identifierList(); + this._expect(')'); + } + func.node = this._nodeList(); + return func; + } + + _identifierList() { + const list = []; + const identifier = this._identifier(true); + if (identifier) { + list.push(identifier); + while (this._match(',')) { + list.push(this._identifier()); + } + } + return list; + } + + _identifier(optional) { + this._whitespace(); + const value = []; + if ((this._char >= 'a' && this._char <= 'z') || (this._char >= 'A' && this._char <= 'Z')) { + value.push(this._char); + this._next(); + while ((this._char >= 'a' && this._char <= 'z') || (this._char >= 'A' && this._char <= 'Z') || (this._char >= '0' && this._char <= '9') || this._char === '_') { + value.push(this._char); + this._next(); + } + } + if (optional !== true && value.length == 0) { + this._throw('Identifier expected.'); + } + return value.join(''); + } + + _literal() { + this._whitespace(); + let decimal_point = false; + if (this._char === '"') { + const value = []; + this._next(); + while (this._char !== undefined && this._char !== '"') { + value.push(this._char); + this._next(); + } + if (this._char !== undefined) { + this._next(); + } + return value.join(''); + } + else if ((this._char >= '0' && this._char <= '9') || this._char === '-') { + const value = [ this._char ]; + this._next(); + while ((this._char >= '0' && this._char <= '9') || this._char === '.') { + if (this._char === '.') { + if (decimal_point) { + this._throw(); + } + decimal_point = true; + } + value.push(this._char); + this._next(); + } + if (value.length === 0) { + this._throw('Value expected.'); + } + if (this._char === 'e' || this._char === 'E') { + decimal_point = true; + value.push(this._char); + this._next(); + if (this._char === '+' || this._char === '-') { + value.push(this._char); + this._next(); + } + while ((this._char >= '0' && this._char <= '9')) { + value.push(this._char); + this._next(); + } + } + return decimal_point ? Number.parseFloat(value.join('')) : Number.parseInt(value.join(''), 10); + } + return undefined; + } + + _integer() { + const value = this._literal(); + if (!Number.isInteger(value)) { + this._throw('Integer value expected.'); + } + return value; + } + + _float() { + const value = this._literal(); + if (typeof value !== 'number') { + this._throw('Float value expected.'); + } + return value; + } + + _string() { + const value = this._literal(); + if (typeof value !== 'string') { + this._throw('String value expected.'); + } + return value; + } + + _operatorSetId() { + const list = []; + this._expect('['); + if (!this._match(']')) { + do { + const value = new onnx.proto.OperatorSetIdProto(); + value.domain = this._string(); + this._expect(':'); + value.version = this._integer(); + list.push(value); + } + while (this._match(',')); + this._expect(']'); + } + return list; + } + + _match(value) { + this._whitespace(); + if (this._char !== value[0]) { + return false; + } + if (value.length === 1) { + this._next(); + return true; + } + const position = this._position; + for (let i = 0; i < value.length; i++) { + if (this._char !== value[i]) { + this._seek(position); + return false; + } + this._next(); + } + return true; + } + + _expect(value) { + if (!this._match(value)) { + this._unexpected(); + } + return true; + } + + _whitespace() { + for (;;) { + while (this._char === ' ' || this._char === '\n' || this._char === '\r' || this._char === '\t') { + this._next(); + } + if (this._char === undefined || this._char !== '#') { + break; + } + while (this._char !== undefined && this._char !== '\n') { + this._next(); + } + } + } + + _next() { + if (this._char === undefined) { + this._unexpected(); + } + this._position = this._decoder.position; + this._char = this._decoder.decode(); + } + + _unexpected() { + let c = this._char; + if (c === undefined) { + throw new onnx.Error('Unexpected end of input.'); + } + else if (c === '"') { + c = 'string'; + } + else if ((c >= '0' && c <= '9') || c === '-') { + c = 'number'; + } + else { + if (c < ' ' || c > '\x7F') { + const name = Object.keys(this._escape).filter((key) => this._escape[key] === c); + c = (name.length === 1) ? '\\' + name : '\\u' + ('000' + c.charCodeAt(0).toString(16)).slice(-4); + } + c = "token '" + c + "'"; + } + this._throw('Unexpected ' + c); + } + + _throw(message) { + throw new onnx.Error(message.replace(/\.$/, '') + this._location()); + } + + _location() { + let line = 1; + let column = 1; + this._decoder.position = 0; + let c; + do { + if (this._decoder.position === this._position) { + return ' at ' + line.toString() + ':' + column.toString() + '.'; + } + c = this._decoder.decode(); + if (c === '\n') { + line++; + column = 1; + } + else { + column++; + } + } + while (c !== undefined); + return ' at ' + line.toString() + ':' + column.toString() + '.'; + } +}; + +onnx.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'Error loading ONNX model.'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.ModelFactory = onnx.ModelFactory; +} diff --git a/pickle.js b/pickle.js new file mode 100644 index 0000000..1d61f33 --- /dev/null +++ b/pickle.js @@ -0,0 +1,189 @@ + +// Experimental + +var pickle = pickle || {}; +var python = python || require('./python'); +var zip = zip || require('./zip'); + +pickle.ModelFactory = class { + + match(context) { + const stream = context.stream; + const signature = [ 0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19 ]; + if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value)) { + // Reject PyTorch models with .pkl file extension. + return undefined; + } + const obj = context.open('pkl'); + if (obj !== undefined) { + return 'pickle'; + } + return undefined; + } + + open(context) { + return new Promise((resolve) => { + let format = 'Pickle'; + const obj = context.open('pkl'); + if (obj === null || obj === undefined) { + context.exception(new pickle.Error("Unknown Pickle null object in '" + context.identifier + "'.")); + } + else if (Array.isArray(obj)) { + if (obj.length > 0 && obj[0] && obj.every((item) => item && item.__class__ && obj[0].__class__ && item.__class__.__module__ === obj[0].__class__.__module__ && item.__class__.__name__ === obj[0].__class__.__name__)) { + const type = obj[0].__class__.__module__ + "." + obj[0].__class__.__name__; + context.exception(new pickle.Error("Unknown Pickle '" + type + "' array object in '" + context.identifier + "'.")); + } + else { + context.exception(new pickle.Error("Unknown Pickle array object in '" + context.identifier + "'.")); + } + } + else if (obj && obj.__class__) { + const formats = new Map([ + [ 'cuml.ensemble.randomforestclassifier.RandomForestClassifier', 'cuML' ] + ]); + const type = obj.__class__.__module__ + "." + obj.__class__.__name__; + if (formats.has(type)) { + format = formats.get(type); + } + else { + context.exception(new pickle.Error("Unknown Pickle type '" + type + "' in '" + context.identifier + "'.")); + } + } + else { + context.exception(new pickle.Error("Unknown Pickle object in '" + context.identifier + "'.")); + } + resolve(new pickle.Model(obj, format)); + }); + } +}; + +pickle.Model = class { + + constructor(value, format) { + this._format = format; + this._graphs = [ new pickle.Graph(value) ]; + } + + get format() { + return this._format; + } + + get graphs() { + return this._graphs; + } +}; + +pickle.Graph = class { + + constructor(obj) { + this._inputs = []; + this._outputs = []; + this._nodes = []; + + if (Array.isArray(obj) && obj.every((item) => item.__class__)) { + for (const item of obj) { + this._nodes.push(new pickle.Node(item)); + } + } + else if (obj && obj instanceof Map) { + for (const entry of obj) { + this._nodes.push(new pickle.Node(entry[1], entry[0])); + } + } + else if (obj && obj.__class__) { + this._nodes.push(new pickle.Node(obj)); + } + else if (obj && Object(obj) === obj) { + this._nodes.push(new pickle.Node(obj)); + } + } + + get inputs() { + return this._inputs; + } + + get outputs() { + return this._outputs; + } + + get nodes() { + return this._nodes; + } +}; + +pickle.Node = class { + + constructor(obj, name) { + this._name = name || ''; + this._inputs = []; + this._outputs = []; + this._attributes = []; + if (Array.isArray(obj)) { + this._type = { name: 'List' }; + this._attributes.push(new pickle.Attribute('value', obj)); + } + else { + const type = obj.__class__ ? obj.__class__.__module__ + '.' + obj.__class__.__name__ : 'Object'; + this._type = { name: type }; + for (const key of Object.keys(obj)) { + const value = obj[key]; + this._attributes.push(new pickle.Attribute(key, value)); + } + } + } + + get type() { + return this._type; + } + + get name() { + return this._name; + } + + get inputs() { + return this._inputs; + } + + get outputs() { + return this._outputs; + } + + get attributes() { + return this._attributes; + } +}; + +pickle.Attribute = class { + + constructor(name, value) { + this._name = name; + this._value = value; + if (value && value.__class__) { + this._type = value.__class__.__module__ + '.' + value.__class__.__name__; + } + } + + get name() { + return this._name; + } + + get value() { + return this._value; + } + + get type() { + return this._type; + } +}; + +pickle.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'Error loading Pickle model.'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.ModelFactory = pickle.ModelFactory; +} \ No newline at end of file diff --git a/protobuf.js b/protobuf.js new file mode 100644 index 0000000..50f14cb --- /dev/null +++ b/protobuf.js @@ -0,0 +1,1348 @@ + +var protobuf = protobuf || {}; +var base = base || require('./base'); +var text = text || require('./text'); + +protobuf.get = (name) => { + protobuf._map = protobuf._map || new Map(); + if (!protobuf._map.has(name)) { + protobuf._map.set(name, {}); + } + return protobuf._map.get(name); +}; + +protobuf.BinaryReader = class { + + static open(buffer) { + return new protobuf.BinaryReader(buffer); + } + + constructor(data) { + const buffer = data instanceof Uint8Array ? data : data.peek(); + this._buffer = buffer; + this._length = buffer.length; + this._position = 0; + this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + this._utf8Decoder = new TextDecoder('utf-8'); + } + + signature() { + const tags = new Map(); + this._position = 0; + try { + if (this._length > 0) { + const type = this._buffer[0] & 7; + if (type !== 4 && type !== 6 && type !== 7) { + const length = this.length; + while (this._position < length) { + const tag = this.uint32(); + const field = tag >>> 3; + const type = tag & 7; + if (type > 5 || field === 0) { + tags.clear(); + break; + } + tags.set(field, type); + if (!this._skipType(type)) { + tags.clear(); + break; + } + } + } + } + } + catch (err) { + tags.clear(); + } + this._position = 0; + return tags; + } + + decode() { + let tags = {}; + this._position = 0; + try { + const decodeMessage = (max) => { + const length = this._uint32(); + if (length === undefined) { + return undefined; + } + if (length === 0) { + // return 2; + } + const end = this.position + length; + if (end > max) { + return undefined; + } + try { + const tags = {}; + while (this.position < end) { + const tag = this._uint32(); + if (tag === undefined) { + this.seek(end); + return 2; + } + const field = tag >>> 3; + const type = tag & 7; + if (type > 5 || field === 0) { + this.seek(end); + return 2; + } + if (type === 2) { + const type = tags[field]; + if (type !== 2) { + const inner = decodeMessage(end); + if (this.position > end) { + this.seek(end); + return 2; + } + if (inner === undefined) { + this.seek(end); + return 2; + } + if (inner === 2) { + tags[field] = inner; + } + else if (!type) { + tags[field] = inner; + } + else { + for (const pair of Object.entries(inner)) { + if (type[pair[0]] === 2 && pair[1] !== 2) { + continue; + } + type[pair[0]] = pair[1]; + } + } + continue; + } + } + tags[field] = type; + if (!this._skipType(type)) { + this.seek(end); + return 2; + } + } + if (this.position === end) { + return tags; + } + } + catch (err) { + // continue regardless of error + } + this.seek(end); + return 2; + }; + if (this._length > 0) { + const type = this._buffer[0] & 7; + if (type !== 4 && type !== 6 && type !== 7) { + const length = this.length; + while (this.position < length) { + const tag = this.uint32(); + const field = tag >>> 3; + const type = tag & 7; + if (type > 5 || field === 0) { + tags = {}; + break; + } + if (type === 2) { + const type = tags[field]; + if (type !== 2) { + const inner = decodeMessage(length); + if (inner === undefined) { + tags = {}; + break; + } + if (inner === 2) { + tags[field] = inner; + } + else if (!type) { + tags[field] = inner; + } + else { + for (const pair of Object.entries(inner)) { + if (type[pair[0]] === 2 && pair[1] !== 2) { + continue; + } + type[pair[0]] = pair[1]; + } + } + continue; + } + } + tags[field] = type; + if (!this._skipType(type)) { + tags = {}; + break; + } + } + } + } + } + catch (err) { + tags = {}; + } + this._position = 0; + return tags; + } + + get length() { + return this._length; + } + + get position() { + return this._position; + } + + seek(position) { + this._position = position >= 0 ? position : this._length + position; + } + + string() { + return this._utf8Decoder.decode(this.bytes()); + } + + bool() { + return this.uint32() !== 0; + } + + byte() { + if (this._position < this._length) { + return this._buffer[this._position++]; + } + throw new RangeError('Unexpected end of file.'); + } + + bytes() { + const length = this.uint32(); + const position = this._position; + this.skip(length); + return this._buffer.slice(position, this._position); + } + + uint32() { + let c; + c = this.byte(); + let value = (c & 127) >>> 0; + if (c < 128) { + return value; + } + c = this.byte(); + value = (value | (c & 127) << 7) >>> 0; + if (c < 128) { + return value; + } + c = this.byte(); + value = (value | (c & 127) << 14) >>> 0; + if (c < 128) { + return value; + } + c = this.byte(); + value = (value | (c & 127) << 21) >>> 0; + if (c < 128) { + return value; + } + c = this.byte(); + value = (value | (c & 15) << 28) >>> 0; + if (c < 128) { + return value; + } + if (this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 1) { + throw new protobuf.Error('Varint is not 32-bit.'); + } + return value; + } + + int32() { + return this.uint32() | 0; + } + + sint32() { + const value = this.uint32(); + return value >>> 1 ^ -(value & 1) | 0; + } + + int64() { + return this._varint().toInt64(); + } + + uint64() { + return this._varint().toInt64(); + } + + sint64() { + return this._varint().zzDecode().toInt64(); + } + + fixed64() { + const position = this._position; + this.skip(8); + return this._view.getUint64(position, true); + } + + sfixed64() { + const position = this._position; + this.skip(8); + return this._view.getInt64(position, true); + } + + fixed32() { + const position = this._position; + this.skip(4); + return this._view.getUint32(position, true); + } + + sfixed32() { + const position = this._position; + this.skip(4); + return this._view.getInt32(position, true); + } + + float() { + const position = this._position; + this.skip(4); + return this._view.getFloat32(position, true); + } + + double() { + const position = this._position; + this.skip(8); + return this._view.getFloat64(position, true); + } + + array(obj, item, tag) { + if ((tag & 7) === 2) { + const end = this.uint32() + this._position; + while (this._position < end) { + obj.push(item()); + } + } + else { + obj.push(item()); + } + return obj; + } + + floats(obj, tag) { + if ((tag & 7) === 2) { + if (obj && obj.length > 0) { + throw new protobuf.Error('Invalid packed float array.'); + } + const size = this.uint32(); + const end = this._position + size; + if (end > this._length) { + this._unexpected(); + } + const length = size >>> 2; + obj = size > 1048576 ? new Float32Array(length) : new Array(length); + let position = this._position; + for (let i = 0; i < length; i++) { + obj[i] = this._view.getFloat32(position, true); + position += 4; + } + this._position = end; + } + else { + if (obj !== undefined && obj.length < 1000000) { + obj.push(this.float()); + } + else { + obj = undefined; + this.float(); + } + } + return obj; + } + + doubles(obj, tag) { + if ((tag & 7) === 2) { + if (obj && obj.length > 0) { + throw new protobuf.Error('Invalid packed float array.'); + } + const size = this.uint32(); + const end = this._position + size; + if (end > this._length) { + this._unexpected(); + } + const length = size >>> 3; + obj = size > 1048576 ? new Float64Array(length) : new Array(length); + let position = this._position; + for (let i = 0; i < length; i++) { + obj[i] = this._view.getFloat64(position, true); + position += 8; + } + this._position = end; + } + else { + if (obj !== undefined && obj.length < 1000000) { + obj.push(this.double()); + } + else { + obj = undefined; + this.double(); + } + } + return obj; + } + + skip(offset) { + this._position += offset; + if (this._position > this._length) { + this._unexpected(); + } + } + + skipVarint() { + do { + if (this._position >= this._length) { + this._unexpected(); + } + } + while (this._buffer[this._position++] & 128); + } + + _uint32() { + let c; + if (this._position < this._length) { + c = this._buffer[this._position++]; + let value = (c & 127) >>> 0; + if (c < 128) { + return value; + } + if (this._position < this._length) { + c = this._buffer[this._position++]; + value = (value | (c & 127) << 7) >>> 0; + if (c < 128) { + return value; + } + if (this._position < this._length) { + c = this._buffer[this._position++]; + value = (value | (c & 127) << 14) >>> 0; + if (c < 128) { + return value; + } + if (this._position < this._length) { + c = this._buffer[this._position++]; + value = (value | (c & 127) << 21) >>> 0; + if (c < 128) { + return value; + } + if (this._position < this._length) { + c = this._buffer[this._position++]; + value = (value | (c & 15) << 28) >>> 0; + if (c < 128) { + return value; + } + if (this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 1) { + return undefined; + } + return value; + } + } + } + } + } + return undefined; + } + + _skipType(wireType) { + switch (wireType) { + case 0: { + // const max = this._position + 9; + do { + if (this._position >= this._length /* || this._position > max */) { + return false; + } + } + while (this._buffer[this._position++] & 128); + break; + } + case 1: { + if (this._position + 8 >= this._length) { + return false; + } + this._position += 8; + break; + } + case 2: { + const length = this._uint32(); + if (length === undefined) { + return false; + } + if (this._position + length > this._end) { + return false; + } + this._position += length; + break; + } + case 3: { + for (;;) { + const tag = this._uint32(); + if (tag === undefined) { + return false; + } + const wireType = tag & 7; + if (wireType === 4) { + break; + } + if (!this._skipType(wireType)) { + return false; + } + } + break; + } + case 5: { + if (this._position + 4 >= this._length) { + return false; + } + this._position += 4; + break; + } + default: { + return false; + } + } + return true; + } + + skipType(wireType) { + switch (wireType) { + case 0: + this.skipVarint(); + break; + case 1: + this.skip(8); + break; + case 2: + this.skip(this.uint32()); + break; + case 3: + while ((wireType = this.uint32() & 7) !== 4) { + this.skipType(wireType); + } + break; + case 5: + this.skip(4); + break; + default: + throw new protobuf.Error('Invalid type ' + wireType + ' at offset ' + this._position + '.'); + } + } + + entry(obj, key, value) { + this.skipVarint(); + this._position++; + let k = key(); + if (!Number.isInteger(k) && typeof k !== 'string') { + k = k.toNumber(); + } + this._position++; + const v = value(); + obj[k] = v; + } + + _varint() { + const bits = new protobuf.LongBits(0, 0); + let i = 0; + if (this._length - this._position > 4) { // fast route (lo) + for (; i < 4; ++i) { + // 1st..4th + bits.lo = (bits.lo | (this._buffer[this._position] & 127) << i * 7) >>> 0; + if (this._buffer[this._position++] < 128) { + return bits; + } + } + // 5th + bits.lo = (bits.lo | (this._buffer[this._position] & 127) << 28) >>> 0; + bits.hi = (bits.hi | (this._buffer[this._position] & 127) >> 4) >>> 0; + if (this._buffer[this._position++] < 128) { + return bits; + } + i = 0; + } + else { + for (; i < 3; i++) { + if (this._position >= this._length) { + this._unexpected(); + } + bits.lo = (bits.lo | (this._buffer[this._position] & 127) << i * 7) >>> 0; + if (this._buffer[this._position++] < 128) { + return bits; + } + } + bits.lo = (bits.lo | (this._buffer[this._position++] & 127) << i * 7) >>> 0; + return bits; + } + if (this._length - this._position > 4) { + for (; i < 5; ++i) { + bits.hi = (bits.hi | (this._buffer[this._position] & 127) << i * 7 + 3) >>> 0; + if (this._buffer[this._position++] < 128) { + return bits; + } + } + } + else { + for (; i < 5; ++i) { + if (this._position >= this._length) { + this._unexpected(); + } + bits.hi = (bits.hi | (this._buffer[this._position] & 127) << i * 7 + 3) >>> 0; + if (this._buffer[this._position++] < 128) { + return bits; + } + } + } + throw new protobuf.Error('Invalid varint encoding.'); + } + + _unexpected() { + throw new RangeError('Unexpected end of file.'); + } +}; + +protobuf.TextReader = class { + + static open(data) { + const buffer = data instanceof Uint8Array ? data : data.peek(); + const decoder = text.Decoder.open(buffer); + let first = true; + for (let i = 0; i < 0x100; i++) { + const c = decoder.decode(); + if (c === undefined) { + if (i === 0) { + return null; + } + break; + } + if (c === '\0') { + return null; + } + const whitespace = c === ' ' || c === '\n' || c === '\r' || c === '\t'; + if (c < ' ' && !whitespace) { + return null; + } + if (first && !whitespace) { + first = false; + if (c === '#') { + let c; + do { + c = decoder.decode(); + } + while (c !== undefined && c !== '\n'); + if (c === undefined) { + break; + } + continue; + } + if (c === '[') { + continue; + } + if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { + continue; + } + return null; + } + } + return new protobuf.TextReader(buffer); + } + + constructor(buffer) { + this._decoder = text.Decoder.open(buffer); + this.reset(); + } + + signature() { + const tags = new Map(); + this.reset(); + try { + this.start(false); + while (!this.end()) { + const tag = this.tag(); + if (this.token() === '{') { + this.start(); + tags.set(tag, true); + while (!this.end()) { + const subtag = this.tag(); + tags.set(tag + '.' + subtag, true); + this.skip(); + this.match(','); + } + } + else { + this.skip(); + tags.set(tag, true); + } + } + } + catch (err) { + // continue regardless of error + } + this.reset(); + return tags; + } + + reset() { + this._decoder.position = 0; + this._position = 0; + this._token = undefined; + this._depth = 0; + this._arrayDepth = 0; + this._token = ''; + this.next(); + } + + start() { + if (this._depth > 0) { + this.expect('{'); + } + this._depth++; + } + + end() { + if (this._depth <= 0) { + throw new protobuf.Error('Invalid depth ' + this.location()); + } + if (this._token === '}') { + this.expect('}'); + this.match(';'); + this._depth--; + return true; + } + if (this._token === undefined) { + if (this._depth !== 1) { + throw new protobuf.Error('Unexpected end of input' + this.location()); + } + this._depth--; + return true; + } + return false; + } + + tag() { + const name = this._token; + this.next(); + if (this._token !== '[' && this._token !== '{') { + this.expect(':'); + } + return name; + } + + integer() { + const token = this._token; + const value = Number.parseInt(token, 10); + if (Number.isNaN(token - value)) { + throw new protobuf.Error("Couldn't parse integer '" + token + "'" + this.location()); + } + this.next(); + this.semicolon(); + return value; + } + + double() { + let value = NaN; + let token = this._token; + switch (token) { + case 'nan': value = NaN; break; + case 'inf': value = Infinity; break; + case '-inf': value = -Infinity; break; + default: + if (token.endsWith('f')) { + token = token.substring(0, token.length - 1); + } + value = Number.parseFloat(token); + if (Number.isNaN(token - value)) { + throw new protobuf.Error("Couldn't parse float '" + token + "'" + this.location()); + } + break; + } + this.next(); + this.semicolon(); + return value; + } + + float() { + return this.double(); + } + + uint32() { + return this.integer(); + } + + int32() { + return this.integer(); + } + + sint32() { + return this.integer(); + } + + int64() { + return base.Int64.create(this.integer()); + } + + uint64() { + return base.Uint64.create(this.integer()); + } + + sint64() { + return base.Int64.create(this.integer()); + } + + fixed64() { + return base.Uint64.create(this.integer()); + } + + sfixed64() { + return base.Int64.create(this.integer()); + } + + fixed32() { + return this.integer(); + } + + sfixed32() { + return this.integer(); + } + + string() { + const token = this._token; + if (token.length < 2) { + throw new protobuf.Error('String is too short' + this.location()); + } + const quote = token[0]; + if (quote !== "'" && quote !== '"') { + throw new protobuf.Error('String is not in quotes' + this.location()); + } + if (quote !== token[token.length - 1]) { + throw new protobuf.Error('String quotes do not match' + this.location()); + } + const value = token.substring(1, token.length - 1); + this.next(); + this.semicolon(); + return value; + } + + bool() { + const token = this._token; + switch (token) { + case 'true': + case 'True': + case '1': + this.next(); + this.semicolon(); + return true; + case 'false': + case 'False': + case '0': + this.next(); + this.semicolon(); + return false; + } + throw new protobuf.Error("Couldn't parse boolean '" + token + "'" + this.location()); + } + + bytes() { + const token = this.string(); + const length = token.length; + const array = new Uint8Array(length); + for (let i = 0; i < length; i++) { + array[i] = token.charCodeAt(i); + } + return array; + } + + enum(type) { + const token = this._token; + let value; + if (Object.prototype.hasOwnProperty.call(type, token)) { + value = type[token]; + } + else { + value = Number.parseInt(token, 10); + if (Number.isNaN(token - value)) { + throw new protobuf.Error("Couldn't parse enum '" + (token === undefined ? '' : token) + "'" + this.location()); + } + } + this.next(); + this.semicolon(); + return value; + } + + any(type) { + this.start(); + const message = type(); + if (this._token.startsWith('[') && this._token.endsWith(']')) { + message.type_url = this._token.substring(1, this._token.length - 1).trim(); + this.next(); + this.match(':'); + message.value = this.read(); + this.match(';'); + if (!this.end()) { + this.expect('}'); + } + } + else { + while (!this.end()) { + const tag = this.tag(); + switch (tag) { + case "type_url": + message.type_url = this.string(); + break; + case "value": + message.value = this.bytes(); + break; + default: + this.field(tag, message); + break; + } + } + } + return message; + } + + anyarray(obj, type) { + this.start(); + if (this._token.startsWith('[') && this._token.endsWith(']')) { + while (!this.end()) { + if (this._token.startsWith('[') && this._token.endsWith(']')) { + const message = type(); + message.type_url = this._token.substring(1, this._token.length - 1).trim(); + this.next(); + this.match(':'); + message.value = this.read(); + this.match(';'); + obj.push(message); + continue; + } + this.expect('['); + } + } + else { + const message = type(); + while (!this.end()) { + const tag = this.tag(); + switch (tag) { + case "type_url": + message.type_url = this.string(); + break; + case "value": + message.value = this.bytes(); + break; + default: + this.field(tag, message); + break; + } + } + obj.push(message); + } + } + + entry(obj, key, value) { + this.start(); + let k; + let v; + while (!this.end()) { + switch (this.tag()) { + case 'key': + k = key(); + break; + case 'value': + v = value(); + break; + } + } + obj[k] = v; + } + + array(obj, item) { + if (this.first()) { + while (!this.last()) { + obj.push(item()); + switch (this._token) { + case ',': + this.next(); + break; + case ']': + break; + default: + this.handle(this._token); + break; + } + } + } + else { + obj.push(item()); + } + } + + first() { + if (this.match('[')) { + this._arrayDepth++; + return true; + } + return false; + } + + last() { + if (this.match(']')) { + this._arrayDepth--; + return true; + } + return false; + } + + read() { + const start = this._position; + this.skip(); + const end = this._position; + const position = this._decoder.position; + this._decoder.position = start; + let content = ''; + while (this._decoder.position < end) { + content += this._decoder.decode(); + } + this._decoder.position = position; + return content; + } + + skip() { + switch (this._token) { + case '{': { + const depth = this._depth; + this.start(); + while (!this.end() || depth < this._depth) { + if (this._token === '{') { + this.start(); + } + else if (this._token !== '}') { + this.next(); + this.match(';'); + } + } + break; + } + case '[': { + const depth = this._arrayDepth; + this.first(); + while (!this.last() || depth < this._arrayDepth) { + this.next(); + if (this._token === '[') { + this.first(); + } + else if (this._token === undefined) { + this.handle(this._token); + } + } + break; + } + default: { + this.next(); + this.semicolon(); + break; + } + } + } + + handle(token) { + throw new protobuf.Error("Unexpected token '" + token + "'" + this.location()); + } + + field(token /*, module */) { + throw new protobuf.Error("Unknown field '" + token + "'" + this.location()); + } + + token() { + return this._token; + } + + next() { + if (this._token === undefined) { + throw new protobuf.Error('Unexpected end of input' + this.location()); + } + this._position = this._decoder.position; + let c = this._decoder.decode(); + for (;;) { + switch (c) { + case ' ': + case '\n': + case '\r': + case '\t': + this._position = this._decoder.position; + c = this._decoder.decode(); + continue; + case '#': + do { + c = this._decoder.decode(); + if (c === undefined) { + this._token = undefined; + return; + } + } + while (c !== '\n'); + this._position = this._decoder.position; + c = this._decoder.decode(); + continue; + } + break; + } + if (c === undefined) { + this._token = undefined; + return; + } + if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c === '_' || c === '$') { + let token = c; + let position = this._decoder.position; + for (;;) { + c = this._decoder.decode(); + if (c === undefined || c === '\n') { + break; + } + if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c === '_' || c === '+' || c === '-') { + token += c; + position = this._decoder.position; + continue; + } + break; + } + this._decoder.position = position; + this._token = token; + return; + } + switch (c) { + case '{': + case '}': + case ':': + case ',': + case ']': + case ';': + this._token = c; + return; + case '[': { + let token = c; + let position = this._decoder.position; + let x = this._decoder.decode(); + if ((x !== undefined) && x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z') { + token += x; + for (;;) { + x = this._decoder.decode(); + if (x === undefined || x === '\n') { + break; + } + if (x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z' || x >= '0' && x <= '9' || x === '.' || x === '/') { + token += x; + position = this._decoder.position; + continue; + } + if (x === ']') { + this._token = token + x; + return; + } + } + } + this._decoder.position = position; + this._token = '['; + return; + } + case '"': + case "'": { + const quote = c; + let content = c; + for (;;) { + c = this._decoder.decode(); + if (c === undefined || c === '\n') { + throw new protobuf.Error('Unexpected end of string' + this.location()); + } + if (c == '\\') { + c = this._decoder.decode(); + if (c === undefined || c === '\n') { + throw new protobuf.Error('Unexpected end of string' + this.location()); + } + switch (c) { + case '\\': c = '\\'; break; + case "'": c = "'"; break; + case '"': c = '"'; break; + case 'r': c = '\r'; break; + case 'n': c = '\n'; break; + case 't': c = '\t'; break; + case 'b': c = '\b'; break; + case 'x': + case 'X': { + let value = 0; + for (let xi = 0; xi < 2; xi++) { + let xd = this._decoder.decode(); + if (xd === undefined) { + throw new protobuf.Error('Unexpected end of string' + this.location()); + } + xd = xd.charCodeAt(0); + xd = xd >= 65 && xd <= 70 ? xd - 55 : xd >= 97 && xd <= 102 ? xd - 87 : xd >= 48 && xd <= 57 ? xd - 48 : -1; + if (xd === -1) { + throw new protobuf.Error("Unexpected hex digit '" + xd + "' in bytes string" + this.location()); + } + value = value << 4 | xd; + } + c = String.fromCharCode(value); + break; + } + default: { + if (c < '0' || c > '9') { + throw new protobuf.Error("Unexpected character '" + c + "' in string" + this.location()); + } + let value = 0; + let od = c; + if (od < '0' || od > '9') { + throw new protobuf.Error("Unexpected octal digit '" + od + "' in bytes string" + this.location()); + } + od = od.charCodeAt(0); + value = value << 3 | od - 48; + od = this._decoder.decode(); + if (od === undefined) { + throw new protobuf.Error('Unexpected end of string' + this.location()); + } + if (od < '0' || od > '9') { + throw new protobuf.Error("Unexpected octal digit '" + od + "' in bytes string" + this.location()); + } + od = od.charCodeAt(0); + value = value << 3 | od - 48; + od = this._decoder.decode(); + if (od === undefined) { + throw new protobuf.Error('Unexpected end of string' + this.location()); + } + if (od < '0' || od > '9') { + throw new protobuf.Error("Unexpected octal digit '" + od + "' in bytes string" + this.location()); + } + od = od.charCodeAt(0); + value = value << 3 | od - 48; + c = String.fromCharCode(value); + break; + } + } + content += c; + continue; + } + else { + content += c; + if (c === quote) { + break; + } + } + } + this._token = content; + return; + } + case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': + case '-': case '+': case '.': { + let token = c; + let position = this._decoder.position; + for (;;) { + c = this._decoder.decode(); + if (c === undefined || c === '\n') { + break; + } + if ((c >= '0' && c <= '9') || c === '_' || c === '+' || c === '-' || c === '.' || c === 'e' || c === 'E') { + token += c; + position = this._decoder.position; + continue; + } + break; + } + if (token === '-' && c === 'i' && this._decoder.decode() === 'n' && this._decoder.decode() === 'f') { + token += 'inf'; + position = this._decoder.position; + } + if (token !== '-' && token !== '+' && token !== '.') { + this._decoder.position = position; + this._token = token; + return; + } + } + } + throw new protobuf.Error("Unexpected token '" + c + "'" + this.location()); + } + + expect(value) { + if (this._token !== value) { + throw new protobuf.Error("Unexpected '" + this._token + "' instead of '" + value + "'" + this.location()); + } + this.next(); + } + + match(value) { + if (value == this._token) { + this.next(); + return true; + } + return false; + } + + location() { + let line = 1; + let column = 1; + this._decoder.position = 0; + let c; + do { + if (this._decoder.position === this._position) { + return ' at ' + line.toString() + ':' + column.toString() + '.'; + } + c = this._decoder.decode(); + if (c === '\n') { + line++; + column = 1; + } + else { + column++; + } + } + while (c !== undefined); + return ' at ' + line.toString() + ':' + column.toString() + '.'; + } + + semicolon() { + if (this._arrayDepth === 0) { + this.match(';'); + } + } +}; + +protobuf.Int64 = base.Int64; +protobuf.Uint64 = base.Uint64; + +protobuf.LongBits = class { + + constructor(lo, hi) { + this.lo = lo >>> 0; + this.hi = hi >>> 0; + } + + zzDecode() { + const mask = -(this.lo & 1); + this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0; + this.hi = ( this.hi >>> 1 ^ mask) >>> 0; + return this; + } + + toUint64() { + return new base.Uint64(this.lo, this.hi); + } + + toInt64() { + return new base.Int64(this.lo, this.hi); + } +}; + +protobuf.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'Protocol Buffer Error'; + this.message = message; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.BinaryReader = protobuf.BinaryReader; + module.exports.TextReader = protobuf.TextReader; + module.exports.Error = protobuf.Error; + module.exports.Int64 = protobuf.Int64; + module.exports.Uint64 = protobuf.Uint64; + module.exports.get = protobuf.get; +} \ No newline at end of file diff --git a/python.js b/python.js new file mode 100644 index 0000000..acb1419 --- /dev/null +++ b/python.js @@ -0,0 +1,4090 @@ + +// Experimental Python parser + +var python = python || {}; + +python.Parser = class { + + constructor(text, file, debug) { + this._tokenizer = new python.Tokenizer(text, file); + this._debug = debug; + if (!python.Parser._precedence) { + python.Parser._precedence = { + 'or': 2, 'and': 3, 'not' : 4, + 'in': 5, 'instanceof': 5, 'is': 5, '<': 5, '>': 5, '<=': 5, '>=': 5, '<>': 5, '==': 5, '!=': 5, + '|': 6, '^' : 7, '&' : 8, + '<<': 9, '>>': 9, '+': 10, '-': 10, '*': 11, '@': 11, '/': 11, '//': 11, '%': 11, + // '+': 12, '-': 12, + '~': 13, '**': 14 + }; + } + } + + parse() { + const node = this._node('program'); + node.body = []; + while (!this._tokenizer.match('eof')) { + const statement = this._parseStatement(); + if (statement) { + node.body.push(statement); + continue; + } + if (this._tokenizer.eat('\n') || this._tokenizer.eat(';') || this._tokenizer.peek().type == 'eof') { + continue; + } + if (this._tokenizer.eat('indent') && this._tokenizer.peek().type == 'eof') { + continue; + } + throw new python.Error('Unknown statement' + this._tokenizer.location()); + } + return node; + } + + _parseSuite() { + const node = this._node('block'); + node.statements = []; + let statement = null; + if (this._tokenizer.eat('\n')) { + if (this._tokenizer.eat('indent')) { + while (!this._tokenizer.eat('eof') && !this._tokenizer.eat('dedent')) { + if (this._tokenizer.eat(';')) { + continue; + } + statement = this._parseStatement(); + if (statement) { + node.statements.push(statement); + continue; + } + if (this._tokenizer.eat('\n')) { + continue; + } + if (this._tokenizer.match('dedent') || this._tokenizer.match('eof')) { + continue; + } + throw new python.Error('Empty statement' + this._tokenizer.location()); + } + } + } + else if (!this._tokenizer.eat('eof')) { + while (!this._tokenizer.match('\n') && !this._tokenizer.match('eof') && !this._tokenizer.match('dedent')) { + if (this._tokenizer.eat(';')) { + continue; + } + statement = this._parseStatement(); + if (statement) { + node.statements.push(statement); + continue; + } + throw new python.Error('Empty statement' + this._tokenizer.location()); + } + this._tokenizer.eat('\n'); + } + + return node; + } + + _parseStatement() { + + let node = this._node(); + + node = this._eat('id', 'break'); + if (node) { + return node; + } + node = this._eat('id', 'continue'); + if (node) { + return node; + } + node = this._eat('id', 'return'); + if (node) { + node.expression = this._parseExpression(-1, [], true); + return node; + } + node = this._eat('id', 'raise'); + if (node) { + node.exception = this._parseExpression(-1, [ 'from' ]); + if (this._tokenizer.eat('id', 'from')) { + node.from = this._parseExpression(); + } + else if (this._tokenizer.eat(',')) { + node.exception = [ node.exception ]; + node.exception.push(this._parseExpression()); + if (this._tokenizer.eat(',')) { + node.exception.push(this._parseExpression()); + } + } + return node; + } + node = this._eat('id', 'assert'); + if (node) { + node.condition = this._parseExpression(); + while (this._tokenizer.eat(',')) { + node.condition = { type: 'list', value: [ node.condition ] }; + node.condition.value.push(this._parseExpression()); + } + return node; + } + node = this._eat('id', 'exec'); + if (node) { + node.variable = this._parseExpression(-1, [ 'in' ]); + if (this._tokenizer.eat('in')) { + do { + node.target = node.target || []; + node.target.push(this._parseExpression(-1, [ 'in' ], false)); + } + while (this._tokenizer.eat(',')); + } + return node; + } + + node = this._eat('id', 'global'); + if (node) { + node.variable = []; + do { + node.variable.push(this._parseName()); + } + while (this._tokenizer.eat(',')); + return node; + } + node = this._eat('id', 'nonlocal'); + if (node) { + node.variable = []; + do { + node.variable.push(this._parseName()); + } + while (this._tokenizer.eat(',')); + return node; + } + node = this._eat('id', 'import'); + if (node) { + node.modules = []; + do { + const module = this._node('module'); + module.name = this._parseExpression(-1, [], false); + if (this._tokenizer.eat('id', 'as')) { + module.as = this._parseExpression(-1, [], false); + } + node.modules.push(module); + } + while (this._tokenizer.eat(',')); + return node; + } + node = this._eat('id', 'from'); + if (node) { + const dots = this._tokenizer.peek(); + if (dots && Array.from(dots.type).every((c) => c == '.')) { + node.from = this._eat(dots.type); + node.from.expression = this._parseExpression(); + } + else { + node.from = this._parseExpression(); + } + this._tokenizer.expect('id', 'import'); + node.import = []; + const close = this._tokenizer.eat('('); + do { + const symbol = this._node(); + symbol.symbol = this._parseExpression(-1, [], false); + if (this._tokenizer.eat('id', 'as')) { + symbol.as = this._parseExpression(-1, [], false); + } + node.import.push(symbol); + } + while (this._tokenizer.eat(',')); + if (close) { + this._tokenizer.expect(')'); + } + return node; + } + node = this._eat('id', 'class'); + if (node) { + node.name = this._parseName().value; + if (this._tokenizer.peek().value === '(') { + node.base = this._parseArguments(); + } + this._tokenizer.expect(':'); + node.body = this._parseSuite(); + return node; + } + + const async = this._eat('id', 'async'); + if (async && + !this._tokenizer.match('id', 'def') && + !this._tokenizer.match('id', 'with') && + !this._tokenizer.match('id', 'for')) { + throw new python.Error("Expected 'def', 'with' or 'for'" + this._tokenizer.location()); + } + + node = this._eat('id', 'def'); + if (node) { + if (async) { + node.async = async; + } + node.name = this._parseName().value; + this._tokenizer.expect('('); + node.parameters = this._parseParameters(')'); + if (this._tokenizer.eat('->')) { + node.returnType = this._parseType(); + } + this._tokenizer.expect(':'); + node.body = this._parseSuite(); + return node; + } + node = this._eat('id', 'del'); + if (node) { + node.expression = this._parseExpression(-1, [], true); + return node; + } + node = this._eat('id', 'print'); + if (node) { + node.expression = this._parseExpression(-1, [], true); + return node; + } + node = this._eat('id', 'if'); + if (node) { + node.condition = this._parseExpression(); + this._tokenizer.expect(':'); + node.then = this._parseSuite(); + let current = node; + this._tokenizer.eat('\n'); + while (this._tokenizer.eat('id', 'elif')) { + current.else = this._node('if'); + current = current.else; + current.condition = this._parseExpression(); + this._tokenizer.expect(':'); + current.then = this._parseSuite(); + this._tokenizer.eat('\n'); + } + if (this._tokenizer.eat('id', 'else')) { + this._tokenizer.expect(':'); + current.else = this._parseSuite(); + } + return node; + } + node = this._eat('id', 'while'); + if (node) { + node.condition = this._parseExpression(); + this._tokenizer.expect(':'); + node.body = this._parseSuite(); + if (this._tokenizer.eat('id', 'else')) { + this._tokenizer.expect(':'); + node.else = this._parseSuite(); + } + return node; + } + node = this._eat('id', 'pass'); + if (node) { + return node; + } + node = this._eat('id', 'for'); + if (node) { + node.variable = []; + node.variable.push(this._parseExpression(-1, [ 'in' ])); + while (this._tokenizer.eat(',')) { + if (this._tokenizer.match('id', 'in')) { + node.variable.push({}); + break; + } + node.variable.push(this._parseExpression(-1, [ 'in' ])); + } + this._tokenizer.expect('id', 'in'); + node.target = []; + node.target.push(this._parseExpression()); + while (this._tokenizer.eat(',')) { + if (this._tokenizer.match(':')) { + node.target.push({}); + break; + } + node.target.push(this._parseExpression(-1, [ 'in' ])); + } + this._tokenizer.expect(':'); + node.body = this._parseSuite(); + if (this._tokenizer.eat('id', 'else')) { + this._tokenizer.expect(':'); + node.else = this._parseSuite(); + } + return node; + } + node = this._eat('id', 'with'); + if (node) { + if (async) { + node.async = async; + } + node.item = []; + do { + const item = this._node(); + item.type = 'with_item'; + item.expression = this._parseExpression(); + if (this._tokenizer.eat('id', 'as')) { + item.variable = this._parseExpression(); + } + node.item.push(item); + } + while (this._tokenizer.eat(',')); + this._tokenizer.expect(':'); + node.body = this._parseSuite(); + return node; + } + node = this._eat('id', 'try'); + if (node) { + this._tokenizer.expect(':'); + node.body = this._parseSuite(); + node.except = []; + while (this._tokenizer.match('id', 'except')) { + const except = this._node('except'); + this._tokenizer.expect('id', 'except'); + except.clause = []; + except.clause.push(this._parseExpression()); + while (this._tokenizer.eat(',')) { + if (this._tokenizer.match(':') || this._tokenizer.match('as')) { + except.clause.push({}); + break; + } + except.clause.push(this._parseExpression()); + } + if (this._tokenizer.eat('id', 'as')) { + except.variable = this._parseExpression(); + } + this._tokenizer.expect(':'); + except.body = this._parseSuite(); + node.except.push(except); + } + if (this._tokenizer.match('id', 'else')) { + node.else = this._node('else'); + this._tokenizer.expect('id', 'else'); + this._tokenizer.expect(':'); + node.else.body = this._parseSuite(); + } + if (this._tokenizer.match('id', 'finally')) { + node.finally = this._node('finally'); + this._tokenizer.expect('id', 'finally'); + this._tokenizer.expect(':'); + node.finally.body = this._parseSuite(); + } + return node; + } + + if (this._tokenizer.match('@')) { + node = this._node('decorator'); + this._tokenizer.expect('@'); + node.value = this._parseExpression(); + if (!node.value || (node.value.type !== 'call' && node.value.type !== 'id' && node.value.type !== '.')) { + throw new python.Error('Invalid decorator' + this._tokenizer.location()); + } + return node; + } + + const expression = this._parseExpression(-1, [], true); + if (expression) { + if (expression.type == 'id' && this._tokenizer.eat(':')) { + node = this._node('var'); + node.name = expression.value; + node.location = expression.location; + node.variableType = this._parseExpression(-1, [ '=' ]); + if (this._tokenizer.eat('=')) { + node.initializer = this._parseExpression(); + } + return node; + } + let statement = false; + switch (expression.type) { + case '=': + case ':=': + case '==': + case '!=': + case '+=': + case '-=': + case '*=': + case '@=': + case '/=': + case '//=': + case '**=': + case '&=': + case '|=': + case '%=': + case '>>=': + case '<<=': + case '>>': + case '<<': + case '>=': + case '<=': + case '<': + case '>': + case '%': + case '^=': + case '...': + case 'call': + case 'assert': + case 'raise': + case 'string': + case 'list': + case 'var': + case '.': + case '[]': + case 'yield': + case '+': + case '-': + case '*': + case '**': + case '@': + case '/': + case '//': + case '~': + case '&': + case '^': + case '|': + case 'not': + case 'id': + case 'number': + case 'in': + case 'and': + case 'or': + case 'if': + case 'for': + case 'tuple': + case 'lambda': + case 'await': + statement = true; + break; + } + if (statement) { + return expression; + } + throw new python.Error("Unhandled expression" + this._tokenizer.location()); + } + + return null; + } + + _parseExpression(minPrecedence, terminal, tuple) { + minPrecedence = minPrecedence || -1; + const terminalSet = new Set(terminal); + const stack = []; + for (;;) { + let node = this._node(); + const token = this._tokenizer.peek(); + if (stack.length == 1 && terminalSet.has(token.value)) { + break; + } + const precedence = python.Parser._precedence[token.value]; + if (precedence) { + if (precedence >= minPrecedence) { + this._tokenizer.read(); + node.type = token.value; + if (token.type == 'id' && (token.value === 'in' || token.value === 'not')) { + if (token.value === 'in') { + node.type = 'in'; + } + else if (this._tokenizer.eat('id', 'in')) { + node.type = 'not in'; + } + else { + node.type = 'not'; + node.expression = this._parseExpression(precedence, terminal, tuple === false ? false : true); + stack.push(node); + continue; + } + } + else if (token.value == '~') { + node.type = '~'; + node.expression = this._parseExpression(precedence, terminal, tuple === false ? false : true); + stack.push(node); + continue; + } + else if (token.type == 'id' && token.value == 'is') { + if (this._tokenizer.eat('id', 'not')) { + node.type = 'is not'; + } + } + node.left = stack.pop(); + node.right = this._parseExpression(precedence, terminal, tuple === false ? false : true); + stack.push(node); + continue; + } + } + if (this._tokenizer.eat(':=')) { + node.type = ':='; + node.target = stack.pop(); + node.expression = this._parseExpression(-1, terminal, tuple === false ? false : true); + stack.push(node); + continue; + } + if (this._tokenizer.eat('=')) { + node.type = '='; + node.target = stack.pop(); + node.expression = this._parseExpression(-1, terminal, tuple === false ? false : true); + stack.push(node); + continue; + } + switch (token.type) { + case '-=': + case '**=': + case '*=': + case '//=': + case '/=': + case '&=': + case '%=': + case '^=': + case '+=': + case '<<=': + case '>>=': + case '|=': + case '@=': + node = this._node(token.type); + this._tokenizer.expect(token.type); + node.target = stack.pop(); + node.expression = this._parseExpression(-1, terminal, true); + stack.push(node); + continue; + } + node = this._eat('id', 'if'); + if (node) { + node.then = stack.pop(); + node.condition = this._parseExpression(); + this._tokenizer.expect('id', 'else'); + node.else = this._parseExpression(); + stack.push(node); + continue; + } + while (this._tokenizer.match('id', 'for') || this._tokenizer.match('id', 'async')) { + const async = this._eat('id', 'async'); + if (async && !this._tokenizer.match('id', 'for')) { + throw new python.Error("Expected 'for'" + this._tokenizer.location()); + } + node = this._eat('id', 'for'); + if (node) { + if (async) { + node.async = async; + } + node.expression = stack.pop(); + node.variable = this._parseExpression(-1, [ 'in' ], true); + this._tokenizer.expect('id', 'in'); + node.target = this._parseExpression(-1, [ 'for', 'if' ], true); + while (this._tokenizer.eat('id', 'if')) { + node.condition = node.condition || []; + node.condition.push(this._parseExpression(-1, [ 'for', 'if' ])); + } + stack.push(node); + } + } + node = this._eat('id', 'lambda'); + if (node) { + node.parameters = this._parseParameters(':'); + node.body = this._parseExpression(-1, terminal, false); + stack.push(node); + continue; + } + node = this._eat('id', 'yield'); + if (node) { + if (this._tokenizer.eat('id', 'from')) { + node.from = this._parseExpression(-1, [], true); + } + else { + node.expression = []; + do { + node.expression.push(this._parseExpression(-1, [], false)); + } + while (this._tokenizer.eat(',')); + } + stack.push(node); + continue; + } + node = this._eat('id', 'await'); + if (node) { + node.expression = this._parseExpression(minPrecedence, terminal, tuple); + stack.push(node); + continue; + } + node = this._eat('.'); + if (node) { + this._tokenizer.eat('\n'); + node.target = stack.pop(); + node.member = this._parseName(); + stack.push(node); + continue; + } + if (this._tokenizer.peek().value === '(') { + if (stack.length == 0) { + node = this._node('tuple'); + const args = this._parseArguments(); + if (args.length == 1) { + stack.push(args[0]); + } + else { + node.value = args; + stack.push(node); + } + } + else { + node = this._node('call'); + node.target = stack.pop(); + node.arguments = this._parseArguments(); + stack.push(node); + } + continue; + } + if (this._tokenizer.peek().value === '[') { + if (stack.length == 0) { + stack.push(this._parseExpressions()); + } + else { + node = this._node('[]'); + node.target = stack.pop(); + node.arguments = this._parseSlice(); + stack.push(node); + } + continue; + } + if (this._tokenizer.peek().value == '{') { + stack.push(this._parseDictOrSetMaker()); + continue; + } + node = this._node(); + const literal = this._parseLiteral(); + if (literal) { + if (stack.length > 0 && literal.type == 'number' && + (literal.value.startsWith('-') || literal.value.startsWith('+'))) { + node.type = literal.value.substring(0, 1); + literal.value = literal.value.substring(1); + node.left = stack.pop(); + node.right = literal; + stack.push(node); + } + else if (stack.length == 1 && literal.type == 'string' && stack[0].type == 'string') { + stack[0].value += literal.value; + } + else { + if (literal.type === 'number') { + switch (literal.value) { + case 'inf': literal.value = Infinity; break; + case '-inf': literal.value = -Infinity; break; + } + } + stack.push(literal); + } + continue; + } + if (this._tokenizer.peek().keyword) { + break; + } + node = this._eat('...'); + if (node) { + stack.push(node); + continue; + } + const identifier = this._parseName(); + if (identifier) { + stack.push(identifier); + continue; + } + + if (tuple === true && stack.length == 1 && this._tokenizer.eat(',')) { + if (stack[0].type === 'tuple') { + node = stack[0]; + } + else { + node = this._node('tuple'); + node.value = [ stack.pop() ]; + stack.push(node); + } + // for, bar, = + if (this._tokenizer.peek().value === '=') { + continue; + } + if (!this._tokenizer.match('=') && !terminalSet.has(this._tokenizer.peek().value)) { + const nextTerminal = terminal.slice(0).concat([ ',', '=' ]); + const expression = this._parseExpression(minPrecedence, nextTerminal, tuple); + if (expression) { + node.value.push(expression); + continue; + } + } + break; + } + break; + } + + if (stack.length == 1) { + return stack.pop(); + } + if (stack.length != 0) { + throw new python.Error('Unexpected expression' + this._tokenizer.location()); + } + return null; + } + + _parseDictOrSetMaker() { + const list = []; + this._tokenizer.expect('{'); + let dict = true; + while (!this._tokenizer.eat('}')) { + const item = this._parseExpression(-1, [], false); + if (item == null) { + throw new python.Error('Expected expression' + this._tokenizer.location()); + } + if (!this._tokenizer.eat(':')) { + dict = false; + } + if (dict) { + const value = this._parseExpression(-1, [], false); + if (value == null) { + throw new python.Error('Expected expression' + this._tokenizer.location()); + } + list.push({ type: 'pair', key: item, value: value }); + } + else { + list.push(item); + } + this._tokenizer.eat(','); + this._tokenizer.eat('\n'); + if (this._tokenizer.eat('}')) { + break; + } + } + if (dict) { + return { type: 'dict', value: list }; + } + return { type: 'set', value: list }; + } + + _parseExpressions() { + const list = []; + this._tokenizer.expect('['); + while (!this._tokenizer.eat(']')) { + const expression = this._parseExpression(); + if (expression == null) { + throw new python.Error('Expected expression' + this._tokenizer.location()); + } + list.push(expression); + this._tokenizer.eat(','); + while (this._tokenizer.eat('\n')) { + // continue + } + if (this._tokenizer.eat(']')) { + break; + } + } + return { type: 'list', value: list }; + } + + _parseSlice() { + let node = { type: '::' }; + let list = []; + const group = [ 'start', 'stop', 'step' ]; + this._tokenizer.expect('['); + while (!this._tokenizer.eat(']')) { + if (this._tokenizer.eat(':')) { + node[group.shift()] = { type: 'list', value: list }; + list = []; + continue; + } + if (this._tokenizer.eat(',')) { + // list.push({}); + continue; + } + if (this._tokenizer.peek().value != ']') { + const expression = this._parseExpression(); + if (expression == null) { + throw new python.Error('Expected expression' + this._tokenizer.location()); + } + list.push(expression); + } + } + if (list.length > 0) { + node[group.shift()] = { type: 'list', value: list }; + } + if (node.start && !node.stop && !node.step) { + node = node.start; + } + return node; + } + + _parseName() { + const token = this._tokenizer.peek(); + if (token.type == 'id' && !token.keyword) { + this._tokenizer.read(); + return token; + } + return null; + } + + _parseLiteral() { + const token = this._tokenizer.peek(); + if (token.type == 'string' || token.type == 'number' || token.type == 'boolean') { + this._tokenizer.read(); + return token; + } + return null; + } + + _parseTypeArguments() { + const list = []; + this._tokenizer.expect('['); + while (!this._tokenizer.eat(']')) { + const type = this._parseType(); + if (type == null) { + throw new python.Error('Expected type ' + this._tokenizer.location()); + } + list.push(type); + if (!this._tokenizer.eat(',')) { + this._tokenizer.expect(']'); + break; + } + } + return list; + } + + _parseType() { + const type = this._node(); + type.type = 'type'; + type.name = this._parseExpression(-1, [ '[', '=' ]); + if (type.name) { + if (this._tokenizer.peek().value === '[') { + type.arguments = this._parseTypeArguments(); + } + return type; + } + return null; + } + + _parseParameter(terminal) { + const node = this._node('parameter'); + if (this._tokenizer.eat('/')) { + node.name = '/'; + return node; + } + if (this._tokenizer.eat('**')) { + node.parameterType = '**'; + } + if (this._tokenizer.eat('*')) { + node.parameterType = '*'; + } + const identifier = this._parseName(); + if (identifier !== null) { + node.name = identifier.value; + if (terminal !== ':' && this._tokenizer.eat(':')) { + node.parameterType = this._parseType(); + } + if (this._tokenizer.eat('=')) { + node.initializer = this._parseExpression(); + } + return node; + } + return null; + } + + _parseParameters(terminal) { + const list = []; + while (!this._tokenizer.eat(terminal)) { + this._tokenizer.eat('\n'); + if (this._tokenizer.eat('(')) { + list.push(this._parseParameters(')')); + } + else { + list.push(this._parseParameter(terminal)); + } + this._tokenizer.eat('\n'); + if (!this._tokenizer.eat(',')) { + this._tokenizer.expect(terminal); + break; + } + } + return list; + } + + _parseArguments() { + const list = []; + this._tokenizer.expect('('); + while (!this._tokenizer.eat(')')) { + if (this._tokenizer.eat('\n')) { + continue; + } + const expression = this._parseExpression(-1, [], false); + if (expression == null) { + throw new python.Error('Expected expression ' + this._tokenizer.location()); + } + list.push(expression); + if (!this._tokenizer.eat(',')) { + this._tokenizer.eat('\n'); + this._tokenizer.expect(')'); + break; + } + } + return list; + } + + _node(type) { + const node = {}; + node.location = this._tokenizer.location(); + if (type) { + node.type = type; + } + return node; + } + + _eat(type, value) { + if (this._tokenizer.match(type, value)) { + const node = this._node(type === 'id' ? value : type); + this._tokenizer.expect(type, value); + return node; + } + return null; + } +}; + +python.Tokenizer = class { + + constructor(text, file) { + this._text = text; + this._file = file; + this._position = 0; + this._lineStart = 0; + this._line = 0; + this._token = { type: '', value: '' }; + this._brackets = 0; + this._indentation = []; + this._outdent = 0; + if (!python.Tokenizer._whitespace) { + python.Tokenizer._whitespace = new RegExp('[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]'); + const identifierStartChars = '\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc'; + const identifierChars = '\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u0620-\u0649\u0672-\u06d3\u06e7-\u06e8\u06fb-\u06fc\u0730-\u074a\u0800-\u0814\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0840-\u0857\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962-\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09d7\u09df-\u09e0\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5f-\u0b60\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2-\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d46-\u0d48\u0d57\u0d62-\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e34-\u0e3a\u0e40-\u0e45\u0e50-\u0e59\u0eb4-\u0eb9\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f41-\u0f47\u0f71-\u0f84\u0f86-\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1029\u1040-\u1049\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u170e-\u1710\u1720-\u1730\u1740-\u1750\u1772\u1773\u1780-\u17b2\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1920-\u192b\u1930-\u193b\u1951-\u196d\u19b0-\u19c0\u19c8-\u19c9\u19d0-\u19d9\u1a00-\u1a15\u1a20-\u1a53\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b46-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1bb0-\u1bb9\u1be6-\u1bf3\u1c00-\u1c22\u1c40-\u1c49\u1c5b-\u1c7d\u1cd0-\u1cd2\u1d00-\u1dbe\u1e01-\u1f15\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2d81-\u2d96\u2de0-\u2dff\u3021-\u3028\u3099\u309a\ua640-\ua66d\ua674-\ua67d\ua69f\ua6f0-\ua6f1\ua7f8-\ua800\ua806\ua80b\ua823-\ua827\ua880-\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8f3-\ua8f7\ua900-\ua909\ua926-\ua92d\ua930-\ua945\ua980-\ua983\ua9b3-\ua9c0\uaa00-\uaa27\uaa40-\uaa41\uaa4c-\uaa4d\uaa50-\uaa59\uaa7b\uaae0-\uaae9\uaaf2-\uaaf3\uabc0-\uabe1\uabec\uabed\uabf0-\uabf9\ufb20-\ufb28\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f'; + python.Tokenizer._identifierStart = new RegExp('[' + identifierStartChars + ']'); + /* eslint-disable */ + python.Tokenizer._identifierChar = new RegExp('[' + identifierStartChars + identifierChars + ']'); + /* eslint-enable */ + } + } + + peek() { + if (!this._cache) { + this._token = this._tokenize(this._token); + this._cache = true; + } + return this._token; + } + + read() { + if (!this._cache) { + this._token = this._tokenize(this._token); + } + const next = this._position + this._token.value.length; + while (this._position < next) { + if (python.Tokenizer._isNewline(this._get(this._position))) { + this._position = this._newLine(this._position); + this._lineStart = this._position; + this._line++; + } + else { + this._position++; + } + } + this._cache = false; + return this._token; + } + + match(type, value) { + const token = this.peek(); + if (token.type === type && (!value || token.value === value)) { + return true; + } + return false; + } + + eat(type, value) { + const token = this.peek(); + if (token.type === type && (!value || token.value === value)) { + this.read(); + return true; + } + return false; + } + + expect(type, value) { + const token = this.peek(); + if (token.type !== type) { + throw new python.Error("Unexpected '" + token.value + "' instead of '" + type + "'" + this.location()); + } + if (value && token.value !== value) { + throw new python.Error("Unexpected '" + token.value + "' instead of '" + value + "'" + this.location()); + } + this.read(); + } + + location() { + return ' at ' + this._file + ':' + (this._line + 1).toString() + ':' + (this._position - this._lineStart + 1).toString(); + } + + static _isSpace(c) { + switch (c) { + case ' ': + case '\t': + case '\v': // 11 + case '\f': // 12 + case '\xA0': // 160 + return true; + default: + if (c.charCodeAt(0) >= 0x1680) { + return python.Tokenizer._whitespace.test(c); + } + return false; + } + } + + static _isNewline(c) { + switch(c) { + case '\n': + case '\r': + case '\u2028': // 8232 + case '\u2029': // 8233 + return true; + } + return false; + } + + static _isIdentifierStartChar(c) { + if (c < 'A') { + return c === '$'; + } + if (c <= 'Z') { + return true; + } + if (c < 'a') { + return c === '_'; + } + if (c <= 'z') { + return true; + } + const code = c.charCodeAt(0); + if (code >= 0xAA) { + return python.Tokenizer._identifierStart.test(c); + } + return false; + } + + static _isIdentifierChar(c) { + if (c < '0') { + return c === '$'; + } + if (c <= '9') { + return true; + } + if (c < 'A') { + return false; + } + if (c <= 'Z') { + return true; + } + if (c < 'a') { + return c === '_'; + } + if (c <= 'z') { + return true; + } + const code = c.charCodeAt(0); + if (code >= 0xAA) { + return python.Tokenizer._identifierChar.test(c); + } + return false; + } + + static _isDecimal(c) { + return c >= '0' && c <= '9' || c === '_'; + } + + static _isHex(c) { + return python.Tokenizer._isDecimal(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || c === '_'; + } + + static _isOctal(c) { + return c >= '0' && c <= '7' || c === '_'; + } + + static _isBinary(c) { + return c === '0' || c === '1' || c === '_'; + } + + _get(position) { + return position >= this._text.length ? '\0' : this._text[position]; + } + + _skipLine() { + while (this._position < this._text.length) { + if (python.Tokenizer._isNewline(this._get(this._position))) { + break; + } + this._position++; + } + } + + _skipWhitespace() { + while (this._position < this._text.length) { + const c = this._text[this._position]; + if (c == '#') { + this._skipLine(); + } + else if (python.Tokenizer._isSpace(c)) { + this._position++; + } + else if (c == '\\') { + // Explicit Line Continuation + this._position++; + if (python.Tokenizer._isNewline(this._get(this._position))) { + this._position = this._newLine(this._position); + this._lineStart = this._position; + this._line++; + } + else { + throw new python.Error("Unexpected '" + this._text[this._position] + "' after line continuation" + this.location()); + } + } + else if (this._brackets > 0 && python.Tokenizer._isNewline(c)) { + // Implicit Line Continuation + this._position = this._newLine(this._position); + this._lineStart = this._position; + this._line++; + } + else { + break; + } + } + } + + _newLine(position) { + if ((this._get(position) === '\n' && this._get(position + 1) === '\r') || + (this._get(position) === '\r' && this._get(position + 1) === '\n')) { + return position + 2; + } + return position + 1; + } + + _tokenize(token) { + if (this._token.type !== '\n') { + this._skipWhitespace(); + } + if (this._token.type === 'dedent') { + this._indentation.pop(); + this._outdent--; + if (this._outdent > 0) { + return { type: 'dedent', value: '' }; + } + } + if (token.type == '\n') { + let indent = ''; + let i = this._position; + while (i < this._text.length) { + const c = this._text[i]; + if (python.Tokenizer._isSpace(c)) { + indent += c; + i++; + } + else if (python.Tokenizer._isNewline(c)) { + indent = ''; + i = this._newLine(i); + this._position = i; + this._lineStart = i; + this._line++; + } + else if (c == '#') { + indent = ''; + while (i < this._text.length && !python.Tokenizer._isNewline(this._text[i])) { + i++; + } + continue; + } + else { + break; + } + } + let type = null; + if (indent.length > 0) { + const current = this._indentation.length > 0 ? this._indentation[this._indentation.length - 1] : ''; + if (indent.length > current.length) { + type = 'indent'; + this._indentation.push(indent); + } + else if (indent.length > 0 && indent.length < current.length) { + type = 'dedent'; + this._outdent = 0; + for (let j = this._indentation.length - 1; j >= 0 && indent.length < this._indentation[j].length; j--) { + this._outdent++; + } + } + else { + this._position += indent.length; + } + } + else if (i >= this._text.length) { + return { type: 'eof', value: '' }; + } + else if (this._indentation.length > 0) { + type = 'dedent'; + this._outdent = this._indentation.length; + } + + switch (type) { + case 'indent': + case 'dedent': + return { type: type, value: indent }; + } + } + if (this._position >= this._text.length) { + return { type: 'eof', value: '' }; + } + const c = this._get(this._position); + const string = this._string(); + if (string) { + return string; + } + switch (c) { + case '(': + case '[': + case '{': + this._brackets++; + return { type: c, value: c }; + case ')': + case ']': + case '}': + if (this._brackets === 0) { + throw new python.Error("Unexpected '" + c + "'" + this.location); + } + this._brackets--; + return { type: c, value: c }; + case ',': + case ';': + case '?': + return { type: c, value: c }; + default: { + const number = this._number(); + if (number) { + return number; + } + if (c === '.') { + let end = this._position + 1; + while (this._get(end) === '.') { + end++; + } + const text = this._text.substring(this._position, end); + return { type: text, value: text }; + } + const identifier = this._identifier(); + if (identifier) { + return identifier; + } + const operator = this._operator(); + if (operator) { + return operator; + } + break; + } + } + if (c === '.') { + return { type: c, value: c }; + } + if (c === '\\') { + return { type: '\\', value: c }; + } + if (python.Tokenizer._isNewline(c)) { + return { type: '\n', value: this._text.substring(this._position, this._newLine(this._position)) }; + } + throw new python.Error("Unexpected token '" + c + "'" + this.location()); + } + + _number() { + let c = this._get(this._position); + const sign = (c === '-' || c === '+') ? 1 : 0; + let i = this._position + sign; + c = this._get(i); + if (c === '0') { + let radix = 0; + const n = this._get(i + 1); + if ((n === 'x' || n === 'X') && python.Tokenizer._isHex(this._get(i + 2))) { + i += 2; + while (python.Tokenizer._isHex(this._get(i))) { + i += 1; + } + if (this._get(i) === 'l' || this._get(i) === 'L') { + i += 1; + } + radix = 16; + } + else if ((n === 'b' || n === 'B') && python.Tokenizer._isBinary(this._get(i + 2))) { + i += 2; + while (python.Tokenizer._isBinary(this._get(i))) { + i++; + } + radix = 2; + } + else if ((n === 'o' || n === 'O') && python.Tokenizer._isOctal(this._get(i + 2))) { + i += 2; + while (python.Tokenizer._isOctal(this._get(i))) { + i++; + } + radix = 8; + } + else if (n >= '0' && n <= '7') { + i++; + while (python.Tokenizer._isOctal(this._get(i))) { + i += 1; + } + if (this._get(i) === 'l' || this._get(i) === 'L') { + i += 1; + } + radix = 8; + } + if (radix > 0 && this._get(i) !== '.') { + const radixText = this._text.substring(this._position, i); + const radixParseText = radixText.indexOf('_') !== -1 ? radixText.split('_').join('') : radixText; + if (!isNaN(parseInt(radixParseText, radix))) { + return { type: 'number', value: radixText }; + } + } + } + i = this._position + sign; + let decimal = false; + if (this._get(i) >= '1' && this._get(i) <= '9') { + while (python.Tokenizer._isDecimal(this._get(i))) { + i++; + } + c = this._get(i).toLowerCase(); + decimal = c !== '.' && c !== 'e'; + } + if (this._get(i) === '0') { + i++; + c = this._get(i).toLowerCase(); + decimal = !python.Tokenizer._isDecimal(c) && c !== '.' && c !== 'e' && c !== 'j'; + } + if (decimal) { + if (this._get(i) === 'j' || this._get(i) === 'J' || this._get(i) === 'l' || this._get(i) === 'L') { + return { 'type': 'number', value: this._text.substring(this._position, i + 1) }; + } + const intText = this._text.substring(this._position, i); + if (!isNaN(parseInt(intText, 10))) { + return { type: 'number', value: intText }; + } + } + i = this._position + sign; + if ((this._get(i) >= '0' && this._get(i) <= '9') || + (this._get(i) === '.' && this._get(i + 1) >= '0' && this._get(i + 1) <= '9')) { + while (python.Tokenizer._isDecimal(this._get(i))) { + i++; + } + if (this._get(i) === '.') { + i++; + } + while (python.Tokenizer._isDecimal(this._get(i))) { + i++; + } + if (i > (this._position + sign)) { + if (this._get(i) === 'e' || this._get(i) === 'E') { + i++; + if (this._get(i) == '-' || this._get(i) == '+') { + i++; + } + if (!python.Tokenizer._isDecimal(this._get(i))) { + i = this._position; + } + else { + while (python.Tokenizer._isDecimal(this._get(i))) { + i++; + } + } + } + else { + while (python.Tokenizer._isDecimal(this._get(i))) { + i++; + } + } + } + if (i > (this._position + sign)) { + if (this._get(i) === 'j' || this._get(i) === 'J') { + return { type: 'number', value: this._text.substring(this._position, i + 1) }; + } + const floatText = this._text.substring(this._position, i); + const floatParseText = floatText.indexOf('_') != -1 ? floatText.split('_').join('') : floatText; + if (!isNaN(parseFloat(floatParseText))) { + return { type: 'number', value: floatText }; + } + } + } + i = this._position + sign; + if (this._get(i) === 'i' && this._get(i + 1) === 'n' && this._get(i + 2) === 'f' && !python.Tokenizer._isIdentifierChar(this._get(i + 3))) { + return { type: 'number', value: this._text.substring(this._position, i + 3) }; + } + return null; + } + + _identifier() { + let i = this._position; + if (python.Tokenizer._isIdentifierStartChar(this._get(i))) { + i++; + while (python.Tokenizer._isIdentifierChar(this._get(i))) { + i++; + } + } + if (i > this._position) { + const text = this._text.substring(this._position, i); + return { type: 'id', value: text, keyword: python.Tokenizer._isKeyword(text) }; + } + return null; + } + + _operator() { + let length = 0; + const c0 = this._get(this._position); + const c1 = this._get(this._position + 1); + const c2 = this._get(this._position + 2); + switch (c0) { + case '+': + case '&': + case '|': + case '^': + case '=': + case '!': + case '%': + case '~': + length = c1 === '=' ? 2 : 1; + break; + case '-': + length = c1 === '=' || c1 === '>' ? 2 : 1; + break; + case '*': + if (c1 === '*') { + length = c2 === '=' ? 3 : 2; + } + else { + length = c1 === '=' ? 2 : 1; + } + break; + case '/': + if (c1 === '/') { + length = c2 === '=' ? 3 : 2; + } + else { + length = c1 === '=' ? 2 : 1; + } + break; + case '<': + if (c1 === '>') { + length = 2; + } + else if (c1 === '<') { + length = c2 === '=' ? 3 : 2; + } + else { + length = c1 === '=' ? 2 : 1; + } + break; + case '>': + if (c1 === '>') { + length = c2 === '=' ? 3 : 2; + } + else { + length = c1 === '=' ? 2 : 1; + } + break; + case '@': + length = c1 === '=' ? 2 : 1; + break; + case ':': + length = c1 === '=' ? 2 : 1; + } + if (length > 0) { + const text = this._text.substring(this._position, this._position + length); + return { type: text, value: text }; + } + return null; + } + + _string() { + let i = this._position; + let prefix = -1; + if (this._get(i) === "'" || this._get(i) === '"') { + prefix = ''; + } + else if (this._get(i + 1) === "'" || this._get(i + 1) === '"') { + const c = this._get(i); + switch (c.toLowerCase()) { + case 'b': + case 'f': + case 'r': + case 'u': + prefix = c; + break; + } + } + else if (this._get(i + 2) === "'" || this._get(i + 2) === '"') { + const cc = this._text.substr(this._position, 2); + switch (cc.toLowerCase()) { + case 'br': + case 'fr': + case 'rb': + case 'rf': + case 'ur': + prefix = cc; + break; + } + } + if (prefix.length >= 0) { + i += prefix.length; + let quote = ''; + let count = 0; + const q0 = this._get(i); + const q1 = this._get(i + 1); + const q2 = this._get(i + 2); + switch (q0) { + case "'": + quote = q0; + count = (q1 === "'" && q2 === "'") ? 3 : 1; + break; + case '"': + quote = q0; + count = (q1 === '"' && q2 === '"') ? 3 : 1; + } + i += count; + if (count == 1) { + while (i < this._text.length) { + if (this._text[i] === quote) { + return { type: 'string', value: this._text.substring(this._position, i + 1) }; + } + else if (this._text[i] === '\\' && + (this._get(i + 1) == quote || this._get(i + 1) == '\n' || this._get(i + 1) == '\\')) { + i += 2; + } + else if (this._text[i] === '\r' || this._text[i] === '\n') { + break; + } + else { + i++; + } + } + } + else if (count == 3) { + while (i < this._text.length) { + if (this._get(i) === quote && this._get(i + 1) === quote && this._get(i + 2) === quote) { + return { type: 'string', value: this._text.substring(this._position, i + 3) }; + } + else if (this._get(i) === '\\' && this._get(i + 1) === quote) { + i += 2; + continue; + } + i++; + } + } + } + i = this._position; + if (this._get(i) === '`') { + i++; + while (i < this._text.length) { + if (this._text[i] === '`') { + return { type: 'string', value: this._text.substring(this._position, i + 1) }; + } + i++; + } + } + return null; + } + + static _isKeyword(value) { + switch (value) { + case 'and': + case 'as': + case 'else': + case 'for': + case 'if': + case 'import': + case 'in': + case 'is': + case 'not': + case 'or': + return true; + } + return false; + } +}; + +python.Execution = class { + + constructor(sources, exceptionCallback) { + const self = this; + this._sources = sources || new Map(); + this._exceptionCallback = exceptionCallback; + this._utf8Decoder = new TextDecoder('utf-8'); + this._packages = new Map(); + this._unknownNameMap = new Set(); + this._context = new python.Execution.Context(); + this._context.scope.builtins = {}; + this._context.scope.builtins.type = { __module__: 'builtins', __name__: 'type' }; + this._context.scope.builtins.type.__class__ = this._context.scope.builtins.type; + this._context.scope.builtins.module = { __module__: 'builtins', __name__: 'module', __class__: this._context.scope.builtins.type }; + this._context.scope.builtins.module.__type__ = this._context.scope.builtins.module; + this.registerModule('__builtin__'); + this.registerModule('_codecs'); + this.registerModule('argparse'); + this.registerModule('collections'); + this.registerModule('copy_reg'); + this.registerModule('cuml'); + this.registerModule('gensim'); + this.registerModule('io'); + this.registerModule('joblib'); + this.registerModule('keras'); + this.registerModule('lightgbm'); + this.registerModule('numpy'); + this.registerModule('nolearn'); + this.registerModule('sklearn'); + this.registerModule('typing'); + this.registerModule('xgboost'); + const builtins = this._context.scope.builtins; + const numpy = this._context.scope.numpy; + const typing = this._context.scope.typing; + this.registerType('builtins.function', class {}); + this.registerType('builtins.method', class {}); + this.registerType('builtins.dict', class {}); + this.registerType('builtins.list', class {}); + this.registerType('builtins.bool', class {}); + this.registerType('builtins.int', class {}); + this.registerType('builtins.float', class {}); + this.registerType('builtins.object', class {}); + this.registerType('builtins.str', class {}); + this.registerType('builtins.tuple', class {}); + this.registerType('typing._Final', class {}); + this.registerType('typing._SpecialForm', class extends typing._Final {}); + this.registerType('typing._BaseGenericAlias', class extends typing._Final {}); + this.registerType('typing._GenericAlias', class extends typing._BaseGenericAlias {}); + this.registerType('typing._SpecialGenericAlias', class extends typing._BaseGenericAlias {}); + this.registerType('typing._TupleType', class extends typing._SpecialGenericAlias {}); + typing.Optional = self.invoke('typing._SpecialForm', []); + typing.List = self.invoke('typing._SpecialGenericAlias', []); + typing.Dict = self.invoke('typing._SpecialGenericAlias', []); + typing.Tuple = self.invoke('typing._TupleType', []); + this.registerType('argparse.Namespace', class { + constructor(args) { + this.args = args; + } + }); + this.registerType('collections.deque', class { + constructor(iterable) { + if (iterable) { + let i = 0; + for (const value of iterable) { + this[i++] = value; + } + this.length = i; + } + } + }); + this.registerType('collections.OrderedDict', class extends Map { + constructor(items) { + super(); + if (items) { + for (const pair of items) { + this.__setitem__(pair[0], pair[1]); + } + } + } + __setitem__(key, value) { + this.set(key, value); + } + }); + this.registerType('cuml.common.array_descriptor.CumlArrayDescriptorMeta', class {}); + this.registerType('cuml.ensemble.randomforestclassifier.RandomForestClassifier', class {}); + this.registerType('cuml.raft.common.handle.Handle', class { + __setstate__(state) { + this._handle = state; + } + }); + this.registerType('haiku._src.data_structures.FlatMapping', class { + constructor(dict) { + for (const key of Object.keys(dict)) { + this[key] = dict[key]; + } + } + }); + this.registerType('io.BytesIO', class { + constructor(buf, mode) { + this.mode = mode || 'r'; + this._buf = this.mode === 'w' ? null : buf; + this._point = 0; + } + seek(offset) { + this._point = offset; + } + read(size) { + const start = this._point; + this._point = size !== undefined ? start + size : this._buf.length; + return this._buf.subarray(start, this._point); + } + write(data) { + const src = this._buf || new Uint8Array(); + this._point = src.length + data.length; + this._buf = new Uint8Array(this._point); + this._buf.set(src, 0); + this._buf.set(data, src.length); + } + }); + this.registerType('numpy.dtype', class { + constructor(obj, align, copy) { + switch (obj) { + case 'b1': case 'bool': this.name = 'bool'; this.itemsize = 1; this.kind = 'b'; break; + case 'i1': case 'int8': this.name = 'int8'; this.itemsize = 1; this.kind = 'i'; break; + case 'i2': case 'int16': this.name = 'int16'; this.itemsize = 2; this.kind = 'i'; break; + case 'i4': case 'int32': this.name = 'int32'; this.itemsize = 4; this.kind = 'i'; break; + case 'i8': case 'int64': case 'int': this.name = 'int64'; this.itemsize = 8; this.kind = 'i'; break; + case 'u1': case 'uint8': this.name = 'uint8'; this.itemsize = 1; this.kind = 'u'; break; + case 'u2': case 'uint16': this.name = 'uint16'; this.itemsize = 2; this.kind = 'u'; break; + case 'u4': case 'uint32': this.name = 'uint32'; this.itemsize = 4; this.kind = 'u'; break; + case 'u8': case 'uint64': case 'uint': this.name = 'uint64'; this.itemsize = 8; this.kind = 'u'; break; + case 'f2': case 'float16': this.name = 'float16'; this.itemsize = 2; this.kind = 'f'; break; + case 'f4': case 'float32': this.name = 'float32'; this.itemsize = 4; this.kind = 'f'; break; + case 'f8': case 'float64': case 'float': this.name = 'float64'; this.itemsize = 8; this.kind = 'f'; break; + case 'c8': case 'complex64': this.name = 'complex64'; this.itemsize = 8; this.kind = 'c'; break; + case 'c16': case 'complex128': case 'complex': this.name = 'complex128'; this.itemsize = 16; this.kind = 'c'; break; + default: + if (obj.startsWith('V')) { + this.itemsize = Number(obj.substring(1)); + this.kind = 'V'; + this.name = 'void' + (this.itemsize * 8).toString(); + } + else if (obj.startsWith('O')) { + this.itemsize = Number(obj.substring(1)); + this.kind = 'O'; + this.name = 'object'; + } + else if (obj.startsWith('S')) { + this.itemsize = Number(obj.substring(1)); + this.kind = 'S'; + this.name = 'string'; + } + else if (obj.startsWith('U')) { + this.itemsize = Number(obj.substring(1)); + this.kind = 'U'; + this.name = 'string'; + } + else if (obj.startsWith('M')) { + this.itemsize = Number(obj.substring(1)); + this.kind = 'M'; + this.name = 'datetime'; + } + else { + throw new python.Error("Unknown dtype '" + obj.toString() + "'."); + } + break; + } + this.byteorder = '='; + if (align) { + this.align = align; + } + if (copy) { + this.copy = copy; + } + } + get str() { + return (this.byteorder === '=' ? '<' : this.byteorder) + this.kind + this.itemsize.toString(); + } + __setstate__(state) { + switch (state.length) { + case 8: + this.version = state[0]; + this.byteorder = state[1]; + this.subarray = state[2]; + this.names = state[3]; + this.fields = state[4]; + this.elsize = state[5]; + this.alignment = state[6]; + this.int_dtypeflags = state[7]; + break; + case 9: + this.version = state[0]; + this.byteorder = state[1]; + this.subarray = state[2]; + this.names = state[3]; + this.fields = state[4]; + this.elsize = state[5]; + this.alignment = state[6]; + this.int_dtypeflags = state[7]; + this.metadata = state[8]; + break; + default: + throw new python.Error("Unknown numpy.dtype setstate length '" + state.length.toString() + "'."); + } + } + }); + this.registerType('gensim.models.doc2vec.Doctag', class {}); + this.registerType('gensim.models.doc2vec.Doc2Vec', class {}); + this.registerType('gensim.models.doc2vec.Doc2VecTrainables', class {}); + this.registerType('gensim.models.doc2vec.Doc2VecVocab', class {}); + this.registerType('gensim.models.fasttext.FastText', class {}); + this.registerType('gensim.models.fasttext.FastTextTrainables', class {}); + this.registerType('gensim.models.fasttext.FastTextVocab', class {}); + this.registerType('gensim.models.fasttext.FastTextKeyedVectors', class {}); + this.registerType('gensim.models.keyedvectors.Doc2VecKeyedVectors', class {}); + this.registerType('gensim.models.keyedvectors.FastTextKeyedVectors', class {}); + this.registerType('gensim.models.keyedvectors.KeyedVectors', class {}); + this.registerType('gensim.models.keyedvectors.Vocab', class {}); + this.registerType('gensim.models.keyedvectors.Word2VecKeyedVectors', class {}); + this.registerType('gensim.models.phrases.Phrases', class {}); + this.registerType('gensim.models.tfidfmodel.TfidfModel', class {}); + this.registerType('gensim.models.word2vec.Vocab', class {}); + this.registerType('gensim.models.word2vec.Word2Vec', class {}); + this.registerType('gensim.models.word2vec.Word2VecTrainables', class {}); + this.registerType('gensim.models.word2vec.Word2VecVocab', class {}); + this.registerType('joblib.numpy_pickle.NumpyArrayWrapper', class { + constructor(/* subtype, shape, dtype */) { + } + __setstate__(state) { + this.subclass = state.subclass; + this.dtype = state.dtype; + this.shape = state.shape; + this.order = state.order; + this.allow_mmap = state.allow_mmap; + } + __read__(unpickler) { + if (this.dtype.name == 'object') { + return unpickler.load((name, args) => self.invoke(name, args), null); + } + else { + const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b, 1); + this.data = unpickler.read(size); + } + return self.invoke(this.subclass, [ this.shape, this.dtype, this.data ]); + } + }); + this.registerType('keras.engine.sequential.Sequential', class {}); + this.registerType('lightgbm.sklearn.LGBMRegressor', class {}); + this.registerType('lightgbm.sklearn.LGBMClassifier', class {}); + this.registerType('lightgbm.basic.Booster', class { + constructor() { + this.average_output = false; + this.models = []; + this.loaded_parameter = ''; + } + __setstate__(state) { + if (typeof state.handle === 'string') { + this.LoadModelFromString(state.handle); + return; + } + Object.assign(this, state); + } + LoadModelFromString(model_str) { + const lines = model_str.split('\n'); + const signature = lines.shift() || '?'; + if (signature.trim() !== 'tree') { + throw new python.Error("Invalid signature '" + signature.trim() + "'."); + } + // GBDT::LoadModelFromString() in https://github.com/microsoft/LightGBM/blob/master/src/boosting/gbdt_model_text.cpp + const key_vals = new Map(); + while (lines.length > 0 && !lines[0].startsWith('Tree=')) { + const cur_line = lines.shift().trim(); + if (cur_line.length > 0) { + const strs = cur_line.split('='); + if (strs.length === 1) { + key_vals.set(strs[0], ''); + } + else if (strs.length === 2) { + key_vals.set(strs[0], strs[1]); + } + else if (strs.length > 2) { + if (strs[0] === "feature_names") { + key_vals.set(strs[0], cur_line.substring("feature_names=".length)); + } + else if (strs[0] == 'monotone_constraints') { + key_vals.set(strs[0], cur_line.substring('monotone_constraints='.length)); + } + else { + throw new python.Error('Wrong line: ' + cur_line.substring(0, Math.min(128, cur_line.length))); + } + } + } + } + const atoi = (key, value) => { + if (key_vals.has(key)) { + return parseInt(key_vals.get(key), 10); + } + if (value !== undefined) { + return value; + } + throw new python.Error('Model file does not specify ' + key + '.'); + }; + const list = (key, size) => { + if (key_vals.has(key)) { + const value = key_vals.get(key).split(' '); + if (value.length !== size) { + throw new python.Error('Wrong size of ' + key + '.'); + } + return value; + } + throw new python.Error('Model file does not contain ' + key + '.'); + }; + this.version = key_vals.get('version') || ''; + this.num_class = atoi('num_class'); + this.num_tree_per_iteration = atoi('num_tree_per_iteration', this.num_class); + this.label_index = atoi('label_index'); + this.max_feature_idx = atoi('max_feature_idx'); + if (key_vals.has('average_output')) { + this.average_output = true; + } + this.feature_names = list('feature_names', this.max_feature_idx + 1); + this.feature_infos = list('feature_infos', this.max_feature_idx + 1); + if (key_vals.has('monotone_constraints')) { + this.monotone_constraints = list('monotone_constraints', this.max_feature_idx + 1, true); + } + if (key_vals.has('objective')) { + this.objective = key_vals.get('objective'); + } + let tree = null; + // let lineNumber = 0; + while (lines.length > 0) { + // lineNumber++; + const text = lines.shift(); + const line = text.trim(); + if (line.length === 0) { + continue; + } + if (line.startsWith('Tree=')) { + tree = { index: parseInt(line.split('=').pop(), 10) }; + this.models.push(tree); + continue; + } + if (line === 'end of trees') { + break; + } + const param = line.split('='); + if (param.length !== 2) { + throw new python.Error("Invalid property '" + line + "'."); + } + const name = param[0].trim(); + const value = param[1].trim(); + tree[name] = value; + } + const ss = []; + let is_inparameter = false; + while (lines.length > 0) { + const text = lines.shift(); + const line = text.trim(); + if (line === 'parameters:') { + is_inparameter = true; + continue; + } + else if (line === 'end of parameters') { + break; + } + else if (is_inparameter) { + ss.push(line); + } + } + if (ss.length > 0) { + this.loaded_parameter = ss.join('\n'); + } + } + }); + this.registerType('nolearn.lasagne.base.BatchIterator', class {}); + this.registerType('nolearn.lasagne.base.Layers', class {}); + this.registerType('nolearn.lasagne.base.NeuralNet', class {}); + this.registerType('nolearn.lasagne.base.TrainSplit', class {}); + this.registerType('nolearn.lasagne.handlers.PrintLayerInfo', class {}); + this.registerType('nolearn.lasagne.handlers.PrintLog', class {}); + this.registerType('numpy.ndarray', class { + constructor(shape, dtype, buffer, offset, strides, order) { + this.shape = shape; + this.dtype = dtype; + this.data = buffer !== undefined ? buffer : null; + this.offset = offset !== undefined ? offset : 0; + this.strides = strides !== undefined ? strides : null; + this.order = offset !== undefined ? order : null; + this.flags = {}; + } + __setstate__(state) { + this.version = state[0]; + this.shape = state[1]; + this.dtype = state[2]; + this.flags.fnc = state[3]; + this.data = state[4]; + } + __read__(unpickler) { + const dims = (this.shape || []).reduce((a, b) => a * b, 1); + const size = this.dtype.itemsize * dims; + if (typeof this.data == 'string') { + this.data = unpickler.unescape(this.data, size); + if (this.data.length != size) { + throw new python.Error('Invalid string array data size.'); + } + } + else { + if (this.data.length != size) { + // throw new pytorch.Error('Invalid array data size.'); + } + } + return this; + } + tobytes() { + return this.data; + } + }); + this.registerType('numpy.ma.core.MaskedArray', class extends numpy.ndarray { + constructor(data /*, mask, dtype, copy, subok, ndmin, fill_value, keep_mask, hard_mask, shrink, order */) { + super(data.shape, data.dtype, data.data); + } + }); + this.registerType('numpy.core.memmap.memmap', class extends numpy.ndarray { + constructor(shape, dtype) { + super(shape, dtype); + } + }); + this.registerType('pathlib.PosixPath', class { + constructor() { + this.path = Array.from(arguments).join('/'); + } + }); + this.registerType('sklearn.calibration._CalibratedClassifier', class {}); + this.registerType('sklearn.calibration._SigmoidCalibration', class {}); + this.registerType('sklearn.calibration.CalibratedClassifierCV', class {}); + this.registerType('sklearn.compose._column_transformer.ColumnTransformer', class {}); + this.registerType('sklearn.compose._target.TransformedTargetRegressor', class {}); + this.registerType('sklearn.cluster._dbscan.DBSCAN', class {}); + this.registerType('sklearn.cluster._kmeans.KMeans', class {}); + this.registerType('sklearn.decomposition._pca.PCA', class {}); + this.registerType('sklearn.decomposition.PCA', class {}); + this.registerType('sklearn.decomposition.pca.PCA', class {}); + this.registerType('sklearn.decomposition._truncated_svd.TruncatedSVD', class {}); + this.registerType('sklearn.decomposition.truncated_svd.TruncatedSVD', class {}); + this.registerType('sklearn.discriminant_analysis.LinearDiscriminantAnalysis', class {}); + this.registerType('sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis', class {}); + this.registerType('sklearn.dummy.DummyClassifier', class {}); + this.registerType('sklearn.dummy.DummyRegressor', class {}); + this.registerType('sklearn.externals.joblib.numpy_pickle.NumpyArrayWrapper', class { + constructor(/* subtype, shape, dtype */) { + } + __setstate__(state) { + this.subclass = state.subclass; + this.dtype = state.dtype; + this.shape = state.shape; + this.order = state.order; + this.allow_mmap = state.allow_mmap; + } + __read__(unpickler) { + if (this.dtype.name == 'object') { + return unpickler.load((name, args) => self.invoke(name, args), null); + } + else { + const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b, 1); + this.data = unpickler.read(size); + } + return self.invoke(this.subclass, [ this.shape, this.dtype, this.data ]); + } + }); + this.registerType('sklearn.externals.joblib.numpy_pickle.NDArrayWrapper', class { + constructor(/* subtype, shape, dtype */) { + } + __setstate__(state) { + this.subclass = state.subclass; + this.filename = state.state; + this.allow_mmap = state.allow_mmap; + } + __read__(/* unpickler */) { + return this; // return self.invoke(this.subclass, [ this.shape, this.dtype, this.data ]); + } + }); + this.registerType('sklearn.ensemble._bagging.BaggingClassifier', class {}); + this.registerType('sklearn.ensemble._forest.RandomForestRegressor', class {}); + this.registerType('sklearn.ensemble._forest.RandomForestClassifier', class {}); + this.registerType('sklearn.ensemble._forest.ExtraTreesClassifier', class {}); + this.registerType('sklearn.ensemble._gb_losses.BinomialDeviance', class {}); + this.registerType('sklearn.ensemble._gb_losses.LeastSquaresError', class {}); + this.registerType('sklearn.ensemble._gb_losses.MultinomialDeviance', class {}); + this.registerType('sklearn.ensemble._gb.GradientBoostingClassifier', class {}); + this.registerType('sklearn.ensemble._gb.GradientBoostingRegressor', class {}); + this.registerType('sklearn.ensemble._iforest.IsolationForest', class {}); + this.registerType('sklearn.ensemble._stacking.StackingClassifier', class {}); + this.registerType('sklearn.ensemble._voting.VotingClassifier', class {}); + this.registerType('sklearn.ensemble.forest.RandomForestClassifier', class {}); + this.registerType('sklearn.ensemble.forest.RandomForestRegressor', class {}); + this.registerType('sklearn.ensemble.forest.ExtraTreesClassifier', class {}); + this.registerType('sklearn.ensemble.gradient_boosting.BinomialDeviance', class {}); + this.registerType('sklearn.ensemble.gradient_boosting.GradientBoostingClassifier', class {}); + this.registerType('sklearn.ensemble.gradient_boosting.LogOddsEstimator', class {}); + this.registerType('sklearn.ensemble.gradient_boosting.MultinomialDeviance', class {}); + this.registerType('sklearn.ensemble.gradient_boosting.PriorProbabilityEstimator', class {}); + this.registerType('sklearn.ensemble.weight_boosting.AdaBoostClassifier', class {}); + this.registerType('sklearn.feature_extraction._hashing.FeatureHasher', class {}); + this.registerType('sklearn.feature_extraction.text.CountVectorizer', class {}); + this.registerType('sklearn.feature_extraction.text.HashingVectorizer', class {}); + this.registerType('sklearn.feature_extraction.text.TfidfTransformer', class {}); + this.registerType('sklearn.feature_extraction.text.TfidfVectorizer', class {}); + this.registerType('sklearn.feature_selection._from_model.SelectFromModel', class {}); + this.registerType('sklearn.feature_selection._univariate_selection.SelectKBest', class {}); + this.registerType('sklearn.feature_selection._univariate_selection.SelectPercentile', class {}); + this.registerType('sklearn.feature_selection._variance_threshold.VarianceThreshold', class {}); + this.registerType('sklearn.feature_selection.univariate_selection.SelectKBest', class {}); + this.registerType('sklearn.feature_selection.variance_threshold.VarianceThreshold', class {}); + this.registerType('sklearn.gaussian_process.gpc.GaussianProcessClassifier', class {}); + this.registerType('sklearn.gaussian_process.kernels.ConstantKernel', class {}); + this.registerType('sklearn.gaussian_process.kernels.Product', class {}); + this.registerType('sklearn.gaussian_process.kernels.RBF', class {}); + this.registerType('sklearn.grid_search._CVScoreTuple', class {}); + this.registerType('sklearn.grid_search.GridSearchCV', class {}); + this.registerType('sklearn.impute._base.SimpleImputer', class {}); + this.registerType('sklearn.impute.SimpleImputer', class {}); + this.registerType('sklearn.isotonic.IsotonicRegression', class {}); + this.registerType('sklearn.linear_model._base.LinearRegression', class {}); + this.registerType('sklearn.linear_model._bayes.BayesianRidge', class {}); + this.registerType('sklearn.linear_model._coordinate_descent.ElasticNetCV', class {}); + this.registerType('sklearn.linear_model._coordinate_descent.ElasticNet', class {}); + this.registerType('sklearn.linear_model._logistic.LogisticRegression', class {}); + this.registerType('sklearn.linear_model._ridge.Ridge', class {}); + this.registerType('sklearn.linear_model._sgd_fast.Hinge', class {}); + this.registerType('sklearn.linear_model._sgd_fast.Log', class {}); + this.registerType('sklearn.linear_model._sgd_fast.ModifiedHuber', class {}); + this.registerType('sklearn.linear_model._sgd_fast.SquaredHinge', class {}); + this.registerType('sklearn.linear_model._stochastic_gradient.SGDClassifier', class {}); + this.registerType('sklearn.linear_model.base.LinearRegression', class {}); + this.registerType('sklearn.linear_model.sgd_fast.Hinge', class {}); + this.registerType('sklearn.linear_model.LogisticRegression', class {}); + this.registerType('sklearn.linear_model.logistic.LogisticRegression', class {}); + this.registerType('sklearn.linear_model.logistic.LogisticRegressionCV', class {}); + this.registerType('sklearn.linear_model.LassoLars​', class {}); + this.registerType('sklearn.linear_model.ridge.Ridge', class {}); + this.registerType('sklearn.linear_model.sgd_fast.Log', class {}); + this.registerType('sklearn.linear_model.stochastic_gradient.SGDClassifier', class {}); + this.registerType('sklearn.metrics._scorer._PredictScorer', class {}); + this.registerType('sklearn.metrics.scorer._PredictScorer', class {}); + this.registerType('sklearn.metrics._scorer._ThresholdScorer', class {}); + this.registerType('sklearn.mixture._bayesian_mixture.BayesianGaussianMixture', class {}); + this.registerType('sklearn.model_selection._search.GridSearchCV', class {}); + this.registerType('sklearn.model_selection._search.RandomizedSearchCV', class {}); + this.registerType('sklearn.model_selection._split.KFold', class {}); + this.registerType('sklearn.model_selection._split.StratifiedKFold', class {}); + this.registerType('sklearn.multiclass.OneVsRestClassifier', class {}); + this.registerType('sklearn.multioutput.MultiOutputClassifier', class {}); + this.registerType('sklearn.multioutput.MultiOutputRegressor', class {}); + this.registerType('sklearn.naive_bayes.BernoulliNB', class {}); + this.registerType('sklearn.naive_bayes.ComplementNB', class {}); + this.registerType('sklearn.naive_bayes.GaussianNB', class {}); + this.registerType('sklearn.naive_bayes.MultinomialNB', class {}); + this.registerType('sklearn.neighbors._classification.KNeighborsClassifier', class {}); + this.registerType('sklearn.neighbors._dist_metrics.newObj', class {}); + this.registerType('sklearn.neighbors._kd_tree.newObj', class {}); + this.registerType('sklearn.neighbors._regression.KNeighborsRegressor', class {}); + this.registerType('sklearn.neighbors.classification.KNeighborsClassifier', class {}); + this.registerType('sklearn.neighbors.dist_metrics.newObj', class {}); + this.registerType('sklearn.neighbors.kd_tree.newObj', class {}); + this.registerType('sklearn.neighbors.KNeighborsClassifier', class {}); + this.registerType('sklearn.neighbors.KNeighborsRegressor', class {}); + this.registerType('sklearn.neighbors.regression.KNeighborsRegressor', class {}); + this.registerType('sklearn.neighbors.unsupervised.NearestNeighbors', class {}); + this.registerType('sklearn.neural_network._multilayer_perceptron.MLPClassifier', class {}); + this.registerType('sklearn.neural_network._multilayer_perceptron.MLPRegressor', class {}); + this.registerType('sklearn.neural_network._stochastic_optimizers.AdamOptimizer', class {}); + this.registerType('sklearn.neural_network._stochastic_optimizers.SGDOptimizer', class {}); + this.registerType('sklearn.neural_network.rbm.BernoulliRBM', class {}); + this.registerType('sklearn.neural_network.multilayer_perceptron.MLPClassifier', class {}); + this.registerType('sklearn.neural_network.multilayer_perceptron.MLPRegressor', class {}); + this.registerType('sklearn.neural_network.stochastic_gradient.SGDClassifier', class {}); + this.registerType('sklearn.pipeline.Pipeline', class {}); + this.registerType('sklearn.pipeline.FeatureUnion', class {}); + this.registerType('sklearn.preprocessing._data.MinMaxScaler', class {}); + this.registerType('sklearn.preprocessing._data.MaxAbsScaler', class {}); + this.registerType('sklearn.preprocessing._data.Normalizer', class {}); + this.registerType('sklearn.preprocessing._data.PolynomialFeatures', class {}); + this.registerType('sklearn.preprocessing._data.QuantileTransformer', class {}); + this.registerType('sklearn.preprocessing._data.RobustScaler', class {}); + this.registerType('sklearn.preprocessing._data.StandardScaler', class {}); + this.registerType('sklearn.preprocessing._discretization.KBinsDiscretizer', class {}); + this.registerType('sklearn.preprocessing._encoders.OneHotEncoder', class {}); + this.registerType('sklearn.preprocessing._function_transformer.FunctionTransformer', class {}); + this.registerType('sklearn.preprocessing._label.LabelBinarizer', class {}); + this.registerType('sklearn.preprocessing._label.LabelEncoder', class {}); + this.registerType('sklearn.preprocessing.data.Binarizer', class {}); + this.registerType('sklearn.preprocessing.data.MaxAbsScaler', class {}); + this.registerType('sklearn.preprocessing.data.MinMaxScaler', class {}); + this.registerType('sklearn.preprocessing.data.Normalizer', class {}); + this.registerType('sklearn.preprocessing.data.OneHotEncoder', class {}); + this.registerType('sklearn.preprocessing.data.PolynomialFeatures', class {}); + this.registerType('sklearn.preprocessing.data.PowerTransformer', class {}); + this.registerType('sklearn.preprocessing.data.RobustScaler', class {}); + this.registerType('sklearn.preprocessing.data.QuantileTransformer', class {}); + this.registerType('sklearn.preprocessing.data.StandardScaler', class {}); + this.registerType('sklearn.preprocessing.imputation.Imputer', class {}); + this.registerType('sklearn.preprocessing.label.LabelBinarizer', class {}); + this.registerType('sklearn.preprocessing.label.LabelEncoder', class {}); + this.registerType('sklearn.preprocessing.label.MultiLabelBinarizer', class {}); + this.registerType('sklearn.svm._classes.LinearSVC', class {}); + this.registerType('sklearn.svm._classes.SVC', class {}); + this.registerType('sklearn.svm._classes.SVR', class {}); + this.registerType('sklearn.svm.classes.LinearSVC', class {}); + this.registerType('sklearn.svm.classes.OneClassSVM', class {}); + this.registerType('sklearn.svm.classes.SVC', class {}); + this.registerType('sklearn.svm.classes.SVR', class {}); + this.registerType('sklearn.tree._classes.DecisionTreeClassifier', class {}); + this.registerType('sklearn.tree._classes.DecisionTreeRegressor', class {}); + this.registerType('sklearn.tree._classes.ExtraTreeClassifier', class {}); + this.registerType('sklearn.tree._classes.ExtraTreeRegressor', class {}); + this.registerType('sklearn.tree._tree.Tree', class { + constructor(n_features, n_classes, n_outputs) { + this.n_features = n_features; + this.n_classes = n_classes; + this.n_outputs = n_outputs; + } + __setstate__(state) { + this.max_depth = state.max_depth; + this.node_count = state.node_count; + this.nodes = state.nodes; + this.values = state.values; + } + }); + this.registerType('sklearn.tree.tree.DecisionTreeClassifier', class {}); + this.registerType('sklearn.tree.tree.DecisionTreeRegressor', class {}); + this.registerType('sklearn.tree.tree.ExtraTreeClassifier', class {}); + this.registerType('sklearn.utils.Bunch', class {}); + this.registerType('sklearn.utils.deprecation.DeprecationDict', class {}); + this.registerType('re.Pattern', function(pattern, flags) { + this.pattern = pattern; + this.flags = flags; + }); + this.registerType('spacy._ml.PrecomputableAffine', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('spacy.syntax._parser_model.ParserModel', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.describe.Biases', class { + __setstate__(state) { + Object.assign(this, state); + } + }); + this.registerType('thinc.describe.Dimension', class { + __setstate__(state) { + Object.assign(this, state); + } + }); + this.registerType('thinc.describe.Gradient', class { + __setstate__(state) { + Object.assign(this, state); + } + }); + this.registerType('thinc.describe.Weights', class { + __setstate__(state) { + Object.assign(this, state); + } + }); + this.registerType('thinc.describe.Synapses', class { + __setstate__(state) { + Object.assign(this, state); + } + }); + this.registerType('thinc.neural._classes.affine.Affine', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural._classes.convolution.ExtractWindow', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural._classes.feature_extracter.FeatureExtracter', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural._classes.feed_forward.FeedForward', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural._classes.function_layer.FunctionLayer', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural._classes.hash_embed.HashEmbed', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural._classes.layernorm.LayerNorm', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural._classes.maxout.Maxout', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural._classes.resnet.Residual', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural._classes.softmax.Softmax', class { + __setstate__(state) { + Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); + } + }); + this.registerType('thinc.neural.mem.Memory', class { + }); + this.registerType('thinc.neural.ops.NumpyOps', class { + }); + this.registerType('types.CodeType', class { + constructor(/* args */) { + } + }); + this.registerType('types.MethodType', class { + constructor(/* args */) { + } + }); + this.registerType('types.ObjectType', builtins.object); + this.registerType('xgboost.compat.XGBoostLabelEncoder', class {}); + this.registerType('xgboost.core.Booster', class {}); + this.registerType('xgboost.sklearn.XGBClassifier', class {}); + this.registerType('xgboost.sklearn.XGBRegressor', class {}); + this.registerFunction('__builtin__.bytearray', function(source, encoding /*, errors */) { + if (source) { + if (encoding === 'latin-1') { + const array = new Uint8Array(source.length); + for (let i = 0; i < source.length; i++) { + array[i] = source.charCodeAt(i); + } + return array; + } + throw new python.Error("Unsupported bytearray encoding '" + JSON.stringify(encoding) + "'."); + } + return []; + }); + this.registerFunction('__builtin__.bytes', function(source, encoding /*, errors */) { + if (source) { + if (encoding === 'latin-1') { + const array = new Uint8Array(source.length); + for (let i = 0; i < source.length; i++) { + array[i] = source.charCodeAt(i); + } + return array; + } + throw new python.Error("Unsupported bytearray encoding '" + JSON.stringify(encoding) + "'."); + } + return []; + }); + this.registerFunction('__builtin__.set', function(iterable) { + return iterable ? iterable : []; + }); + this.registerFunction('__builtin__.frozenset', function(iterable) { + return iterable ? iterable : []; + }); + this.registerFunction('__builtin__.getattr', function(obj, name, defaultValue) { + if (Object.prototype.hasOwnProperty.call(obj, name)) { + return obj[name]; + } + return defaultValue; + }); + this.registerFunction('__builtin__.slice', function(start, stop , step) { + return [ start, stop, step ]; + }); + this.registerFunction('__builtin__.type', function(obj) { + return obj ? obj.__class__ : undefined; + }); + this.registerFunction('_codecs.encode', function(obj /*, econding */) { + return obj; + }); + this.registerFunction('builtins.bytearray', function(data) { + return { data: data }; + }); + this.registerFunction('builtins.getattr', function(obj, name, defaultValue) { + if (Object.prototype.hasOwnProperty.call(obj, name)) { + return obj[name]; + } + return defaultValue; + }); + this.registerFunction('builtins.set', function(iterable) { + return iterable ? iterable : []; + }); + this.registerFunction('builtins.slice', function(start, stop, step) { + return { start: start, stop: stop, step: step }; + }); + this.registerFunction('cloudpickle.cloudpickle._builtin_type', function(name) { + return name; + }); + this.registerFunction('collections.Counter', function(/* iterable */) { + return { __module__: 'collections', __name__: 'Counter' }; + }); + this.registerFunction('collections.defaultdict', function(/* default_factory */) { + return {}; + }); + this.registerFunction('copy_reg._reconstructor', function(cls, base, state) { + // copyreg._reconstructor in Python 3 + if (base === '__builtin__.object' || base === builtins.object) { + return self.invoke(cls, []); + } + else if (base === '__builtin__.tuple' || base === builtins.tuple) { + const obj = self.invoke(cls, []); + for (let i = 0; i < state.length; i++) { + obj[i] = state[i]; + } + return obj; + } + throw new python.Error("Unknown copy_reg._reconstructor base type '" + base + "'."); + }); + this.registerFunction('dill._dill._create_cell', function(/* args */) { + return function() { + // TODO + }; + }); + this.registerFunction('dill._dill._create_code', function(args) { + return self.invoke('types.CodeType', [ args ]); + }); + this.registerFunction('dill._dill._create_function', function(/* fcode, fglobals, fname, fdefaults, fclosure, fdict, fkwdefaults */) { + return function() { + // TODO + }; + }); + this.registerFunction('dill._dill._get_attr', function(self, name) { + if (Object.prototype.hasOwnProperty.call(self, name)) { + return self[name]; + } + return undefined; + }); + this.registerFunction('dill._dill._import_module', function(import_name, safe) { + try { + return self.context.getx(import_name); + } + catch (err) { + if (safe) { + return null; + } + } + }); + this.registerFunction('dill.dill._load_type', function(name) { + return self.context.getx('types.' + name); + }); + this.registerFunction('dill._dill._load_type', function(name) { + return self.context.getx('types.' + name); + }); + this.registerFunction('getattr', function(obj, name, defaultValue) { + if (Object.prototype.hasOwnProperty.call(obj, name)) { + return obj[name]; + } + return defaultValue; + }); + this.registerFunction('numpy.core._multiarray_umath._reconstruct', function(subtype, shape, dtype) { + return self.invoke(subtype, [ shape, dtype ]); + }); + this.registerFunction('numpy.core.multiarray._reconstruct', function(subtype, shape, dtype) { + return self.invoke(subtype, [ shape, dtype ]); + }); + this.registerFunction('numpy.core.multiarray.scalar', function(dtype, rawData) { + let data = rawData; + if (typeof rawData === 'string' || rawData instanceof String) { + data = new Uint8Array(rawData.length); + for (let i = 0; i < rawData.length; i++) { + data[i] = rawData.charCodeAt(i); + } + } + const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength); + switch (dtype.name) { + case 'float32': + return dataView.getFloat32(0, true); + case 'float64': + return dataView.getFloat64(0, true); + case 'uint8': + return dataView.getUint8(0, true); + case 'int8': + return dataView.getInt8(0, true); + case 'int16': + return dataView.getInt16(0, true); + case 'int32': + return dataView.getInt32(0, true); + case 'int64': + return dataView.getInt64(0, true); + case 'bool': + return dataView.getInt8(0, true) ? true : false; + } + throw new python.Error("Unknown scalar type '" + dtype.name + "'."); + }); + this.registerFunction('numpy.core._multiarray_umath.scalar', function(dtype, rawData) { + let data = rawData; + if (typeof rawData === 'string') { + data = new Uint8Array(rawData.length); + for (let i = 0; i < rawData.length; i++) { + data[i] = rawData.charCodeAt(i); + } + } + const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength); + switch (dtype.name) { + case 'uint8': + return dataView.getUint8(0); + case 'float32': + return dataView.getFloat32(0, true); + case 'float64': + return dataView.getFloat64(0, true); + case 'int8': + return dataView.getInt8(0, true); + case 'int16': + return dataView.getInt16(0, true); + case 'int32': + return dataView.getInt32(0, true); + case 'int64': + return dataView.getInt64(0, true); + } + throw new python.Error("Unknown scalar type '" + dtype.name + "'."); + }); + this.registerFunction('numpy.load', function(file) { + // https://github.com/numpy/numpy/blob/main/numpy/lib/format.py + const signature = [ 0x93, 0x4E, 0x55, 0x4D, 0x50, 0x59 ]; + if (!file.read(6).every((v, i) => v == signature[i])) { + throw new numpy.Error('Invalid signature.'); + } + const major = file.read(1)[0]; + const minor = file.read(1)[0]; + if (major > 3) { + throw new python.Error("Invalid version '" + [ major, minor ].join('.') + "'."); + } + const buffer = new Uint8Array([ 0, 0, 0, 0 ]); + buffer.set(file.read(major >= 2 ? 4 : 2), 0); + const header_length = buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0]; + let header = file.read(header_length); + const decoder = new TextDecoder(major >= 3 ? 'utf-8' : 'ascii'); + header = decoder.decode(header); + header = JSON.parse(header.replace(/\(/,'[').replace(/\)/,']').replace('[,','[1,]').replace(',]',',1]').replace(/'/g, '"').replace(/:\s*False\s*,/,':false,').replace(/:\s*True\s*,/,':true,').replace(/,\s*\}/, ' }')); + if (!header.descr || header.descr.length < 2) { + throw new numpy.Error("Missing property 'descr'."); + } + if (!header.shape) { + throw new numpy.Error("Missing property 'shape'."); + } + const shape = header.shape; + const dtype = self.invoke('numpy.dtype', [ header.descr.substring(1) ]); + dtype.byteorder = header.descr[0]; + let data = null; + switch (dtype.byteorder) { + case '|': { + data = file.read(); + break; + } + case '>': + case '<': { + if (header.descr.length !== 3) { + throw new numpy.Error("Unsupported data type '" + header.descr + "'."); + } + const count = shape.length === 0 ? 1 : shape.reduce((a, b) => a * b, 1); + data = file.read(dtype.itemsize * count); + break; + } + default: { + throw new numpy.Error("Unsupported data type '" + header.descr + "'."); + } + } + if (header.fortran_order) { + data = null; + } + return self.invoke('numpy.ndarray', [ shape, dtype, data ]); + }); + this.registerFunction('numpy.save', function(file, arr) { + const descr = arr.dtype.str; + if (descr[0] !== '<' && descr[0] !== '>') { + throw new numpy.Error("Unknown byte order '" + descr + "'."); + } + if (descr.length !== 3 || (descr[1] !== 'f' && descr[1] !== 'i' && descr[1] !== 'u' && descr.substring(1) !== 'b1')) { + throw new numpy.Error("Unsupported data type '" + descr + "'."); + } + let shape = ''; + switch (arr.shape.length) { + case 0: shape = '()'; break; + case 1: shape = '(' + arr.shape[0].toString() + ',)'; break; + default: shape = '(' + arr.shape.map((dimension) => dimension.toString()).join(', ') + ')'; break; + } + const properties = [ + "'descr': '" + descr + "'", + "'fortran_order': False", + "'shape': " + shape + ]; + let header = '{ ' + properties.join(', ') + ' }'; + header += ' '.repeat(64 - ((header.length + 2 + 8 + 1) & 0x3f)) + '\n'; + const encoder = new TextEncoder('ascii'); + file.write([ 0x93, 0x4E, 0x55, 0x4D, 0x50, 0x59, 0x01, 0x00 ]); // '\\x93NUMPY' + version + file.write([ header.length & 0xff, (header.length >> 8) & 0xff ]); + file.write(encoder.encode(header)); + file.write(arr.tobytes()); + }); + this.registerFunction('numpy.asarray', function(a, dtype) { + const encode = (context, data, dim) => { + const size = context.shape[dim]; + const littleendian = context.littleendian; + if (dim == context.shape.length - 1) { + for (let i = 0; i < size; i++) { + switch (context.dtype) { + case 'f2': + context.view.setFloat16(context.position, data[i], littleendian); + break; + case 'f4': + context.view.setFloat32(context.position, data[i], littleendian); + break; + case 'f8': + context.view.setFloat64(context.position, data[i], littleendian); + break; + case 'i1': + context.view.setInt8(context.position, data[i], littleendian); + break; + case 'i2': + context.view.setInt16(context.position, data[i], littleendian); + break; + case 'i4': + context.view.setInt32(context.position, data[i], littleendian); + break; + case 'i8': + context.view.setInt64(context.position, data[i], littleendian); + break; + case 'u1': + context.view.setUint8(context.position, data[i], littleendian); + break; + case 'u2': + context.view.setUint16(context.position, data[i], littleendian); + break; + case 'u4': + context.view.setUint32(context.position, data[i], littleendian); + break; + case 'u8': + context.view.setUint64(context.position, data[i], littleendian); + break; + } + context.position += context.itemsize; + } + } + else { + for (let j = 0; j < size; j++) { + encode(context, data[j], dim + 1); + } + } + }; + const array_size = (value) => { + if (value.every((item) => Array.isArray(item))) { + const dims = value.map((item) => array_size(item)); + const dim = dims[0]; + for (let i = 1; i < dims.length; i++) { + if (dim.length === dims[i].length) { + if (!dims[i].every((value, i) => value ===dim[i])) { + throw new python.Error('Invalid array shape.'); + } + } + } + return [ value.length ].concat(dim); + } + return [ value.length ]; + }; + const shape = Array.isArray(a) ? array_size(a) : []; + const size = dtype.itemsize * shape.reduce((a, b) => a * b, 1); + const context = { + position: 0, + itemsize: dtype.itemsize, + dtype: dtype.str.substring(1), + littleendian: dtype.str[0], + shape: shape, + data: new Uint8Array(size) + }; + context.view = new DataView(context.data.buffer, context.data.byteOffset, size); + encode(context, a, 0); + return self.invoke('numpy.ndarray', [ shape, dtype, context.data ]); + + }); + this.registerFunction('numpy.ma.core._mareconstruct', function(subtype, baseclass, baseshape, basetype) { + const data = self.invoke(baseclass, [ baseshape, basetype ]); + // = ndarray.__new__(ndarray, baseshape, make_mask_descr(basetype)) + const mask = self.invoke('numpy.ndarray', [ baseshape, '' ]); + return self.invoke(subtype, [ data, mask, basetype ]); + }); + this.registerFunction('numpy.random.__RandomState_ctor', function() { + return {}; + }); + this.registerFunction('numpy.random._pickle.__randomstate_ctor', function() { + return {}; + }); + this.registerFunction('numpy.core.numeric._frombuffer', function(/* buf, dtype, shape, order */) { + return {}; + }); + this.registerFunction('re._compile', function(pattern, flags) { + return self.invoke('re.Pattern', [ pattern, flags ]); + }); + this.registerFunction('srsly.cloudpickle.cloudpickle._builtin_type', function(name) { + return function() { + return self.invoke('types.' + name, arguments); + }; + }); + } + + get context() { + return this._context; + } + + source(file) { + return this._sources.has(file) ? this._sources.get(file) : null; + } + + debug(/* file */) { + } + + parse(file) { + const buffer = this.source(file); + if (buffer) { + const debug = this.debug(file); + const code = this._utf8Decoder.decode(buffer); + const reader = new python.Parser(code, file, debug); + const program = reader.parse(); + if (!program) { + throw new python.Error("Module '" + file + "' parse error."); + } + return program; + } + return null; + } + + package(name) { + const index = name.lastIndexOf('.'); + if (index > 0) { + this.package(name.substring(0, index)); + } + if (!this._packages.has(name)) { + const file = 'code/' + name.split('.').join('/') + '.py'; + const program = this.parse(file); + if (program) { + let globals = this._context.getx(name); + if (globals === undefined) { + globals = {}; + this._context.setx(name, globals); + } + globals.__class__ = this._context.scope.builtins.module; + globals.__name__ = name; + globals.__file__ = file; + this._packages.set(name, globals); + const context = this._context.push(globals); + this.block(program.body, context); + } + } + return this._packages.get(name); + } + + type(name) { + const type = this._context.getx(name); + if (type !== undefined) { + return type; + } + const parts = name.split('.'); + const className = parts.pop(); + const moduleName = parts.join('.'); + const module = this.package(moduleName); + if (module) { + return module[className]; + } + return null; + } + + invoke(name, args) { + const target = name.__class__ ? name : this.type(name); + if (target) { + if (target.__class__ === this._context.scope.builtins.type) { + if (target.prototype && target.prototype.__class__ === target) { + return Reflect.construct(target, args); + } + const obj = {}; + obj.__proto__ = target; + if (obj.__init__ && typeof obj.__init__ === 'function') { + obj.__init__.apply(obj, args); + } + return obj; + } + else if (target.__class__ === this._context.scope.builtins.function) { + if (target.__call__) { + return target.__call__(args); + } + else { + return target.apply(null, args); + } + } + } + this._raiseUnkownName(name); + this.registerType(name, class {}); + return this.invoke(name, []); + } + + call(target, name, args, context) { + const callTarget = this._target(target, context); + const callArguments = args.map((argument) => this.expression(argument, context)); + if (!callTarget || (name !== null && !callTarget[name])) { + if (name === '__new__' && callArguments.length === 1 && callArguments[0] == callTarget) { + name = null; + callArguments.shift(); + } + else { + const targetName = python.Utility.target(target) + '.' + name; + if (this.type(targetName)) { + return this.invoke(targetName, callArguments); + } + throw new python.Error("Unsupported function '" + targetName + "'."); + } + } + const func = name ? callTarget[name] : callTarget; + if (func.__class__ === this._context.scope.builtins.type) { + if (func.prototype && func.prototype.__class__ === func) { + return Reflect.construct(func, args); + } + const obj = {}; + obj.__proto__ = func; + obj.__class__ = func; + if (obj.__init__ && typeof obj.__init__ === 'function') { + obj.__init__.apply(obj, args); + } + return obj; + } + if (func.__class__ === this._context.scope.builtins.function) { + if (func.__call__) { + return func.__call__(callArguments); + } + } + if (func.__class__ === this._context.scope.builtins.method) { + if (func.__call__) { + return func.__call__([ callTarget ].concat(callArguments)); + } + } + if (typeof func === 'function') { + return func.apply(callTarget, callArguments); + } + throw new python.Error("Unsupported call expression."); + } + + apply(method, args, context) { + const locals = Array.prototype.slice.call(args); + context = context.push(); + for (const parameter of method.parameters) { + let value = locals.shift(); + if (value === undefined && parameter.initializer) { + value = this.expression(parameter.initializer, context); + } + context.set(parameter.name, value); + } + return this.block(method.body.statements, context); + } + + block(statements, context) { + statements = Array.prototype.slice.call(statements); + while (statements.length > 0) { + const statement = statements.shift(); + const value = this.statement(statement, context); + if (value !== undefined) { + return value; + } + } + } + + statement(statement, context) { + switch (statement.type) { + case 'pass': { + break; + } + case 'return': { + return this.expression(statement.expression, context); + } + case 'def': { + const module = context.get('__name__'); + const self = this; + const parent = context.get('__class__'); + let type = null; + if (parent === this._context.scope.builtins.type) { + type = this._context.scope.builtins.method; + } + else if (parent === this._context.scope.builtins.module) { + type = this._context.scope.builtins.function; + } + else { + throw new python.Error('Invalid function scope.'); + } + const func = { + __class__: type, + __globals__: context, + __module__: module, + __name__: statement.name, + __code__: statement, + __call__: function(args) { + return self.apply(this.__code__, args, this.__globals__); + } + }; + context.set(statement.name, func); + break; + } + case 'class': { + const scope = { + __class__:this._context.scope.builtins.type, + __module__: context.get('__name__'), + __name__: statement.name, + }; + context.set(statement.name, scope); + context = context.push(scope); + this.block(statement.body.statements, context); + context = context.pop(); + break; + } + case 'var': { + context.set(statement.name, statement.initializer ? this.expression(statement.initializer, context) : undefined); + break; + } + case '=': { + this.expression(statement, context); + break; + } + case 'if': { + const condition = this.expression(statement.condition, context); + if (condition === true || condition) { + const value = this.block(statement.then.statements, context); + if (value !== undefined) { + return value; + } + break; + } + else if (condition === false) { + const value = this.block(statement.else.statements, context); + if (value !== undefined) { + return value; + } + break; + } + throw new python.Error("Unknown condition."); + } + case 'for': { + if (statement.target.length == 1 && + statement.variable.length === 1 && statement.variable[0].type === 'id') { + const range = this.expression(statement.target[0], context); + const variable = statement.variable[0]; + for (const current of range) { + this.statement({ type: '=', target: variable, expression: { type: 'number', value: current }}, context); + const value = this.block(statement.body.statements, context); + if (value !== undefined) { + return value; + } + } + break; + } + throw new python.Error("Unsupported 'for' statement."); + } + case 'while': { + const condition = this.expression(statement.condition, context); + if (condition) { + const value = this.block(statement.body.statements, context); + if (value !== undefined) { + return value; + } + } + break; + } + case 'call': { + this.expression(statement, context); + break; + } + case 'import': { + for (const module of statement.modules) { + const moduleName = python.Utility.target(module.name); + const globals = this.package(moduleName); + if (module.as) { + context.set(module.as, globals); + } + } + break; + } + default: { + throw new python.Error("Unknown statement '" + statement.type + "'."); + } + } + } + + + expression(expression, context) { + const self = context.getx('self'); + switch (expression.type) { + case '=': { + const target = expression.target; + if (target.type === 'id') { + context.set(target.value, this.expression(expression.expression, context)); + return; + } + else if (target.type === '[]') { + if (target.target.type === 'id' && + target.arguments.type === 'list' && + target.arguments.value.length === 1) { + const index = this.expression(target.arguments.value[0], context); + if (target.target.value === '__annotations__') { + context.set(target.target.value, context.get(target.target.value) || {}); + } + context.get(target.target.value)[index] = this.expression(expression.expression, context); + return; + } + } + else if (target.type === '.' && + target.member.type === 'id') { + this.expression(target.target, context)[target.member.value] = this.expression(expression.expression, context); + return; + } + else if (target.type === 'tuple') { + context.target.push(target.value); + const value = this.expression(expression.expression, context); + context.target.pop(); + if (target.value.every((item) => item.type === 'id')) { + if (target.value.length < value.length) { + throw new python.Error('ValueError: too many values to unpack (expected ' + target.value.length + ', actual ' + value.length + ').'); + } + if (target.value.length > value.length) { + throw new python.Error('ValueError: not enough values to unpack (expected ' + target.value.length + ', actual ' + value.length + ').'); + } + for (let i = 0; i < value.length; i++) { + context.set(target.value[i].value, value[i]); + } + return; + } + } + break; + } + case 'list': { + return expression.value.map((item) => this.expression(item, context)); + } + case 'string': { + return expression.value.substring(1, expression.value.length - 1); + } + case 'number': { + return Number(expression.value); + } + case '[]': { + if (expression.target.type === 'id' && + expression.arguments.type === 'list' && + expression.arguments.value.length === 1) { + if (context.get(expression.target.value)) { + const index = this.expression(expression.arguments.value[0], context); + const target = context.get(expression.target.value); + return target[index < 0 ? target.length + index : index]; + } + } + const target = this.expression(expression.target, context); + if (target && expression.arguments.type === 'list' && + (target.__class__ === this.context.scope.typing._TupleType || + target.__class__ === this.context.scope.typing._SpecialGenericAlias || + target.__class__ === this.context.scope.typing._SpecialForm)) { + const type = Object.assign({}, target); + type.__args__ = expression.arguments.value.map((arg) => this.expression(arg, context)); + return type; + } + if (expression.arguments.type === 'list' && expression.arguments.value.length === 1) { + const index = this.expression(expression.arguments.value[0], context); + return target[index < 0 ? target.length + index : index]; + } + break; + } + case '.': { + if (expression.member.type == 'id') { + const target = this._target(expression.target, context); + return target[expression.member.value]; + } + throw new python.Error("Unsupported field expression."); + } + case 'call': { + if (expression.target.type === 'id' && expression.target.value === 'unchecked_cast' && expression.arguments.length === 2) { + return this.expression(expression.arguments[1], context); + } + if (expression.target.type === '.') { + return this.call(expression.target.target, expression.target.member.value, expression.arguments, context); + } + return this.call(expression.target, null, expression.arguments, context); + } + case 'id': { + switch (expression.value) { + case 'self': return self; + case 'None': return null; + case 'True': return true; + case 'False': return false; + } + const type = (value) => { + return value && + (value.__class__ === this._context.scope.builtins.type || + value.__class__ === this._context.scope.typing._TupleType || + value.__class__ === this._context.scope.typing._SpecialGenericAlias || + value.__class__ === this._context.scope.typing._SpecialForm); + }; + const builtin = this._context.scope.builtins[expression.value]; + if (type(builtin)) { + return builtin; + } + const value = context.get(expression.value); + if (value === undefined) { + const typing = this._context.scope.typing[expression.value]; + if (type(typing)) { + return typing; + } + const torch = this._context.scope.torch[expression.value]; + if (type(torch)) { + return torch; + } + } + return value; + } + case 'tuple': { + return expression.value.map((expression) => this.expression(expression, context)); + } + case 'dict': { + const dict = {}; + for (const pair of expression.value) { + if (pair.type !== 'pair') { + throw new python.Error("Unsupported dict item type '" + pair.type + "'."); + } + const key = this.expression(pair.key, context); + const value = this.expression(pair.value, context); + dict[key] = value; + } + return dict; + } + } + throw new python.Error("Unknown expression '" + expression.type + "'."); + } + + _target(expression, context) { + let current = expression; + let packageName = ''; + for (;;) { + if (current.type === '.' && current.member && current.member.type === 'id') { + packageName = '.' + current.member.value + packageName; + current = current.target; + } + else if (current.type === 'id' && current.value !== 'self' && current.value !== 'CONSTANTS') { + packageName = current.value + packageName; + break; + } + else { + packageName = null; + break; + } + } + if (packageName) { + let target = context.getx(packageName); + if (!target) { + target = this.package(packageName); + if (!target) { + target = context.getx(packageName); + if (!target) { + throw new python.Error("Failed to resolve module '" + packageName + "'."); + } + } + } + return target; + } + return this.expression(expression, context); + } + + registerFunction(name, callback) { + if (this._context.getx(name)) { + throw new python.Error("Function '" + name + "' is already registered."); + } + const parts = name.split('.'); + callback.__class__ = this._context.scope.builtins.function; + callback.__name__ = parts.pop(); + callback.__module__ = parts.join('.'); + this._context.setx(name, callback); + } + + registerType(name, type) { + if (this._context.getx(name)) { + throw new python.Error("Class '" + name + "' is already registered."); + } + const parts = name.split('.'); + type.__class__ = this._context.scope.builtins.type; + type.__name__ = parts.pop(); + type.__module__ = parts.join('.'); + type.prototype.__class__ = type; + this._context.setx(name, type); + } + + registerModule(name) { + let scope = this._context.scope; + const items = name.split('.'); + while (items.length > 0) { + const item = items.shift(); + scope[item] = { __name__: name, __class__: this._context.scope.builtins.module }; + scope = scope[item]; + } + } + + _raiseUnkownName(name) { + if (name && !this._unknownNameMap.has(name)) { + this._unknownNameMap.add(name); + const module = name.split('.').shift(); + if (this._context.scope[module] && this._context.scope[module].__class__ == this._context.scope.builtins.module) { + this._exceptionCallback(new python.Error("Unknown function '" + name + "'."), false); + } + } + } +}; + +python.Execution.Context = class { + + constructor(parent, scope) { + this._parent = parent || null; + this._scope = scope || {}; + } + + push(scope) { + return new python.Execution.Context(this, scope); + } + + pop() { + return this._parent; + } + + get scope() { + return this._scope; + } + + set(name, value) { + this._scope[name] = value; + } + + get(name) { + if (name in this._scope) { + return this._scope[name]; + } + if (this._parent) { + return this._parent.get(name); + } + return undefined; + } + + setx(name, value) { + if (typeof name !== 'string' || !name.split) { + throw new python.Error("Invalid name '" + JSON.stringify(name) + "'."); + } + const parts = name.split('.'); + if (parts.length == 1) { + this.set(parts[0], value); + } + else { + let parent = this.get(parts[0]); + if (!parent) { + parent = {}; + this.set(parts[0], parent); + } + parts.shift(); + while (parts.length > 1) { + const part = parts.shift(); + parent[part] = parent[part] || {}; + parent = parent[part]; + } + parent[parts[0]] = value; + } + } + + getx(name) { + const parts = name.split('.'); + let value = this.get(parts[0]); + if (value !== undefined) { + parts.shift(); + while (parts.length > 0 && value[parts[0]]) { + value = value[parts[0]]; + parts.shift(); + } + if (parts.length === 0) { + return value; + } + } + return undefined; + } + + get target() { + this._target = this._target || []; + return this._target; + } +}; + +python.Utility = class { + + static target(expression) { + if (expression.type == 'id') { + return expression.value; + } + if (expression.type == '.') { + return python.Utility.target(expression.target) + '.' + python.Utility.target(expression.member); + } + return null; + } +}; + +python.Unpickler = class { + + static open(data) { + const reader = data instanceof Uint8Array ? new python.Unpickler.BinaryReader(data) : new python.Unpickler.StreamReader(data); + if (reader.length > 2) { + const head = reader.peek(2); + if (head[0] === 0x80 && head[1] < 7) { + return new python.Unpickler(reader); + } + reader.seek(-1); + const tail = reader.peek(1); + reader.seek(0); + if (tail[0] === 0x2e) { + return new python.Unpickler(reader); + } + } + return null; + } + + constructor(reader) { + this._reader = reader; + } + + load(function_call, persistent_load) { + const reader = this._reader; + const marker = []; + let stack = []; + const memo = new Map(); + const OpCode = python.Unpickler.OpCode; + while (reader.position < reader.length) { + const opcode = reader.byte(); + switch (opcode) { + case OpCode.PROTO: { + const version = reader.byte(); + if (version > 5) { + throw new python.Error("Unsupported protocol version '" + version + "'."); + } + break; + } + case OpCode.GLOBAL: + stack.push([ reader.line(), reader.line() ].join('.')); + break; + case OpCode.STACK_GLOBAL: + stack.push([ stack.pop(), stack.pop() ].reverse().join('.')); + break; + case OpCode.PUT: { + const index = parseInt(reader.line(), 10); + memo.set(index, stack[stack.length - 1]); + break; + } + case OpCode.OBJ: { + const items = stack; + stack = marker.pop(); + stack.push(function_call(items.pop(), items)); + break; + } + case OpCode.GET: { + const index = parseInt(reader.line(), 10); + stack.push(memo.get(index)); + break; + } + case OpCode.POP: + stack.pop(); + break; + case OpCode.POP_MARK: + stack = marker.pop(); + break; + case OpCode.DUP: + stack.push(stack[stack.length-1]); + break; + case OpCode.PERSID: + stack.push(persistent_load(reader.line())); + break; + case OpCode.BINPERSID: + stack.push(persistent_load(stack.pop())); + break; + case OpCode.REDUCE: { + const items = stack.pop(); + const type = stack.pop(); + stack.push(function_call(type, items)); + break; + } + case OpCode.NEWOBJ: { + const items = stack.pop(); + const type = stack.pop(); + stack.push(function_call(type, items)); + break; + } + case OpCode.BINGET: + stack.push(memo.get(reader.byte())); + break; + case OpCode.INST: { + const module = reader.line(); + const name = reader.line(); + const type = module + '.' + name; + const items = stack; + stack = marker.pop(); + stack.push(function_call(type, items)); + break; + } + case OpCode.LONG_BINGET: + stack.push(memo.get(reader.uint32())); + break; + case OpCode.BINPUT: + memo.set(reader.byte(), stack[stack.length - 1]); + break; + case OpCode.LONG_BINPUT: + memo.set(reader.uint32(), stack[stack.length - 1]); + break; + case OpCode.BININT: + stack.push(reader.int32()); + break; + case OpCode.BININT1: + stack.push(reader.byte()); + break; + case OpCode.LONG: + stack.push(parseInt(reader.line(), 10)); + break; + case OpCode.BININT2: + stack.push(reader.uint16()); + break; + case OpCode.BINBYTES: + stack.push(reader.read(reader.int32())); + break; + case OpCode.BINBYTES8: + stack.push(reader.read(reader.int64())); + break; + case OpCode.SHORT_BINBYTES: + stack.push(reader.read(reader.byte())); + break; + case OpCode.FLOAT: + stack.push(parseFloat(reader.line())); + break; + case OpCode.BINFLOAT: + stack.push(reader.float64()); + break; + case OpCode.INT: { + const value = reader.line(); + if (value == '01') { + stack.push(true); + } + else if (value == '00') { + stack.push(false); + } + else { + stack.push(parseInt(value, 10)); + } + break; + } + case OpCode.EMPTY_LIST: + stack.push([]); + break; + case OpCode.EMPTY_TUPLE: + stack.push([]); + break; + case OpCode.EMPTY_SET: + stack.push([]); + break; + case OpCode.ADDITEMS: { + const items = stack; + stack = marker.pop(); + const obj = stack[stack.length - 1]; + for (let i = 0; i < items.length; i++) { + obj.push(items[i]); + } + break; + } + case OpCode.FROZENSET: { + const items = stack; + stack = marker.pop(); + stack.push(items); + break; + } + case OpCode.DICT: { + const items = stack; + stack = marker.pop(); + const dict = {}; + for (let i = 0; i < items.length; i += 2) { + dict[items[i]] = items[i + 1]; + } + stack.push(dict); + break; + } + case OpCode.LIST: { + const items = stack; + stack = marker.pop(); + stack.push(items); + break; + } + case OpCode.TUPLE: { + const items = stack; + stack = marker.pop(); + stack.push(items); + break; + } + case OpCode.SETITEM: { + const value = stack.pop(); + const key = stack.pop(); + const obj = stack[stack.length - 1]; + if (obj.__setitem__) { + obj.__setitem__(key, value); + } + else { + obj[key] = value; + } + break; + } + case OpCode.SETITEMS: { + const items = stack; + stack = marker.pop(); + const obj = stack[stack.length - 1]; + for (let i = 0; i < items.length; i += 2) { + if (obj.__setitem__) { + obj.__setitem__(items[i], items[i + 1]); + } + else { + obj[items[i]] = items[i + 1]; + } + } + break; + } + case OpCode.EMPTY_DICT: + stack.push({}); + break; + case OpCode.APPEND: { + const append = stack.pop(); + stack[stack.length-1].push(append); + break; + } + case OpCode.APPENDS: { + const appends = stack; + stack = marker.pop(); + const list = stack[stack.length - 1]; + list.push.apply(list, appends); + break; + } + case OpCode.STRING: { + const str = reader.line(); + stack.push(str.substr(1, str.length - 2)); + break; + } + case OpCode.BINSTRING: + stack.push(reader.string(reader.uint32())); + break; + case OpCode.SHORT_BINSTRING: + stack.push(reader.string(reader.byte())); + break; + case OpCode.UNICODE: + stack.push(reader.line()); + break; + case OpCode.BINUNICODE: + stack.push(reader.string(reader.uint32(), 'utf-8')); + break; + case OpCode.SHORT_BINUNICODE: + stack.push(reader.string(reader.byte(), 'utf-8')); + break; + case OpCode.BUILD: { + const state = stack.pop(); + let obj = stack.pop(); + if (obj.__setstate__) { + if (obj.__setstate__.__call__) { + obj.__setstate__.__call__([ obj, state ]); + } + else { + obj.__setstate__(state); + } + } + else if (ArrayBuffer.isView(state) || Object(state) !== state) { + obj.__state__ = state; + } + else if (obj instanceof Map) { + for (const key in state) { + obj.set(key, state[key]); + } + } + else { + Object.assign(obj, state); + } + if (obj.__read__) { + obj = obj.__read__(this); + } + stack.push(obj); + break; + } + case OpCode.MARK: + marker.push(stack); + stack = []; + break; + case OpCode.NEWTRUE: + stack.push(true); + break; + case OpCode.NEWFALSE: + stack.push(false); + break; + case OpCode.LONG1: { + const data = reader.read(reader.byte()); + let number = 0; + switch (data.length) { + case 0: number = 0; break; + case 1: number = data[0]; break; + case 2: number = data[1] << 8 | data[0]; break; + case 3: number = data[2] << 16 | data[1] << 8 | data[0]; break; + case 4: number = data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]; break; + case 5: number = data[4] * 0x100000000 + ((data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]) >>> 0); break; + default: number = Array.prototype.slice.call(data, 0); break; + } + stack.push(number); + break; + } + case OpCode.LONG4: + // TODO decode LONG4 + stack.push(reader.read(reader.uint32())); + break; + case OpCode.TUPLE1: + stack.push([ stack.pop() ]); + break; + case OpCode.TUPLE2: { + const b = stack.pop(); + const a = stack.pop(); + stack.push([ a, b ]); + break; + } + case OpCode.TUPLE3: { + const c = stack.pop(); + const b = stack.pop(); + const a = stack.pop(); + stack.push([ a, b, c ]); + break; + } + case OpCode.MEMOIZE: + memo.set(memo.size, stack[stack.length - 1]); + break; + case OpCode.FRAME: + reader.read(8); + break; + case OpCode.BYTEARRAY8: { + stack.push(reader.read(reader.int64())); + break; + } + case OpCode.NONE: + stack.push(null); + break; + case OpCode.STOP: + return stack.pop(); + default: + throw new python.Error('Unknown opcode ' + opcode + ' at position ' + (reader.position - 1).toString() + '.'); + } + } + throw new python.Error('Unexpected end of file.'); + } + + read(size) { + return this._reader.read(size); + } + + stream(size) { + return this._reader.stream(size); + } + + int32() { + return this._reader.int32(); + } + + int64() { + return this._reader.int64(); + } + + unescape(token, size) { + const length = token.length; + const a = new Uint8Array(length); + if (size && size == length) { + for (let p = 0; p < size; p++) { + a[p] = token.charCodeAt(p); + } + return a; + } + let i = 0; + let o = 0; + while (i < length) { + let c = token.charCodeAt(i++); + if (c !== 0x5C || i >= length) { + a[o++] = c; + } + else { + c = token.charCodeAt(i++); + switch (c) { + case 0x27: a[o++] = 0x27; break; // ' + case 0x5C: a[o++] = 0x5C; break; // \\ + case 0x22: a[o++] = 0x22; break; // " + case 0x72: a[o++] = 0x0D; break; // \r + case 0x6E: a[o++] = 0x0A; break; // \n + case 0x74: a[o++] = 0x09; break; // \t + case 0x62: a[o++] = 0x08; break; // \b + case 0x58: // x + case 0x78: { // X + const xsi = i - 1; + const xso = o; + for (let xi = 0; xi < 2; xi++) { + if (i >= length) { + i = xsi; + o = xso; + a[o] = 0x5c; + break; + } + let xd = token.charCodeAt(i++); + xd = xd >= 65 && xd <= 70 ? xd - 55 : xd >= 97 && xd <= 102 ? xd - 87 : xd >= 48 && xd <= 57 ? xd - 48 : -1; + if (xd === -1) { + i = xsi; + o = xso; + a[o] = 0x5c; + break; + } + a[o] = a[o] << 4 | xd; + } + o++; + break; + } + default: + if (c < 48 || c > 57) { // 0-9 + a[o++] = 0x5c; + a[o++] = c; + } + else { + i--; + const osi = i; + const oso = o; + for (let oi = 0; oi < 3; oi++) { + if (i >= length) { + i = osi; + o = oso; + a[o] = 0x5c; + break; + } + const od = token.charCodeAt(i++); + if (od < 48 || od > 57) { + i = osi; + o = oso; + a[o] = 0x5c; + break; + } + a[o] = a[o] << 3 | od - 48; + } + o++; + } + break; + } + } + } + return a.slice(0, o); + } +}; + +// https://svn.python.org/projects/python/trunk/Lib/pickletools.py +// https://github.com/python/cpython/blob/master/Lib/pickle.py +python.Unpickler.OpCode = { + MARK: 40, // '(' + EMPTY_TUPLE: 41, // ')' + STOP: 46, // '.' + POP: 48, // '0' + POP_MARK: 49, // '1' + DUP: 50, // '2' + BINBYTES: 66, // 'B' (Protocol 3) + SHORT_BINBYTES: 67, // 'C' (Protocol 3) + FLOAT: 70, // 'F' + BINFLOAT: 71, // 'G' + INT: 73, // 'I' + BININT: 74, // 'J' + BININT1: 75, // 'K' + LONG: 76, // 'L' + BININT2: 77, // 'M' + NONE: 78, // 'N' + PERSID: 80, // 'P' + BINPERSID: 81, // 'Q' + REDUCE: 82, // 'R' + STRING: 83, // 'S' + BINSTRING: 84, // 'T' + SHORT_BINSTRING: 85, // 'U' + UNICODE: 86, // 'V' + BINUNICODE: 88, // 'X' + EMPTY_LIST: 93, // ']' + APPEND: 97, // 'a' + BUILD: 98, // 'b' + GLOBAL: 99, // 'c' + DICT: 100, // 'd' + APPENDS: 101, // 'e' + GET: 103, // 'g' + BINGET: 104, // 'h' + INST: 105, // 'i' + LONG_BINGET: 106, // 'j' + LIST: 108, // 'l' + OBJ: 111, // 'o' + PUT: 112, // 'p' + BINPUT: 113, // 'q' + LONG_BINPUT: 114, // 'r' + SETITEM: 115, // 's' + TUPLE: 116, // 't' + SETITEMS: 117, // 'u' + EMPTY_DICT: 125, // '}' + PROTO: 128, + NEWOBJ: 129, + TUPLE1: 133, // '\x85' + TUPLE2: 134, // '\x86' + TUPLE3: 135, // '\x87' + NEWTRUE: 136, // '\x88' + NEWFALSE: 137, // '\x89' + LONG1: 138, // '\x8a' + LONG4: 139, // '\x8b' + SHORT_BINUNICODE: 140, // '\x8c' (Protocol 4) + BINUNICODE8: 141, // '\x8d' (Protocol 4) + BINBYTES8: 142, // '\x8e' (Protocol 4) + EMPTY_SET: 143, // '\x8f' (Protocol 4) + ADDITEMS: 144, // '\x90' (Protocol 4) + FROZENSET: 145, // '\x91' (Protocol 4) + NEWOBJ_EX: 146, // '\x92' (Protocol 4) + STACK_GLOBAL: 147, // '\x93' (Protocol 4) + MEMOIZE: 148, // '\x94' (Protocol 4) + FRAME: 149, // '\x95' (Protocol 4) + BYTEARRAY8: 150, // '\x96' (Protocol 5) + NEXT_BUFFER: 151, // '\x97' (Protocol 5) + READONLY_BUFFER: 152 // '\x98' (Protocol 5) +}; + +python.Unpickler.BinaryReader = class { + + constructor(buffer) { + this._buffer = buffer; + this._length = buffer.length; + this._position = 0; + this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + this._utf8Decoder = new TextDecoder('utf-8'); + this._asciiDecoder = new TextDecoder('ascii'); + } + + get position() { + return this._position; + } + + get length() { + return this._length; + } + + seek(position) { + this._position = position >= 0 ? position : this._length + position; + if (this._position > this._buffer.length) { + throw new Error('Expected ' + (this._position - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + skip(offset) { + this._position += offset; + if (this._position > this._buffer.length) { + throw new python.Error('Expected ' + (this._position - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + stream(length) { + const buffer = this.read(length); + return new python.Unpickler.BinaryReader(buffer); + } + + peek(length) { + const position = this._position; + length = length !== undefined ? length : this._length - this._position; + this.skip(length); + const end = this._position; + this.skip(-length); + if (position === 0 && length === this._length) { + return this._buffer; + } + return this._buffer.subarray(position, end); + } + + read(length) { + const position = this._position; + length = length !== undefined ? length : this._length - this._position; + this.skip(length); + if (position === 0 && length === this._length) { + return this._buffer; + } + return this._buffer.subarray(position, this._position); + } + + byte() { + const position = this._position; + this.skip(1); + return this._view.getUint8(position); + } + + uint16() { + const position = this._position; + this.skip(2); + return this._view.getUint16(position, true); + } + + int32() { + const position = this._position; + this.skip(4); + return this._view.getInt32(position, true); + } + + uint32() { + const position = this._position; + this.skip(4); + return this._view.getUint32(position, true); + } + + int64() { + const position = this._position; + this.skip(8); + return this._view.getInt64(position, true).toNumber(); + } + + float32() { + const position = this._position; + this.skip(4); + return this._view.getFloat32(position, true); + } + + float64() { + const position = this._position; + this.skip(8); + return this._view.getFloat64(position, true); + } + + string(size, encoding) { + const data = this.read(size); + return (encoding == 'utf-8') ? + this._utf8Decoder.decode(data) : + this._asciiDecoder.decode(data); + } + + line() { + const index = this._buffer.indexOf(0x0A, this._position); + if (index == -1) { + throw new python.Error("Could not find end of line."); + } + const size = index - this._position; + const text = this.string(size, 'ascii'); + this.skip(1); + return text; + } +}; + +python.Unpickler.StreamReader = class { + + constructor(stream) { + this._stream = stream; + this._length = stream.length; + this._position = 0; + this._utf8Decoder = new TextDecoder('utf-8'); + this._asciiDecoder = new TextDecoder('ascii'); + } + + get position() { + return this._position; + } + + get length() { + return this._length; + } + + seek(position) { + this._stream.seek(position); + this._position = this._stream.position; + } + + skip(offset) { + this._position += offset; + if (this._position > this._length) { + throw new python.Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + stream(length) { + this._stream.seek(this._position); + this.skip(length); + return this._stream.stream(length); + } + + peek(length) { + this._stream.seek(this._position); + return this._stream.peek(length); + } + + read(length) { + this._stream.seek(this._position); + this.skip(length); + return this._stream.read(length); + } + + byte() { + const position = this._fill(1); + return this._view.getUint8(position); + } + + uint16() { + const position = this._fill(2); + return this._view.getUint16(position, true); + } + + int32() { + const position = this._fill(4); + return this._view.getInt32(position, true); + } + + uint32() { + const position = this._fill(4); + return this._view.getUint32(position, true); + } + + int64() { + const position = this._fill(8); + return this._view.getInt64(position, true).toNumber(); + } + + float32() { + const position = this._fill(4); + return this._view.getFloat32(position, true); + } + + float64() { + const position = this._fill(8); + return this._view.getFloat64(position, true); + } + + string(size, encoding) { + const data = this.read(size); + return (encoding == 'utf-8') ? + this._utf8Decoder.decode(data) : + this._asciiDecoder.decode(data); + } + + line() { + let position = this._fill(0); + let index = this._buffer.indexOf(0x0A, position); + if (index == -1) { + const size = Math.min(0x1000000, this._stream.length - this._position); + this._fill(size); + this.skip(-size); + position = this._fill(0); + index = this._buffer.indexOf(0x0A, position); + if (index == -1) { + throw new python.Error("Could not find end of line."); + } + } + const size = index - position; + const text = this.string(size, 'ascii'); + this.skip(1); + return text; + } + + _fill(length) { + if (this._position + length > this._length) { + throw new Error('Expected ' + (this._position + length - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + if (!this._buffer || this._position < this._offset || this._position + length > this._offset + this._buffer.length) { + this._offset = this._position; + this._stream.seek(this._offset); + this._buffer = this._stream.read(Math.min(0x10000000, this._length - this._offset)); + this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength); + } + const position = this._position; + this._position += length; + return position - this._offset; + } +}; + +python.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'Error loading Python module.'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.Execution = python.Execution; + module.exports.Unpickler = python.Unpickler; +} \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 0000000..a1919d5 --- /dev/null +++ b/server.py @@ -0,0 +1,298 @@ + +import codecs +import errno +import os +import random +import re +import socket +import sys +import threading +import webbrowser +import time + +from .__version__ import __version__ + +if sys.version_info[0] > 2: + from urllib.parse import urlparse + from urllib.parse import unquote + from http.server import HTTPServer + from http.server import BaseHTTPRequestHandler + from socketserver import ThreadingMixIn +else: + from urlparse import urlparse + from urlparse import unquote + from BaseHTTPServer import HTTPServer + from BaseHTTPServer import BaseHTTPRequestHandler + from SocketServer import ThreadingMixIn + +class HTTPRequestHandler(BaseHTTPRequestHandler): + def handler(self): + if not hasattr(self, 'mime_types_map'): + self.mime_types_map = { + '.html': 'text/html', + '.js': 'text/javascript', + '.css': 'text/css', + '.png': 'image/png', + '.gif': 'image/gif', + '.jpg': 'image/jpeg', + '.ico': 'image/x-icon', + '.json': 'application/json', + '.pb': 'application/octet-stream', + '.ttf': 'font/truetype', + '.otf': 'font/opentype', + '.eot': 'application/vnd.ms-fontobject', + '.woff': 'font/woff', + '.woff2': 'application/font-woff2', + '.svg': 'image/svg+xml' + } + pathname = urlparse(self.path).path + folder = os.path.dirname(os.path.realpath(__file__)) + location = folder + pathname + status_code = 0 + headers = {} + buffer = None + data = '/data/' + if status_code == 0: + if pathname == '/': + meta = [] + meta.append('') + meta.append('') + if self.file: + meta.append('') + with codecs.open(location + 'index.html', mode="r", encoding="utf-8") as open_file: + buffer = open_file.read() + buffer = re.sub(r'', '\n'.join(meta), buffer) + buffer = buffer.encode('utf-8') + headers['Content-Type'] = 'text/html' + headers['Content-Length'] = len(buffer) + status_code = 200 + elif pathname.startswith(data): + file = pathname[len(data):] + if file == self.file and self.data: + buffer = self.data + else: + file = self.folder + '/' + unquote(file) + status_code = 404 + if os.path.exists(file): + with open(file, 'rb') as binary: + buffer = binary.read() + if buffer: + headers['Content-Type'] = 'application/octet-stream' + headers['Content-Length'] = len(buffer) + status_code = 200 + else: + if os.path.exists(location) and not os.path.isdir(location): + extension = os.path.splitext(location)[1] + content_type = self.mime_types_map[extension] + if content_type: + with open(location, 'rb') as binary: + buffer = binary.read() + headers['Content-Type'] = content_type + headers['Content-Length'] = len(buffer) + status_code = 200 + else: + status_code = 404 + if self.log: + sys.stdout.write(str(status_code) + ' ' + self.command + ' ' + self.path + '\n') + sys.stdout.flush() + self.send_response(status_code) + for key in headers: + self.send_header(key, headers[key]) + self.end_headers() + if self.command != 'HEAD': + if status_code == 404 and buffer is None: + self.wfile.write(bytes(status_code)) + elif (status_code in (200, 404)) and buffer is not None: + self.wfile.write(buffer) + def do_GET(self): + self.handler() + def do_HEAD(self): + self.handler() + def log_message(self, format, *args): + return + +class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): + pass + +class HTTPServerThread(threading.Thread): + def __init__(self, data, file, address, log): + threading.Thread.__init__(self) + self.address = address + self.url = 'http://' + address[0] + ':' + str(address[1]) + self.file = file + self.server = ThreadedHTTPServer(address, HTTPRequestHandler) + self.server.timeout = 0.25 + if file: + self.server.RequestHandlerClass.folder = os.path.dirname(file) if os.path.dirname(file) else '.' + self.server.RequestHandlerClass.file = os.path.basename(file) + else: + self.server.RequestHandlerClass.folder = '' + self.server.RequestHandlerClass.file = '' + self.server.RequestHandlerClass.data = data + self.server.RequestHandlerClass.log = log + self.terminate_event = threading.Event() + self.terminate_event.set() + self.stop_event = threading.Event() + + def run(self): + self.stop_event.clear() + self.terminate_event.clear() + try: + while not self.stop_event.is_set(): + self.server.handle_request() + except Exception: + pass + self.terminate_event.set() + self.stop_event.clear() + + def stop(self): + if self.alive(): + sys.stdout.write("Stopping " + self.url + "\n") + self.stop_event.set() + self.server.server_close() + self.terminate_event.wait(1000) + + def alive(self): + return not self.terminate_event.is_set() + +_thread_list = [] + +def _add_thread(thread): + global _thread_list + _thread_list.append(thread) + +def _update_thread_list(address=None): + global _thread_list + _thread_list = [ thread for thread in _thread_list if thread.alive() ] + threads = _thread_list + if address is not None: + address = _make_address(address) + if address[1] is None: + threads = [ thread for thread in threads if address[0] == thread.address[0] ] + else: + threads = [ thread for thread in threads if address[0] == thread.address[0] and address[1] == thread.address[1] ] + return threads + +def _make_address(address): + if address is None or isinstance(address, int): + port = address + address = ('localhost', port) + if isinstance(address, tuple) and len(address) == 2: + host = address[0] + port = address[1] + if isinstance(host, str) and (port is None or isinstance(port, int)): + return address + raise ValueError('Invalid address.') + +def _make_port(address): + if address[1] is None or address[1] == 0: + ports = [] + if address[1] != 0: + ports.append(8080) + ports.append(8081) + rnd = random.Random() + for _ in range(4): + port = rnd.randrange(15000, 25000) + if port not in ports: + ports.append(port) + ports.append(0) + for port in ports: + temp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + temp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + temp_socket.settimeout(1) + try: + temp_socket.bind((address[0], port)) + sockname = temp_socket.getsockname() + address = (address[0], sockname[1]) + return address + except: + pass + finally: + temp_socket.close() + if isinstance(address[1], int): + return address + raise ValueError('Failed to allocate port.') + +def stop(address=None): + '''Stop serving model at address. + + Args: + address (tuple, optional): A (host, port) tuple, or a port number. + ''' + threads = _update_thread_list(address) + for thread in threads: + thread.stop() + _update_thread_list() + +def status(adrress=None): + '''Is model served at address. + + Args: + address (tuple, optional): A (host, port) tuple, or a port number. + ''' + threads = _update_thread_list(adrress) + return len(threads) > 0 + +def wait(): + '''Wait for console exit and stop all model servers.''' + try: + while len(_update_thread_list()) > 0: + time.sleep(1000) + except (KeyboardInterrupt, SystemExit): + sys.stdout.write('\n') + sys.stdout.flush() + stop() + +def serve(file, data, address=None, browse=False, log=False): + '''Start serving model from file or data buffer at address and open in web browser. + + Args: + file (string): Model file to serve. Required to detect format. + data (bytes): Model data to serve. None will load data from file. + log (bool, optional): Log details to console. Default: False + browse (bool, optional): Launch web browser. Default: True + address (tuple, optional): A (host, port) tuple, or a port number. + + Returns: + A (host, port) address tuple. + ''' + if not data and file and not os.path.exists(file): + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), file) + + _update_thread_list() + address = _make_address(address) + if isinstance(address[1], int) and address[1] != 0: + stop(address) + else: + address = _make_port(address) + _update_thread_list() + + thread = HTTPServerThread(data, file, address, log) + thread.start() + while not thread.alive(): + time.sleep(10) + _add_thread(thread) + + if file: + sys.stdout.write("Serving '" + file + "' at " + thread.url + "\n") + else: + sys.stdout.write("Serving at " + thread.url + "\n") + sys.stdout.flush() + if browse: + webbrowser.open(thread.url) + + return address + +def start(file=None, address=None, browse=True, log=False): + '''Start serving model file at address and open in web browser. + + Args: + file (string): Model file to serve. + log (bool, optional): Log details to console. Default: False + browse (bool, optional): Launch web browser, Default: True + address (tuple, optional): A (host, port) tuple, or a port number. + + Returns: + A (host, port) address tuple. + ''' + return serve(file, None, browse=browse, address=address, log=log) diff --git a/tar.js b/tar.js new file mode 100644 index 0000000..5d40ac1 --- /dev/null +++ b/tar.js @@ -0,0 +1,171 @@ + +var tar = tar || {}; + +tar.Archive = class { + + static open(data) { + const stream = data instanceof Uint8Array ? new tar.BinaryReader(data) : data; + if (stream.length > 512) { + const buffer = stream.peek(512); + const sum = buffer.map((value, index) => (index >= 148 && index < 156) ? 32 : value).reduce((a, b) => a + b, 0); + let checksum = ''; + for (let i = 148; i < 156 && buffer[i] !== 0x00; i++) { + checksum += String.fromCharCode(buffer[i]); + } + checksum = parseInt(checksum, 8); + if (!isNaN(checksum) && sum === checksum) { + return new tar.Archive(stream); + } + } + return null; + } + + constructor(stream) { + this._entries = new Map(); + const position = stream.position; + while (stream.position < stream.length) { + const entry = new tar.Entry(stream); + if (entry.type === '0' || entry.type === '1' || entry.type === '2') { + this._entries.set(entry.name, entry.stream); + } + if (stream.position + 512 > stream.length || + stream.peek(512).every((value) => value === 0x00)) { + break; + } + } + stream.seek(position); + } + + get entries() { + return this._entries; + } +}; + +tar.Entry = class { + + constructor(stream) { + const buffer = stream.read(512); + const reader = new tar.BinaryReader(buffer); + const sum = buffer.map((value, index) => (index >= 148 && index < 156) ? 32 : value).reduce((a, b) => a + b, 0); + let checksum = ''; + for (let i = 148; i < 156 && buffer[i] !== 0x00; i++) { + checksum += String.fromCharCode(buffer[i]); + } + checksum = parseInt(checksum, 8); + if (isNaN(checksum) || sum !== checksum) { + throw new tar.Error('Invalid tar archive.'); + } + this._name = reader.string(100); + reader.string(8); // file mode + reader.string(8); // owner + reader.string(8); // group + const size = parseInt(reader.string(12).trim(), 8); + reader.string(12); // timestamp + reader.string(8); // checksum + this._type = reader.string(1); + reader.string(100); // name of linked file + if (reader.string(6) === 'ustar') { + reader.string(2); // ustar version + reader.string(32); // owner user name + reader.string(32); // owner group name + reader.string(8); // device major number + reader.string(8); // device number number + this._name = reader.string(155) + this._name; + } + this._stream = stream.stream(size); + stream.read(((size % 512) != 0) ? (512 - (size % 512)) : 0); + } + + get type() { + return this._type; + } + + get name() { + return this._name; + } + + get stream() { + return this._stream; + } +}; + +tar.BinaryReader = class { + + constructor(buffer) { + this._buffer = buffer; + this._length = buffer.length; + this._position = 0; + this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + } + + get position() { + return this._position; + } + + get length() { + return this._length; + } + + create(buffer) { + return new tar.BinaryReader(buffer); + } + + stream(length) { + return this.create(this.read(length)); + } + + seek(position) { + this._position = position >= 0 ? position : this._length + position; + } + + skip(offset) { + this._position += offset; + } + + peek(length) { + if (this._position === 0 && length === undefined) { + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + const end = this._position; + this.seek(position); + return this._buffer.subarray(position, end); + } + + read(length) { + if (this._position === 0 && length === undefined) { + this._position = this._length; + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + return this._buffer.subarray(position, this._position); + } + + string(length) { + const buffer = this.read(length); + let position = 0; + let content = ''; + for (let i = 0; i < length; i++) { + const c = buffer[position++]; + if (c === 0) { + break; + } + content += String.fromCharCode(c); + } + return content; + } +}; + +tar.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'tar Error'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.Archive = tar.Archive; +} \ No newline at end of file diff --git a/text.js b/text.js new file mode 100644 index 0000000..7308dd7 --- /dev/null +++ b/text.js @@ -0,0 +1,292 @@ + +var text = text || {}; + +text.Decoder = class { + + static open(data, encoding) { + if (typeof data === 'string') { + return new text.Decoder.String(data); + } + const assert = (encoding, condition) => { + if (encoding && encoding !== condition) { + throw new text.Error("Invalid encoding '" + encoding + "'."); + } + }; + const buffer = data instanceof Uint8Array ? data : data.peek(); + const length = buffer.length; + if (length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) { + assert(encoding, 'utf-8'); + return new text.Decoder.Utf8(buffer, 3, true); + } + if (length >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) { + assert(encoding, 'utf-16'); + return new text.Decoder.Utf16LE(buffer, 2); + } + if (length >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) { + assert(encoding, 'utf-16'); + return new text.Decoder.Utf16BE(buffer, 2); + } + if (length >= 4 && buffer[0] === 0x00 && buffer[1] === 0x00 && buffer[2] === 0xfe && buffer[3] === 0xff) { + throw new text.Error("Unsupported UTF-32 big-endian encoding."); + } + if (length >= 4 && buffer[0] === 0xff && buffer[1] === 0xfe && buffer[2] === 0x00 && buffer[3] === 0x00) { + throw new text.Error("Unsupported UTF-32 little-endian encoding."); + } + if (length >= 5 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && buffer[3] === 0x38 && buffer[4] === 0x2D) { + throw new text.Error("Unsupported UTF-7 encoding."); + } + if (length >= 4 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && (buffer[3] === 0x38 || buffer[3] === 0x39 || buffer[3] === 0x2B || buffer[3] === 0x2F)) { + throw new text.Error("Unsupported UTF-7 encoding."); + } + if (length >= 4 && buffer[0] === 0x84 && buffer[1] === 0x31 && buffer[2] === 0x95 && buffer[3] === 0x33) { + throw new text.Error("Unsupported GB-18030 encoding."); + } + if (length > 4 && (length % 2) == 0 && (buffer[0] === 0x00 || buffer[1] === 0x00 || buffer[2] === 0x00 || buffer[3] === 0x00)) { + const lo = new Uint32Array(256); + const hi = new Uint32Array(256); + for (let i = 0; i < length; i += 2) { + lo[buffer[i]]++; + hi[buffer[i + 1]]++; + } + if (lo[0x00] === 0 && (hi[0x00] / (length >> 1)) > 0.5) { + assert(encoding, 'utf-16'); + return new text.Decoder.Utf16LE(buffer, 0); + } + if (hi[0x00] === 0 && (lo[0x00] / (length >> 1)) > 0.5) { + assert(encoding, 'utf-16'); + return new text.Decoder.Utf16BE(buffer, 0); + } + } + if (encoding && (encoding.startsWith('iso-8859-') || encoding.startsWith('latin-'))) { + return new text.Decoder.Latin1(buffer, 0); + } + assert(encoding, 'utf-8'); + return new text.Decoder.Utf8(buffer, 0, encoding === 'utf-8'); + } +}; + +text.Decoder.String = class { + + constructor(buffer) { + this.buffer = buffer ? buffer.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF]/g) : []; + this.position = 0; + this.length = this.buffer.length; + } + + get encoding() { + return null; + } + + decode() { + if (this.position < this.length) { + return this.buffer[this.position++]; + } + return undefined; + } +}; + +text.Decoder.Utf8 = class { + + constructor(buffer, position, fatal) { + this.position = position || 0; + this.buffer = buffer; + this.fatal = fatal; + } + + get encoding() { + return 'utf-8'; + } + + decode() { + const c = this.buffer[this.position]; + if (c === undefined) { + return c; + } + this.position++; + if (c < 0x80) { + return String.fromCodePoint(c); + } + if (c >= 0xC2 && c <= 0xDF) { + if (this.buffer[this.position] !== undefined) { + const c2 = this.buffer[this.position]; + this.position++; + return String.fromCharCode(((c & 0x1F) << 6) | (c2 & 0x3F)); + } + } + if (c >= 0xE0 && c <= 0xEF) { + if (this.buffer[this.position + 1] !== undefined) { + const c2 = this.buffer[this.position]; + if ((c !== 0xE0 || c2 >= 0xA0) && (c !== 0xED || c2 <= 0x9f)) { + const c3 = this.buffer[this.position + 1]; + if (c3 >= 0x80 && c3 < 0xFB) { + this.position += 2; + return String.fromCharCode(((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0)); + } + } + } + } + if (c >= 0xF0 && c <= 0xF4) { + if (this.buffer[this.position + 2] !== undefined) { + const c2 = this.buffer[this.position]; + if (c2 >= 0x80 && c2 <= 0xBF) { + const c3 = this.buffer[this.position + 1]; + if (c3 >= 0x80 && c3 <= 0xBF) { + const c4 = this.buffer[this.position + 2]; + if (c4 >= 0x80 && c4 <= 0xBF) { + this.position += 3; + return String.fromCodePoint(((c & 0x07) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F)); + } + } + } + } + } + if (this.fatal) { + throw new text.Error('Invalid utf-8 character.'); + } + return String.fromCharCode(0xfffd); + } +}; + +text.Decoder.Latin1 = class { + + constructor(buffer, position) { + this.position = position || 0; + this.buffer = buffer; + } + + get encoding() { + return 'latin-1'; + } + + decode() { + const c = this.buffer[this.position]; + if (c === undefined) { + return c; + } + this.position++; + return String.fromCodePoint(c); + } +}; + +text.Decoder.Utf16LE = class { + + constructor(buffer, position) { + this.buffer = buffer; + this.position = position || 0; + this.length = buffer.length; + } + + get encoding() { + return 'utf-16'; + } + + decode() { + if (this.position + 1 < this.length) { + const c = this.buffer[this.position++] | (this.buffer[this.position++] << 8); + if (c < 0xD800 || c >= 0xDFFF) { + return String.fromCharCode(c); + } + if (c >= 0xD800 && c < 0xDBFF) { + if (this._position + 1 < this._length) { + const c2 = this._buffer[this._position++] | (this._buffer[this._position++] << 8); + if (c >= 0xDC00 || c < 0xDFFF) { + return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff)); + } + } + } + return String.fromCharCode(0xfffd); + } + return undefined; + } +}; + +text.Decoder.Utf16BE = class { + + constructor(buffer, position) { + this.buffer = buffer; + this.position = position || 0; + this.length = buffer.length; + } + + get encoding() { + return 'utf-16'; + } + + decode() { + if (this.position + 1 < this.length) { + const c = (this.buffer[this.position++] << 8) | this.buffer[this.position++]; + if (c < 0xD800 || c >= 0xDFFF) { + return String.fromCharCode(c); + } + if (c >= 0xD800 && c < 0xDBFF) { + if (this._position + 1 < this._length) { + const c2 = (this._buffer[this._position++] << 8) | this._buffer[this._position++]; + if (c >= 0xDC00 || c < 0xDFFF) { + return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff)); + } + } + } + return String.fromCharCode(0xfffd); + } + return undefined; + } +}; + +text.Reader = class { + + constructor(data, length) { + this._decoder = text.Decoder.open(data); + this._position = 0; + this._length = length || Number.MAX_SAFE_INTEGER; + } + + static open(data, length) { + return new text.Reader(data, length); + } + + read() { + if (this._position >= this._length) { + return undefined; + } + let line = ''; + let buffer = null; + for (;;) { + const c = this._decoder.decode(); + if (c === undefined) { + this._length = this._position; + break; + } + this._position++; + if (this._position > this._length) { + break; + } + if (c === '\n') { + break; + } + line += c; + if (line.length >= 32) { + buffer = buffer || []; + buffer.push(line); + line = ''; + } + } + if (buffer) { + buffer.push(line); + return buffer.join(''); + } + return line; + } +}; + +text.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'Text Error'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.Decoder = text.Decoder; + module.exports.Reader = text.Reader; +} diff --git a/view-grapher.css b/view-grapher.css new file mode 100644 index 0000000..0e44895 --- /dev/null +++ b/view-grapher.css @@ -0,0 +1,136 @@ + +.node path { stroke: #333; fill: none; stroke-width: 1px; } +.node line { stroke: #333; fill: none; stroke-width: 1px; } + +.node-item path { stroke-width: 0; stroke: #000; fill: #fff; } +.node-item text { font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "PingFang SC"; font-size: 11px; text-rendering: geometricPrecision; } + +.node-item-function path { fill: #fff; } +.node-item-function text { fill: #000; } +.node-item-function:hover { cursor: pointer; } +.node-item-function:hover path { fill: #eee; } + +.node-item-type path { fill: #000; } +.node-item-type text { fill: #fff; } +.node-item-type:hover { cursor: pointer; } +.node-item-type:hover path { fill: #fff; } +.node-item-type:hover text { fill: #000; } + +.node-item-type-constant path { fill: #eee; } +.node-item-type-constant text { fill: #000; } +.node-item-type-constant:hover path { fill: #fff; } + +.node-item-type-control path { fill: #eee; } +.node-item-type-control text { fill: #000; } +.node-item-type-control:hover path { fill: #fff; } + +.node-item-type-layer path { fill: rgb(51, 85, 136); } +.node-item-type-wrapper path { fill: rgb(238, 238, 238); } +.node-item-type-wrapper text { fill: rgb(0, 0, 0) } +.node-item-type-activation path { fill: rgb(112, 41, 33); } +.node-item-type-pool path { fill: rgb(51, 85, 51); } +.node-item-type-normalization path { fill: rgb(51, 85, 68); } +.node-item-type-dropout path { fill: rgb(69, 71, 112); } +.node-item-type-shape path { fill: rgb(108, 79, 71); } +.node-item-type-tensor path { fill: rgb(89, 66, 59); } +.node-item-type-transform path { fill: rgb(51, 85, 68); } +.node-item-type-data path { fill: rgb(85, 85, 85); } +.node-item-type-quantization path { fill: rgb(80, 40, 0); } +.node-item-type-custom path { fill: rgb(128, 128, 128); } + +.node-item-input path { fill: #fff; } +.node-item-input:hover { cursor: pointer; } +.node-item-input:hover path { fill: #fff; } + +.node-item-constant path { fill: #eee; } +.node-item-constant:hover { cursor: pointer; } +.node-item-constant:hover path { fill: #fff; } + +.node-item-undefined path { fill: #f00; } +.node-item-undefined:hover { cursor: pointer; } +.node-item-undefined:hover path { fill: #fff; } + +.node-attribute:hover { cursor: pointer; } +.node-attribute text { font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "PingFang SC"; font-size: 9px; font-weight: normal; text-rendering: geometricPrecision; } +.node-attribute path { fill: #fff; stroke-width: 0; stroke: #000; } +.node-attribute:hover path { fill: #f6f6f6; } + +.graph-item-input path { fill: #eee; } +.graph-item-input:hover { cursor: pointer; } +.graph-item-input:hover path { fill: #fff; } + +.graph-item-output path { fill: #eee; } +.graph-item-output:hover { cursor: pointer; } +.graph-item-output:hover path { fill: #fff; } + +.edge-label { font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "PingFang SC"; font-size: 10px; } +.edge-path { stroke: #000; stroke-width: 1px; fill: none; marker-end: url("#arrowhead-vee"); } +#arrowhead-vee { fill: #000; } +.edge-path-control-dependency { stroke-dasharray: 3, 2; } + +.cluster rect { stroke: #000; fill: #000; fill-opacity: 0.02; stroke-opacity: 0.06; stroke-width: 1px; } + +.select .node.border { stroke: #e00; stroke-width: 2px; } +.select.edge-path { stroke: #e00; stroke-width: 2px; marker-end: url("#arrowhead-vee-select"); } +#arrowhead-vee-select { fill: #e00; } + +@keyframes pulse { from { stroke-dashoffset: 100px; } to { stroke-dashoffset: 0; } } + +@media (prefers-color-scheme: dark) { + + .edge-label { fill: #b2b2b2; } + .edge-path { stroke: #888; } + #arrowhead-vee { fill: #888; } + + .node path { stroke: #1d1d1d; } + .node line { stroke: #1d1d1d; } + + .select .node.border { stroke: #b00; } + .select.edge-path { stroke: #b00; } + #arrowhead-vee-select { fill: #b00 } + + .node-item-function path { fill: #404040; } + .node-item-function text { fill: #dfdfdfdf; } + .node-item-function:hover { cursor: pointer; } + .node-item-function:hover path { fill: #666666; } + + .node-item-type path { fill: #303030; } + .node-item-type text { fill: #dfdfdf; } + .node-item-type:hover { cursor: pointer; } + .node-item-type:hover path { fill: #808080; } + .node-item-type:hover text { fill: #dfdfdf; } + + .node-item path { stroke: #fff; } + .node-item text { fill: #dfdfdf; } + + .node-attribute text { fill: #b2b2b2; } + .node-attribute path { fill: #2d2d2d; } + .node-attribute:hover path { fill: #666666; } + + .graph-item-input path { fill: #404040; } + .graph-item-input:hover { cursor: pointer; } + .graph-item-input:hover path { fill: #666666; } + + .graph-item-output path { fill: #404040; } + .graph-item-output:hover { cursor: pointer; } + .graph-item-output:hover path { fill: #666666; } + + .node-item-input path { fill: #404040; } + .node-item-input:hover path { fill: #666666; } + .node-item-constant path { fill: #4b4b4b; } + .node-item-constant:hover path { fill: #666666; } + + .node-item-type-layer path { fill: rgba(51, 85, 136, 0.7); } + .node-item-type-activation path { fill: rgba(75, 27, 22, 0.7); } + .node-item-type-activation path { fill: rgba(75, 27, 22, 0.7); } + .node-item-type-pool path { fill: rgba(51, 85, 51, 0.7); } + .node-item-type-pool path { fill: rgba(51, 85, 51, 0.7); } + .node-item-type-normalization path { fill: rgba(51, 85, 68, 0.7); } + .node-item-type-dropout path { fill: rgba(69, 71, 112, 0.7); } + .node-item-type-shape path { fill: rgba(108, 79, 71, 0.7); } + .node-item-type-tensor path { fill: rgba(89, 66, 59, 0.7); } + .node-item-type-transform path { fill: rgba(51, 85, 68, 0.7); } + .node-item-type-data path { fill: rgba(85, 85, 85, 0.7); } + .node-item-type-quantization path { fill: rgb(80, 40, 0, 0.7); } + .node-item-type-custom path { fill: rgb(64, 64, 64, 0.7); } +} diff --git a/view-grapher.js b/view-grapher.js new file mode 100644 index 0000000..308ca2c --- /dev/null +++ b/view-grapher.js @@ -0,0 +1,786 @@ + +var grapher = grapher || {}; +var dagre = dagre || require('./dagre'); + +grapher.Graph = class { + + constructor(compound, options) { + this._isCompound = compound; + this._options = options; + this._nodes = new Map(); + this._edges = new Map(); + this._children = {}; + this._children['\x00'] = {}; + this._parent = {}; + + // My code + this._modelNodeName2ViewNode = new Map(); + this._modelNodeName2State = new Map(); + } + + get options() { + return this._options; + } + + setNode(node) { + const key = node.name; + const value = this._nodes.get(key); + if (value) { + value.label = node; + } + else { + this._nodes.set(key, { v: key, label: node }); + if (this._isCompound) { + this._parent[key] = '\x00'; + this._children[key] = {}; + this._children['\x00'][key] = true; + } + } + + // My code + const modelNodeName = node.value.name; + // console.log(modelNodeName); + const viewNode = this._modelNodeName2ViewNode.get(modelNodeName); + if (viewNode) { + this._modelNodeName2ViewNode[modelNodeName] = node; + } + else { + this._modelNodeName2ViewNode.set(modelNodeName, node); + } + + this._modelNodeName2State.set(modelNodeName, 'Exist'); + + } + + setEdge(edge) { + if (!this._nodes.has(edge.v)) { + throw new grapher.Error(); + } + if (!this._nodes.has(edge.w)) { + throw new grapher.Error(); + } + const key = edge.v + ':' + edge.w; + if (!this._edges.has(key)) { + this._edges.set(key, { v: edge.v, w: edge.w, label: edge }); + } + } + + setParent(node, parent) { + if (!this._isCompound) { + throw new Error("Cannot set parent in a non-compound graph"); + } + parent += ""; + for (let ancestor = parent; ancestor; ancestor = this.parent(ancestor)) { + if (ancestor === node) { + throw new Error("Setting " + parent + " as parent of " + node + " would create a cycle"); + } + } + delete this._children[this._parent[node]][node]; + this._parent[node] = parent; + this._children[parent][node] = true; + return this; + } + + get nodes() { + return this._nodes; + } + + hasNode(key) { + return this._nodes.has(key); + } + + node(key) { + return this._nodes.get(key); + } + + get edges() { + return this._edges; + } + + parent(key) { + if (this._isCompound) { + const parent = this._parent[key]; + if (parent !== '\x00') { + return parent; + } + } + } + + children(key) { + key = key === undefined ? '\x00' : key; + if (this._isCompound) { + const children = this._children[key]; + if (children) { + return Object.keys(children); + } + } + else if (key === '\x00') { + return this.nodes.keys(); + } + else if (this.hasNode(key)) { + return []; + } + } + + build(document, origin) { + const createGroup = (name) => { + const element = document.createElementNS('http://www.w3.org/2000/svg', 'g'); + element.setAttribute('id', name); + element.setAttribute('class', name); + origin.appendChild(element); + return element; + }; + + const clusterGroup = createGroup('clusters'); + const edgePathGroup = createGroup('edge-paths'); + const edgeLabelGroup = createGroup('edge-labels'); + const nodeGroup = createGroup('nodes'); + + // ====> 显示 边上的箭头 + const edgePathGroupDefs = document.createElementNS('http://www.w3.org/2000/svg', 'defs'); + edgePathGroup.appendChild(edgePathGroupDefs); + const marker = (id) => { + const element = document.createElementNS('http://www.w3.org/2000/svg', 'marker'); + element.setAttribute('id', id); + element.setAttribute('viewBox', '0 0 10 10'); + element.setAttribute('refX', 9); + element.setAttribute('refY', 5); + element.setAttribute('markerUnits', 'strokeWidth'); + element.setAttribute('markerWidth', 8); + element.setAttribute('markerHeight', 6); + element.setAttribute('orient', 'auto'); + const markerPath = document.createElementNS('http://www.w3.org/2000/svg', 'path'); + markerPath.setAttribute('d', 'M 0 0 L 10 5 L 0 10 L 4 5 z'); + markerPath.style.setProperty('stroke-width', 1); + element.appendChild(markerPath); + return element; + }; + edgePathGroupDefs.appendChild(marker("arrowhead-vee")); + edgePathGroupDefs.appendChild(marker("arrowhead-vee-select")); + // <==== 显示 边上的箭头 + + // console.log(this.nodes) + for (const nodeId of this.nodes.keys()) { + const node = this.node(nodeId); + if (this.children(nodeId).length == 0) { + // node + // type(node.label) == view.Node + node.label.build(document, nodeGroup); // 如果注释:TypeError Cannot read properties of undefined (reading 'setAttribute') + } + + else { + // cluster + node.label.rectangle = document.createElementNS('http://www.w3.org/2000/svg', 'rect'); + if (node.label.rx) { + node.label.rectangle.setAttribute('rx', node.rx); + } + if (node.label.ry) { + node.label.rectangle.setAttribute('ry', node.ry); + } + node.label.element = document.createElementNS('http://www.w3.org/2000/svg', 'g'); + node.label.element.setAttribute('class', 'cluster'); + node.label.element.appendChild(node.label.rectangle); + clusterGroup.appendChild(node.label.element); + } + } + + for (const edge of this.edges.values()) { + edge.label.build(document, edgePathGroup, edgeLabelGroup); + } + } + + update() { + dagre.layout(this); + for (const nodeId of this.nodes.keys()) { + const node = this.node(nodeId); + if (this.children(nodeId).length == 0) { + // node + node.label.update(); // 让节点显示出来 + } + // ===> 这段没有操作 + else { + // cluster + const node = this.node(nodeId); + node.label.element.setAttribute('transform', 'translate(' + node.label.x + ',' + node.label.y + ')'); + node.label.rectangle.setAttribute('x', - node.label.width / 2); + node.label.rectangle.setAttribute('y', - node.label.height / 2 ); + node.label.rectangle.setAttribute('width', node.label.width); + node.label.rectangle.setAttribute('height', node.label.height); + } + // <=== 这段没有操作 + } + // console.log(this.edges) + for (const edge of this.edges.values()) { + // console.log(edge.label) + edge.label.update(); // 让边显示出来 + + } + } +}; + +grapher.Node = class { + + constructor() { + this._blocks = []; + } + + header() { + const block = new grapher.Node.Header(); + this._blocks.push(block); + return block; + } + + list() { + const block = new grapher.Node.List(); + this._blocks.push(block); + return block; + } + + canvas() { + const block = new grapher.Node.Canvas(); + this._blocks.push(block); + return block; + } + + build(document, parent) { + this.element = document.createElementNS('http://www.w3.org/2000/svg', 'g'); + if (this.id) { + this.element.setAttribute('id', this.id); + } + this.element.setAttribute('class', this.class ? 'node ' + this.class : 'node'); + this.element.style.opacity = 0; + parent.appendChild(this.element); + + // ===> 配置每个节点的框边界 + this.border = document.createElementNS('http://www.w3.org/2000/svg', 'path'); + this.border.setAttribute('class', [ 'node', 'border' ].join(' ')); + this.element.appendChild(this.border); + // <=== 配置每个节点的框边界 + + for (let i = 0; i < this._blocks.length; i++) { + const block = this._blocks[i]; + block.first = i === 0; + block.last = i === this._blocks.length - 1; + block.build(document, this.element); + } + + this.layout(); // 这一行注释后,边和节点全部乱成一团,报错一堆 + } + + layout() { + const width = Math.max(...this._blocks.map((block) => block.width)); + let height = 0; + for (let i = 0; i < this._blocks.length; i++) { + const block = this._blocks[i]; + block.y = height; + block.update(this.element, height, width, i == 0, i == this._blocks.length - 1); + height = height + block.height; + } + + // 这一行画每个节点的框边界 + this.border.setAttribute('d', grapher.Node.roundedRect(0, 0, width, height, true, true, true, true)); + + const nodeBox = this.element.getBBox(); + this.width = nodeBox.width; + this.height = nodeBox.height; + } + + update() { + // console.log(this) + // 没有这一行,所有节点都左对齐到左上角 + // 这一行对所有节点框进行平移 + this.element.setAttribute('transform', 'translate(' + (this.x - (this.width / 2)) + ',' + (this.y - (this.height / 2)) + ')'); + + // 设定不透明度 + this.element.style.opacity = 1; + } + + + static roundedRect(x, y, width, height, r1, r2, r3, r4) { + const radius = 5; + r1 = r1 ? radius : 0; + r2 = r2 ? radius : 0; + r3 = r3 ? radius : 0; + r4 = r4 ? radius : 0; + return "M" + (x + r1) + "," + y + + "h" + (width - r1 - r2) + + "a" + r2 + "," + r2 + " 0 0 1 " + r2 + "," + r2 + + "v" + (height - r2 - r3) + + "a" + r3 + "," + r3 + " 0 0 1 " + -r3 + "," + r3 + + "h" + (r3 + r4 - width) + + "a" + r4 + "," + r4 + " 0 0 1 " + -r4 + "," + -r4 + + 'v' + (-height + r4 + r1) + + "a" + r1 + "," + r1 + " 0 0 1 " + r1 + "," + -r1 + + "z"; + } +}; + +grapher.Node.Header = class { + + constructor() { + this._entries = []; + } + + add(id, classList, content, tooltip, handler) { + const entry = new grapher.Node.Header.Entry(id, classList, content, tooltip, handler); + this._entries.push(entry); + return entry; + } + + build(document, parent) { + this._document = document; + this.width = 0; + this.height = 0; + let x = 0; + const y = 0; + for (const entry of this._entries) { + entry.x = x; + entry.y = y; + entry.build(document, parent); + x += entry.width; + this.height = Math.max(entry.height, this.height); + this.width = Math.max(x, this.width); + } + if (!this.first) { + this.line = document.createElementNS('http://www.w3.org/2000/svg', 'line'); + parent.appendChild(this.line); + } + } + + update(parent, top, width) { + const document = this._document; + const dx = width - this.width; + for (let i = 0; i < this._entries.length; i++) { + const entry = this._entries[i]; + if (i == 0) { + entry.width = entry.width + dx; + } + else { + entry.x = entry.x + dx; + entry.tx = entry.tx + dx; + } + entry.y = entry.y + top; + } + for (let i = 0; i < this._entries.length; i++) { + const entry = this._entries[i]; + entry.element.setAttribute('transform', 'translate(' + entry.x + ',' + entry.y + ')'); + const r1 = i == 0 && this.first; + const r2 = i == this._entries.length - 1 && this.first; + const r3 = i == this._entries.length - 1 && this.last; + const r4 = i == 0 && this.last; + entry.path.setAttribute('d', grapher.Node.roundedRect(0, 0, entry.width, entry.height, r1, r2, r3, r4)); + entry.text.setAttribute('x', 6); + entry.text.setAttribute('y', entry.ty); + } + for (let i = 0; i < this._entries.length; i++) { + const entry = this._entries[i]; + if (i != 0) { + const line = document.createElementNS('http://www.w3.org/2000/svg', 'line'); + line.setAttribute('class', 'node'); + line.setAttribute('x1', entry.x); + line.setAttribute('x2', entry.x); + line.setAttribute('y1', top); + line.setAttribute('y2', top + this.height); + parent.appendChild(line); + } + } + if (this.line) { + this.line.setAttribute('class', 'node'); + this.line.setAttribute('x1', 0); + this.line.setAttribute('x2', width); + this.line.setAttribute('y1', top); + this.line.setAttribute('y2', top); + } + } +}; + +grapher.Node.Header.Entry = class { + + constructor(id, classList, content, tooltip, handler) { + this.id = id; + this.classList = classList; + this.content = content; + this.tooltip = tooltip; + this.handler = handler; + this.events = {}; + } + + on(event, callback) { + this.events[event] = this.events[event] || []; + this.events[event].push(callback); + } + + raise(event, data) { + if (this.events && this.events[event]) { + for (const callback of this.events[event]) { + callback(this, data); + } + } + } + + build(document, parent) { + const yPadding = 4; + const xPadding = 7; + this.element = document.createElementNS('http://www.w3.org/2000/svg', 'g'); + parent.appendChild(this.element); + this.path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); + this.text = document.createElementNS('http://www.w3.org/2000/svg', 'text'); + this.element.appendChild(this.path); + this.element.appendChild(this.text); + const classList = [ 'node-item' ]; + if (this.classList) { + classList.push(...this.classList); + } + this.element.setAttribute('class', classList.join(' ')); + if (this.id) { + this.element.setAttribute('id', this.id); + } + if (this.events.click) { + this.element.addEventListener('click', () => this.raise('click')); + } + if (this.tooltip) { + const titleElement = document.createElementNS('http://www.w3.org/2000/svg', 'title'); + titleElement.textContent = this.tooltip; + this.element.appendChild(titleElement); + } + if (this.content) { + this.text.textContent = this.content; + } + const boundingBox = this.text.getBBox(); + this.width = boundingBox.width + xPadding + xPadding; + this.height = boundingBox.height + yPadding + yPadding; + this.tx = xPadding; + this.ty = yPadding - boundingBox.y; + } +}; + +grapher.Node.List = class { + + constructor() { + this._items = []; + this.events = {}; + } + + add(id, name, value, tooltip, separator) { + const item = new grapher.Node.List.Item(id, name, value, tooltip, separator); + this._items.push(item); + return item; + } + + on(event, callback) { + this.events[event] = this.events[event] || []; + this.events[event].push(callback); + } + + raise(event, data) { + if (this.events && this.events[event]) { + for (const callback of this.events[event]) { + callback(this, data); + } + } + } + + build(document, parent) { + this._document = document; + this.width = 0; + this.height = 0; + const x = 0; + const y = 0; + this.element = document.createElementNS('http://www.w3.org/2000/svg', 'g'); + this.element.setAttribute('class', 'node-attribute'); + if (this.events.click) { + this.element.addEventListener('click', () => this.raise('click')); + } + this.element.setAttribute('transform', 'translate(' + x + ',' + y + ')'); + this.background = document.createElementNS('http://www.w3.org/2000/svg', 'path'); + this.element.appendChild(this.background); + parent.appendChild(this.element); + this.height += 3; + for (const item of this._items) { + const yPadding = 1; + const xPadding = 6; + const text = document.createElementNS('http://www.w3.org/2000/svg', 'text'); + if (item.id) { + text.setAttribute('id', item.id); + } + text.setAttribute('xml:space', 'preserve'); + this.element.appendChild(text); + if (item.tooltip) { + const title = document.createElementNS('http://www.w3.org/2000/svg', 'title'); + title.textContent = item.tooltip; + text.appendChild(title); + } + const name = document.createElementNS('http://www.w3.org/2000/svg', 'tspan'); + name.textContent = item.name; + if (item.separator.trim() != '=') { + name.style.fontWeight = 'bold'; + } + text.appendChild(name); + const textValueElement = document.createElementNS('http://www.w3.org/2000/svg', 'tspan'); + textValueElement.textContent = item.separator + item.value; + text.appendChild(textValueElement); + const size = text.getBBox(); + const width = xPadding + size.width + xPadding; + this.width = Math.max(width, this.width); + text.setAttribute('x', x + xPadding); + text.setAttribute('y', this.height + yPadding - size.y); + this.height += yPadding + size.height + yPadding; + } + this.height += 3; + this.width = Math.max(75, this.width); + if (!this.first) { + this.line = document.createElementNS('http://www.w3.org/2000/svg', 'line'); + this.line.setAttribute('class', 'node'); + this.element.appendChild(this.line); + } + } + + update(parent, top, width) { + this.element.setAttribute('transform', 'translate(0,' + this.y + ')'); + this.background.setAttribute('d', grapher.Node.roundedRect(0, 0, width, this.height, this.first, this.first, this.last, this.last)); + if (this.line) { + this.line.setAttribute('x1', 0); + this.line.setAttribute('x2', width); + this.line.setAttribute('y1', 0); + this.line.setAttribute('y2', 0); + } + } +}; + +grapher.Node.List.Item = class { + + constructor(id, name, value, tooltip, separator) { + this.id = id; + this.name = name; + this.value = value; + this.tooltip = tooltip; + this.separator = separator; + } +}; + +grapher.Node.Canvas = class { + + constructor() { + this.width = 0; + this.height = 0; + } + + build(/* document, parent */) { + } + + update(/* parent, top, width , first, last */) { + } +}; + +grapher.Edge = class { + + constructor(from, to) { + this.from = from; + this.to = to; + } + + get arrowhead() { + return 'vee'; + } + + build(document, edgePathGroupElement, edgeLabelGroupElement) { + const createElement = (name) => { + return document.createElementNS('http://www.w3.org/2000/svg', name); + }; + + // 生成path对应的element + this.element = createElement('path'); + if (this.id) { + this.element.setAttribute('id', this.id); + } + this.element.setAttribute('class', this.class ? 'edge-path ' + this.class : 'edge-path'); + edgePathGroupElement.appendChild(this.element); + + // 生成label对应的element + if (this.label) { + const tspan = createElement('tspan'); + tspan.setAttribute('xml:space', 'preserve'); + tspan.setAttribute('dy', '1em'); + tspan.setAttribute('x', '1'); + tspan.appendChild(document.createTextNode(this.label)); + this.labelElement = createElement('text'); + this.labelElement.appendChild(tspan); + this.labelElement.style.opacity = 0; + this.labelElement.setAttribute('class', 'edge-label'); + if (this.id) { + this.labelElement.setAttribute('id', 'edge-label-' + this.id); + } + edgeLabelGroupElement.appendChild(this.labelElement); + const edgeBox = this.labelElement.getBBox(); + this.width = edgeBox.width; + this.height = edgeBox.height; + } + } + + update() { + const edgePath = grapher.Edge._computeCurvePath(this, this.from, this.to); + this.element.setAttribute('d', edgePath); // ===> 把边画出来 + + // ===> 让label显示出来 + if (this.labelElement) { + this.labelElement.setAttribute('transform', 'translate(' + (this.x - (this.width / 2)) + ',' + (this.y - (this.height / 2)) + ')'); + this.labelElement.style.opacity = 1; + } + // <=== 让label显示出来 + } + + static _computeCurvePath(edge, tail, head) { + const points = edge.points.slice(1, edge.points.length - 1); + points.unshift(grapher.Edge._intersectRect(tail, points[0])); + points.push(grapher.Edge._intersectRect(head, points[points.length - 1])); + const curve = new grapher.Edge.Curve(points); + return curve.path.data; + } + + static _intersectRect(node, point) { + const x = node.x; + const y = node.y; + const dx = point.x - x; + const dy = point.y - y; + let w = node.width / 2; + let h = node.height / 2; + let sx; + let sy; + if (Math.abs(dy) * w > Math.abs(dx) * h) { + if (dy < 0) { + h = -h; + } + sx = dy === 0 ? 0 : h * dx / dy; + sy = h; + } + else { + if (dx < 0) { + w = -w; + } + sx = w; + sy = dx === 0 ? 0 : w * dy / dx; + } + return { + x: x + sx, + y: y + sy + }; + } +}; + +grapher.Edge.Curve = class { + + constructor(points) { + this._path = new grapher.Edge.Path(); + this._x0 = NaN; + this._x1 = NaN; + this._y0 = NaN; + this._y1 = NaN; + this._state = 0; + for (let i = 0; i < points.length; i++) { + const point = points[i]; + this.point(point.x, point.y); + if (i === points.length - 1) { + switch (this._state) { + case 3: + this.curve(this._x1, this._y1); + this._path.lineTo(this._x1, this._y1); + break; + case 2: + this._path.lineTo(this._x1, this._y1); + break; + } + if (this._line || (this._line !== 0 && this._point === 1)) { + this._path.closePath(); + } + this._line = 1 - this._line; + } + } + } + + get path() { + return this._path; + } + + point(x, y) { + x = +x; + y = +y; + switch (this._state) { + case 0: + this._state = 1; + if (this._line) { + this._path.lineTo(x, y); + } + else { + this._path.moveTo(x, y); + } + break; + case 1: + this._state = 2; + break; + case 2: + this._state = 3; + this._path.lineTo((5 * this._x0 + this._x1) / 6, (5 * this._y0 + this._y1) / 6); + this.curve(x, y); + break; + default: + this.curve(x, y); + break; + } + this._x0 = this._x1; + this._x1 = x; + this._y0 = this._y1; + this._y1 = y; + } + + curve(x, y) { + this._path.bezierCurveTo( + (2 * this._x0 + this._x1) / 3, + (2 * this._y0 + this._y1) / 3, + (this._x0 + 2 * this._x1) / 3, + (this._y0 + 2 * this._y1) / 3, + (this._x0 + 4 * this._x1 + x) / 6, + (this._y0 + 4 * this._y1 + y) / 6 + ); + } +}; + +grapher.Edge.Path = class { + + constructor() { + this._x0 = null; + this._y0 = null; + this._x1 = null; + this._y1 = null; + this._data = ''; + } + + moveTo(x, y) { + this._data += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y); + } + + lineTo(x, y) { + this._data += "L" + (this._x1 = +x) + "," + (this._y1 = +y); + } + + bezierCurveTo(x1, y1, x2, y2, x, y) { + this._data += "C" + (+x1) + "," + (+y1) + "," + (+x2) + "," + (+y2) + "," + (this._x1 = +x) + "," + (this._y1 = +y); + } + + closePath() { + if (this._x1 !== null) { + this._x1 = this._x0; + this._y1 = this._y0; + this._data += "Z"; + } + } + + get data() { + return this._data; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.Graph = grapher.Graph; + module.exports.Node = grapher.Node; + module.exports.Edge = grapher.Edge; +} \ No newline at end of file diff --git a/view-sidebar.css b/view-sidebar.css new file mode 100644 index 0000000..340a8b5 --- /dev/null +++ b/view-sidebar.css @@ -0,0 +1,89 @@ + +.sidebar { font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif; font-size: 12px; height: 100%; width: 0; position: fixed; transition: 0.2s; z-index: 1; top: 0; right: 0; background-color: #ececec; color: #242424; overflow: hidden; border-left: 1px solid #ccc; } +.sidebar-title { font-weight: bold; font-size: 12px; letter-spacing: 0.5px; height: 20px; margin: 0; padding: 20px; user-select: none; -webkit-user-select: none; -moz-user-select: none; } +.sidebar-closebutton { padding: 8px 8px 8px 32px; text-decoration: none; font-size: 25px; color: #777777; opacity: 1.0; display: block; transition: 0.2s; position: absolute; top: 0; right: 15px; margin-left: 50px; user-select: none; -webkit-user-select: none; -moz-user-select: none; } +.sidebar-closebutton:hover { color: #242424; } +.sidebar-content { padding-left: 20px; padding-right: 20px; margin-bottom: 20px; overflow-y: auto; height: calc(100vh - 60px); } + +.sidebar-view-title { font-weight: bold; font-size: 11px; line-height: 1.25; border-bottom: 1px solid #ececec; padding-bottom: 0.3em; margin-top: 0; margin-bottom: 16px; color: #333; user-select: none; -webkit-user-select: none; -moz-user-select: none; cursor: default; } +.sidebar-view-title-button { display: inline-block; color: #888; text-align: center; vertical-align: middle; font-weight: bold; width: 12px; height: 12px; font-size: 10px; line-height: 12px; border-radius: 50%; transform: translateY(-1px); padding: 1px; background: transparent; border: 1px solid #aaa; text-decoration: none; cursor: pointer; user-select: none; -webkit-user-select: none; -moz-user-select: none; } +.sidebar-view-title-button:hover { color: #333; border: 1px solid #333; } +.sidebar-view-header { font-weight: bold; font-size: 11px; text-transform: uppercase; line-height: 1.25; margin-top: 16px; margin-bottom: 16px; border-bottom: 1px solid #ececec; display: block; user-select: none; -webkit-user-select: none; -moz-user-select: none; cursor: default; } +.sidebar-view-item { margin-bottom: 0px; display: block; } +.sidebar-view-item-name { float: left; font-size: 11px; min-width: 95px; max-width: 95px; padding-right: 5px; padding-top: 7px; display: block; } +.sidebar-view-item-name input { color: #777; font-family: inherit; font-size: inherit; color: inherit; background-color: inherit; width: 100%; text-align: right; margin: 0; padding: 0; border: 0; outline: none; text-overflow: ellipsis; } +.sidebar-view-item-value-list { margin: 0; margin-left: 105px; overflow: hidden; display: block; padding: 0; } +.sidebar-view-item-value { font-size: 11px; background-color: #fcfcfc; border-radius: 2px; border: 1px solid #fcfcfc; margin-top: 3px; margin-bottom: 3px; overflow: auto; } +.sidebar-view-item-value-dark { background-color: #f8f8f8; border: 1px solid #f8f8f8; } +.sidebar-view-item-value b { font-weight: bold; } +.sidebar-view-item-value code { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace; overflow: auto; white-space: pre-wrap; word-wrap: break-word; } +.sidebar-view-item-value pre { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace; margin: 0; overflow: auto; white-space: pre; word-wrap: normal; display: block; } +.sidebar-view-item-value-line { padding: 4px 6px 4px 6px; } +.sidebar-view-item-value-line-link { padding: 4px 6px 4px 6px; cursor: default; } +.sidebar-view-item-value-line-link:hover { text-decoration: underline; } +.sidebar-view-item-value-line-border { padding: 4px 6px 4px 6px; border-top: 1px solid rgba(27, 31, 35, 0.05); } +.sidebar-view-item-value-line-content { white-space: pre; word-wrap: normal; overflow: auto; display: block; } +.sidebar-view-item-value-expander { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace; float: right; color: #aaa; cursor: pointer; user-select: none; -webkit-user-select: none; -moz-user-select: none; padding: 4px 6px 4px 6px; } +.sidebar-view-item-value-expander:hover { color: #000; } +.sidebar-view-item-select { + font-family: inherit; font-size: 12px; + background-color: #fcfcfc; border: #fcfcfc; color: #333; + border-radius: 2px; width: 100%; height: 23px; padding: 3px 12px 3px 7px; + margin-top: 3px; margin-bottom: 3px; box-sizing: border-box; outline: none; + -moz-box-sizing: border-box; -webkit-appearance: none; -moz-appearance: none; + background-image: linear-gradient(45deg, transparent 50%, #333 50%), linear-gradient(135deg, #333 50%, transparent 50%); + background-position: calc(100% - 12px) calc(10px), calc(100% - 7px) calc(10px); + background-size: 5px 5px, 5px 5px; + background-repeat: no-repeat; +} +.sidebar-view-separator { margin-bottom: 20px; } + +.sidebar-view-find input[type=text] { font-family: inherit; font-size: 13px; padding: 4px 6px 4px 6px; background: #fff; border-radius: 4px; border: 1px solid #ccc; outline: 0; } +.sidebar-view-find ol { list-style-type: none; overflow-y: auto; margin: 8px 0 20px 0; padding: 0; } +.sidebar-view-find li { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; margin: 0; padding: 5px 8px 5px 8px; outline: 0; white-space: nowrap; user-select: none; -webkit-user-select: none; -moz-user-select: none; } +.sidebar-view-find li:not(:first-child) { border-top: 1px solid #f0f0f0; } +.sidebar-view-find li:hover { background: #eee; } + +.sidebar-view-documentation { font-size: 13px; line-height: 1.5; margin: 0; } +.sidebar-view-documentation h1 { font-weight: bold; font-size: 13px; line-height: 1.25; border-bottom: 1px solid #e8e8e8; padding-bottom: 0.3em; margin-top: 0; margin-bottom: 16px; } +.sidebar-view-documentation h2 { font-weight: bold; font-size: 11px; line-height: 1.25; margin-top: 20px; margin-bottom: 16px; text-transform: uppercase; border: 0; } +.sidebar-view-documentation h3 { font-weight: bold; font-size: 11px; line-height: 1.25; } +.sidebar-view-documentation p { margin-top: 4px; margin-bottom: 4px; margin-left: 0px; } +.sidebar-view-documentation a { color: #237; } +.sidebar-view-documentation code { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; background-color: rgba(27, 31, 35, 0.05); padding: 0.2em 0.4em; margin: 0; border-radius: 3px; } +.sidebar-view-documentation pre { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; padding: 16px; overflow: auto; line-height: 1.45; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; } +.sidebar-view-documentation pre code { font-size: 13px; padding: 16px; line-height: 1.45; background-color: transparent; padding: 0; border-radius: 0; } +.sidebar-view-documentation tt { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-weight: bold; font-size: 90%; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; padding: 0.2em 0.4em; margin: 0; } +.sidebar-view-documentation dl dt { font-size: 13px; font-weight: bold; padding: 0; margin-top: 16px; margin-left: 0px; } +.sidebar-view-documentation dd { padding: 0 16px; margin-left: 0; margin-bottom: 16px; } +.sidebar-view-documentation ul { margin-top: 6px; margin-bottom: 6px; padding-left: 20px; } +.sidebar-view-documentation blockquote { margin-left: 15px; margin-right: 15px; } + +@media (prefers-color-scheme: dark) { + .sidebar html { color: #dfdfdf; } + .sidebar { background-color: #2d2d2d; color: #dfdfdf; border-left: 1px solid #000; } + .sidebar-closebutton { padding: 8px 8px 8px 32px; text-decoration: none; font-size: 25px; color: #777777; opacity: 1.0; display: block; transition: 0.2s; position: absolute; top: 0; right: 15px; margin-left: 50px; user-select: none; -webkit-user-select: none; -moz-user-select: none; } + .sidebar-closebutton:hover { color: #ffffff; } + .sidebar-view-item-value { background-color: #383838; border-color: #383838; } + .sidebar-view-item-value-dark { background-color: #3e3e3e; border-color: #3e3e3e; } + .sidebar-view-item-value-line-border { border-color: rgba(0, 0, 0, 0.09); } + .sidebar-view-item-select { background-color: #383838; border: #383838; color: #dfdfdf; background-image: linear-gradient(45deg, transparent 50%, #aaa 50%), linear-gradient(135deg, #aaa 50%, transparent 50%); } + + .sidebar-view-title { border-bottom-color: #2d2d2d; color: #dfdfdf; } + .sidebar-view-title-button { color: #888888; border-color: #888888; } + .sidebar-view-title-button:hover { color: #dfdfdf; border-color: #dfdfdf; } + .sidebar-view-header { border-bottom-color: #2d2d2d; color: #dfdfdf; } + + .sidebar-view-documentation h1 { border-bottom: 1px solid #424242; color: #dfdfdf; } + .sidebar-view-documentation h2 { color: #dfdfdf; } + .sidebar-view-documentation p { color: #aaaaaa; } + .sidebar-view-documentation a { color: #6688aa; } + .sidebar-view-documentation tt { background-color:#1e1e1e; } + .sidebar-view-documentation code { background-color: #1e1e1e; } + .sidebar-view-documentation pre { background-color: #1e1e1e; } + + .sidebar-view-find input[type=text] { background: #383838; color: #dfdfdf; border-color: #424242; } + .sidebar-view-find li:not(:first-child) { border-top: 1px solid #2a2a2a; } + .sidebar-view-find li:hover { background: #383838; } + +} diff --git a/view-sidebar.js b/view-sidebar.js new file mode 100644 index 0000000..26cd81f --- /dev/null +++ b/view-sidebar.js @@ -0,0 +1,2277 @@ + +var sidebar = sidebar || {}; +var base = base || require('./base'); + +sidebar.Sidebar = class { + + constructor(host, id) { + this._host = host; + this._id = id ? ('-' + id) : ''; + this._stack = []; + this._closeSidebarHandler = () => { + this._pop(); + }; + this._closeSidebarKeyDownHandler = (e) => { + if (e.keyCode == 27) { + e.preventDefault(); + this._pop(); + } + }; + } + + _getElementById(id) { + return this._host.document.getElementById(id + this._id); + } + + open(content, title) { + this.close(); + this.push(content, title); + } + + close() { + this._deactivate(); + this._stack = []; + this._hide(); + } + + push(content, title) { + const item = { title: title, content: content }; + this._stack.push(item); + this._activate(item); + } + + _pop() { + this._deactivate(); + if (this._stack.length > 0) { + this._stack.pop(); + } + if (this._stack.length > 0) { + this._activate(this._stack[this._stack.length - 1]); + } + else { + this._hide(); + } + } + + _hide() { + const sidebar = this._getElementById('sidebar'); + if (sidebar) { + sidebar.style.width = '0px'; + } + const container = this._getElementById('graph'); + if (container) { + container.style.width = '100%'; + container.focus(); + } + } + + _deactivate() { + const sidebar = this._getElementById('sidebar'); + if (sidebar) { + const closeButton = this._getElementById('sidebar-closebutton'); + if (closeButton) { + closeButton.removeEventListener('click', this._closeSidebarHandler); + closeButton.style.color = '#f8f8f8'; + } + + this._host.document.removeEventListener('keydown', this._closeSidebarKeyDownHandler); + } + } + + _activate(item) { + const sidebar = this._getElementById('sidebar'); + if (sidebar) { + sidebar.innerHTML = ''; + + const title = this._host.document.createElement('h1'); + title.classList.add('sidebar-title'); + title.innerHTML = item.title ? item.title.toUpperCase() : ''; + sidebar.appendChild(title); + + const closeButton = this._host.document.createElement('a'); + closeButton.classList.add('sidebar-closebutton'); + closeButton.setAttribute('id', 'sidebar-closebutton'); + closeButton.setAttribute('href', 'javascript:void(0)'); + closeButton.innerHTML = '×'; + closeButton.addEventListener('click', this._closeSidebarHandler); + sidebar.appendChild(closeButton); + + const content = this._host.document.createElement('div'); + content.classList.add('sidebar-content'); + content.setAttribute('id', 'sidebar-content'); + sidebar.appendChild(content); + + if (typeof item.content == 'string') { + content.innerHTML = item.content; + } + else if (item.content instanceof Array) { + for (const element of item.content) { + content.appendChild(element); + } + } + else { + content.appendChild(item.content); + } + sidebar.style.width = 'min(calc(100% * 0.6), 500px)'; + this._host.document.addEventListener('keydown', this._closeSidebarKeyDownHandler); + } + const container = this._getElementById('graph'); + if (container) { + container.style.width = 'max(40vw, calc(100vw - 500px))'; + } + } +}; + +sidebar.NodeSidebar = class { + + constructor(host, node) { + this._host = host; + this._node = node; + this._elements = []; + this._attributes = []; + this._inputs = []; + this._outputs = []; + + if (node.type) { + let showDocumentation = null; + const type = node.type; + if (type && (type.description || type.inputs || type.outputs || type.attributes)) { + showDocumentation = {}; + showDocumentation.text = type.nodes ? '\u0192': '?'; + showDocumentation.callback = () => { + this._raise('show-documentation', null); + }; + } + this._addProperty('type', new sidebar.ValueTextView(this._host, node.type.name, showDocumentation)); + if (node.type.module) { + this._addProperty('module', new sidebar.ValueTextView(this._host, node.type.module)); + } + } + + if (node.name) { + this._addProperty('name', new sidebar.ValueTextView(this._host, node.name)); + } + + if (node.location) { + this._addProperty('location', new sidebar.ValueTextView(this._host, node.location)); + } + + if (node.description) { + this._addProperty('description', new sidebar.ValueTextView(this._host, node.description)); + } + + if (node.device) { + this._addProperty('device', new sidebar.ValueTextView(this._host, node.device)); + } + + const attributes = node.attributes; + // console.log(attributes) + if (attributes && attributes.length > 0) { + const sortedAttributes = node.attributes.slice(); + // console.log(sortedAttributes) + sortedAttributes.sort((a, b) => { + const au = a.name.toUpperCase(); + const bu = b.name.toUpperCase(); + return (au < bu) ? -1 : (au > bu) ? 1 : 0; + }); + this._addHeader('Attributes'); + for (const attribute of sortedAttributes) { + this._addAttribute(attribute.name, attribute); + } + } + + const inputs = node.inputs; + if (inputs && inputs.length > 0) { + this._addHeader('Inputs'); + for (const input of inputs) { + this._addInput(input.name, input); + } + } + + const outputs = node.outputs; + if (outputs && outputs.length > 0) { + this._addHeader('Outputs'); + for (const output of outputs) { + this._addOutput(output.name, output); + } + } + + const separator = this._host.document.createElement('div'); + separator.className = 'sidebar-view-separator'; + this._elements.push(separator); + + // My code here + // console.log(node) + // console.log(this._node) + this._addButton('Delete'); + this._addButton('Recover'); + this._addButton('Download'); + + // console.log(this._host._view._graph._nodes) + // console.log(node) + + // console.log(this._host._view._graph._modelNodeName2ViewNode) + // console.log(this._host._view._graph._modelNodeName2ViewNode.get(node.name)) + // this._host._view._graph._modelNodeName2ViewNode.get(node.name).element.style.opacity = 0.5; + + + // this._host.view._graph._nodes[node.name] + } + + render() { + return this._elements; + } + + _addHeader(title) { + const headerElement = this._host.document.createElement('div'); + headerElement.className = 'sidebar-view-header'; + headerElement.innerText = title; + this._elements.push(headerElement); + } + + _addProperty(name, value) { + const item = new sidebar.NameValueView(this._host, name, value); + this._elements.push(item.render()); + } + + _addAttribute(name, attribute) { + const item = new NodeAttributeView(this._host, attribute); + item.on('show-graph', (sender, graph) => { + this._raise('show-graph', graph); + }); + const view = new sidebar.NameValueView(this._host, name, item); + this._attributes.push(view); + this._elements.push(view.render()); + } + + _addInput(name, input) { + if (input.arguments.length > 0) { + const view = new sidebar.ParameterView(this._host, input); + view.on('export-tensor', (sender, tensor) => { + this._raise('export-tensor', tensor); + }); + view.on('error', (sender, tensor) => { + this._raise('error', tensor); + }); + const item = new sidebar.NameValueView(this._host, name, view); + this._inputs.push(item); + this._elements.push(item.render()); + } + } + + _addOutput(name, output) { + if (output.arguments.length > 0) { + const item = new sidebar.NameValueView(this._host, name, new sidebar.ParameterView(this._host, output)); + this._outputs.push(item); + this._elements.push(item.render()); + } + } + + // My code + _addButton(title) { + const buttonElement = this._host.document.createElement('button'); + buttonElement.className = 'sidebar-view-button'; + buttonElement.innerText = title; + this._elements.push(buttonElement); + + // console.log(title) + if (title === 'Delete') { + buttonElement.addEventListener('click', () => { + console.log(this._host._view._graph._modelNodeName2State.get(this._node.name)) + this._host._view._graph._modelNodeName2ViewNode.get(this._node.name).element.style.opacity = 0.3; + this._host._view._graph._modelNodeName2State.set(this._node.name,'Deleted'); + console.log(this._host._view._graph._modelNodeName2State.get(this._node.name)) + }); + } + if (title === 'Recover') { + // console.log('pressed') + buttonElement.addEventListener('click', () => { + console.log(this._host._view._graph._modelNodeName2State.get(this._node.name)) + this._host._view._graph._modelNodeName2ViewNode.get(this._node.name).element.style.opacity = 1; + this._host._view._graph._modelNodeName2State.set(this._node.name,'Existed'); + console.log(this._host._view._graph._modelNodeName2State.get(this._node.name)) + }); + } + + + + } + + toggleInput(name) { + for (const input of this._inputs) { + if (name == input.name) { + input.toggle(); + } + } + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } + + static formatAttributeValue(value, type, quote) { + if (typeof value === 'function') { + return value(); + } + if (value && (value instanceof base.Int64 || value instanceof base.Uint64)) { + return value.toString(); + } + if (Number.isNaN(value)) { + return 'NaN'; + } + switch (type) { + case 'shape': + return value ? value.toString() : '(null)'; + case 'shape[]': + if (value && !Array.isArray(value)) { + throw new Error("Invalid shape '" + JSON.stringify(value) + "'."); + } + return value ? value.map((item) => item.toString()).join(', ') : '(null)'; + case 'graph': + return value ? value.name : '(null)'; + case 'graph[]': + return value ? value.map((graph) => graph.name).join(', ') : '(null)'; + case 'tensor': + if (value && value.type && value.type.shape && value.type.shape.dimensions && value.type.shape.dimensions.length == 0) { + return value.toString(); + } + return '[...]'; + case 'function': + return value.type.name; + case 'function[]': + return value ? value.map((item) => item.type.name).join(', ') : '(null)'; + } + if (typeof value === 'string' && (!type || type != 'string')) { + return quote ? '"' + value + '"' : value; + } + if (Array.isArray(value)) { + if (value.length == 0) { + return quote ? '[]' : ''; + } + let ellipsis = false; + if (value.length > 1000) { + value = value.slice(0, 1000); + ellipsis = true; + } + const itemType = (type && type.endsWith('[]')) ? type.substring(0, type.length - 2) : null; + const array = value.map((item) => { + if (item && (item instanceof base.Int64 || item instanceof base.Uint64)) { + return item.toString(); + } + if (Number.isNaN(item)) { + return 'NaN'; + } + const quote = !itemType || itemType === 'string'; + return sidebar.NodeSidebar.formatAttributeValue(item, itemType, quote); + }); + if (ellipsis) { + array.push('\u2026'); + } + return quote ? [ '[', array.join(', '), ']' ].join(' ') : array.join(', '); + } + if (value === null) { + return quote ? 'null' : ''; + } + if (value === undefined) { + return 'undefined'; + } + if (value !== Object(value)) { + return value.toString(); + } + const list = []; + const keys = Object.keys(value).filter((key) => !key.startsWith('__') && !key.endsWith('__')); + if (keys.length == 1) { + list.push(sidebar.NodeSidebar.formatAttributeValue(value[Object.keys(value)[0]], null, true)); + } + else { + for (const key of keys) { + list.push(key + ': ' + sidebar.NodeSidebar.formatAttributeValue(value[key], null, true)); + } + } + let objectType = value.__type__; + if (!objectType && value.constructor.name && value.constructor.name !== 'Object') { + objectType = value.constructor.name; + } + if (objectType) { + return objectType + (list.length == 0 ? '()' : [ '(', list.join(', '), ')' ].join('')); + } + switch (list.length) { + case 0: + return quote ? '()' : ''; + case 1: + return list[0]; + default: + return quote ? [ '(', list.join(', '), ')' ].join(' ') : list.join(', '); + } + } +}; + +sidebar.NameValueView = class { + + constructor(host, name, value) { + this._host = host; + this._name = name; + this._value = value; + + const nameElement = this._host.document.createElement('div'); + nameElement.className = 'sidebar-view-item-name'; + + const nameInputElement = this._host.document.createElement('input'); + nameInputElement.setAttribute('type', 'text'); + nameInputElement.setAttribute('value', name); + nameInputElement.setAttribute('title', name); + nameInputElement.setAttribute('readonly', 'true'); + nameElement.appendChild(nameInputElement); + + const valueElement = this._host.document.createElement('div'); + valueElement.className = 'sidebar-view-item-value-list'; + + for (const element of value.render()) { + valueElement.appendChild(element); + } + + this._element = this._host.document.createElement('div'); + this._element.className = 'sidebar-view-item'; + this._element.appendChild(nameElement); + this._element.appendChild(valueElement); + } + + get name() { + return this._name; + } + + render() { + return this._element; + } + + toggle() { + this._value.toggle(); + } +}; + +sidebar.SelectView = class { + + constructor(host, values, selected) { + this._host = host; + this._elements = []; + this._values = values; + + const selectElement = this._host.document.createElement('select'); + selectElement.setAttribute('class', 'sidebar-view-item-select'); + selectElement.addEventListener('change', (e) => { + this._raise('change', this._values[e.target.selectedIndex]); + }); + this._elements.push(selectElement); + + for (const value of values) { + const optionElement = this._host.document.createElement('option'); + optionElement.innerText = value.name || ''; + if (value == selected) { + optionElement.setAttribute('selected', 'selected'); + } + selectElement.appendChild(optionElement); + } + } + + render() { + return this._elements; + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } +}; + +sidebar.ValueTextView = class { + + constructor(host, value, action) { + this._host = host; + this._elements = []; + const element = this._host.document.createElement('div'); + element.className = 'sidebar-view-item-value'; + this._elements.push(element); + + if (action) { + this._action = this._host.document.createElement('div'); + this._action.className = 'sidebar-view-item-value-expander'; + this._action.innerHTML = action.text; + this._action.addEventListener('click', () => { + action.callback(); + }); + element.appendChild(this._action); + } + + const list = Array.isArray(value) ? value : [ value ]; + let className = 'sidebar-view-item-value-line'; + for (const item of list) { + const line = this._host.document.createElement('div'); + line.className = className; + line.innerText = item; + element.appendChild(line); + className = 'sidebar-view-item-value-line-border'; + } + } + + render() { + return this._elements; + } + + toggle() { + } +}; + +class NodeAttributeView { + + constructor(host, attribute) { + this._host = host; + this._attribute = attribute; + this._element = this._host.document.createElement('div'); + this._element.className = 'sidebar-view-item-value'; + + const type = this._attribute.type; + if (type) { + this._expander = this._host.document.createElement('div'); + this._expander.className = 'sidebar-view-item-value-expander'; + this._expander.innerText = '+'; + this._expander.addEventListener('click', () => { + this.toggle(); + }); + this._element.appendChild(this._expander); + } + // console.log(this._attribute) + // console.log(this._attribute.value) + const value = this._attribute.value; + // console.log(value) + // console.log(type) + switch (type) { + case 'graph': { + const line = this._host.document.createElement('div'); + line.className = 'sidebar-view-item-value-line-link'; + line.innerHTML = value.name; + line.addEventListener('click', () => { + this._raise('show-graph', value); + }); + this._element.appendChild(line); + break; + } + case 'function': { + const line = this._host.document.createElement('div'); + line.className = 'sidebar-view-item-value-line-link'; + line.innerHTML = type === value.type.name; + line.addEventListener('click', () => { + this._raise('show-graph', value.type); + }); + this._element.appendChild(line); + break; + } + default: { + let content = sidebar.NodeSidebar.formatAttributeValue(value, type); + // console.log(content) + if (content && content.length > 1000) { + content = content.substring(0, 1000) + '\u2026'; + } + if (content && typeof content === 'string') { + content = content.split('<').join('<').split('>').join('>'); + } + const line = this._host.document.createElement('div'); + line.className = 'sidebar-view-item-value-line'; + line.innerHTML = content ? content : ' '; + this._element.appendChild(line); + } + } + } + + render() { + return [ this._element ]; + } + + toggle() { + if (this._expander.innerText == '+') { + this._expander.innerText = '-'; + + const typeLine = this._host.document.createElement('div'); + typeLine.className = 'sidebar-view-item-value-line-border'; + const type = this._attribute.type; + const value = this._attribute.value; + if (type == 'tensor' && value && value.type) { + typeLine.innerHTML = 'type: ' + '' + value.type.toString() + ''; + this._element.appendChild(typeLine); + } + else { + typeLine.innerHTML = 'type: ' + '' + this._attribute.type + ''; + this._element.appendChild(typeLine); + } + + const description = this._attribute.description; + if (description) { + const descriptionLine = this._host.document.createElement('div'); + descriptionLine.className = 'sidebar-view-item-value-line-border'; + descriptionLine.innerHTML = description; + this._element.appendChild(descriptionLine); + } + + if (this._attribute.type == 'tensor' && value) { + const state = value.state; + const valueLine = this._host.document.createElement('div'); + valueLine.className = 'sidebar-view-item-value-line-border'; + const contentLine = this._host.document.createElement('pre'); + contentLine.innerHTML = state || value.toString(); + valueLine.appendChild(contentLine); + this._element.appendChild(valueLine); + } + } + else { + this._expander.innerText = '+'; + while (this._element.childElementCount > 2) { + this._element.removeChild(this._element.lastChild); + } + } + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } +} + +sidebar.ParameterView = class { + + constructor(host, list) { + this._list = list; + this._elements = []; + this._items = []; + for (const argument of list.arguments) { + const item = new sidebar.ArgumentView(host, argument); + item.on('export-tensor', (sender, tensor) => { + this._raise('export-tensor', tensor); + }); + item.on('error', (sender, tensor) => { + this._raise('error', tensor); + }); + this._items.push(item); + this._elements.push(item.render()); + } + } + + render() { + return this._elements; + } + + toggle() { + for (const item of this._items) { + item.toggle(); + } + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } +}; + +sidebar.ArgumentView = class { + + constructor(host, argument) { + this._host = host; + this._argument = argument; + + this._element = this._host.document.createElement('div'); + this._element.className = 'sidebar-view-item-value'; + + const initializer = argument.initializer; + if (initializer) { + this._element.classList.add('sidebar-view-item-value-dark'); + } + + const quantization = argument.quantization; + const type = argument.type; + const location = this._argument.location !== undefined; + if (type || initializer || quantization || location) { + this._expander = this._host.document.createElement('div'); + this._expander.className = 'sidebar-view-item-value-expander'; + this._expander.innerText = '+'; + this._expander.addEventListener('click', () => { + this.toggle(); + }); + this._element.appendChild(this._expander); + } + + let name = this._argument.name || ''; + this._hasId = name ? true : false; + this._hasKind = initializer && initializer.kind ? true : false; + if (this._hasId || (!this._hasKind && !type)) { + this._hasId = true; + const nameLine = this._host.document.createElement('div'); + nameLine.className = 'sidebar-view-item-value-line'; + if (typeof name !== 'string') { + throw new Error("Invalid argument identifier '" + JSON.stringify(name) + "'."); + } + name = name.split('\n').shift(); // custom argument id + name = name || ' '; + nameLine.innerHTML = 'name: ' + name + ''; + this._element.appendChild(nameLine); + } + else if (this._hasKind) { + const kindLine = this._host.document.createElement('div'); + kindLine.className = 'sidebar-view-item-value-line'; + kindLine.innerHTML = 'kind: ' + initializer.kind + ''; + this._element.appendChild(kindLine); + } + else if (type) { + const typeLine = this._host.document.createElement('div'); + typeLine.className = 'sidebar-view-item-value-line-border'; + typeLine.innerHTML = 'type: ' + type.toString().split('<').join('<').split('>').join('>') + ''; + this._element.appendChild(typeLine); + } + } + + render() { + return this._element; + } + + toggle() { + if (this._expander) { + if (this._expander.innerText == '+') { + this._expander.innerText = '-'; + + const initializer = this._argument.initializer; + if (this._hasId && this._hasKind) { + const kindLine = this._host.document.createElement('div'); + kindLine.className = 'sidebar-view-item-value-line-border'; + kindLine.innerHTML = 'kind: ' + '' + initializer.kind + ''; + this._element.appendChild(kindLine); + } + let type = null; + let denotation = null; + if (this._argument.type) { + type = this._argument.type.toString(); + denotation = this._argument.type.denotation || null; + } + if (type && (this._hasId || this._hasKind)) { + const typeLine = this._host.document.createElement('div'); + typeLine.className = 'sidebar-view-item-value-line-border'; + typeLine.innerHTML = 'type: ' + type.split('<').join('<').split('>').join('>') + ''; + this._element.appendChild(typeLine); + } + if (denotation) { + const denotationLine = this._host.document.createElement('div'); + denotationLine.className = 'sidebar-view-item-value-line-border'; + denotationLine.innerHTML = 'denotation: ' + denotation + ''; + this._element.appendChild(denotationLine); + } + + const description = this._argument.description; + if (description) { + const descriptionLine = this._host.document.createElement('div'); + descriptionLine.className = 'sidebar-view-item-value-line-border'; + descriptionLine.innerHTML = description; + this._element.appendChild(descriptionLine); + } + + const quantization = this._argument.quantization; + if (quantization) { + const quantizationLine = this._host.document.createElement('div'); + quantizationLine.className = 'sidebar-view-item-value-line-border'; + const content = !Array.isArray(quantization) ? quantization : '

' + quantization.map((value) => ' ' + value).join('
'); + quantizationLine.innerHTML = 'quantization: ' + '' + content + ''; + this._element.appendChild(quantizationLine); + } + + if (this._argument.location !== undefined) { + const location = this._host.document.createElement('div'); + location.className = 'sidebar-view-item-value-line-border'; + location.innerHTML = 'location: ' + '' + this._argument.location + ''; + this._element.appendChild(location); + } + + if (initializer) { + const contentLine = this._host.document.createElement('pre'); + const valueLine = this._host.document.createElement('div'); + try { + const state = initializer.state; + if (state === null && this._host.save && + initializer.type.dataType && initializer.type.dataType != '?' && + initializer.type.shape && initializer.type.shape.dimensions /*&& initializer.type.shape.dimensions.length > 0*/) { + this._saveButton = this._host.document.createElement('div'); + this._saveButton.className = 'sidebar-view-item-value-expander'; + this._saveButton.innerHTML = '💾'; + this._saveButton.addEventListener('click', () => { + this._raise('export-tensor', initializer); + }); + this._element.appendChild(this._saveButton); + } + + valueLine.className = 'sidebar-view-item-value-line-border'; + contentLine.innerHTML = state || initializer.toString(); + } + catch (err) { + contentLine.innerHTML = err.toString(); + this._raise('error', err); + } + valueLine.appendChild(contentLine); + this._element.appendChild(valueLine); + } + } + else { + this._expander.innerText = '+'; + while (this._element.childElementCount > 2) { + this._element.removeChild(this._element.lastChild); + } + } + } + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } +}; + +sidebar.ModelSidebar = class { + + constructor(host, model, graph) { + this._host = host; + this._model = model; + this._elements = []; + + if (model.format) { + this._addProperty('format', new sidebar.ValueTextView(this._host, model.format)); + } + if (model.producer) { + this._addProperty('producer', new sidebar.ValueTextView(this._host, model.producer)); + } + if (model.source) { + this._addProperty('source', new sidebar.ValueTextView(this._host, model.source)); + } + if (model.name) { + this._addProperty('name', new sidebar.ValueTextView(this._host, model.name)); + } + if (model.version) { + this._addProperty('version', new sidebar.ValueTextView(this._host, model.version)); + } + if (model.description) { + this._addProperty('description', new sidebar.ValueTextView(this._host, model.description)); + } + if (model.author) { + this._addProperty('author', new sidebar.ValueTextView(this._host, model.author)); + } + if (model.company) { + this._addProperty('company', new sidebar.ValueTextView(this._host, model.company)); + } + if (model.license) { + this._addProperty('license', new sidebar.ValueTextView(this._host, model.license)); + } + if (model.domain) { + this._addProperty('domain', new sidebar.ValueTextView(this._host, model.domain)); + } + if (model.imports) { + this._addProperty('imports', new sidebar.ValueTextView(this._host, model.imports)); + } + if (model.runtime) { + this._addProperty('runtime', new sidebar.ValueTextView(this._host, model.runtime)); + } + + const metadata = model.metadata; + if (metadata) { + for (const property of model.metadata) { + this._addProperty(property.name, new sidebar.ValueTextView(this._host, property.value)); + } + } + + const graphs = Array.isArray(model.graphs) ? model.graphs : []; + if (graphs.length > 1) { + const graphSelector = new sidebar.SelectView(this._host, model.graphs, graph); + graphSelector.on('change', (sender, data) => { + this._raise('update-active-graph', data); + }); + this._addProperty('subgraph', graphSelector); + } + + if (graph) { + if (graph.version) { + this._addProperty('version', new sidebar.ValueTextView(this._host, graph.version)); + } + if (graph.type) { + this._addProperty('type', new sidebar.ValueTextView(this._host, graph.type)); + } + if (graph.tags) { + this._addProperty('tags', new sidebar.ValueTextView(this._host, graph.tags)); + } + if (graph.description) { + this._addProperty('description', new sidebar.ValueTextView(this._host, graph.description)); + } + if (Array.isArray(graph.inputs) && graph.inputs.length > 0) { + this._addHeader('Inputs'); + for (const input of graph.inputs) { + this.addArgument(input.name, input); + } + } + if (Array.isArray(graph.outputs) && graph.outputs.length > 0) { + this._addHeader('Outputs'); + for (const output of graph.outputs) { + this.addArgument(output.name, output); + } + } + } + + const separator = this._host.document.createElement('div'); + separator.className = 'sidebar-view-separator'; + this._elements.push(separator); + } + + render() { + return this._elements; + } + + _addHeader(title) { + const headerElement = this._host.document.createElement('div'); + headerElement.className = 'sidebar-view-header'; + headerElement.innerText = title; + this._elements.push(headerElement); + } + + _addProperty(name, value) { + const item = new sidebar.NameValueView(this._host, name, value); + this._elements.push(item.render()); + } + + addArgument(name, argument) { + const view = new sidebar.ParameterView(this._host, argument); + view.toggle(); + const item = new sidebar.NameValueView(this._host, name, view); + this._elements.push(item.render()); + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } +}; + +sidebar.DocumentationSidebar = class { + + constructor(host, metadata) { + this._host = host; + this._metadata = metadata; + } + + render() { + if (!this._elements) { + this._elements = []; + + const type = sidebar.DocumentationSidebar.formatDocumentation(this._metadata); + + const element = this._host.document.createElement('div'); + element.setAttribute('class', 'sidebar-view-documentation'); + + this._append(element, 'h1', type.name); + + if (type.summary) { + this._append(element, 'p', type.summary); + } + + if (type.description) { + this._append(element, 'p', type.description); + } + + if (Array.isArray(type.attributes) && type.attributes.length > 0) { + this._append(element, 'h2', 'Attributes'); + const attributes = this._append(element, 'dl'); + for (const attribute of type.attributes) { + this._append(attributes, 'dt', attribute.name + (attribute.type ? ': ' + attribute.type + '' : '')); + this._append(attributes, 'dd', attribute.description); + } + element.appendChild(attributes); + } + + if (Array.isArray(type.inputs) && type.inputs.length > 0) { + this._append(element, 'h2', 'Inputs' + (type.inputs_range ? ' (' + type.inputs_range + ')' : '')); + const inputs = this._append(element, 'dl'); + for (const input of type.inputs) { + this._append(inputs, 'dt', input.name + (input.type ? ': ' + input.type + '' : '') + (input.option ? ' (' + input.option + ')' : '')); + this._append(inputs, 'dd', input.description); + } + } + + if (Array.isArray(type.outputs) && type.outputs.length > 0) { + this._append(element, 'h2', 'Outputs' + (type.outputs_range ? ' (' + type.outputs_range + ')' : '')); + const outputs = this._append(element, 'dl'); + for (const output of type.outputs) { + this._append(outputs, 'dt', output.name + (output.type ? ': ' + output.type + '' : '') + (output.option ? ' (' + output.option + ')' : '')); + this._append(outputs, 'dd', output.description); + } + } + + if (Array.isArray(type.type_constraints) && type.type_constraints.length > 0) { + this._append(element, 'h2', 'Type Constraints'); + const type_constraints = this._append(element, 'dl'); + for (const type_constraint of type.type_constraints) { + this._append(type_constraints, 'dt', type_constraint.type_param_str + ': ' + type_constraint.allowed_type_strs.map((item) => '' + item + '').join(', ')); + this._append(type_constraints, 'dd', type_constraint.description); + } + } + + if (Array.isArray(type.examples) && type.examples.length > 0) { + this._append(element, 'h2', 'Examples'); + for (const example of type.examples) { + this._append(element, 'h3', example.summary); + this._append(element, 'pre', example.code); + } + } + + if (Array.isArray(type.references) && type.references.length > 0) { + this._append(element, 'h2', 'References'); + const references = this._append(element, 'ul'); + for (const reference of type.references) { + this._append(references, 'li', reference.description); + } + } + + if (type.domain && type.version && type.support_level) { + this._append(element, 'h2', 'Support'); + this._append(element, 'dl', 'In domain ' + type.domain + ' since version ' + type.version + ' at support level ' + type.support_level + '.'); + } + + if (!this._host.type !== 'Electron') { + element.addEventListener('click', (e) => { + if (e.target && e.target.href) { + const link = e.target.href; + if (link.startsWith('http://') || link.startsWith('https://')) { + e.preventDefault(); + this._raise('navigate', { link: link }); + } + } + }); + } + + this._elements = [ element ]; + + const separator = this._host.document.createElement('div'); + separator.className = 'sidebar-view-separator'; + this._elements.push(separator); + } + return this._elements; + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } + + _append(parent, type, content) { + const element = this._host.document.createElement(type); + if (content) { + element.innerHTML = content; + } + parent.appendChild(element); + return element; + } + + static formatDocumentation(source) { + if (source) { + const generator = new markdown.Generator(); + const target = {}; + if (source.name !== undefined) { + target.name = source.name; + } + if (source.module !== undefined) { + target.module = source.module; + } + if (source.category !== undefined) { + target.category = source.category; + } + if (source.summary !== undefined) { + target.summary = generator.html(source.summary); + } + if (source.description !== undefined) { + target.description = generator.html(source.description); + } + if (Array.isArray(source.attributes)) { + target.attributes = source.attributes.map((source) => { + const target = {}; + target.name = source.name; + if (source.type !== undefined) { + target.type = source.type; + } + if (source.option !== undefined) { + target.option = source.option; + } + if (source.optional !== undefined) { + target.optional = source.optional; + } + if (source.required !== undefined) { + target.required = source.required; + } + if (source.minimum !== undefined) { + target.minimum = source.minimum; + } + if (source.src !== undefined) { + target.src = source.src; + } + if (source.src_type !== undefined) { + target.src_type = source.src_type; + } + if (source.description !== undefined) { + target.description = generator.html(source.description); + } + if (source.default !== undefined) { + target.default = source.default; + } + if (source.visible !== undefined) { + target.visible = source.visible; + } + return target; + }); + } + if (Array.isArray(source.inputs)) { + target.inputs = source.inputs.map((source) => { + const target = {}; + target.name = source.name; + if (source.type !== undefined) { + target.type = source.type; + } + if (source.description !== undefined) { + target.description = generator.html(source.description); + } + if (source.default !== undefined) { + target.default = source.default; + } + if (source.src !== undefined) { + target.src = source.src; + } + if (source.list !== undefined) { + target.list = source.list; + } + if (source.isRef !== undefined) { + target.isRef = source.isRef; + } + if (source.typeAttr !== undefined) { + target.typeAttr = source.typeAttr; + } + if (source.numberAttr !== undefined) { + target.numberAttr = source.numberAttr; + } + if (source.typeListAttr !== undefined) { + target.typeListAttr = source.typeListAttr; + } + if (source.option !== undefined) { + target.option = source.option; + } + if (source.optional !== undefined) { + target.optional = source.optional; + } + if (source.visible !== undefined) { + target.visible = source.visible; + } + return target; + }); + } + if (Array.isArray(source.outputs)) { + target.outputs = source.outputs.map((source) => { + const target = {}; + target.name = source.name; + if (source.type) { + target.type = source.type; + } + if (source.description !== undefined) { + target.description = generator.html(source.description); + } + if (source.list !== undefined) { + target.list = source.list; + } + if (source.typeAttr !== undefined) { + target.typeAttr = source.typeAttr; + } + if (source.typeListAttr !== undefined) { + target.typeListAttr = source.typeAttr; + } + if (source.numberAttr !== undefined) { + target.numberAttr = source.numberAttr; + } + if (source.isRef !== undefined) { + target.isRef = source.isRef; + } + if (source.option !== undefined) { + target.option = source.option; + } + return target; + }); + } + if (Array.isArray(source.references)) { + target.references = source.references.map((source) => { + if (source) { + target.description = generator.html(source.description); + } + return target; + }); + } + if (source.version !== undefined) { + target.version = source.version; + } + if (source.operator !== undefined) { + target.operator = source.operator; + } + if (source.identifier !== undefined) { + target.identifier = source.identifier; + } + if (source.package !== undefined) { + target.package = source.package; + } + if (source.support_level !== undefined) { + target.support_level = source.support_level; + } + if (source.min_input !== undefined) { + target.min_input = source.min_input; + } + if (source.max_input !== undefined) { + target.max_input = source.max_input; + } + if (source.min_output !== undefined) { + target.min_output = source.min_output; + } + if (source.max_input !== undefined) { + target.max_output = source.max_output; + } + if (source.inputs_range !== undefined) { + target.inputs_range = source.inputs_range; + } + if (source.outputs_range !== undefined) { + target.outputs_range = source.outputs_range; + } + if (source.examples !== undefined) { + target.examples = source.examples; + } + if (source.constants !== undefined) { + target.constants = source.constants; + } + if (source.type_constraints !== undefined) { + target.type_constraints = source.type_constraints; + } + return target; + } + return ''; + } +}; + +sidebar.FindSidebar = class { + + constructor(host, element, graph) { + this._host = host; + this._graphElement = element; + this._graph = graph; + this._contentElement = this._host.document.createElement('div'); + this._contentElement.setAttribute('class', 'sidebar-view-find'); + this._searchElement = this._host.document.createElement('input'); + this._searchElement.setAttribute('id', 'search'); + this._searchElement.setAttribute('type', 'text'); + this._searchElement.setAttribute('spellcheck', 'false'); + this._searchElement.setAttribute('placeholder', 'Search...'); + this._searchElement.setAttribute('style', 'width: 100%'); + this._searchElement.addEventListener('input', (e) => { + this.update(e.target.value); + this._raise('search-text-changed', e.target.value); + }); + this._resultElement = this._host.document.createElement('ol'); + this._resultElement.addEventListener('click', (e) => { + this.select(e); + }); + this._contentElement.appendChild(this._searchElement); + this._contentElement.appendChild(this._resultElement); + } + + on(event, callback) { + this._events = this._events || {}; + this._events[event] = this._events[event] || []; + this._events[event].push(callback); + } + + _raise(event, data) { + if (this._events && this._events[event]) { + for (const callback of this._events[event]) { + callback(this, data); + } + } + } + + select(e) { + const selection = []; + const id = e.target.id; + + const nodesElement = this._graphElement.getElementById('nodes'); + let nodeElement = nodesElement.firstChild; + while (nodeElement) { + if (nodeElement.id == id) { + selection.push(nodeElement); + } + nodeElement = nodeElement.nextSibling; + } + + const edgePathsElement = this._graphElement.getElementById('edge-paths'); + let edgePathElement = edgePathsElement.firstChild; + while (edgePathElement) { + if (edgePathElement.id == id) { + selection.push(edgePathElement); + } + edgePathElement = edgePathElement.nextSibling; + } + + let initializerElement = this._graphElement.getElementById(id); + if (initializerElement) { + while (initializerElement.parentElement) { + initializerElement = initializerElement.parentElement; + if (initializerElement.id && initializerElement.id.startsWith('node-')) { + selection.push(initializerElement); + break; + } + } + } + + if (selection.length > 0) { + this._raise('select', selection); + } + } + + focus(searchText) { + this._searchElement.focus(); + this._searchElement.value = ''; + this._searchElement.value = searchText; + this.update(searchText); + } + + update(searchText) { + while (this._resultElement.lastChild) { + this._resultElement.removeChild(this._resultElement.lastChild); + } + + let terms = null; + let callback = null; + const unquote = searchText.match(new RegExp(/^'(.*)'|"(.*)"$/)); + if (unquote) { + const term = unquote[1] || unquote[2]; + terms = [ term ]; + callback = (name) => { + return term == name; + }; + } + else { + terms = searchText.trim().toLowerCase().split(' ').map((term) => term.trim()).filter((term) => term.length > 0); + callback = (name) => { + return terms.every((term) => name.toLowerCase().indexOf(term) !== -1); + }; + } + + const nodes = new Set(); + const edges = new Set(); + + for (const node of this._graph.nodes.values()) { + const label = node.label; + const initializers = []; + if (label.class === 'graph-node' || label.class === 'graph-input') { + for (const input of label.inputs) { + for (const argument of input.arguments) { + if (argument.name && !edges.has(argument.name)) { + const match = (argument, term) => { + if (argument.name && argument.name.toLowerCase().indexOf(term) !== -1) { + return true; + } + if (argument.type) { + if (argument.type.dataType && term === argument.type.dataType.toLowerCase()) { + return true; + } + if (argument.type.shape) { + if (term === argument.type.shape.toString().toLowerCase()) { + return true; + } + if (argument.type.shape && Array.isArray(argument.type.shape.dimensions)) { + const dimensions = argument.type.shape.dimensions.map((dimension) => dimension ? dimension.toString().toLowerCase() : ''); + if (term === dimensions.join(',')) { + return true; + } + if (dimensions.some((dimension) => term === dimension)) { + return true; + } + } + } + } + return false; + }; + if (terms.every((term) => match(argument, term))) { + if (!argument.initializer) { + const inputItem = this._host.document.createElement('li'); + inputItem.innerText = '\u2192 ' + argument.name.split('\n').shift(); // custom argument id + inputItem.id = 'edge-' + argument.name; + this._resultElement.appendChild(inputItem); + edges.add(argument.name); + } + else { + initializers.push(argument); + } + } + } + } + } + } + if (label.class === 'graph-node') { + const name = label.value.name; + const type = label.value.type.name; + if (!nodes.has(label.id) && + ((name && callback(name) || (type && callback(type))))) { + const nameItem = this._host.document.createElement('li'); + nameItem.innerText = '\u25A2 ' + (name || '[' + type + ']'); + nameItem.id = label.id; + this._resultElement.appendChild(nameItem); + nodes.add(label.id); + } + } + for (const argument of initializers) { + if (argument.name) { + const initializeItem = this._host.document.createElement('li'); + initializeItem.innerText = '\u25A0 ' + argument.name.split('\n').shift(); // custom argument id + initializeItem.id = 'initializer-' + argument.name; + this._resultElement.appendChild(initializeItem); + } + } + } + + for (const node of this._graph.nodes.values()) { + const label = node.label; + if (label.class === 'graph-node' || label.class === 'graph-output') { + for (const output of label.outputs) { + for (const argument of output.arguments) { + if (argument.name && !edges.has(argument.name) && terms.every((term) => argument.name.toLowerCase().indexOf(term) != -1)) { + const outputItem = this._host.document.createElement('li'); + outputItem.innerText = '\u2192 ' + argument.name.split('\n').shift(); // custom argument id + outputItem.id = 'edge-' + argument.name; + this._resultElement.appendChild(outputItem); + edges.add(argument.name); + } + } + } + } + } + + this._resultElement.style.display = this._resultElement.childNodes.length != 0 ? 'block' : 'none'; + } + + get content() { + return this._contentElement; + } +}; + +const markdown = {}; + +markdown.Generator = class { + + constructor() { + this._newlineRegExp = /^\n+/; + this._codeRegExp = /^( {4}[^\n]+\n*)+/; + this._fencesRegExp = /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?:\n+|$)|$)/; + this._hrRegExp = /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/; + this._headingRegExp = /^ {0,3}(#{1,6}) +([^\n]*?)(?: +#+)? *(?:\n+|$)/; + this._blockquoteRegExp = /^( {0,3}> ?(([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)|[^\n]*)(?:\n|$))+/; + this._listRegExp = /^( {0,3})((?:[*+-]|\d{1,9}[.)])) [\s\S]+?(?:\n+(?=\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$))|\n+(?= {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$))|\n{2,}(?! )(?!\1(?:[*+-]|\d{1,9}[.)]) )\n*|\s*$)/; + this._htmlRegExp = /^ {0,3}(?:<(script|pre|style)[\s>][\s\S]*?(?:<\/\1>[^\n]*\n+|$)||$)[^\n]*(\n+|$)|<\?[\s\S]*?(?:\?>\n*|$)|\n*|$)|\n*|$)|<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)[\s\S]*?(?:\n{2,}|$)|<(?!script|pre|style)([a-z][\w-]*)(?: +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?)*? *\/?>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$)|<\/(?!script|pre|style)[a-z][\w-]*\s*>(?=[ \t]*(?:\n|$))[\s\S]*?(?:\n{2,}|$))/i; + this._defRegExp = /^ {0,3}\[((?!\s*\])(?:\\[[\]]|[^[\]])+)\]: *\n? *]+)>?(?:(?: +\n? *| *\n *)((?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))))? *(?:\n+|$)/; + this._nptableRegExp = /^ *([^|\n ].*\|.*)\n {0,3}([-:]+ *\|[-| :]*)(?:\n((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/; + this._tableRegExp = /^ *\|(.+)\n {0,3}\|?( *[-:]+[-| :]*)(?:\n *((?:(?!\n| {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {4}[^\n]| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--)).*(?:\n|$))*)\n*|$)/; + this._lheadingRegExp = /^([^\n]+)\n {0,3}(=+|-+) *(?:\n+|$)/; + this._textRegExp = /^[^\n]+/; + this._bulletRegExp = /(?:[*+-]|\d{1,9}[.)])/; + this._itemRegExp = /^( *)((?:[*+-]|\d{1,9}[.)])) ?[^\n]*(?:\n(?!\1(?:[*+-]|\d{1,9}[.)]) ?)[^\n]*)*/gm; + this._paragraphRegExp = /^([^\n]+(?:\n(?! {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)| {0,3}#{1,6} | {0,3}>| {0,3}(?:`{3,}(?=[^`\n]*\n)|~{3,})[^\n]*\n| {0,3}(?:[*+-]|1[.)]) |<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?: +|\n|\/?>)|<(?:script|pre|style|!--))[^\n]+)*)/; + this._backpedalRegExp = /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/; + this._escapeRegExp = /^\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~~|])/; + this._escapesRegExp = /\\([!"#$%&'()*+,\-./:;<=>?@[\]\\^_`{|}~])/g; + /* eslint-disable no-control-regex */ + this._autolinkRegExp = /^<([a-zA-Z][a-zA-Z0-9+.-]{1,31}:[^\s\x00-\x1f<>]*|[a-zA-Z0-9.!#$%&'*+/=?_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_]))>/; + this._linkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\(\s*(<(?:\\[<>]?|[^\s<>\\])*>|[^\s\x00-\x1f]*)(?:\s+("(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)))?\s*\)/; + /* eslint-enable no-control-regex */ + this._urlRegExp = /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9-]+\.?)+[^\s<]*|^[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/i; + this._tagRegExp = /^|^<\/[a-zA-Z][\w:-]*\s*>|^<[a-zA-Z][\w-]*(?:\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?)*?\s*\/?>|^<\?[\s\S]*?\?>|^|^/; + this._reflinkRegExp = /^!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]/; + this._nolinkRegExp = /^!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?/; + this._reflinkSearchRegExp = /!?\[((?:\[(?:\\.|[^[\]\\])*\]|\\.|`[^`]*`|[^[\]\\`])*?)\]\[(?!\s*\])((?:\\[[\]]?|[^[\]\\])+)\]|!?\[(?!\s*\])((?:\[[^[\]]*\]|\\[[\]]|[^[\]])*)\](?:\[\])?(?!\()/g; + this._strongStartRegExp = /^(?:(\*\*(?=[*!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*\*)(?![\s])|__/; + this._strongMiddleRegExp = /^\*\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*\*$|^__(?![\s])((?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?)__$/; + this._strongEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g; + this._strongEndUndRegExp = /[^\s]__(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g; + this._emStartRegExp = /^(?:(\*(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]))|\*)(?![*\s])|_/; + this._emMiddleRegExp = /^\*(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|\*(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^*]|\\\*)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?\*)+?\*$|^_(?![_\s])(?:(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)|_(?:(?!__[^_]*?__|\*\*\[^\*\]*?\*\*)(?:[^_]|\\_)|__[^_]*?__|\*\*\[^\*\]*?\*\*)*?_)+?_$/; + this._emEndAstRegExp = /[^!"#$%&'()+\-.,/:;<=>?@[\]`{|}~\s]\*(?!\*)|[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~]\*(?!\*)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~_\s]|$))/g; + this._emEndUndRegExp = /[^\s]_(?!_)(?:(?=[!"#$%&'()+\-.,/:;<=>?@[\]`{|}~*\s])|$)/g, + this._codespanRegExp = /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/; + this._brRegExp = /^( {2,}|\\)\n(?!\s*$)/; + this._delRegExp = /^~+(?=\S)([\s\S]*?\S)~+/; + this._textspanRegExp = /^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\?@[\]`{|}~])/; + this._blockSkipRegExp = /\[[^\]]*?\]\([^)]*?\)|`[^`]*?`|<[^>]*?>/g; + this._escapeTestRegExp = /[&<>"']/; + this._escapeReplaceRegExp = /[&<>"']/g; + this._escapeTestNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/; + this._escapeReplaceNoEncodeRegExp = /[<>"']|&(?!#?\w+;)/g; + this._escapeReplacementsMap = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; + } + + html(source) { + const tokens = []; + const links = new Map(); + this._tokenize(source.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' '), tokens, links, true); + this._tokenizeBlock(tokens, links); + const slugs = new Map(); + const result = this._render(tokens, slugs, true); + return result; + } + + _tokenize(source, tokens, links, top) { + source = source.replace(/^ +$/gm, ''); + while (source) { + let match = this._newlineRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + if (match[0].length > 1) { + tokens.push({ type: 'space' }); + } + continue; + } + match = this._codeRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + const lastToken = tokens[tokens.length - 1]; + if (lastToken && lastToken.type === 'paragraph') { + lastToken.text += '\n' + match[0].trimRight(); + } + else { + const text = match[0].replace(/^ {4}/gm, '').replace(/\n*$/, ''); + tokens.push({ type: 'code', text: text }); + } + continue; + } + match = this._fencesRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + const language = match[2] ? match[2].trim() : match[2]; + let content = match[3] || ''; + const matchIndent = match[0].match(/^(\s+)(?:```)/); + if (matchIndent !== null) { + const indent = matchIndent[1]; + content = content.split('\n').map(node => { + const match = node.match(/^\s+/); + return (match !== null && match[0].length >= indent.length) ? node.slice(indent.length) : node; + }).join('\n'); + } + tokens.push({ type: 'code', language: language, text: content }); + continue; + } + match = this._headingRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + tokens.push({ type: 'heading', depth: match[1].length, text: match[2] }); + continue; + } + match = this._nptableRegExp.exec(source); + if (match) { + const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, '')); + const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */); + if (header.length === align.length) { + const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : []; + const token = { type: 'table', header: header, align: align, cells: cells, raw: match[0] }; + for (let i = 0; i < token.align.length; i++) { + if (/^ *-+: *$/.test(token.align[i])) { + token.align[i] = 'right'; + } + else if (/^ *:-+: *$/.test(token.align[i])) { + token.align[i] = 'center'; + } + else if (/^ *:-+ *$/.test(token.align[i])) { + token.align[i] = 'left'; + } + else { + token.align[i] = null; + } + } + token.cells = token.cells.map((cell) => this._splitCells(cell, token.header.length)); + source = source.substring(token.raw.length); + tokens.push(token); + continue; + } + } + match = this._hrRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + tokens.push({ type: 'hr' }); + continue; + } + match = this._blockquoteRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + const text = match[0].replace(/^ *> ?/gm, ''); + tokens.push({ type: 'blockquote', text: text, tokens: this._tokenize(text, [], links, top) }); + continue; + } + match = this._listRegExp.exec(source); + if (match) { + let raw = match[0]; + const bull = match[2]; + const ordered = bull.length > 1; + const parent = bull[bull.length - 1] === ')'; + const list = { type: 'list', raw: raw, ordered: ordered, start: ordered ? +bull.slice(0, -1) : '', loose: false, items: [] }; + const itemMatch = match[0].match(this._itemRegExp); + let next = false; + const length = itemMatch.length; + for (let i = 0; i < length; i++) { + let item = itemMatch[i]; + raw = item; + let space = item.length; + item = item.replace(/^ *([*+-]|\d+[.)]) ?/, ''); + if (~item.indexOf('\n ')) { + space -= item.length; + item = item.replace(new RegExp('^ {1,' + space + '}', 'gm'), ''); + } + if (i !== length - 1) { + const bullet = this._bulletRegExp.exec(itemMatch[i + 1])[0]; + if (ordered ? bullet.length === 1 || (!parent && bullet[bullet.length - 1] === ')') : (bullet.length > 1)) { + const addBack = itemMatch.slice(i + 1).join('\n'); + list.raw = list.raw.substring(0, list.raw.length - addBack.length); + i = length - 1; + } + } + let loose = next || /\n\n(?!\s*$)/.test(item); + if (i !== length - 1) { + next = item.charAt(item.length - 1) === '\n'; + if (!loose) { + loose = next; + } + } + if (loose) { + list.loose = true; + } + const task = /^\[[ xX]\] /.test(item); + let checked = undefined; + if (task) { + checked = item[1] !== ' '; + item = item.replace(/^\[[ xX]\] +/, ''); + } + list.items.push({ type: 'list_item', raw, task: task, checked: checked, loose: loose, text: item }); + } + source = source.substring(list.raw.length); + for (const item of list.items) { + item.tokens = this._tokenize(item.text, [], links, false); + } + tokens.push(list); + continue; + } + match = this._htmlRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + tokens.push({ type: 'html', pre: (match[1] === 'pre' || match[1] === 'script' || match[1] === 'style'), text: match[0] }); + continue; + } + if (top) { + match = this._defRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + match[3] = match[3] ? match[3].substring(1, match[3].length - 1) : match[3]; + const tag = match[1].toLowerCase().replace(/\s+/g, ' '); + if (!links.has(tag)) { + links.set(tag, { href: match[2], title: match[3] }); + } + continue; + } + } + match = this._tableRegExp.exec(source); + if (match) { + const header = this._splitCells(match[1].replace(/^ *| *\| *$/g, '')); + const align = match[2].replace(/^ *|\| *$/g, '').split(/ *\| */); + if (header.length === align.length) { + const cells = match[3] ? match[3].replace(/\n$/, '').split('\n') : []; + const token = { type: 'table', header: header, align: align, cells: cells, raw: match[0] }; + for (let i = 0; i < token.align.length; i++) { + if (/^ *-+: *$/.test(token.align[i])) { + token.align[i] = 'right'; + } + else if (/^ *:-+: *$/.test(token.align[i])) { + token.align[i] = 'center'; + } + else if (/^ *:-+ *$/.test(token.align[i])) { + token.align[i] = 'left'; + } + else { + token.align[i] = null; + } + } + token.cells = token.cells.map((cell) => this._splitCells(cell.replace(/^ *\| *| *\| *$/g, ''), token.header.length)); + source = source.substring(token.raw.length); + tokens.push(token); + continue; + } + } + match = this._lheadingRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + tokens.push({ type: 'heading', depth: match[2].charAt(0) === '=' ? 1 : 2, text: match[1] }); + continue; + } + if (top) { + match = this._paragraphRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + tokens.push({ type: 'paragraph', text: match[1].charAt(match[1].length - 1) === '\n' ? match[1].slice(0, -1) : match[1] }); + continue; + } + } + match = this._textRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + const lastToken = tokens[tokens.length - 1]; + if (lastToken && lastToken.type === 'text') { + lastToken.text += '\n' + match[0]; + } + else { + tokens.push({ type: 'text', text: match[0] }); + } + continue; + } + throw new Error("Unexpected '" + source.charCodeAt(0) + "'."); + } + return tokens; + } + + _tokenizeInline(source, links, inLink, inRawBlock, prevChar) { + const tokens = []; + let maskedSource = source; + if (links.size > 0) { + while (maskedSource) { + const match = this._reflinkSearchRegExp.exec(maskedSource); + if (match) { + if (links.has(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) { + maskedSource = maskedSource.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSource.slice(this._reflinkSearchRegExp.lastIndex); + } + continue; + } + break; + } + } + while (maskedSource) { + const match = this._blockSkipRegExp.exec(maskedSource); + if (match) { + maskedSource = maskedSource.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSource.slice(this._blockSkipRegExp.lastIndex); + continue; + } + break; + } + while (source) { + let match = this._escapeRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + tokens.push({ type: 'escape', text: this._escape(match[1]) }); + continue; + } + match = this._tagRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + if (!inLink && /^/i.test(match[0])) { + inLink = false; + } + if (!inRawBlock && /^<(pre|code|kbd|script)(\s|>)/i.test(match[0])) { + inRawBlock = true; + } + else if (inRawBlock && /^<\/(pre|code|kbd|script)(\s|>)/i.test(match[0])) { + inRawBlock = false; + } + tokens.push({ type: 'html', raw: match[0], text: match[0] }); + continue; + } + match = this._linkRegExp.exec(source); + if (match) { + let index = -1; + const ref = match[2]; + if (ref.indexOf(')') !== -1) { + let level = 0; + for (let i = 0; i < ref.length; i++) { + switch (ref[i]) { + case '\\': + i++; + break; + case '(': + level++; + break; + case ')': + level--; + if (level < 0) { + index = i; + i = ref.length; + } + break; + } + } + } + if (index > -1) { + const length = (match[0].indexOf('!') === 0 ? 5 : 4) + match[1].length + index; + match[2] = match[2].substring(0, index); + match[0] = match[0].substring(0, length).trim(); + match[3] = ''; + } + const title = (match[3] ? match[3].slice(1, -1) : '').replace(this._escapesRegExp, '$1'); + const href = match[2].trim().replace(/^<([\s\S]*)>$/, '$1').replace(this._escapesRegExp, '$1'); + const token = this._outputLink(match, href, title); + source = source.substring(match[0].length); + if (token.type === 'link') { + token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, ''); + } + tokens.push(token); + continue; + } + match = this._reflinkRegExp.exec(source) || this._nolinkRegExp.exec(source); + if (match) { + let link = (match[2] || match[1]).replace(/\s+/g, ' '); + link = links.get(link.toLowerCase()); + if (!link || !link.href) { + const text = match[0].charAt(0); + source = source.substring(text.length); + tokens.push({ type: 'text', text: text }); + } + else { + source = source.substring(match[0].length); + const token = this._outputLink(match, link); + if (token.type === 'link') { + token.tokens = this._tokenizeInline(token.text, links, true, inRawBlock, ''); + } + tokens.push(token); + } + continue; + } + match = this._strongStartRegExp.exec(source); + if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) { + const masked = maskedSource.slice(-1 * source.length); + const endReg = match[0] === '**' ? this._strongEndAstRegExp : this._strongEndUndRegExp; + endReg.lastIndex = 0; + let cap; + while ((match = endReg.exec(masked)) != null) { + cap = this._strongMiddleRegExp.exec(masked.slice(0, match.index + 3)); + if (cap) { + break; + } + } + if (cap) { + const text = source.substring(2, cap[0].length - 2); + source = source.substring(cap[0].length); + tokens.push({ type: 'strong', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') }); + continue; + } + } + match = this._emStartRegExp.exec(source); + if (match && (!match[1] || (match[1] && (prevChar === '' || this._punctuationRegExp.exec(prevChar))))) { + const masked = maskedSource.slice(-1 * source.length); + const endReg = match[0] === '*' ? this._emEndAstRegExp : this._emEndUndRegExp; + endReg.lastIndex = 0; + let cap; + while ((match = endReg.exec(masked)) != null) { + cap = this._emMiddleRegExp.exec(masked.slice(0, match.index + 2)); + if (cap) { + break; + } + } + if (cap) { + const text = source.slice(1, cap[0].length - 1); + source = source.substring(cap[0].length); + tokens.push({ type: 'em', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') }); + continue; + } + } + match = this._codespanRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + let content = match[2].replace(/\n/g, ' '); + if (/[^ ]/.test(content) && content.startsWith(' ') && content.endsWith(' ')) { + content = content.substring(1, content.length - 1); + } + tokens.push({ type: 'codespan', text: this._encode(content) }); + continue; + } + match = this._brRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + tokens.push({ type: 'br' }); + continue; + } + match = this._delRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + const text = match[1]; + tokens.push({ type: 'del', text: text, tokens: this._tokenizeInline(text, links, inLink, inRawBlock, '') }); + continue; + } + match = this._autolinkRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + const text = this._escape(match[1]); + const href = match[2] === '@' ? 'mailto:' + text : text; + tokens.push({ type: 'link', text: text, href: href, tokens: [ { type: 'text', raw: text, text } ] }); + continue; + } + if (!inLink) { + match = this._urlRegExp.exec(source); + if (match) { + const email = match[2] === '@'; + if (!email) { + let prevCapZero; + do { + prevCapZero = match[0]; + match[0] = this._backpedalRegExp.exec(match[0])[0]; + } while (prevCapZero !== match[0]); + } + const text = this._escape(match[0]); + const href = email ? ('mailto:' + text) : (match[1] === 'www.' ? 'http://' + text : text); + source = source.substring(match[0].length); + tokens.push({ type: 'link', text: text, href: href, tokens: [ { type: 'text', text: text } ] }); + continue; + } + } + match = this._textspanRegExp.exec(source); + if (match) { + source = source.substring(match[0].length); + prevChar = match[0].slice(-1); + tokens.push({ type: 'text' , text: inRawBlock ? match[0] : this._escape(match[0]) }); + continue; + } + throw new Error("Unexpected '" + source.charCodeAt(0) + "'."); + } + return tokens; + } + + _tokenizeBlock(tokens, links) { + for (const token of tokens) { + switch (token.type) { + case 'paragraph': + case 'text': + case 'heading': { + token.tokens = this._tokenizeInline(token.text, links, false, false, ''); + break; + } + case 'table': { + token.tokens = {}; + token.tokens.header = token.header.map((header) => this._tokenizeInline(header, links, false, false, '')); + token.tokens.cells = token.cells.map((cell) => cell.map((row) => this._tokenizeInline(row, links, false, false, ''))); + break; + } + case 'blockquote': { + this._tokenizeBlock(token.tokens, links); + break; + } + case 'list': { + for (const item of token.items) { + this._tokenizeBlock(item.tokens, links); + } + break; + } + } + } + } + + _render(tokens, slugs, top) { + let html = ''; + while (tokens.length > 0) { + const token = tokens.shift(); + switch (token.type) { + case 'space': { + continue; + } + case 'hr': { + html += '
\n'; + continue; + } + case 'heading': { + const level = token.depth; + const id = this._slug(slugs, this._renderInline(token.tokens, true)); + html += '' + this._renderInline(token.tokens) + '\n'; + continue; + } + case 'code': { + const code = token.text; + const language = (token.language || '').match(/\S*/)[0]; + html += '
' + (token.escaped ? code : this._encode(code)) + '
\n'; + continue; + } + case 'table': { + let header = ''; + let cell = ''; + for (let j = 0; j < token.header.length; j++) { + const content = this._renderInline(token.tokens.header[j]); + const align = token.align[j]; + cell += '' + content + '\n'; + } + header += '\n' + cell + '\n'; + let body = ''; + for (let j = 0; j < token.cells.length; j++) { + const row = token.tokens.cells[j]; + cell = ''; + for (let k = 0; k < row.length; k++) { + const content = this._renderInline(row[k]); + const align = token.align[k]; + cell += '' + content + '\n'; + } + body += '\n' + cell + '\n'; + } + html += '\n\n' + header + '\n' + (body ? '' + body + '' : body) + '
\n'; + continue; + } + case 'blockquote': { + html += '
\n' + this._render(token.tokens, slugs, true) + '
\n'; + continue; + } + case 'list': { + const ordered = token.ordered; + const start = token.start; + const loose = token.loose; + let body = ''; + for (const item of token.items) { + let itemBody = ''; + if (item.task) { + const checkbox = ' '; + if (loose) { + if (item.tokens.length > 0 && item.tokens[0].type === 'text') { + item.tokens[0].text = checkbox + ' ' + item.tokens[0].text; + if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') { + item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text; + } + } + else { + item.tokens.unshift({ type: 'text', text: checkbox }); + } + } + else { + itemBody += checkbox; + } + } + itemBody += this._render(item.tokens, slugs, loose); + body += '
  • ' + itemBody + '
  • \n'; + } + const type = (ordered ? 'ol' : 'ul'); + html += '<' + type + (ordered && start !== 1 ? (' start="' + start + '"') : '') + '>\n' + body + '\n'; + continue; + } + case 'html': { + html += token.text; + continue; + } + case 'paragraph': { + html += '

    ' + this._renderInline(token.tokens) + '

    \n'; + continue; + } + case 'text': { + html += top ? '

    ' : ''; + html += token.tokens ? this._renderInline(token.tokens) : token.text; + while (tokens.length > 0 && tokens[0].type === 'text') { + const token = tokens.shift(); + html += '\n' + (token.tokens ? this._renderInline(token.tokens) : token.text); + } + html += top ? '

    \n' : ''; + continue; + } + default: { + throw new Error("Unexpected token type '" + token.type + "'."); + } + } + } + return html; + } + + _renderInline(tokens, slug) { + let html = ''; + for (const token of tokens) { + switch (token.type) { + case 'escape': + case 'html': + case 'text': { + html += token.text; + break; + } + case 'link': { + const text = this._renderInline(token.tokens, slug); + html += slug ? text : '
    ' + text + ''; + break; + } + case 'image': { + html += slug ? token.text : '' + token.text + ''; + break; + } + case 'strong': { + const text = this._renderInline(token.tokens, slug); + html += slug ? text : '' + text + ''; + break; + } + case 'em': { + const text = this._renderInline(token.tokens, slug); + html += slug ? text : '' + text + ''; + break; + } + case 'codespan': { + html += slug ? token.text : '' + token.text + ''; + break; + } + case 'br': { + html += slug ? '' : '
    '; + break; + } + case 'del': { + const text = this._renderInline(token.tokens, slug); + html += slug ? text : '' + text + ''; + break; + } + default: { + throw new Error("Unexpected token type '" + token.type + "'."); + } + } + } + return html; + } + + _outputLink(match, href, title) { + title = title ? this._escape(title) : null; + const text = match[1].replace(/\\([[\]])/g, '$1'); + return match[0].charAt(0) !== '!' ? + { type: 'link', href: href, title: title, text: text } : + { type: 'image', href: href, title: title, text: this._escape(text) }; + } + + _splitCells(tableRow, count) { + const row = tableRow.replace(/\|/g, (match, offset, str) => { + let escaped = false; + let position = offset; + while (--position >= 0 && str[position] === '\\') { + escaped = !escaped; + } + return escaped ? '|' : ' |'; + }); + const cells = row.split(/ \|/); + if (cells.length > count) { + cells.splice(count); + } + else { + while (cells.length < count) { + cells.push(''); + } + } + return cells.map((cell) => cell.trim().replace(/\\\|/g, '|')); + } + + _slug(slugs, value) { + value = value.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig, (_, n) => { + n = n.toLowerCase(); + if (n === 'colon') { + return ':'; + } + if (n.charAt(0) === '#') { + return String.fromCharCode(n.charAt(1) === 'x' ? parseInt(n.substring(2), 16) : +n.substring(1)); + } + return ''; + }); + value = value.toLowerCase().trim() + .replace(/<[!/a-z].*?>/ig, '') + .replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g, '') + .replace(/\s/g, '-'); + let slug = value; + let count = 0; + if (slugs.has(value)) { + count = slugs.get(value); + do { + count++; + slug = value + '-' + count; + } + while (slugs.has(slug)); + } + slugs.set(value, count); + slugs.set(slug, 0); + return slug; + } + + _encode(content) { + if (this._escapeTestRegExp.test(content)) { + return content.replace(this._escapeReplaceRegExp, (ch) => this._escapeReplacementsMap[ch]); + } + return content; + } + + _escape(content) { + if (this._escapeTestNoEncodeRegExp.test(content)) { + return content.replace(this._escapeReplaceNoEncodeRegExp, (ch) => this._escapeReplacementsMap[ch]); + } + return content; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.Sidebar = sidebar.Sidebar; + module.exports.ModelSidebar = sidebar.ModelSidebar; + module.exports.NodeSidebar = sidebar.NodeSidebar; + module.exports.DocumentationSidebar = sidebar.DocumentationSidebar; + module.exports.FindSidebar = sidebar.FindSidebar; +} \ No newline at end of file diff --git a/view.js b/view.js new file mode 100644 index 0000000..e143c06 --- /dev/null +++ b/view.js @@ -0,0 +1,2142 @@ + +var view = view || {}; + +var base = base || require('./base'); +var zip = zip || require('./zip'); +var gzip = gzip || require('./gzip'); +var tar = tar || require('./tar'); +var json = json || require('./json'); +var xml = xml || require('./xml'); +var protobuf = protobuf || require('./protobuf'); +var flatbuffers = flatbuffers || require('./flatbuffers'); +var python = python || require('./python'); +var sidebar = sidebar || require('./view-sidebar'); +var grapher = grapher || require('./view-grapher'); + +view.View = class { + + constructor(host, id) { + this._host = host; + this._id = id ? ('-' + id) : ''; + this._options = { + initializers: true, + attributes: false, + names: false, + direction: 'vertical', + mousewheel: 'scroll' + }; + this._host.initialize(this).then(() => { + this._model = null; + this._graphs = []; + this._selection = []; + this._sidebar = new sidebar.Sidebar(this._host, id); + this._searchText = ''; + this._modelFactoryService = new view.ModelFactoryService(this._host); + this._getElementById('zoom-in-button').addEventListener('click', () => { + this.zoomIn(); + }); + this._getElementById('zoom-out-button').addEventListener('click', () => { + this.zoomOut(); + }); + this._getElementById('back-button').addEventListener('click', () => { + this.popGraph(); + }); + this._getElementById('name-button').addEventListener('click', () => { + this.showDocumentation(this.activeGraph); + }); + this._getElementById('sidebar').addEventListener('mousewheel', (e) => { + this._preventDefault(e); + }, { passive: true }); + this._host.document.addEventListener('keydown', () => { + this.clearSelection(); + }); + // console.log('before host start'); + this._host.start(); + // console.log('host started!'); + const container = this._getElementById('graph'); + container.addEventListener('scroll', (e) => this._scrollHandler(e)); + container.addEventListener('wheel', (e) => this._wheelHandler(e), { passive: false }); + container.addEventListener('mousedown', (e) => this._mouseDownHandler(e)); + switch (this._host.agent) { + case 'safari': + container.addEventListener('gesturestart', (e) => this._gestureStartHandler(e), false); + break; + default: + container.addEventListener('touchstart', (e) => this._touchStartHandler(e), { passive: true }); + break; + } + }).catch((err) => { + this.error(err, null, null); + }); + } + + show(page) { + if (!page) { + page = (!this._model && !this.activeGraph) ? 'welcome' : 'default'; + } + this._host.screen(page); + if (this._sidebar) { + this._sidebar.close(); + } + this._host.document.body.setAttribute('class', page); + switch (page) { + case 'default': { + const container = this._getElementById('graph'); + if (container) { + container.focus(); + } + break; + } + case 'welcome': { + const element = this._getElementById('open-file-button'); + if (element) { + element.focus(); + } + break; + } + } + this._page = page; + } + + cut() { + this._host.document.execCommand('cut'); + } + + copy() { + this._host.document.execCommand('copy'); + } + + paste() { + this._host.document.execCommand('paste'); + } + + selectAll() { + this._host.document.execCommand('selectall'); + } + + find() { + if (this._graph) { + this.clearSelection(); + const graphElement = this._getElementById('canvas'); + const view = new sidebar.FindSidebar(this._host, graphElement, this._graph); + view.on('search-text-changed', (sender, text) => { + this._searchText = text; + }); + view.on('select', (sender, selection) => { + this.select(selection); + }); + this._sidebar.open(view.content, 'Find'); + view.focus(this._searchText); + } + } + + get model() { + // console.log('model() is called'); + return this._model; + } + + get options() { + return this._options; + } + + toggle(name) { + switch (name) { + case 'names': + case 'attributes': + case 'initializers': + this._options[name] = !this._options[name]; + this._reload(); + break; + case 'direction': + this._options.direction = this._options.direction === 'vertical' ? 'horizontal' : 'vertical'; + this._reload(); + break; + case 'mousewheel': + this._options.mousewheel = this._options.mousewheel === 'scroll' ? 'zoom' : 'scroll'; + break; + } + } + + _reload() { + this.show('welcome spinner'); + if (this._model && this._graphs.length > 0) { + this._updateGraph(this._model, this._graphs).catch((error) => { + if (error) { + this.error(error, 'Graph update failed.', 'welcome'); + } + }); + } + } + + _timeout(time) { + return new Promise((resolve) => { + setTimeout(() => { resolve(); }, time); + }); + } + + _getElementById(id) { + return this._host.document.getElementById(id + this._id); + } + + zoomIn() { + this._updateZoom(this._zoom * 1.1); + } + + zoomOut() { + this._updateZoom(this._zoom * 0.9); + } + + resetZoom() { + this._updateZoom(1); + } + + _preventDefault(e) { + if (e.shiftKey || e.ctrlKey) { + e.preventDefault(); + } + } + + _updateZoom(zoom, e) { + const container = this._getElementById('graph'); + const canvas = this._getElementById('canvas'); + const limit = this._options.direction === 'vertical' ? + container.clientHeight / this._height : + container.clientWidth / this._width; + const min = Math.min(Math.max(limit, 0.15), 1); + zoom = Math.max(min, Math.min(zoom, 1.4)); + const scrollLeft = this._scrollLeft || container.scrollLeft; + const scrollTop = this._scrollTop || container.scrollTop; + const x = (e ? e.pageX : (container.clientWidth / 2)) + scrollLeft; + const y = (e ? e.pageY : (container.clientHeight / 2)) + scrollTop; + const width = zoom * this._width; + const height = zoom * this._height; + canvas.style.width = width + 'px'; + canvas.style.height = height + 'px'; + this._scrollLeft = Math.max(0, ((x * zoom) / this._zoom) - (x - scrollLeft)); + this._scrollTop = Math.max(0, ((y * zoom) / this._zoom) - (y - scrollTop)); + container.scrollLeft = this._scrollLeft; + container.scrollTop = this._scrollTop; + this._zoom = zoom; + } + + _mouseDownHandler(e) { + if (e.buttons === 1) { + const document = this._host.document.documentElement; + document.style.cursor = 'grabbing'; + const container = this._getElementById('graph'); + this._mousePosition = { + left: container.scrollLeft, + top: container.scrollTop, + x: e.clientX, + y: e.clientY + }; + e.stopImmediatePropagation(); + const mouseMoveHandler = (e) => { + e.preventDefault(); + e.stopImmediatePropagation(); + const dx = e.clientX - this._mousePosition.x; + const dy = e.clientY - this._mousePosition.y; + this._mousePosition.moved = dx * dx + dy * dy > 0; + if (this._mousePosition.moved) { + const container = this._getElementById('graph'); + container.scrollTop = this._mousePosition.top - dy; + container.scrollLeft = this._mousePosition.left - dx; + } + }; + const mouseUpHandler = () => { + document.style.cursor = null; + container.removeEventListener('mouseup', mouseUpHandler); + container.removeEventListener('mouseleave', mouseUpHandler); + container.removeEventListener('mousemove', mouseMoveHandler); + if (this._mousePosition && this._mousePosition.moved) { + e.preventDefault(); + e.stopImmediatePropagation(); + delete this._mousePosition; + document.addEventListener('click', clickHandler, true); + } + }; + const clickHandler = (e) => { + e.stopPropagation(); + document.removeEventListener('click', clickHandler, true); + }; + container.addEventListener('mousemove', mouseMoveHandler); + container.addEventListener('mouseup', mouseUpHandler); + container.addEventListener('mouseleave', mouseUpHandler); + } + } + + _touchStartHandler(e) { + if (e.touches.length === 2) { + this._touchPoints = Array.from(e.touches); + this._touchZoom = this._zoom; + } + const touchMoveHandler = (e) => { + if (Array.isArray(this._touchPoints) && this._touchPoints.length === 2 && e.touches.length === 2) { + const distance = (points) => { + const dx =(points[1].clientX - points[0].clientX); + const dy =(points[1].clientY - points[0].clientY); + return Math.sqrt(dx * dx + dy * dy); + }; + const d1 = distance(Array.from(e.touches)); + const d2 = distance(this._touchPoints); + if (d2 !== 0) { + const points = this._touchPoints; + const e = { + pageX: (points[1].pageX + points[0].pageX) / 2, + pageY: (points[1].pageY + points[0].pageY) / 2 + }; + const zoom = d2 === 0 ? d1 : d1 / d2; + this._updateZoom(this._touchZoom * zoom, e); + } + } + }; + const touchEndHandler = () => { + container.removeEventListener('touchmove', touchMoveHandler, { passive: true }); + container.removeEventListener('touchcancel', touchEndHandler, { passive: true }); + container.removeEventListener('touchend', touchEndHandler, { passive: true }); + delete this._touchPoints; + delete this._touchZoom; + }; + const container = this._getElementById('graph'); + container.addEventListener('touchmove', touchMoveHandler, { passive: true }); + container.addEventListener('touchcancel', touchEndHandler, { passive: true }); + container.addEventListener('touchend', touchEndHandler, { passive: true }); + } + + _gestureStartHandler(e) { + e.preventDefault(); + this._gestureZoom = this._zoom; + const container = this._getElementById('graph'); + const gestureChangeHandler = (e) => { + e.preventDefault(); + this._updateZoom(this._gestureZoom * e.scale, e); + }; + const gestureEndHandler = (e) => { + container.removeEventListener('gesturechange', gestureChangeHandler, false); + container.removeEventListener('gestureend', gestureEndHandler, false); + e.preventDefault(); + if (this._gestureZoom) { + this._updateZoom(this._gestureZoom * e.scale, e); + delete this._gestureZoom; + } + }; + container.addEventListener('gesturechange', gestureChangeHandler, false); + container.addEventListener('gestureend', gestureEndHandler, false); + } + + _scrollHandler(e) { + if (this._scrollLeft && e.target.scrollLeft !== Math.floor(this._scrollLeft)) { + delete this._scrollLeft; + } + if (this._scrollTop && e.target.scrollTop !== Math.floor(this._scrollTop)) { + delete this._scrollTop; + } + } + + _wheelHandler(e) { + if (e.shiftKey || e.ctrlKey || this._options.mousewheel === 'zoom') { + const delta = -e.deltaY * (e.deltaMode === 1 ? 0.05 : e.deltaMode ? 1 : 0.002) * (e.ctrlKey ? 10 : 1); + this._updateZoom(this._zoom * Math.pow(2, delta), e); + e.preventDefault(); + } + } + + select(selection) { + this.clearSelection(); + if (selection && selection.length > 0) { + const container = this._getElementById('graph'); + let x = 0; + let y = 0; + for (const element of selection) { + element.classList.add('select'); + this._selection.push(element); + const rect = element.getBoundingClientRect(); + x += rect.left + (rect.width / 2); + y += rect.top + (rect.height / 2); + } + x = x / selection.length; + y = y / selection.length; + const rect = container.getBoundingClientRect(); + const left = (container.scrollLeft + x - rect.left) - (rect.width / 2); + const top = (container.scrollTop + y - rect.top) - (rect.height / 2); + container.scrollTo({ left: left, top: top, behavior: 'smooth' }); + } + } + + clearSelection() { + while (this._selection.length > 0) { + const element = this._selection.pop(); + element.classList.remove('select'); + } + } + + error(err, name, screen) { + if (this._sidebar) { + this._sidebar.close(); + } + this._host.exception(err, false); + + const knowns = [ + { name: '', message: /^Invalid argument identifier/, url: 'https://github.com/lutzroeder/netron/issues/540' }, + { name: '', message: /^Cannot read property/, url: 'https://github.com/lutzroeder/netron/issues/647' }, + { name: '', message: /^Failed to render tensor/, url: 'https://github.com/lutzroeder/netron/issues/681' }, + { name: 'Error', message: /^EPERM: operation not permitted/, url: 'https://github.com/lutzroeder/netron/issues/551' }, + { name: 'Error', message: /^EACCES: permission denied/, url: 'https://github.com/lutzroeder/netron/issues/504' }, + { name: 'RangeError', message: /^Offset is outside the bounds of the DataView/, url: 'https://github.com/lutzroeder/netron/issues/563' }, + { name: 'RangeError', message: /^start offset of Int32Array/, url: 'https://github.com/lutzroeder/netron/issues/565' }, + { name: 'RangeError', message: /^Maximum call stack size exceeded/, url: 'https://github.com/lutzroeder/netron/issues/589' }, + { name: 'RangeError', message: /^Invalid string length/, url: 'https://github.com/lutzroeder/netron/issues/648' }, + { name: 'Error loading model.', message: /^Unsupported file content \(/, url: 'https://github.com/lutzroeder/netron/issues/550' }, + { name: 'Error loading model.', message: /^Unsupported Protocol Buffers content/, url: 'https://github.com/lutzroeder/netron/issues/593' }, + { name: 'Error loading model.', message: /^Unsupported Protocol Buffers text content/, url: 'https://github.com/lutzroeder/netron/issues/594' }, + { name: 'Error loading model.', message: /^Unsupported JSON content/, url: 'https://github.com/lutzroeder/netron/issues/595' }, + { name: 'Error loading Caffe model.', message: /^File format is not caffe\.NetParameter/, url: 'https://github.com/lutzroeder/netron/issues/563' }, + { name: 'Error loading Darknet model.', message: /^Invalid tensor shape/, url: 'https://github.com/lutzroeder/netron/issues/541' }, + { name: 'Error loading Keras model.', message: /^Unsupported data object header version/, url: 'https://github.com/lutzroeder/netron/issues/548' }, + { name: 'Error loading MNN model.', message: /^File format is not mnn\.Net/, url: 'https://github.com/lutzroeder/netron/issues/746' }, + { name: 'Error loading PyTorch model.', message: /^File does not contain root module or state dictionary/, url: 'https://github.com/lutzroeder/netron/issues/543' }, + { name: 'Error loading PyTorch model.', message: /^Module does not contain modules/, url: 'https://github.com/lutzroeder/netron/issues/544' }, + { name: 'Error loading PyTorch model.', message: /^Failed to resolve module/, url: 'https://github.com/lutzroeder/netron/issues/545' }, + { name: 'Error loading PyTorch model.', message: /^Unsupported function/, url: 'https://github.com/lutzroeder/netron/issues/546' }, + { name: 'Error loading PyTorch model.', message: /^Unsupported uninitialized argument/, url: 'https://github.com/lutzroeder/netron/issues/547' }, + { name: 'Error loading ONNX model.', message: /^File format is not onnx\.ModelProto/, url: 'https://github.com/lutzroeder/netron/issues/549' }, + { name: 'Error loading TensorFlow model.', message: /^File text format is not TensorFlow\.js graph-model/, url: 'https://github.com/lutzroeder/netron/issues/764' }, + { name: 'Error loading TensorFlow Lite model.', message: /^Offset is outside the bounds of the DataView/, url: 'https://github.com/lutzroeder/netron/issues/563' }, + { name: 'Error loading UFF model.', message: /^Unknown attribute/, url: 'https://github.com/lutzroeder/netron/issues/649' } + ]; + const known = knowns.find((known) => (known.name.length === 0 || known.name === err.name) && err.message.match(known.message)); + const message = err.message + (known ? '\n\nPlease provide information about this issue at ' + known.url + '.' : ''); + name = name || err.name; + this._host.error(name, message); + this.show(screen !== undefined ? screen : 'welcome'); + if (known) { + this._host.openURL(known.url); + } + } + + accept(file) { + return this._modelFactoryService.accept(file); + } + + open(context) { + // console.log("view open()"); + // console.log(context); + this._host.event('Model', 'Open', 'Size', context.stream ? context.stream.length : 0); + this._sidebar.close(); + return this._timeout(2).then(() => { + return this._modelFactoryService.open(context).then((model) => { + // console.log(model); + const format = []; + if (model.format) { + format.push(model.format); + } + if (model.producer) { + format.push('(' + model.producer + ')'); + } + if (format.length > 0) { + this._host.event('Model', 'Format', format.join(' ')); + } + return this._timeout(20).then(() => { + const graphs = Array.isArray(model.graphs) && model.graphs.length > 0 ? [ model.graphs[0] ] : []; + return this._updateGraph(model, graphs); + }); + }); + }); + } + + _updateActiveGraph(graph) { + this._sidebar.close(); + if (this._model) { + // console.log('_updateActiveGraph() is called'); + const model = this._model; + this.show('welcome spinner'); + this._timeout(200).then(() => { + return this._updateGraph(model, [ graph ]).catch((error) => { + if (error) { + this.error(error, 'Graph update failed.', 'welcome'); + } + }); + }); + } + } + + get activeGraph() { + return Array.isArray(this._graphs) && this._graphs.length > 0 ? this._graphs[0] : null; + } + + _updateGraph(model, graphs) { + // console.log(model); + // console.log(graphs); + const lastModel = this._model; + const lastGraphs = this._graphs; + this._model = model; + this._graphs = graphs; + const graph = this.activeGraph; + // console.log("_updateGraph is called"); + return this._timeout(100).then(() => { + if (graph && graph != lastGraphs[0]) { + const nodes = graph.nodes; + // console.log(nodes); + if (nodes.length > 2048) { + if (!this._host.confirm('Large model detected.', 'This graph contains a large number of nodes and might take a long time to render. Do you want to continue?')) { + this._host.event('Graph', 'Render', 'Skip', nodes.length); + this.show(null); + return null; + } + } + } + const update = () => { + const nameButton = this._getElementById('name-button'); + const backButton = this._getElementById('back-button'); + if (this._graphs.length > 1) { + const graph = this.activeGraph; + nameButton.innerHTML = graph ? graph.name : ''; + backButton.style.opacity = 1; + nameButton.style.opacity = 1; + } + else { + backButton.style.opacity = 0; + nameButton.style.opacity = 0; + } + }; + return this.renderGraph(this._model, this.activeGraph).then(() => { + if (this._page !== 'default') { + this.show('default'); + } + update(); + return this._model; + }).catch((error) => { + this._model = lastModel; + this._graphs = lastGraphs; + return this.renderGraph(this._model, this.activeGraph).then(() => { + if (this._page !== 'default') { + this.show('default'); + } + update(); + throw error; + }); + }); + }); + } + + pushGraph(graph) { + if (graph !== this.activeGraph) { + this._sidebar.close(); + this._updateGraph(this._model, [ graph ].concat(this._graphs)); + } + } + + popGraph() { + if (this._graphs.length > 1) { + this._sidebar.close(); + return this._updateGraph(this._model, this._graphs.slice(1)); + } + } + + renderGraph(model, graph) { + try { + this._graph = null; + + const canvas = this._getElementById('canvas'); + while (canvas.lastChild) { + canvas.removeChild(canvas.lastChild); + } + if (!graph) { + return Promise.resolve(); + } + else { + this._zoom = 1; + + const groups = graph.groups; + // console.log(groups) // undefined + const nodes = graph.nodes; + this._host.event('Graph', 'Render', 'Size', nodes.length); + + const options = {}; + options.nodesep = 20; + options.ranksep = 20; + const rotate = graph.nodes.every((node) => node.inputs.filter((input) => input.arguments.every((argument) => !argument.initializer)).length === 0 && node.outputs.length === 0); + const horizontal = rotate ? this._options.direction === 'vertical' : this._options.direction !== 'vertical'; + if (horizontal) { + options.rankdir = "LR"; + } + if (nodes.length > 3000) { + options.ranker = 'longest-path'; + } + + const viewGraph = new view.Graph(this, model, groups, options); + // console.log(viewGraph) + viewGraph.add(graph); + // console.log(viewGraph._arguments) + + // Workaround for Safari background drag/zoom issue: + // https://stackoverflow.com/questions/40887193/d3-js-zoom-is-not-working-with-mousewheel-in-safari + const background = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'rect'); + background.setAttribute('id', 'background'); + background.setAttribute('fill', 'none'); + background.setAttribute('pointer-events', 'all'); + canvas.appendChild(background); + + const origin = this._host.document.createElementNS('http://www.w3.org/2000/svg', 'g'); + origin.setAttribute('id', 'origin'); + canvas.appendChild(origin); + + viewGraph.build(this._host.document, origin); + + this._zoom = 1; + + return this._timeout(20).then(() => { + + viewGraph.update(); + + + // 让画面可拖动/缩放 =======> + const elements = Array.from(canvas.getElementsByClassName('graph-input') || []); + if (elements.length === 0) { + const nodeElements = Array.from(canvas.getElementsByClassName('graph-node') || []); + if (nodeElements.length > 0) { + elements.push(nodeElements[0]); + } + } + + const size = canvas.getBBox(); + const margin = 100; + const width = Math.ceil(margin + size.width + margin); + const height = Math.ceil(margin + size.height + margin); + origin.setAttribute('transform', 'translate(' + margin.toString() + ', ' + margin.toString() + ') scale(1)'); + background.setAttribute('width', width); + background.setAttribute('height', height); + this._width = width; + this._height = height; + delete this._scrollLeft; + delete this._scrollRight; + canvas.setAttribute('viewBox', '0 0 ' + width + ' ' + height); + canvas.setAttribute('width', width); + canvas.setAttribute('height', height); + + this._zoom = 1; + this._updateZoom(this._zoom); + + const container = this._getElementById('graph'); + if (elements && elements.length > 0) { + // Center view based on input elements + const xs = []; + const ys = []; + for (let i = 0; i < elements.length; i++) { + const element = elements[i]; + const rect = element.getBoundingClientRect(); + xs.push(rect.left + (rect.width / 2)); + ys.push(rect.top + (rect.height / 2)); + } + let x = xs[0]; + const y = ys[0]; + if (ys.every(y => y === ys[0])) { + x = xs.reduce((a, b) => a + b, 0) / xs.length; + } + const graphRect = container.getBoundingClientRect(); + const left = (container.scrollLeft + x - graphRect.left) - (graphRect.width / 2); + const top = (container.scrollTop + y - graphRect.top) - (graphRect.height / 2); + container.scrollTo({ left: left, top: top, behavior: 'auto' }); + } + else { + const canvasRect = canvas.getBoundingClientRect(); + const graphRect = container.getBoundingClientRect(); + const left = (container.scrollLeft + (canvasRect.width / 2) - graphRect.left) - (graphRect.width / 2); + const top = (container.scrollTop + (canvasRect.height / 2) - graphRect.top) - (graphRect.height / 2); + container.scrollTo({ left: left, top: top, behavior: 'auto' }); + } + + // <======= 让画面可拖动/缩放 + + + this._graph = viewGraph; + return; + }); + } + } + catch (error) { + return Promise.reject(error); + } + } + + applyStyleSheet(element, name) { + let rules = []; + for (const styleSheet of this._host.document.styleSheets) { + if (styleSheet && styleSheet.href && styleSheet.href.endsWith('/' + name)) { + rules = styleSheet.cssRules; + break; + } + } + const nodes = element.getElementsByTagName('*'); + for (const node of nodes) { + for (const rule of rules) { + if (node.matches(rule.selectorText)) { + for (const item of rule.style) { + node.style[item] = rule.style[item]; + } + } + } + } + } + + export(file) { + const lastIndex = file.lastIndexOf('.'); + const extension = (lastIndex != -1) ? file.substring(lastIndex + 1) : ''; + if (this.activeGraph && (extension === 'png' || extension === 'svg')) { + const canvas = this._getElementById('canvas'); + const clone = canvas.cloneNode(true); + this.applyStyleSheet(clone, 'view-grapher.css'); + clone.setAttribute('id', 'export'); + clone.removeAttribute('viewBox'); + clone.removeAttribute('width'); + clone.removeAttribute('height'); + clone.style.removeProperty('opacity'); + clone.style.removeProperty('display'); + const background = clone.querySelector('#background'); + const origin = clone.querySelector('#origin'); + origin.setAttribute('transform', 'translate(0,0) scale(1)'); + background.removeAttribute('width'); + background.removeAttribute('height'); + + const parent = canvas.parentElement; + parent.insertBefore(clone, canvas); + const size = clone.getBBox(); + parent.removeChild(clone); + parent.removeChild(canvas); + parent.appendChild(canvas); + const delta = (Math.min(size.width, size.height) / 2.0) * 0.1; + const width = Math.ceil(delta + size.width + delta); + const height = Math.ceil(delta + size.height + delta); + origin.setAttribute('transform', 'translate(' + (delta - size.x).toString() + ', ' + (delta - size.y).toString() + ') scale(1)'); + clone.setAttribute('width', width); + clone.setAttribute('height', height); + background.setAttribute('width', width); + background.setAttribute('height', height); + background.setAttribute('fill', '#fff'); + + const data = new XMLSerializer().serializeToString(clone); + + if (extension === 'svg') { + const blob = new Blob([ data ], { type: 'image/svg' }); + this._host.export(file, blob); + } + + if (extension === 'png') { + const image = new Image(); + image.onload = () => { + const max = Math.max(width, height); + const scale = Math.min(24000.0 / max, 2.0); + const canvas = this._host.document.createElement('canvas'); + canvas.width = Math.ceil(width * scale); + canvas.height = Math.ceil(height * scale); + const context = canvas.getContext('2d'); + context.scale(scale, scale); + context.drawImage(image, 0, 0); + canvas.toBlob((blob) => { + if (blob) { + this._host.export(file, blob); + } + else { + const err = new Error(); + err.name = 'Error exporting image.'; + err.message = 'Image may be too large to render as PNG.'; + this._host.exception(err, false); + this._host.error(err.name, err.message); + } + }, 'image/png'); + }; + image.src = 'data:image/svg+xml;base64,' + this._host.window.btoa(unescape(encodeURIComponent(data))); + } + } + } + + showModelProperties() { + if (this._model) { + try { + const modelSidebar = new sidebar.ModelSidebar(this._host, this._model, this.activeGraph); + modelSidebar.on('update-active-graph', (sender, graph) => { + this._updateActiveGraph(graph); + }); + const content = modelSidebar.render(); + this._sidebar.open(content, 'Model Properties'); + } + catch (error) { + const content = " in '" + this._model.identifier + "'."; + if (error && !error.message.endsWith(content) && (error.context === undefined || error.context === true)) { + error.message = error.message.replace(/\.$/, '') + content; + } + this.error(error, 'Error showing model properties.', null); + } + } + } + + showNodeProperties(node, input) { + if (node) { + try { + // console.log(node) // 注意是onnx.Node, 不是grapher.Node,所以没有update(), 没有element元素 + // console.log(node.element) // undefined + // node.update() + const nodeSidebar = new sidebar.NodeSidebar(this._host, node); + nodeSidebar.on('show-documentation', (/* sender, e */) => { + this.showDocumentation(node.type); + }); + nodeSidebar.on('show-graph', (sender, graph) => { + this.pushGraph(graph); + }); + nodeSidebar.on('export-tensor', (sender, tensor) => { + const defaultPath = tensor.name ? tensor.name.split('/').join('_').split(':').join('_').split('.').join('_') : 'tensor'; + this._host.save('NumPy Array', 'npy', defaultPath, (file) => { + try { + let data_type = tensor.type.dataType; + switch (data_type) { + case 'boolean': data_type = 'bool'; break; + } + const execution = new python.Execution(null); + const bytes = execution.invoke('io.BytesIO', []); + const dtype = execution.invoke('numpy.dtype', [ data_type ]); + const array = execution.invoke('numpy.asarray', [ tensor.value, dtype ]); + execution.invoke('numpy.save', [ bytes, array ]); + bytes.seek(0); + const blob = new Blob([ bytes.read() ], { type: 'application/octet-stream' }); + this._host.export(file, blob); + } + catch (error) { + this.error(error, 'Error saving NumPy tensor.', null); + } + }); + }); + nodeSidebar.on('error', (sender, error) => { + if (this._model) { + error.message = error.message.replace(/\.$/, '') + " in '" + this._model.identifier + "'."; + } + this.error(error, null, null); + }); + if (input) { + nodeSidebar.toggleInput(input.name); + } + this._sidebar.open(nodeSidebar.render(), 'Node Properties'); + } + catch (error) { + const content = " in '" + this._model.identifier + "'."; + if (error && !error.message.endsWith(content) && (error.context === undefined || error.context === true)) { + error.message = error.message.replace(/\.$/, '') + content; + } + this.error(error, 'Error showing node properties.', null); + } + } + } + + showDocumentation(type) { + if (type && (type.description || type.inputs || type.outputs || type.attributes)) { + if (type.nodes && type.nodes.length > 0) { + this.pushGraph(type); + } + const documentationSidebar = new sidebar.DocumentationSidebar(this._host, type); + documentationSidebar.on('navigate', (sender, e) => { + this._host.openURL(e.link); + }); + const title = type.type === 'function' ? 'Function' : 'Documentation'; + this._sidebar.push(documentationSidebar.render(), title); + } + } +}; + +view.Graph = class extends grapher.Graph { + + constructor(view, model, compound, options) { + super(compound, options); + this.view = view; + this.model = model; + this._arguments = new Map(); + this._nodeKey = 0; + } + + createNode(node) { + const value = new view.Node(this, node); + value.name = (this._nodeKey++).toString(); + // value.name = node.name; + this.setNode(value); + return value; + } + + createInput(input) { + const value = new view.Input(this, input); + value.name = (this._nodeKey++).toString(); + this.setNode(value); + return value; + } + + createOutput(output) { + const value = new view.Output(this, output); + value.name = (this._nodeKey++).toString(); + this.setNode(value); + return value; + } + + createArgument(argument) { + const name = argument.name; + if (!this._arguments.has(name)) { + this._arguments.set(name, new view.Argument(this, argument)); + } + return this._arguments.get(name); + } + + createEdge(from, to) { + const value = new view.Edge(from, to); + return value; + } + + add(graph) { + const clusters = new Set(); + const clusterParentMap = new Map(); + const groups = graph.groups; + // console.log(groups) // undefined + if (groups) { + for (const node of graph.nodes) { + if (node.group) { + const path = node.group.split('/'); + while (path.length > 0) { + const name = path.join('/'); + path.pop(); + clusterParentMap.set(name, path.join('/')); + } + } + } + } + + // console.log(graph) + for (const input of graph.inputs) { + const viewInput = this.createInput(input); + for (const argument of input.arguments) { + this.createArgument(argument).from(viewInput); + } + } + + for (const node of graph.nodes) { + // console.log(node) + + const viewNode = this.createNode(node); + + const inputs = node.inputs; + for (const input of inputs) { + for (const argument of input.arguments) { + if (argument.name != '' && !argument.initializer) { + this.createArgument(argument).to(viewNode); + } + } + } + let outputs = node.outputs; + if (node.chain && node.chain.length > 0) { + const chainOutputs = node.chain[node.chain.length - 1].outputs; + if (chainOutputs.length > 0) { + outputs = chainOutputs; + } + } + for (const output of outputs) { + for (const argument of output.arguments) { + if (!argument) { + throw new view.Error("Invalid null argument in '" + this.model.identifier + "'."); + } + if (argument.name != '') { + this.createArgument(argument).from(viewNode); + } + } + } + + if (node.controlDependencies && node.controlDependencies.length > 0) { + for (const argument of node.controlDependencies) { + this.createArgument(argument).to(viewNode, true); + } + } + + const createCluster = (name) => { + if (!clusters.has(name)) { + this.setNode({ name: name, rx: 5, ry: 5}); + clusters.add(name); + const parent = clusterParentMap.get(name); + if (parent) { + createCluster(parent); + this.setParent(name, parent); + } + } + }; + + if (groups) { + let groupName = node.group; + if (groupName && groupName.length > 0) { + if (!clusterParentMap.has(groupName)) { + const lastIndex = groupName.lastIndexOf('/'); + if (lastIndex != -1) { + groupName = groupName.substring(0, lastIndex); + if (!clusterParentMap.has(groupName)) { + groupName = null; + } + } + else { + groupName = null; + } + } + if (groupName) { + createCluster(groupName); + this.setParent(viewNode.name, groupName); + } + } + } + } + + for (const output of graph.outputs) { + const viewOutput = this.createOutput(output); + for (const argument of output.arguments) { + this.createArgument(argument).to(viewOutput); + } + } + } + + build(document, origin) { + for (const argument of this._arguments.values()) { + argument.build(); + } + super.build(document, origin); + } +}; + +view.Node = class extends grapher.Node { + + // 这里的value是一个onnx.Node,这里正在构建的是view.Node + // context 是指Graph + constructor(context, value) { + super(); + this.context = context; + this.value = value; + view.Node.counter = view.Node.counter || 0; + this.id = 'node-' + (value.name ? 'name-' + value.name : 'id-' + (view.Node.counter++).toString()); + this._add(this.value); + } + + get class() { + return 'graph-node'; + } + + get inputs() { + return this.value.inputs; + } + + get outputs() { + return this.value.outputs; + } + + _add(node) { + + // header + const header = this.header(); + const styles = [ 'node-item-type' ]; + const type = node.type; + const category = type && type.category ? type.category : ''; + if (category) { + styles.push('node-item-type-' + category.toLowerCase()); + } + if (typeof type.name !== 'string' || !type.name.split) { // #416 + const identifier = this.context.model && this.context.model.identifier ? this.context.model.identifier : '?'; + throw new view.Error("Unknown node type '" + JSON.stringify(type.name) + "' in '" + identifier + "'."); + } + const content = this.context.view.options.names && (node.name || node.location) ? (node.name || node.location) : type.name.split('.').pop(); + const tooltip = this.context.view.options.names && (node.name || node.location) ? type.name : (node.name || node.location); + const title = header.add(null, styles, content, tooltip); + title.on('click', () => this.context.view.showNodeProperties(node, null)); + if (node.type.nodes && node.type.nodes.length > 0) { + const definition = header.add(null, styles, '\u0192', 'Show Function Definition'); + definition.on('click', () => this.context.view.pushGraph(node.type)); + } + if (node.nodes) { + // this._expand = header.add(null, styles, '+', null); + // this._expand.on('click', () => this.toggle()); + } + + // 节点在图上的显示信息 + const initializers = []; + let hiddenInitializers = false; + if (this.context.view.options.initializers) { + for (const input of node.inputs) { + if (input.visible && input.arguments.length === 1 && input.arguments[0].initializer != null) { + initializers.push(input); + } + if ((!input.visible || input.arguments.length > 1) && + input.arguments.some((argument) => argument.initializer != null)) { + hiddenInitializers = true; + } + } + } + let sortedAttributes = []; + const attributes = node.attributes || []; + if (this.context.view.options.attributes) { + sortedAttributes = attributes.filter((attribute) => attribute.visible).slice(); + } + sortedAttributes.sort((a, b) => { + const au = a.name.toUpperCase(); + const bu = b.name.toUpperCase(); + return (au < bu) ? -1 : (au > bu) ? 1 : 0; + }); + if (initializers.length > 0 || hiddenInitializers || sortedAttributes.length > 0) { + const list = this.list(); + list.on('click', () => this.context.view.showNodeProperties(node)); + for (const initializer of initializers) { + const argument = initializer.arguments[0]; + const type = argument.type; + let shape = ''; + let separator = ''; + if (type && type.shape && type.shape.dimensions && Array.isArray(type.shape.dimensions)) { + shape = '\u3008' + type.shape.dimensions.map((d) => d ? d : '?').join('\u00D7') + '\u3009'; + if (type.shape.dimensions.length === 0 && argument.initializer && !argument.initializer.state) { + try { + shape = argument.initializer.toString(); + if (shape && shape.length > 10) { + shape = shape.substring(0, 10) + '\u2026'; + } + separator = ' = '; + } + catch (err) { + let type = '?'; + try { + type = argument.initializer.type.toString(); + } + catch (error) { + // continue regardless of error + } + const identifier = this.context.view.model && this.context.view.model.identifier ? this.context.view.model.identifier : '?'; + throw new view.Error("Failed to render tensor of type '" + type + "' in '" + identifier + "' (" + err.message + ")."); + } + } + } + list.add(argument.name ? 'initializer-' + argument.name : '', initializer.name, shape, type ? type.toString() : '', separator); + } + if (hiddenInitializers) { + list.add(null, '\u3008' + '\u2026' + '\u3009', '', null, ''); + } + + // 节点属性(侧边栏显示) + for (const attribute of sortedAttributes) { + if (attribute.visible) { + let value = sidebar.NodeSidebar.formatAttributeValue(attribute.value, attribute.type); + if (value && value.length > 25) { + value = value.substring(0, 25) + '\u2026'; + } + list.add(null, attribute.name, value, attribute.type, ' = '); + } + } + } + if (Array.isArray(node.chain) && node.chain.length > 0) { + for (const innerNode of node.chain) { + this._add(innerNode); + } + } + if (node.inner) { + this._add(node.inner); + } + if (node.nodes) { + this.canvas = this.canvas(); + } + } + + toggle() { + this._expand.content = '-'; + this._graph = new view.Graph(this.context.view, this.context.model, false, {}); + this._graph.add(this.value); + // const document = this.element.ownerDocument; + // const parent = this.element.parentElement; + // this._graph.build(document, parent); + // this._graph.update(); + this.canvas.width = 300; + this.canvas.height = 300; + this.layout(); + this.context.update(); + } +}; + +view.Input = class extends grapher.Node { + + constructor(context, value) { + super(); + this.context = context; + this.value = value; + view.Input.counter = view.Input.counter || 0; + const types = value.arguments.map((argument) => argument.type || '').join('\n'); + let name = value.name || ''; + if (name.length > 16) { + name = name.split('/').pop(); + } + const header = this.header(); + const title = header.add(null, [ 'graph-item-input' ], name, types); + title.on('click', () => this.context.view.showModelProperties()); + this.id = 'input-' + (name ? 'name-' + name : 'id-' + (view.Input.counter++).toString()); + } + + get class() { + return 'graph-input'; + } + + get inputs() { + return []; + } + + get outputs() { + return [ this.value ]; + } +}; + +view.Output = class extends grapher.Node { + + constructor(context, value) { + super(); + this.context = context; + this.value = value; + const types = value.arguments.map((argument) => argument.type || '').join('\n'); + let name = value.name || ''; + if (name.length > 16) { + name = name.split('/').pop(); + } + const header = this.header(); + const title = header.add(null, [ 'graph-item-output' ], name, types); + title.on('click', () => this.context.view.showModelProperties()); + } + + get inputs() { + return [ this.value ]; + } + + get outputs() { + return []; + } +}; + +view.Argument = class { + + constructor(context, argument) { + this.context = context; + this._argument = argument; + } + + from(node) { + this._from = node; + } + + to(node, controlDependency) { + this._to = this._to || []; + if (controlDependency) { + this._controlDependencies = this._controlDependencies || new Set(); + this._controlDependencies.add(this._to.length); + } + this._to.push(node); + } + + build() { + this._edges = this._edges || []; + if (this._from && this._to) { + for (let i = 0; i < this._to.length; i++) { + const to = this._to[i]; + let content = ''; + const type = this._argument.type; + + if (type && + type.shape && + type.shape.dimensions && + type.shape.dimensions.length > 0 && + type.shape.dimensions.every((dim) => !dim || Number.isInteger(dim) || dim instanceof base.Int64 || (typeof dim === 'string'))) { + content = type.shape.dimensions.map((dim) => dim || '?').join('\u00D7'); + content = content.length > 16 ? '' : content; + } + if (this.context.view.options.names) { + content = this._argument.name.split('\n').shift(); // custom argument id + } + const edge = this.context.createEdge(this._from, to); + edge.v = this._from.name; + edge.w = to.name; + if (content) { + edge.label = content; + } + edge.id = 'edge-' + this._argument.name; + if (this._controlDependencies && this._controlDependencies.has(i)) { + edge.class = 'edge-path-control-dependency'; + } + this.context.setEdge(edge); + this._edges.push(edge); + } + } + } +}; + +view.Edge = class extends grapher.Edge { + + constructor(from, to) { + super(from, to); + } + + get minlen() { + if (this.from.inputs.every((parameter) => parameter.arguments.every((argument) => argument.initializer))) { + return 2; + } + return 1; + } +}; + +view.ModelContext = class { + + constructor(context, formats) { + this._context = context; + this._tags = new Map(); + this._content = new Map(); + this._formats = formats || new Map(); + } + + get identifier() { + return this._context.identifier; + } + + get stream() { + return this._context.stream; + } + + request(file, encoding, base) { + return this._context.request(file, encoding, base); + } + + require(id) { + return this._context.require(id); + } + + exception(error, fatal) { + this._context.exception(error, fatal); + } + + entries(format) { + return this._formats.get(format) || new Map(); + } + + open(type) { + if (!this._content.has(type)) { + this._content.set(type, undefined); + const stream = this.stream; + const position = stream.position; + const signatures = [ + [ 0x89, 0x48, 0x44, 0x46, 0x0D, 0x0A, 0x1A, 0x0A ], // HDF5 + [ 0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19 ] // PyTorch + ]; + const skip = + signatures.some((signature) => signature.length <= stream.length && stream.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value)) || + Array.from(this._tags).some((pair) => pair[0] !== 'flatbuffers' && pair[1].size > 0) || + Array.from(this._content.values()).some((obj) => obj !== undefined); + if (!skip) { + switch (type) { + case 'json': { + try { + const reader = json.TextReader.open(this.stream); + if (reader) { + const obj = reader.read(); + this._content.set(type, obj); + } + } + catch (err) { + // continue regardless of error + } + break; + } + case 'json.gz': { + try { + const archive = gzip.Archive.open(this.stream); + if (archive) { + const entries = archive.entries; + if (entries.size === 1) { + const stream = entries.values().next().value; + const reader = json.TextReader.open(stream); + if (reader) { + const obj = reader.read(); + this._content.set(type, obj); + } + } + } + } + catch (err) { + // continue regardless of error + } + break; + } + case 'pkl': { + let unpickler = null; + try { + if (stream.length > 2) { + const zlib = (stream) => { + const buffer = stream.peek(2); + if (buffer[0] === 0x78) { + const check = (buffer[0] << 8) + buffer[1]; + if (check % 31 === 0) { + const archive = zip.Archive.open(stream); + return archive.entries.get(''); + } + } + return stream; + }; + unpickler = python.Unpickler.open(zlib(stream)); + } + } + catch (err) { + // continue regardless of error + } + if (unpickler) { + const execution = new python.Execution(null, (error, fatal) => { + const message = error && error.message ? error.message : error.toString(); + this.exception(new view.Error(message.replace(/\.$/, '') + " in '" + this.identifier + "'."), fatal); + }); + const persistent_load = (saved_id) => { + return saved_id; + }; + const obj = unpickler.load((name, args) => execution.invoke(name, args), persistent_load); + this._content.set(type, obj); + } + break; + } + } + } + if (stream.position !== position) { + stream.seek(0); + } + } + return this._content.get(type); + } + + tags(type) { + if (!this._tags.has(type)) { + let tags = new Map(); + const stream = this.stream; + const position = stream.position; + if (stream) { + const signatures = [ + [ 0x89, 0x48, 0x44, 0x46, 0x0D, 0x0A, 0x1A, 0x0A ], // HDF5 + [ 0x80, undefined, 0x8a, 0x0a, 0x6c, 0xfc, 0x9c, 0x46, 0xf9, 0x20, 0x6a, 0xa8, 0x50, 0x19 ], // PyTorch + [ 0x50, 0x4b ], // Zip + [ 0x1f, 0x8b ] // Gzip + ]; + const skip = + signatures.some((signature) => signature.length <= stream.length && stream.peek(signature.length).every((value, index) => signature[index] === undefined || signature[index] === value)) || + (Array.from(this._tags).some((pair) => pair[0] !== 'flatbuffers' && pair[1].size > 0) && type !== 'pb+') || + Array.from(this._content.values()).some((obj) => obj !== undefined); + if (!skip) { + try { + switch (type) { + case 'pbtxt': { + const reader = protobuf.TextReader.open(stream); + tags = reader ? reader.signature() : tags; + break; + } + case 'pb': { + const reader = protobuf.BinaryReader.open(stream); + tags = reader.signature(); + break; + } + case 'pb+': { + const reader = protobuf.BinaryReader.open(stream); + tags = reader.decode(); + break; + } + case 'flatbuffers': { + if (stream.length >= 8) { + const buffer = stream.peek(Math.min(32, stream.length)); + const reader = flatbuffers.BinaryReader.open(buffer); + const identifier = reader.identifier; + if (identifier.length > 0) { + tags.set('file_identifier', identifier); + } + } + break; + } + case 'xml': { + const reader = xml.TextReader.open(stream); + if (reader) { + const document = reader.peek(); + const element = document.documentElement; + const namespaceURI = element.namespaceURI; + const localName = element.localName; + const name = namespaceURI ? namespaceURI + ':' + localName : localName; + tags.set(name, element); + } + break; + } + } + } + catch (error) { + tags.clear(); + } + } + } + if (stream.position !== position) { + stream.seek(position); + } + this._tags.set(type, tags); + } + return this._tags.get(type); + } +}; + +view.ArchiveContext = class { + + constructor(host, entries, rootFolder, identifier, stream) { + this._host = host; + this._entries = new Map(); + if (entries) { + for (const entry of entries) { + if (entry[0].startsWith(rootFolder)) { + const name = entry[0].substring(rootFolder.length); + this._entries.set(name, entry[1]); + } + } + } + this._identifier = identifier.substring(rootFolder.length); + this._stream = stream; + } + + get identifier() { + return this._identifier; + } + + get stream() { + return this._stream; + } + + request(file, encoding, base) { + if (base === undefined) { + const stream = this._entries.get(file); + if (!stream) { + return Promise.reject(new Error('File not found.')); + } + if (encoding) { + const decoder = new TextDecoder(encoding); + const buffer = stream.peek(); + const value = decoder.decode(buffer); + return Promise.resolve(value); + } + return Promise.resolve(stream); + } + return this._host.request(file, encoding, base); + } + + require(id) { + return this._host.require(id); + } + + exception(error, fatal) { + this._host.exception(error, fatal); + } +}; + +view.ArchiveError = class extends Error { + + constructor(message) { + super(message); + this.name = 'Error loading archive.'; + } +}; + +view.ModelFactoryService = class { + + constructor(host) { + this._host = host; + this._extensions = new Set([ '.zip', '.tar', '.tar.gz', '.tgz', '.gz' ]); + this._factories = []; + this.register('./pytorch', [ '.pt', '.pth', '.ptl', '.pt1', '.pyt', '.pyth', '.pkl', '.pickle', '.h5', '.t7', '.model', '.dms', '.tar', '.ckpt', '.chkpt', '.tckpt', '.bin', '.pb', '.zip', '.nn', '.torchmodel', '.torchscript', '.pytorch', '.ot', '.params', '.trt' ], [ '.model' ]); + this.register('./onnx', [ '.onnx', '.onn', '.pb', '.onnxtxt', '.pbtxt', '.prototxt', '.txt', '.model', '.pt', '.pth', '.pkl', '.ort', '.ort.onnx' ]); + this.register('./mxnet', [ '.json', '.params' ], [ '.mar'] ); + this.register('./coreml', [ '.mlmodel', '.bin', 'manifest.json', 'metadata.json', 'featuredescriptions.json', '.pb' ], [ '.mlpackage' ]); + this.register('./caffe', [ '.caffemodel', '.pbtxt', '.prototxt', '.pt', '.txt' ]); + this.register('./caffe2', [ '.pb', '.pbtxt', '.prototxt' ]); + this.register('./torch', [ '.t7', '.net' ]); + this.register('./tflite', [ '.tflite', '.lite', '.tfl', '.bin', '.pb', '.tmfile', '.h5', '.model', '.json', '.txt' ]); + this.register('./circle', [ '.circle' ]); + this.register('./tf', [ '.pb', '.meta', '.pbtxt', '.prototxt', '.txt', '.pt', '.json', '.index', '.ckpt', '.graphdef', '.pbmm', /.data-[0-9][0-9][0-9][0-9][0-9]-of-[0-9][0-9][0-9][0-9][0-9]$/, /^events.out.tfevents./ ], [ '.zip' ]); + this.register('./mediapipe', [ '.pbtxt' ]); + this.register('./uff', [ '.uff', '.pb', '.pbtxt', '.uff.txt', '.trt', '.engine' ]); + this.register('./tensorrt', [ '.trt', '.engine', '.model', '.txt', '.uff', '.pb', '.tmfile', '.onnx', '.pth', '.dnn', '.plan' ]); + this.register('./numpy', [ '.npz', '.npy', '.pkl', '.pickle' ]); + this.register('./lasagne', [ '.pkl', '.pickle', '.joblib', '.model', '.pkl.z', '.joblib.z' ]); + this.register('./lightgbm', [ '.txt', '.pkl', '.model' ]); + this.register('./keras', [ '.h5', '.hd5', '.hdf5', '.keras', '.json', '.cfg', '.model', '.pb', '.pth', '.weights', '.pkl', '.lite', '.tflite', '.ckpt' ], [ '.zip' ]); + this.register('./sklearn', [ '.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z' ]); + this.register('./pickle', [ '.pkl', '.pickle', '.joblib', '.model', '.meta', '.pb', '.pt', '.h5', '.pkl.z', '.joblib.z' ]); + this.register('./cntk', [ '.model', '.cntk', '.cmf', '.dnn' ]); + this.register('./paddle', [ '.pdmodel', '.pdparams', '.pdiparams', '.paddle', '__model__', '.__model__', '.pbtxt', '.txt', '.tar', '.tar.gz', '.nb' ]); + this.register('./bigdl', [ '.model', '.bigdl' ]); + this.register('./darknet', [ '.cfg', '.model', '.txt', '.weights' ]); + this.register('./weka', [ '.model' ]); + this.register('./rknn', [ '.rknn', '.onnx' ]); + this.register('./dlc', [ '.dlc' ]); + this.register('./armnn', [ '.armnn', '.json' ]); + this.register('./mnn', ['.mnn']); + this.register('./ncnn', [ '.param', '.bin', '.cfg.ncnn', '.weights.ncnn', '.ncnnmodel' ]); + this.register('./tnn', [ '.tnnproto', '.tnnmodel' ]); + this.register('./tengine', ['.tmfile']); + this.register('./mslite', [ '.ms']); + this.register('./barracuda', [ '.nn' ]); + this.register('./dnn', [ '.dnn' ]); + this.register('./xmodel', [ '.xmodel' ]); + this.register('./kmodel', [ '.kmodel' ]); + this.register('./flux', [ '.bson' ]); + this.register('./dl4j', [ '.json', '.bin' ]); + this.register('./openvino', [ '.xml', '.bin' ]); + this.register('./mlnet', [ '.zip' ]); + this.register('./acuity', [ '.json' ]); + this.register('./imgdnn', [ '.dnn', 'params', '.json' ]); + this.register('./flax', [ '.msgpack' ]); + this.register('./om', [ '.om', '.onnx', '.pb', '.engine' ]); + this.register('./nnabla', [ '.nntxt' ], [ '.nnp' ]); + } + + register(id, factories, containers) { + for (const extension of factories) { + this._factories.push({ extension: extension, id: id }); + this._extensions.add(extension); + } + for (const extension of containers || []) { + this._extensions.add(extension); + } + } + + open(context) { + // console.log(context) + return this._openSignature(context).then((context) => { + const containers = new Map(); + let stream = context.stream; + const entries = context.entries; + // console.log(entries); // undefined + if (!stream && entries && entries.size > 0) { + containers.set('', entries); + } + else { + // console.log('here'); + const identifier = context.identifier; + try { + const archive = gzip.Archive.open(stream); + if (archive) { + const entries = archive.entries; + containers.set('gzip', entries); + if (entries.size === 1) { + stream = entries.values().next().value; + } + } + } + catch (error) { + const message = error && error.message ? error.message : error.toString(); + throw new view.ArchiveError(message.replace(/\.$/, '') + " in '" + identifier + "'."); + } + try { + const formats = new Map([ [ 'zip', zip ], [ 'tar', tar ] ]); + for (const pair of formats) { + const format = pair[0]; + const module = pair[1]; + const archive = module.Archive.open(stream); + if (archive) { + containers.set(format, archive.entries); + containers.delete('gzip'); + break; + } + } + } + catch (error) { + const message = error && error.message ? error.message : error.toString(); + throw new view.ArchiveError(message.replace(/\.$/, '') + " in '" + identifier + "'."); + } + } + // console.log(containers); // Map(0) + // console.log(context) + const modelContext = new view.ModelContext(context, containers); + // console.log(modelContext); + return this._openContext(modelContext).then((model) => { + if (model) { + return model; + } + if (containers.size > 0) { + return this._openEntries(containers.values().next().value).then((context) => { + if (context) { + return this._openContext(context); + } + this._unsupported(modelContext); + }); + } + this._unsupported(modelContext); + }); + }); + } + + _unsupported(context) { + const identifier = context.identifier; + const extension = identifier.split('.').pop().toLowerCase(); + const stream = context.stream; + for (const module of [ zip, tar, gzip ]) { + let archive = null; + try { + archive = module.Archive.open(stream); + } + catch (error) { + // continue regardless of error + } + if (archive) { + throw new view.Error("Archive contains no model files in '" + identifier + "'.", true); + } + } + const skip = () => { + const knownUnsupportedIdentifiers = new Set([ + 'natives_blob.bin', + 'v8_context_snapshot.bin', + 'snapshot_blob.bin', + 'image_net_labels.json', + 'package.json', + 'models.json', + 'LICENSE.meta', + 'input_0.pb', + 'output_0.pb' + ]); + return knownUnsupportedIdentifiers.has(context.identifier); + }; + const json = () => { + const obj = context.open('json'); + if (obj) { + const formats = [ + { name: 'Netron metadata', tags: [ '[].name', '[].schema' ] }, + { name: 'Netron metadata', tags: [ '[].name', '[].attributes' ] }, + { name: 'Netron metadata', tags: [ '[].name', '[].category' ] }, + { name: 'Darkflow metadata', tags: [ 'net', 'type', 'model' ] }, + { name: 'keras-yolo2 configuration', tags: [ 'model', 'train', 'valid' ] }, + { name: 'Vulkan SwiftShader ICD manifest', tags: [ 'file_format_version', 'ICD' ] }, + { name: 'DeepLearningExamples configuration', tags: [ 'attention_probs_dropout_prob', 'hidden_act', 'hidden_dropout_prob', 'hidden_size', ] }, + { name: 'NuGet assets', tags: [ 'version', 'targets', 'packageFolders' ] }, + { name: 'NuGet data', tags: [ 'format', 'restore', 'projects' ] }, + { name: 'NPM package', tags: [ 'name', 'version', 'dependencies' ] }, + { name: 'NetworkX adjacency_data', tags: [ 'directed', 'graph', 'nodes' ] }, + { name: 'Waifu2x data', tags: [ 'name', 'arch_name', 'channels' ] }, + { name: 'Waifu2x data', tags: [ '[].nInputPlane', '[].nOutputPlane', '[].weight', '[].bias' ] }, + { name: 'Brain.js data', tags: [ 'type', 'sizes', 'layers' ] }, + { name: 'Custom Vision metadata', tags: [ 'CustomVision.Metadata.Version' ] } + ]; + const match = (obj, tag) => { + if (tag.startsWith('[].')) { + tag = tag.substring(3); + return (Array.isArray(obj) && obj.some((item) => Object.prototype.hasOwnProperty.call(item, tag))); + } + return Object.prototype.hasOwnProperty.call(obj, tag); + }; + for (const format of formats) { + if (format.tags.every((tag) => match(obj, tag))) { + throw new view.Error('Invalid file content. File contains ' + format.name + '.', true); + } + } + const content = JSON.stringify(obj).substring(0, 100).replace(/\s/, '').substr(0, 48) + '...'; + throw new view.Error("Unsupported JSON content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "' in '" + identifier + "'.", !skip()); + } + }; + const pbtxt = () => { + const formats = [ + { name: 'ImageNet LabelMap data', tags: [ 'entry', 'entry.target_class' ] }, + { name: 'StringIntLabelMapProto data', tags: [ 'item', 'item.id', 'item.name' ] }, + { name: 'caffe.LabelMap data', tags: [ 'item', 'item.name', 'item.label' ] }, + { name: 'Triton Inference Server configuration', tags: [ 'name', 'platform', 'input', 'output' ] }, + { name: 'TensorFlow OpList data', tags: [ 'op', 'op.name', 'op.input_arg' ] }, + { name: 'vitis.ai.proto.DpuModelParamList data', tags: [ 'model', 'model.name', 'model.kernel' ] }, + { name: 'object_detection.protos.DetectionModel data', tags: [ 'model', 'model.ssd' ] }, + { name: 'object_detection.protos.DetectionModel data', tags: [ 'model', 'model.faster_rcnn' ] }, + { name: 'tensorflow.CheckpointState data', tags: [ 'model_checkpoint_path', 'all_model_checkpoint_paths' ] }, + { name: 'apollo.perception.camera.traffic_light.detection.DetectionParam data', tags: [ 'min_crop_size', 'crop_method' ] }, + { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'caffe_ssd' ] }, // https://github.com/TexasInstruments/edgeai-mmdetection/blob/master/mmdet/utils/proto/mmdet_meta_arch.proto + { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tf_od_api_ssd' ] }, + { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tidl_ssd' ] }, + { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tidl_faster_rcnn' ] }, + { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tidl_yolo' ] }, + { name: 'tidl_meta_arch.TIDLMetaArch data', tags: [ 'tidl_retinanet' ] }, + { name: 'domi.InsertNewOps data', tags: [ 'aipp_op' ] } // https://github.com/Ascend/parser/blob/development/parser/proto/insert_op.proto + ]; + const tags = context.tags('pbtxt'); + if (tags.size > 0) { + for (const format of formats) { + if (format.tags.every((tag) => tags.has(tag))) { + throw new view.Error('Invalid file content. File contains ' + format.name + '.', true); + } + } + const entries = []; + entries.push(...Array.from(tags).filter((pair) => pair[0].toString().indexOf('.') === -1)); + entries.push(...Array.from(tags).filter((pair) => pair[0].toString().indexOf('.') !== -1)); + const content = entries.map((pair) => pair[1] === true ? pair[0] : pair[0] + ':' + JSON.stringify(pair[1])).join(','); + throw new view.Error("Unsupported Protocol Buffers text content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "' in '" + identifier + "'.", !skip()); + } + }; + const pb = () => { + const tags = context.tags('pb+'); + if (Object.keys(tags).length > 0) { + const formats = [ + { name: 'sentencepiece.ModelProto data', tags: [[1,[[1,2],[2,5],[3,0]]],[2,[[1,2],[2,2],[3,0],[4,0],[5,2],[6,0],[7,2],[10,5],[16,0],[40,0],[41,0],[42,0],[43,0]]],[3,[]],[4,[]],[5,[]]] }, + { name: 'mediapipe.BoxDetectorIndex data', tags: [[1,[[1,[[1,[[1,5],[2,5],[3,5],[4,5],[6,0],[7,5],[8,5],[10,5],[11,0],[12,0]]],[2,5],[3,[]]]],[2,false],[3,false],[4,false],[5,false]]],[2,false],[3,false]] }, + { name: 'third_party.tensorflow.python.keras.protobuf.SavedMetadata data', tags: [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]] }, + { name: 'pblczero.Net data', tags: [[1,5],[2,2],[3,[[1,0],[2,0],[3,0]],[10,[[1,[]],[2,[]],[3,[]],[4,[]],[5,[]],[6,[]]]],[11,[]]]] } // https://github.com/LeelaChessZero/lczero-common/blob/master/proto/net.proto + ]; + const match = (tags, schema) => { + for (const pair of schema) { + const key = pair[0]; + const inner = pair[1]; + const value = tags[key]; + if (value === undefined) { + continue; + } + if (inner === false) { + return false; + } + if (Array.isArray(inner)) { + if (typeof value !== 'object' || !match(value, inner)) { + return false; + } + } + else if (inner !== value) { + if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) { + return true; + } + return false; + } + } + return true; + }; + const tags = context.tags('pb+'); + for (const format of formats) { + if (match(tags, format.tags)) { + throw new view.Error('Invalid file content. File contains ' + format.name + '.', true); + } + } + const format = (tags) => { + const content = Object.entries(tags).map((pair) => { + const key = pair[0]; + const value = pair[1]; + return key.toString() + ':' + (Object(value) === value ? '{' + format(value) + '}' : value.toString()); + }); + return content.join(','); + }; + const content = format(tags); + throw new view.Error("Unsupported Protocol Buffers content '" + (content.length > 64 ? content.substring(0, 100) + '...' : content) + "' for extension '." + extension + "' in '" + identifier + "'.", !skip()); + } + }; + const flatbuffers = () => { + const tags = context.tags('flatbuffers'); + if (tags.has('file_identifier')) { + const file_identifier = tags.get('file_identifier'); + const formats = [ + { name: 'onnxruntime.experimental.fbs.InferenceSession data', identifier: 'ORTM' }, + { name: 'tflite.Model data', identifier: 'TFL3' }, + { name: 'FlatBuffers ENNC data', identifier: 'ENNC' }, + ]; + for (const format of formats) { + if (file_identifier === format.identifier) { + throw new view.Error('Invalid file content. File contains ' + format.name + '.', true); + } + } + } + }; + const xml = () => { + const tags = context.tags('xml'); + if (tags.size > 0) { + const formats = [ + { name: 'OpenCV storage data', tags: [ 'opencv_storage' ] }, + { name: 'XHTML markup', tags: [ 'http://www.w3.org/1999/xhtml:html' ]} + ]; + for (const format of formats) { + if (format.tags.some((tag) => tags.has(tag))) { + throw new view.Error('Invalid file content. File contains ' + format.name + '.', true); + } + } + throw new view.Error("Unsupported XML content '" + tags.keys().next().value + "' in '" + identifier + "'.", !skip()); + } + }; + const unknown = () => { + stream.seek(0); + const buffer = stream.peek(Math.min(16, stream.length)); + const bytes = Array.from(buffer).map((c) => (c < 16 ? '0' : '') + c.toString(16)).join(''); + const content = stream.length > 268435456 ? '(' + bytes + ') [' + stream.length.toString() + ']': '(' + bytes + ')'; + throw new view.Error("Unsupported file content " + content + " for extension '." + extension + "' in '" + identifier + "'.", !skip()); + }; + json(); + pbtxt(); + pb(); + flatbuffers(); + xml(); + unknown(); + } + + _openContext(context) { + const modules = this._filter(context).filter((module) => module && module.length > 0); + // console.log(modules) // ['./onnx', './tensorrt', './rknn', './om'] + + const errors = []; + let success = false; + const nextModule = () => { + if (modules.length > 0) { + const id = modules.shift(); + return this._host.require(id).then((module) => { + const updateErrorContext = (error, context) => { + const content = " in '" + context.identifier + "'."; + if (error && typeof error.message === 'string' && !error.message.endsWith(content) && (error.context === undefined || error.context === true)) { + error.message = error.message.replace(/\.$/, '') + content; + } + }; + // console.log(module.ModelFactory) + if (!module.ModelFactory) { + throw new view.Error("Failed to load module '" + id + "'."); + } + const modelFactory = new module.ModelFactory(); + let match = undefined; + try { + match = modelFactory.match(context); + // console.log(match) // onnx.pb.ModelProto + if (!match) { + return nextModule(); + } + } + catch (error) { + updateErrorContext(error, context); + return Promise.reject(error); + } + success = true; + return modelFactory.open(context, match).then((model) => { + if (!model.identifier) { + model.identifier = context.identifier; + } + return model; + }).catch((error) => { + updateErrorContext(error, context); + errors.push(error); + return nextModule(); + }); + }); + } + else { + if (success) { + if (errors.length === 1) { + const error = errors[0]; + return Promise.reject(error); + } + return Promise.reject(new view.Error(errors.map((err) => err.message).join('\n'))); + } + return Promise.resolve(null); + } + }; + return nextModule(); + } + + _openEntries(entries) { + try { + const rootFolder = (files) => { + const map = files.map((file) => file.split('/').slice(0, -1)); + const at = index => list => list[index]; + const rotate = list => list.length === 0 ? [] : list[0].map((item, index) => list.map(at(index))); + const equals = list => list.every((item) => item === list[0]); + const folder = rotate(map).filter(equals).map(at(0)).join('/'); + return folder.length === 0 ? folder : folder + '/'; + }; + const filter = (queue) => { + let matches = []; + const nextEntry = () => { + if (queue.length > 0) { + const entry = queue.shift(); + const context = new view.ModelContext(new view.ArchiveContext(this._host, null, folder, entry.name, entry.stream)); + let modules = this._filter(context); + const nextModule = () => { + if (modules.length > 0) { + const id = modules.shift(); + return this._host.require(id).then((module) => { + if (!module.ModelFactory) { + throw new view.ArchiveError("Failed to load module '" + id + "'.", null); + } + const factory = new module.ModelFactory(); + if (factory.match(context)) { + matches.push(entry); + modules = []; + } + return nextModule(); + }); + } + else { + return nextEntry(); + } + }; + return nextModule(); + } + else { + if (matches.length === 0) { + return Promise.resolve(null); + } + // MXNet + if (matches.length === 2 && + matches.some((e) => e.name.toLowerCase().endsWith('.params')) && + matches.some((e) => e.name.toLowerCase().endsWith('-symbol.json'))) { + matches = matches.filter((e) => e.name.toLowerCase().endsWith('.params')); + } + // TensorFlow.js + if (matches.length > 0 && + matches.some((e) => e.name.toLowerCase().endsWith('.bin')) && + matches.some((e) => e.name.toLowerCase().endsWith('.json'))) { + matches = matches.filter((e) => e.name.toLowerCase().endsWith('.json')); + } + // ncnn + if (matches.length > 0 && + matches.some((e) => e.name.toLowerCase().endsWith('.bin')) && + matches.some((e) => e.name.toLowerCase().endsWith('.param'))) { + matches = matches.filter((e) => e.name.toLowerCase().endsWith('.param')); + } + // ncnn + if (matches.length > 0 && + matches.some((e) => e.name.toLowerCase().endsWith('.bin')) && + matches.some((e) => e.name.toLowerCase().endsWith('.param.bin'))) { + matches = matches.filter((e) => e.name.toLowerCase().endsWith('.param.bin')); + } + // Paddle + if (matches.length > 0 && + matches.some((e) => e.name.toLowerCase().endsWith('.pdmodel')) && + matches.some((e) => e.name.toLowerCase().endsWith('.pdiparams'))) { + matches = matches.filter((e) => e.name.toLowerCase().endsWith('.pdmodel')); + } + // Paddle Lite + if (matches.length > 0 && + matches.some((e) => e.name.toLowerCase().split('/').pop() === '__model__.nb') && + matches.some((e) => e.name.toLowerCase().split('/').pop() === 'param.nb')) { + matches = matches.filter((e) => e.name.toLowerCase().split('/').pop() == '__model__.nb'); + } + // TensorFlow Bundle + if (matches.length > 1 && + matches.some((e) => e.name.toLowerCase().endsWith('.data-00000-of-00001'))) { + matches = matches.filter((e) => !e.name.toLowerCase().endsWith('.data-00000-of-00001')); + } + // TensorFlow SavedModel + if (matches.length === 2 && + matches.some((e) => e.name.toLowerCase().split('/').pop() === 'keras_metadata.pb')) { + matches = matches.filter((e) => e.name.toLowerCase().split('/').pop() !== 'keras_metadata.pb'); + } + if (matches.length > 1) { + return Promise.reject(new view.ArchiveError('Archive contains multiple model files.')); + } + const match = matches.shift(); + return Promise.resolve(new view.ModelContext(new view.ArchiveContext(this._host, entries, folder, match.name, match.stream))); + } + }; + return nextEntry(); + }; + const list = Array.from(entries).map((entry) => { + return { name: entry[0], stream: entry[1] }; + }); + const files = list.filter((entry) => { + if (entry.name.endsWith('/')) { + return false; + } + if (entry.name.split('/').pop().startsWith('.')) { + return false; + } + if (!entry.name.startsWith('./') && entry.name.startsWith('.')) { + return false; + } + return true; + }); + const folder = rootFolder(files.map((entry) => entry.name)); + const queue = files.slice(0).filter((entry) => entry.name.substring(folder.length).indexOf('/') < 0); + return filter(queue).then((context) => { + if (context) { + return Promise.resolve(context); + } + const queue = files.slice(0).filter((entry) => entry.name.substring(folder.length).indexOf('/') >= 0); + return filter(queue); + }); + } + catch (error) { + return Promise.reject(new view.ArchiveError(error.message)); + } + } + + accept(identifier) { + const extension = identifier.indexOf('.') === -1 ? '' : identifier.split('.').pop().toLowerCase(); + identifier = identifier.toLowerCase().split('/').pop(); + for (const extension of this._extensions) { + if ((typeof extension === 'string' && identifier.endsWith(extension)) || (extension instanceof RegExp && extension.exec(identifier))) { + this._host.event('File', 'Accept', extension, 1); + return true; + } + } + this._host.event('File', 'Reject', extension, 1); + return false; + } + + _filter(context) { + // console.log(context.identifier) // squeezenet1.0-12-int8.onnx + const identifier = context.identifier.toLowerCase().split('/').pop(); + // console.log(identifier) // squeezenet1.0-12-int8.onnx + const list = this._factories.filter((entry) => + (typeof entry.extension === 'string' && identifier.endsWith(entry.extension)) || + (entry.extension instanceof RegExp && entry.extension.exec(identifier))); + // console.log(list) + // console.log(Array.from(new Set(list.map((entry) => entry.id)))) + return Array.from(new Set(list.map((entry) => entry.id))); + } + + _openSignature(context) { + const stream = context.stream; + if (stream) { + let empty = true; + let position = 0; + while (empty && position < stream.length) { + const buffer = stream.read(Math.min(4096, stream.length - position)); + position += buffer.length; + if (!buffer.every((value) => value === 0x00)) { + empty = false; + break; + } + } + stream.seek(0); + if (empty) { + return Promise.reject(new view.Error('File has no content.', true)); + } + /* eslint-disable no-control-regex */ + const entries = [ + { name: 'ELF executable', value: /^\x7FELF/ }, + { name: 'PNG image', value: /^\x89PNG/ }, + { name: 'Git LFS header', value: /^version https:\/\/git-lfs.github.com/ }, + { name: 'Git LFS header', value: /^\s*oid sha256:/ }, + { name: 'HTML markup', value: /^\s*/ }, + { name: 'HTML markup', value: /^\s*/ }, + { name: 'HTML markup', value: /^\s*/ }, + { name: 'HTML markup', value: /^\s*/ }, + { name: 'HTML markup', value: /^\s*= 5) { + const signature = [ 0xac, 0xed ]; + if (stream.peek(2).every((value, index) => value === signature[index])) { + const reader = new java.io.InputObjectStream(stream); + const obj = reader.read(); + if (obj && obj.$class && obj.$class.name) { + return 'weka'; + } + } + } + } + catch (err) { + // continue regardless of error + } + return undefined; + } + + open(context) { + return Promise.resolve().then(() => { + const reader = new java.io.InputObjectStream(context.stream); + const obj = reader.read(); + throw new weka.Error("Unsupported type '" + obj.$class.name + "'."); + }); + } +}; + +weka.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'Error loading Weka model.'; + } +}; + +java.io = {}; + +java.io.InputObjectStream = class { + + constructor(stream) { + // Object Serialization Stream Protocol + // https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/guide/serialization/spec/protocol.doc.html + if (stream.length < 5) { + throw new java.io.Error('Invalid stream size'); + } + const signature = [ 0xac, 0xed ]; + if (!stream.peek(2).every((value, index) => value === signature[index])) { + throw new java.io.Error('Invalid stream signature'); + } + this._reader = new java.io.InputObjectStream.BinaryReader(stream.peek()); + this._references = []; + this._reader.skip(2); + const version = this._reader.uint16(); + if (version !== 0x0005) { + throw new java.io.Error("Unsupported version '" + version + "'."); + } + } + + read() { + return this._object(); + } + + _object() { + const code = this._reader.byte(); + switch (code) { + case 0x73: { // TC_OBJECT + const obj = {}; + obj.$class = this._classDesc(); + this._newHandle(obj); + this._classData(obj); + return obj; + } + case 0x74: { // TC_STRING + return this._newString(false); + } + } + throw new java.io.Error("Unsupported code '" + code + "'."); + } + + _classDesc() { + const code = this._reader.byte(); + switch (code) { + case 0x72: // TC_CLASSDESC + this._reader.skip(-1); + return this._newClassDesc(); + case 0x71: // TC_REFERENCE + return this._references[this._reader.uint32() - 0x7e0000]; + case 0x70: // TC_NULL + this._reader.byte(); + return null; + } + throw new java.io.Error("Unsupported code '" + code + "'."); + } + + _newClassDesc() { + const code = this._reader.byte(); + switch (code) { + case 0x72: { // TC_CLASSDESC + const classDesc = {}; + classDesc.name = this._reader.string(), + classDesc.id = this._reader.uint64().toString(); + this._newHandle(classDesc); + classDesc.flags = this._reader.byte(); + classDesc.fields = []; + const count = this._reader.uint16(); + for (let i = 0; i < count; i++) { + const field = {}; + field.type = String.fromCharCode(this._reader.byte()); + field.name = this._reader.string(); + if (field.type === '[' || field.type === 'L') { + field.classname = this._object(); + } + classDesc.fields.push(field); + } + if (this._reader.byte() !== 0x78) { + throw new java.io.Error('Expected TC_ENDBLOCKDATA.'); + } + classDesc.superClass = this._classDesc(); + return classDesc; + } + case 0x7D: // TC_PROXYCLASSDESC + break; + } + throw new java.io.Error("Unsupported code '" + code + "'."); + } + + _classData(/* obj */) { + /* + const classname = obj.$class.name; + let flags = obj.$class.flags; + let superClass = obj.$class.superClass; + while (superClass) { + flags |= superClass.flags; + superClass = superClass.superClass; + } + if (flags & 0x02) { // SC_SERIALIZABLE + debugger; + var customObject = objects[classname]; + var hasReadObjectMethod = customObject && customObject.readObject; + if (flags & 0x01) { // SC_WRITE_METHOD + if (!hasReadObjectMethod) { + throw new Error('Class "'+ classname + '" dose not implement readObject()'); + } + customObject.readObject(this, obj); + if (this._reader.byte() !== 0x78) { // TC_ENDBLOCKDATA + throw new java.io.Error('Expected TC_ENDBLOCKDATA.'); + } + } + else { + if (hasReadObjectMethod) { + customObject.readObject(this, obj); + if (this._reader.byte() !== 0x78) { // TC_ENDBLOCKDATA + throw new java.io.Error('Expected TC_ENDBLOCKDATA.'); + } + } + else { + this._nowrclass(obj); + } + } + } + else if (flags & 0x04) { // SC_EXTERNALIZABLE + if (flags & 0x08) { // SC_BLOCK_DATA + this._objectAnnotation(obj); + } + else { + this._externalContents(); + } + } + else { + throw new Error('Illegal flags: ' + flags); + } + */ + } + + _newString(long) { + const value = this._reader.string(long); + this._newHandle(value); + return value; + } + + _newHandle(obj) { + this._references.push(obj); + } +}; + +java.io.InputObjectStream.BinaryReader = class { + + constructor(buffer) { + this._buffer = buffer; + this._position = 0; + this._length = buffer.length; + this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + this._decoder = new TextDecoder('utf-8'); + } + + skip(offset) { + this._position += offset; + if (this._position > this._end) { + throw new java.io.Error('Expected ' + (this._position - this._end) + ' more bytes. The file might be corrupted. Unexpected end of file.'); + } + } + + byte() { + const position = this._position; + this.skip(1); + return this._buffer[position]; + } + + uint16() { + const position = this._position; + this.skip(2); + return this._view.getUint16(position, false); + } + + uint32() { + const position = this._position; + this.skip(4); + return this._view.getUint32(position, false); + } + + uint64() { + const position = this._position; + this.skip(8); + return this._view.getUint64(position, false); + } + + string(long) { + const size = long ? this.uint64().toNumber() : this.uint16(); + const position = this._position; + this.skip(size); + return this._decoder.decode(this._buffer.subarray(position, this._position)); + } +}; + +java.io.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'Error loading Object Serialization Stream Protocol.'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.ModelFactory = weka.ModelFactory; +} \ No newline at end of file diff --git a/xml.js b/xml.js new file mode 100644 index 0000000..eade4a5 --- /dev/null +++ b/xml.js @@ -0,0 +1,1790 @@ + +var xml = xml || {}; +var text = text || require('./text'); + +// https://www.w3.org/TR/xml + +xml.TextReader = class { + + static open(data, callback) { + const decoder = text.Decoder.open(data); + for (;;) { + const c = decoder.decode(); + if (c === '<') { + break; + } + if (c === ' ' || c === '\n' || c === '\r' || c === '\t') { + continue; + } + return null; + } + return new xml.TextReader(data, callback); + } + + constructor(data, callback) { + this._data = data; + this._callback = callback; + this._entities = new Map([ [ 'quot', '"' ], [ 'amp', '&' ], [ 'apos', "'" ], [ 'lt', '<' ], [ 'gt', '>' ] ]); + this._nameStartCharRegExp = /[:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/; + this._nameCharRegExp = new RegExp("[-.0-9\\xB7" + this._nameStartCharRegExp.source.slice(1, -1) + "]"); + xml.Utility.nameStartCharRegExp = this._nameStartCharRegExp; + } + + peek() { + this._peek = true; + const value = this.read(); + delete this._peek; + return value; + } + + read() { + this._stack = []; + this._context = []; + this._pushBuffer(this._data, '', '', false); + this._version = 0; + /* eslint-disable */ + this._charRegExp = /[\x09\x0a\x0d\x20-\uD7FF\uE000-\uFFFD]/; + /* eslint-enable */ + this._parameterEntities = false; + this._characterData = true; + this._push(new xml.Document()); + const document = this._document(); + for (;;) { + this._start = this._position; + switch (this._char) { + case '<': { + this._next(); + switch (this._char) { + case '?': { + this._processingInstruction(); + break; + } + case '!': { + this._next(); + if (this._match('--')) { + this._comment(); + } + else if (this._match('[CDATA')) { + this._assert(this._stack.length > 1); + this._characterData = true; + this._expect('['); + const data = this._terminal(']]>'); + const node = document.createCDATASection(data); + this._appendChild(node); + } + else if (this._match('DOCTYPE')) { + this._assert(this._stack.length > 1 || !document.documentElement || !document.documentType); + this._whitespace(1); + const name = this._name(); + this._assert(name !== null); + let systemId = ''; + let publicId = ''; + let whitespace = this._whitespace(0); + if (whitespace && this._match('SYSTEM')) { + this._whitespace(1); + systemId = this._systemLiteral(); + this._whitespace(0); + whitespace = true; + } + else if (whitespace && this._match('PUBLIC')) { + this._whitespace(1); + publicId = this._pubidLiteral(); + this._whitespace(1); + systemId = this._systemLiteral(); + this._whitespace(0); + whitespace = true; + } + const node = document.createDocumentType(name, publicId, systemId); + this._appendChild(node); + this._push(node); + node.parameterEntities = new xml.NamedNodeMap(); + node.elements = new xml.NamedNodeMap(); + this._parameterEntities = true; + this._characterData = false; + const internalSubset = whitespace && this._match('['); + if (internalSubset) { + this._internalSubset(']'); + } + if (systemId && !this._standalone) { + this._pushResource(systemId, '', true); + this._internalSubset(undefined); + this._popContext(); + } + this._characterData = true; + this._parameterEntities = false; + const values = node.entities.filter((entity) => entity.value).map((entity) => entity.value); + for (const entity of node.entities.filter((entity) => entity.notationName)) { + const reference = '&' + entity.localName + ';'; + if (values.some((value) => value.indexOf(reference) >= 0)) { + this._error("Entity references unparsed entity '" + entity.localName + "'"); + } + } + if (internalSubset) { + this._expect(']'); + this._whitespace(0); + } + this._expect('>'); + this._assert(this._pop().nodeType === xml.NodeType.DocumentType); + } + else { + this._unexpected(); + } + break; + } + case '/': { + this._next(); + const name = this._name(); + this._assert(name !== null); + this._whitespace(0); + this._expect('>'); + const node = this._pop(); + const nodeName = node.prefix ? node.prefix + ':' + node.localName : node.localName; + if (name !== nodeName) { + this._error("Opening tag <" + nodeName + "> and ending tag mismatch", this._start); + } + break; + } + default: { + this._assert(this._stack.length > 1 || !this._document.documentElement); + const name = this._name(); + this._assert(name !== null); + this._assert(!name.startsWith('xmlns:')); + const attributes = []; + let whitespace = this._whitespace(0); + if (whitespace) { + while (this._char !== '/' && this._char !== '>') { + if (!whitespace) { + this._unexpected(); + } + const position = this._position; + const name = this._name(); + if (!name) { + this._unexpected(); + } + this._whitespace(0); + this._expect('='); + this._whitespace(0); + const valuePosition = this._valuePosition; + const value = this._attributeValue(); + attributes.push({ + qualifiedName: name, + value: value, + position: position, + valuePosition: valuePosition + }); + whitespace = this._whitespace(0); + if (name === 'xmlns' && (!this._validateNamespace(value) || value === 'http://www.w3.org/2000/xmlns/' || value === 'http://www.w3.org/XML/1998/namespace')) { + this._error("Invalid namespace '" + value + "'", valuePosition); + } + if (name === 'xml:space' && value !== 'preserve' && value !== 'default') { + this._error("Unexpected xml:space attribute value '" + value + "'", position); + } + } + } + const namespaces = new Map(); + for (const entry of attributes.reverse()) { + const name = entry.qualifiedName; + const value = entry.value; + const pair = xml.Utility.split(name); + this._assert(name !== 'xmlns:'); + entry.prefix = pair[0]; + entry.localName = pair[1]; + if (entry.prefix !== null) { + this._assert(entry.localName !== ''); + if (entry.prefix === 'xmlns' && entry.localName) { + if (!this._validateNamespace(value) || value === 'http://www.w3.org/2000/xmlns/') { + this._error("Invalid namespace '" + value + "'", entry.valuePosition); + } + if (entry.localName === 'xmlns' || (entry.localName === 'xml' && value !== 'http://www.w3.org/XML/1998/namespace') || (entry.localName !== 'xml' && value === 'http://www.w3.org/XML/1998/namespace')) { + this._error("Invalid namespace prefix '" + entry.localName + "'", entry.position); + } + if (this._version === 0 && value.length === 0) { + this._error("Invalid namespace declaration'", entry.position); + } + namespaces.set(entry.localName, value); + } + } + else { + if (entry.localName === 'xmlns') { + namespaces.set('', value); + } + } + } + const pair = xml.Utility.split(name); + const prefix = pair[0] || ''; + const namespaceURI = namespaces.has(prefix) ? namespaces.get(prefix) : this._lookupNamespaceURI(prefix); + let element = null; + const documentType = document.documentType; + const elementType = documentType ? documentType.elements.getNamedItem(name) : null; + if (namespaceURI !== null) { + this._assert(name === ':' || (!name.endsWith(':') && !name.startsWith(':'))); + if (prefix && (namespaceURI === '' || namespaceURI === null)) { + this._error("Invalid namespace prefix '" + prefix + "'", this._start); + } + element = document.createElementNS(namespaceURI, name); + } + else { + this._assert((pair[0] === null && !name.endsWith(':')) || name === ':' || elementType !== null); + element = document.createElement(name); + } + const parent = this._node(); + if (parent.nodeType === xml.NodeType.Document && parent.documentElement !== null) { + this._error('Duplicate document element', this._start); + } + this._appendChild(element); + const keys = new Set(); + for (const attr of attributes) { + const name = attr.qualifiedName; + const prefix = attr.prefix || ''; + const namespaceURI = namespaces.has(prefix) ? namespaces.get(prefix) : this._lookupNamespaceURI(prefix); + let attribute = null; + if (namespaceURI) { + attribute = document.createAttributeNS(namespaceURI, name); + } + else { + const attributeType = elementType ? elementType.attributes.getNamedItem(name) : null; + this._assert(name.indexOf(':') === -1 || attributeType); + attribute = document.createAttribute(name); + } + const key = (attribute.namespaceURI || '') + '|' + attribute.localName; + this._assert(!keys.has(key)); + keys.add(key); + attribute.value = attr.value; + attribute.ownerElement = element; + element.setAttributeNode(attribute); + } + const close = this._match('/'); + this._expect('>'); + if (this._peek && this._stack.length === 1 && this._nodeType() === xml.NodeType.Document) { + return this._pop(); + } + if (!close) { + this._push(element); + } + break; + } + } + break; + } + default: { + while (this._char === undefined && this._context.length > 0) { + this._popContext(); + } + if (this._char === undefined) { + if (this._stack.length === 1 && this._nodeType() === xml.NodeType.Document) { + this._assert(document.documentElement); + const documentType = document.documentType; + if (documentType) { + delete documentType.parameterEntities; + delete documentType.elements; + } + const value = this._pop(); + for (const key of Object.keys(this)) { + if (key !== '_data' && key !== '_callback' && key !== '_entities' && !key.startsWith('_name')) { + delete this[key]; + } + } + return value; + } + this._unexpected(); + } + const node = this._node(); + if (node.nodeType === xml.NodeType.Element) { + const documentType = document.documentType; + const name = node.prefix ? node.prefix + ':' + node.localName : node.localName; + const elementType = documentType ? documentType.elements.getNamedItem(name) : null; + this._characterData = elementType ? elementType.characterData : false; + this._seek(this._position); + const data = []; + while (this._char !== '<' && this._char !== undefined) { + if (this._char === ']' && this._match(']]>')) { + this._unexpected(); + } + data.push(this._content()); + if (data.length > 65536) { + this._error('Invalid character data buffer size.'); + } + } + if (data.length > 0) { + const content = data.splice(0, data.length).join(''); + if (content.trim().length > 0) { + const node = document.createTextNode(content); + this._appendChild(node); + } + } + continue; + } + if (!this._whitespace(0)) { + this._unexpected(); + } + break; + } + } + } + } + + _internalSubset(terminal) { + for (;;) { + this._start = this._position; + switch (this._char) { + case '<': { + this._next(); + switch (this._char) { + case '?': { + this._processingInstruction(); + break; + } + case '!': { + this._next(); + if (this._match('--')) { + this._parameterEntities = false; + this._characterData = true; + this._comment(); + this._parameterEntities = true; + } + else if (this._match('ENTITY')) { + const documentType = this._node(); + this._assert(documentType.nodeType === xml.NodeType.DocumentType); + this._parameterEntities = false; + this._whitespace(1); + const parameter = this._char === '%'; + if (parameter) { + this._next(); + this._whitespace(1); + } + this._parameterEntities = true; + const name = this._entityName(); + const node = documentType.createEntity(name); + let whitespace = this._whitespace(0); + if (whitespace && (this._char === '"' || this._char === "'")) { + node.value = this._entityValue(); + whitespace = this._whitespace(0); + } + else { + if (whitespace && this._match('SYSTEM')) { + this._whitespace(1); + node.systemId = this._systemLiteral(); + whitespace = this._whitespace(0); + } + else if (whitespace && this._match('PUBLIC')) { + this._whitespace(1); + node.publicId = this._pubidLiteral(); + this._whitespace(1); + node.systemId = this._systemLiteral(); + whitespace = this._whitespace(0); + } + else { + this._unexpected(); + } + if (whitespace && !parameter) { + if (this._match('NDATA')) { + this._whitespace(1); + const name = this._name(); + this._assert(name !== null); + node.notationName = name; + this._whitespace(0); + } + } + } + this._expect('>'); + if (parameter) { + documentType.parameterEntities.setNamedItem(node); + } + else { + this._appendChild(node); + } + } + else if (this._match('ELEMENT')) { + const documentType = this._node(); + this._assert(documentType.nodeType === xml.NodeType.DocumentType); + this._whitespace(1); + const name = this._name(); + this._assert(name !== null); + this._whitespace(1); + const elementType = this._elementType(name); + if (this._match('EMPTY')) { + this._whitespace(0); + } + else if (this._match('ANY')) { + this._whitespace(0); + } + else { + this._expect('('); + this._whitespace(0); + if (this._match('#PCDATA')) { + elementType.characterData = true; + this._whitespace(0); + if (this._match(')')) { + this._match('*'); + } + else { + this._whitespace(0); + while (this._match('|')) { + this._whitespace(0); + const name = this._name(); + this._assert(name); + this._whitespace(0); + } + this._expect(')*'); + } + } + else { + this._elementChildren(); + } + } + this._whitespace(0); + this._expect('>'); + } + else if (this._match('ATTLIST')) { + const documentType = this._node(); + this._assert(documentType.nodeType === xml.NodeType.DocumentType); + this._whitespace(1); + const name = this._name(); + this._assert(name !== null); + const elementType = this._elementType(name); + while (this._whitespace(0)) { + const attributeType = this._attributeDefinition(); + if (!attributeType) { + break; + } + elementType.attributes.setNamedItem(attributeType); + } + this._whitespace(0); + this._expect('>'); + } + else if (this._match('NOTATION')) { + this._assert(this._nodeType() === xml.NodeType.DocumentType); + const notation = { systemId: null, publicId: null }; + this._whitespace(1); + notation.name = this._entityName(); + let whitespace = this._whitespace(0); + if (whitespace && this._match('SYSTEM')) { + this._whitespace(1); + notation.systemId = this._systemLiteral(); + whitespace = this._whitespace(0); + } + if (whitespace && this._match('PUBLIC')) { + this._whitespace(1); + notation.publicId = this._pubidLiteral(); + if (this._whitespace(0) && (this._char === '"') || this._char === "'") { + notation.systemId = this._systemLiteral(); + whitespace = this._whitespace(0); + } + } + this._assert(notation.systemId || notation.publicId); + this._expect('>'); + } + else if (this._match('[')) { + this._whitespace(0); + if (this._match('INCLUDE')) { + this._assert(this._context.length > 0); + this._whitespace(0); + this._expect('['); + this._internalSubset(']'); + this._expect(']]>'); + } + else if (this._match('IGNORE')) { + this._whitespace(0); + this._expect('['); + this._ignoreSectContents(); + } + } + else { + this._unexpected(); + } + } + } + break; + } + case '%': { + this._resolveParameterEntityReference(); + break; + } + default: { + if (this._char === terminal) { + return; + } + if (!this._whitespace(0)) { + this._unexpected(); + } + break; + } + } + } + } + + _ignoreSectContents() { + while (!this._match(']]>')) { + if (this._match('= 0x10000 && c <= 0xEFFFF)) { + name.push(this._char); + this._next(); + if (this._char !== undefined) { + let c = this._char.codePointAt(0); + while (this._nameCharRegExp.test(this._char) || (c >= 0x300 && c <= 0x36f) || (c >= 0x203F && c <= 0x2040)) { + name.push(this._char); + this._next(); + if (this._char === undefined || this._implicitSpace) { + break; + } + c = this._char.codePointAt(0); + } + } + } + if (name.length > 0) { + return name.join(''); + } + this._seek(position); + return null; + } + + _nmtoken() { + const position = this._position; + const name = []; + let c = this._char.codePointAt(0); + while (this._nameCharRegExp.test(this._char) || (c >= 0x300 && c <= 0x36f) || (c >= 0x203F && c <= 0x2040)) { + name.push(this._char); + this._next(); + if (this._char === undefined) { + break; + } + c = this._char.codePointAt(0); + } + if (name.length > 0) { + return name.join(''); + } + this._seek(position); + return null; + } + + _entityName() { + const position = this._position; + const name = this._name(); + if (name === null) { + this._error('Expected entity name', position); + } + if (!name.endsWith(':') && name.indexOf(':') !== -1) { + this._error('Invalid colon in entity name', position); + } + return name; + } + + _entityValue() { + const quote = this._char; + this._parameterEntities = false; + this._characterData = true; + const decoder = this._decoder; + const position = this._position; + this._next(); + while (this._char !== quote) { + if (this._char === undefined) { + this._unexpected(); + } + this._next(); + } + const end = this._position; + this._parameterEntities = true; + this._seek(position); + this._next(); + const data = []; + while (this._position !== end || this._decoder !== decoder) { + if (this._char === undefined) { + this._unexpected(); + } + if (this._char === '%') { + if (this._context.length === 0) { + this._error('Invalid parameter entity reference in internal subset'); + } + this._assert(); + this._resolveParameterEntityReference(); + continue; + } + if (this._char === '&') { + data.push(this._entityReference()); + this._expect(';'); + continue; + } + + data.push(this._char); + this._next(); + } + this._next(); + this._parameterEntities = true; + this._characterData = false; + return data.join(''); + } + + _elementType(name) { + const documentType = this._document().documentType; + let elementType = documentType.elements.getNamedItem(name); + if (!elementType) { + elementType = { localName: name, characterData: false, attributes: new xml.NamedNodeMap() }; + documentType.elements.setNamedItem(elementType); + } + return elementType; + } + + _elementChildren() { + let separator = undefined; + const choice = new Set(); + for (;;) { + const name = this._name(); + if (name) { + this._assert(separator !== '|' || !choice.has(name)); + choice.add(name); + this._match('?') || this._match('*') || this._match('+'); + this._whitespace(0); + } + else if (this._match('(')) { + this._elementChildren(); + this._whitespace(0); + } + else { + this._unexpected(); + } + if (this._match(')')) { + break; + } + if (separator && separator !== this._char) { + this._unexpected(); + } + if (this._char !== '|' && this._char !== ',') { + this._unexpected(); + } + separator = this._char; + this._next(); + this._whitespace(0); + } + this._match('?') || this._match('*') || this._match('+'); + } + + _attributeDefinition() { + this._whitespace(0); + const name = this._name(); + if (name) { + this._whitespace(1); + if (this._match('CDATA') || this._match('IDREFS') || this._match('IDREF') || this._match('ID') || this._match('ENTITIES') || this._match('ENTITY') || this._match('NMTOKENS') || this._match('NMTOKEN') || + this._enumeratedType()) { + this._whitespace(1); + if (!this._match('#REQUIRED') && !this._match('#IMPLIED')) { + if (this._match('#FIXED')) { + this._whitespace(1); + } + this._parameterEntities = false; + this._attributeValue(); + this._parameterEntities = true; + } + return { localName: name }; + } + this._assert(false); + } + return null; + } + + _enumeratedType() { + if (this._match('NOTATION')) { + this._whitespace(1); + this._expect('('); + do { + this._whitespace(0); + const name = this._name(); + this._assert(name); + this._whitespace(0); + } + while (this._match('|')); + this._expect(')'); + return true; + } + if (this._match('(')) { + do { + this._whitespace(0); + const name = this._nmtoken(); + this._assert(name); + this._whitespace(0); + } + while (this._match('|')); + this._expect(')'); + return true; + } + return false; + } + + _content() { + const c = this._char !== '&' ? this._char : this._resolveEntityReference(); + if (c === undefined) { + return ''; + } + const code = c.codePointAt(0); + if ((!this._charRegExp.test(c) && (code < 0x10000 || c > 0x10FFFF))) { + this._unexpected(); + } + this._next(); + return c; + } + + _attributeValue() { + const quote = this._char; + if (quote !== '"' && quote !== "'") { + this._unexpected(); + } + this._characterData = true; + const decoder = this._decoder; + const position = this._position; + this._next(); + while (this._char !== quote) { + if (this._char === undefined || this._char === '<') { + this._unexpected(); + } + this._next(); + } + const end = this._position; + this._characterData = false; + this._seek(position); + this._next(); + const data = []; + while (this._position !== end || this._decoder !== decoder) { + if (this._char === undefined && this._context.length > 0) { + this._popContext(); + continue; + } + if (this._char === '<') { + this._unexpected(); + } + data.push(this._content()); + if (data.length > 65536) { + this._error('Invalid character data buffer size.'); + } + } + this._characterData = true; + this._next(); + return data.join(''); + } + + _validateNamespace(value) { + if (value && (value.startsWith('#') || value.indexOf(':') === -1)) { + return false; + } + if (this._version > 0) { + return true; + } + return /^[A-Za-z0-9-._~:/?#[\]@!$&'()*+,;%=]*$/.exec(value) !== null; + } + + _pubidLiteral() { + const quote = this._char; + if (quote !== '"' && quote !== "'") { + this._unexpected(); + } + this._next(); + const data = []; + while (this._char !== quote) { + if (/[a-zA-Z0-9-'()+,./:=?;!*#@$_%]/.test(this._char) || this._char === ' ' || this._char === '\r' || this._char === '\n') { + data.push(this._char); + this._next(); + if (this._char === undefined) { + this._unexpected(); + } + continue; + } + this._unexpected(); + } + this._next(); + return data.join(''); + } + + _systemLiteral() { + const quote = this._char; + if (quote !== '"' && quote !== "'") { + this._unexpected(); + } + this._next(); + const data = []; + while (this._char !== quote) { + data.push(this._char); + this._next(); + if (this._char === undefined) { + this._unexpected(); + } + } + this._next(); + const value = data.join(''); + if (value.indexOf('#') >= 0) { + this._unexpected(); + } + const match = /(.*\/)[^/]*/.exec(this._base); + return (match ? match[1] : '') + value; + } + + _terminal(terminal) { + const data = []; + while (!this._match(terminal)) { + if (this._char === undefined) { + this._unexpected(); + } + const c = this._char.codePointAt(0); + if (c !== 0x09 && c !== 0x0A && c !== 0x0D && (c < 0x20 || c > 0xD7FF) && (c < 0xE000 || c > 0xFFFD) && (c < 0x10000 || c > 0x10FFFF)) { + this._unexpected(); + } + data.push(this._char); + this._next(); + } + return data.join(''); + } + + _resolveParameterEntityReference() { + const position = this._position; + this._next(); + const name = this._name(); + this._assert(name !== null); + if (this._char === ';') { + const entity = this._document().documentType.parameterEntities.getNamedItem(name); + if (entity) { + const implicitSpace = !this._entity && !this._context.some((context) => context.entity); + if (entity.systemId) { + this._pushResource(entity.systemId, name, false); + } + else { + this._pushString(entity.value, name, false); + } + if (implicitSpace) { + this._implicitSpace = true; + } + return; + } + this._error("Undefined ENTITY '" + name + "'", position); + } + this._unexpected(); + } + + _resolveEntityReference() { + const position = this._position; + const entity = this._entityReference(); + const name = entity.substring(1, entity.length - 1); + if (name.startsWith('#x')) { + const value = parseInt(name.substring(2), 16); + return String.fromCodePoint(value); + } + else if (name.startsWith('#')) { + const value = parseInt(name.substring(1), 10); + return String.fromCodePoint(value); + } + else if (this._entities.has(name)) { + return this._entities.get(name); + } + else { + const documentType = this._document().documentType; + const entity = documentType ? documentType.entities.getNamedItem(name) : null; + if (entity) { + if (entity.systemId) { + this._pushResource(entity.systemId, name, true); + } + else { + this._pushString(entity.value, name, true); + } + } + else { + if (this._context.length !== 0 || !documentType || documentType.parameterEntities.length === 0) { + this._error("Undefined ENTITY '" + name + "'", position); + } + } + return undefined; + } + } + + _entityReference() { + if (this._char === '&') { + const position = this._position; + this._next(); + if (this._match('#x')) { + const data = []; + while (/[0-9a-fA-F]/.test(this._char)) { + data.push(this._char); + this._next(); + if (this._char === undefined) { + this._unexpected(); + } + } + this._assert(this._char === ';'); + if (data.length > 0) { + const text = data.join(''); + const value = parseInt(text, 16); + this._assert(value <= 0x10FFFF, "Invalid value '&#x" + text + ";'", position); + return '&#x' + text + ';'; + } + } + else if (this._match('#')) { + const data = []; + while (/[0-9]/.test(this._char)) { + data.push(this._char); + this._next(); + if (this._char === undefined) { + this._unexpected(); + } + } + this._assert(this._char === ';'); + if (data.length > 0) { + const text = data.join(''); + const value = parseInt(text, 10); + this._assert(value <= 0x10FFFF, "Invalid value '&#" + text + ";'", position); + return '&#' + text + ';'; + } + } + else { + const name = this._name(); + this._assert(name !== null); + this._assert(this._char === ';'); + return '&' + name + ';'; + } + } + this._unexpected(); + } + + _comment() { + const data = this._terminal('--'); + const node = this._document().createComment(data); + this._appendChild(node); + this._expect('>'); + } + + _processingInstruction() { + this._next(); + const name = this._entityName(); + let whitespace = this._char === '?' ? false : this._whitespace(1); + const position = this._position; + const data = this._terminal('?>'); + if (name.toLowerCase() === 'xml') { + this._seek(position); + this._assert(name === 'xml', "'" + name + "' must be lower case"); + this._assert(this._start === this._prolog, "Prolog must start with XML declaration", this._start); + this._assert(typeof this._data !== 'string', 'Invalid text declaration', this._start); + const obj = { version: '', encoding: '', standalone: 'no' }; + for (const name of Object.keys(obj)) { + const expect = (name == 'version' && this._context.length === 0) || (name == 'encoding' && this._context.length > 0); + if ((whitespace || expect) && (expect ? this._expect(name) : this._match(name))) { + this._whitespace(0); + this._expect('='); + this._whitespace(0); + obj[name] = this._attributeValue(); + whitespace = this._whitespace(0); + } + } + this._expect('?>'); + obj.encoding = obj.encoding.toLowerCase(); + if (this._decoder.encoding && obj.encoding !== this._decoder.encoding) { + const position = this._position; + this._decoder = text.Decoder.open(this._data, obj.encoding); + this._seek(position); + } + if (obj.version.length > 0) { + const match = /^(\d)\.(\d)$/.exec(obj.version); + this._assert(match && match[1] === '1', "Invalid XML version '" + obj.version + "'"); + const version = Number.parseInt(match[2], 10); + if (version > this._version) { + /* eslint-disable */ + this._charRegExp = /[\x01-\uD7FF\uE000-\uFFFD]/; + /* eslint-enable */ + this._version = version; + } + this._assert(this._context.length === 0 || this._context.some((context) => context.version >= this._version)); + } + this._assert(obj.standalone === 'no' || (obj.standalone === 'yes' && !this._entity && this._context.length === 0)); + this._standalone = obj.standalone === 'yes'; + } + const node = this._document().createProcessingInstruction(name, data); + this._appendChild(node); + } + + _whitespace(count) { + const position = this._position; + let index = 0; + if (this._implicitSpace) { + index++; + this._implicitSpace = false; + } + while (this._char === ' ' || this._char === '\n' || this._char === '\r' || this._char === '\t' || (this._version > 0 && this._char === '\x85')) { + index++; + this._next(); + } + if (index < count) { + this._seek(position); + this._unexpected(); + } + return index > 0; + } + + _pushResource(identifier, entity, stop) { + const content = this._callback(identifier); + this._pushBuffer(content, identifier, entity, stop); + } + + _pushBuffer(data, base, entity, stop) { + const signature = text.Decoder.open(data); + const decoder = signature.encoding === 'utf-8' ? text.Decoder.open(data, 'utf-8') : signature; + this._pushContext(decoder, data, base, entity, stop, false); + this._data = data; + } + + _pushString(value, entity, stop) { + const decoder = text.Decoder.open(value); + this._pushContext(decoder, value, this._base, entity, stop); + } + + _pushContext(decoder, data, base, entity, stop) { + if (this._context.some((context) => context && context.base === base && context.entity === entity)) { + this._assert(!entity, "Recursive entity '" + entity + "'"); + this._assert(!base, "Recursive base '" + base + "'"); + } + if (base.length !== 0 || entity.length !== 0) { + this._context.push(this._state); + } + this._stop = stop; + this._entity = entity; + this._base = base; + this._data = data; + this._decoder = decoder; + this._prolog = this._decoder.position; + this._char = ''; + this._next(); + } + + _popContext() { + const entity = this._entity; + this._state = this._context.pop(); + if (entity) { + this._expect(';'); + this._implicitSpace = !this._context.some((context) => context.entity); + } + } + + get _state() { + return { + base: this._base, + data: this._data, + decoder: this._decoder, + position: this._position, + version: this._version, + entity: this._entity, + prolog: this._prolog, + stop: this._stop, + }; + } + + set _state(value) { + this._stop = value.stop; + this._base = value.base; + this._data = value.data; + this._decoder = value.decoder; + this._seek(value.position); + this._version = value.version; + this._entity = value.entity; + this._prolog = value.prolog; + } + + _next() { + if (this._char === undefined) { + this._unexpected(); + } + this._position = this._decoder.position; + this._char = this._decoder.decode(); + this._implicitSpace = false; + if (this._parameterEntities && this._char === '%' && (this._entity || this._base)) { + this._resolveParameterEntityReference(); + } + if (!this._characterData) { + if (this._char === '&' && (this._entity || this._base)) { + const c = this._resolveEntityReference(); + if (c !== undefined) { + this._char = c; + } + } + } + if (this._char === '\uffff' || this._char === '\ufffe' || (this._version > 0 && this._char >= '\x7f' && this._char <= '\x9f' && this._char != '\x85')) { + this._unexpected(); + } + if (this._char === undefined) { + if (!this._stop && this._context.length > 0) { + this._popContext(); + } + } + } + + _seek(position) { + this._decoder.position = position; + this._char = ''; + this._next(); + } + + _expect(value) { + if (!this._match(value)) { + this._unexpected(); + } + return true; + } + + _match(value) { + if (this._char !== value[0]) { + return false; + } + if (value.length === 1) { + this._next(); + return true; + } + if (this._context.length === 0) { + const position = this._position; + for (let i = 0; i < value.length; i++) { + if (this._char !== value[i]) { + this._seek(position); + return false; + } + this._next(); + } + return true; + } + const context = Array.from(this._context); + const state = this._state; + for (let i = 0; i < value.length; i++) { + if (this._char !== value[i]) { + this._context = context; + this._state = state; + return false; + } + this._next(); + } + return true; + } + + _assert(value, message, position) { + if (value === false || value === undefined || value === null) { + this._error(message, position); + } + } + + _error(message, position) { + if (position) { + this._parameterEntities = false; + this._characterData = true; + this._seek(position); + } + if (message) { + throw new xml.Error(message + this._location()); + } + this._unexpected(); + } + + _unexpected() { + let c = this._char; + if (c === undefined) { + throw new xml.Error('Unexpected end of XML input.'); + } + else if (c === '"') { + c = 'string'; + } + else if ((c >= '0' && c <= '9') || c === '-') { + c = 'number'; + } + else { + if (c < ' ' || c > '\x7F') { + c = c.codePointAt(0); + if (c < 0x0100) { + c = '\\x' + ('0' + c.toString(16)).slice(-2); + } + else if (c < 0x010000) { + c = '\\u' + ('000' + c.toString(16)).slice(-4); + } + else { + c = '\\u' + ('00000' + c.toString(16)).slice(-6); + } + } + c = "token '" + c + "'"; + } + this._error('Unexpected ' + c); + } + + _location() { + while (typeof this._data === 'string') { + this._popContext(); + } + this._parameterEntities = false; + this._characterData = true; + let line = 1; + let column = 1; + this._decoder.position = 0; + let c; + do { + if (this._decoder.position === this._position) { + break; + } + c = this._decoder.decode(); + if (c === '\n') { + line++; + column = 1; + } + else { + column++; + } + } + while (c !== undefined); + return ' at ' + (this._base ? this._base + ':' : '') + line.toString() + ':' + column.toString() + '.'; + } +}; + +xml.NodeList = class extends Array { + + constructor() { + super(); + } + + item(index) { + return this[index] || null; + } +}; + +xml.Node = class { + + constructor(document, nodeType) { + this._ownerDocument = document; + this._nodeType = nodeType; + this._childNodes = new xml.NodeList(); + } + + get ownerDocument() { + return this._ownerDocument; + } + + get nodeType() { + return this._nodeType; + } + + get localName() { + throw new xml.Error('Not implemented.'); + } + + get namespaceURI() { + return null; + } + + get childNodes() { + return this._childNodes; + } + + get parentNode() { + return this._parentNode; + } + + set parentNode(value) { + this._parentNode = value; + } + + get firstChild() { + return this._firstChild; + } + + set firstChild(value) { + this._firstChild = value; + } + + get lastChild() { + return this._lastChild || null; + } + + set lastChild(value) { + this._lastChild = value; + } + + get previousSibling() { + return this._previousSibling; + } + + set previousSibling(value) { + this._previousSibling = value; + } + + get nextSibling() { + return this._nextSibling; + } + + set nextSibling(value) { + this._nextSibling = value; + } + + appendChild(newChild) { + this.firstChild = this.firstChild || newChild; + newChild.previousSibling = this.lastChild; + if (newChild.previousSibling) { + newChild.previousSibling.nextSibling = newChild; + } + this.lastChild = newChild; + this.childNodes[this.childNodes.length] = newChild; + newChild.parentNode = this; + } + + lookupNamespaceURI(prefix) { + switch (prefix) { + case 'xml': return 'http://www.w3.org/XML/1998/namespace'; + case 'xmlns': return 'http://www.w3.org/2000/xmlns/'; + } + return null; + } +}; + +xml.Element = class extends xml.Node { + + constructor(document, namespaceURI, qualifiedName) { + super(document, xml.NodeType.Element); + this._namespaces = new Map(); + this._attributes = new xml.NamedNodeMap(); + this._namespaceURI = namespaceURI; + if (namespaceURI === null) { + this._prefix = null; + this._localName = qualifiedName; + } + else { + const pair = xml.Utility.split(qualifiedName); + this._prefix = pair[0]; + this._localName = pair[1]; + } + } + + get localName() { + return this._localName; + } + + get prefix() { + return this._prefix; + } + + get namespaceURI() { + return this._namespaceURI; + } + + get attributes() { + return this._attributes; + } + + get textContent() { + return this.childNodes.map((node) => node.nodeType === xml.NodeType.ProcessingInstruction || node.nodeType === xml.NodeType.Comment ? '' : node.textContent).join(''); + } + + getElementsByTagName(tagName) { + const list = new xml.NodeList(); + let node = this.firstChild; + while (node) { + if (node.nodeType === xml.NodeType.Element && (tagName === '*' || tagName === (node.prefix ? node.prefix + ':' + node.localName : node.localName))) { + list.push(node); + } + node = node.nextSibling; + } + return list; + } + + getAttribute(name) { + const node = this.getAttributeNode(name); + return node ? node.value || '' : ''; + } + + getAttributeNode(name) { + return this.attributes.getNamedItem(name); + } + + setAttributeNode(node) { + const oldNode = this.attributes.setNamedItem(node); + if (node.namespaceURI === 'http://www.w3.org/2000/xmlns/') { + const prefix = node.prefix ? node.localName : ''; + this._namespaces.set(prefix, node.value); + } + return oldNode; + } + + lookupNamespaceURI(prefix) { + if (this._namespaces.has(prefix)) { + return this._namespaces.get(prefix); + } + if (this.parentNode) { + return this.parentNode.lookupNamespaceURI(prefix); + } + return super.lookupNamespaceURI(prefix); + } +}; + +xml.Attribute = class extends xml.Node { + + constructor(document, namespaceURI, qualifiedName) { + super(document, xml.NodeType.Attribute); + this._namespaceURI = namespaceURI; + if (namespaceURI === null) { + this._prefix = null; + this._localName = qualifiedName; + } + else { + const pair = xml.Utility.split(qualifiedName); + this._prefix = pair[0]; + this._localName = pair[1]; + } + } + + get ownerElement() { + return this._ownerElement; + } + + set ownerElement(value) { + this._ownerElement = value; + } + + get localName() { + return this._localName; + } + + get prefix() { + return this._prefix; + } + + get namespaceURI() { + return this._namespaceURI; + } + + get value() { + return this._value; + } + + set value(value) { + this._value = value; + } +}; + +xml.CharacterData = class extends xml.Node { + + constructor(document, nodeType, data) { + super(document, nodeType); + this._data = data; + } + + get data() { + return this._data; + } + + get textContent() { + return this._data; + } +}; + +xml.Text = class extends xml.CharacterData { + + constructor(document, data) { + super(document, xml.NodeType.Text, data); + } + + get localName() { + return '#text'; + } +}; + +xml.CDataSection = class extends xml.CharacterData { + + constructor(document, data) { + super(document, xml.NodeType.CDATA, data); + } +}; + +xml.Entity = class extends xml.Node { + + constructor(document, name) { + super(document, xml.NodeType.Entity); + this._name = name; + this._publicId = ''; + this._systemId = ''; + this._notationName = ''; + this._value = ''; + } + + get localName() { + return this._name; + } + + get publicId() { + return this._publicId; + } + + set publicId(value) { + this._publicId = value; + } + + get systemId() { + return this._systemId; + } + + set systemId(value) { + this._systemId = value; + } + + get notationName() { + return this._notationName; + } + + set notationName(value) { + this._notationName = value; + } + + set value(value) { + this._value = value; + } + + get value() { + return this._value; + } +}; + +xml.ProcessingInstruction = class extends xml.Node { + + constructor(document, target, data) { + super(document, xml.NodeType.ProcessingInstruction); + this._target = target; + this._data = data; + } + + get localName() { + return this._target; + } + + get target() { + return this._target; + } + + get data() { + return this._data; + } +}; + +xml.Comment = class extends xml.CharacterData { + + constructor(document, data) { + super(document, xml.NodeType.Comment, data); + } + + get localName() { + return '#comment'; + } +}; + +xml.Document = class extends xml.Node { + + constructor() { + super(null, xml.NodeType.Document); + this._documentElement = null; + this._documentType = null; + } + + get documentElement() { + return this._documentElement; + } + + get documentType() { + return this._documentType; + } + + appendChild(newChild) { + super.appendChild(newChild); + if (newChild.nodeType === xml.NodeType.Element) { + this._documentElement = newChild; + } + if (newChild.nodeType === xml.NodeType.DocumentType) { + this._documentType = newChild; + } + } + + createElement(localName) { + return new xml.Element(this, null, localName); + } + + createElementNS(namespaceURI, qualifiedName) { + return new xml.Element(this, namespaceURI, qualifiedName); + } + + createAttribute(localName) { + return new xml.Attribute(this, null, localName); + } + + createAttributeNS(namespaceURI, qualifiedName) { + return new xml.Attribute(this, namespaceURI, qualifiedName); + } + + createTextNode(data) { + return new xml.Text(this, data); + } + + createCDATASection(data) { + return new xml.CDataSection(this, data); + } + + createProcessingInstruction(target, data) { + return new xml.ProcessingInstruction(this, target, data); + } + + createComment(data) { + return new xml.Comment(this, data); + } + + createDocumentType(qualifiedName, publicId, systemId) { + return new xml.DocumentType(this, qualifiedName, publicId, systemId); + } +}; + +xml.DocumentType = class extends xml.Node { + + constructor(document, qualifiedName, publicId, systemId) { + super(document, xml.NodeType.DocumentType); + this._name = qualifiedName; + this._publicId = publicId; + this._systemId = systemId; + this._entities = new xml.NamedNodeMap(); + } + + get name() { + return this._name; + } + + get publicId() { + return this._publicId; + } + + get systemId() { + return this._systemId; + } + + get entities() { + return this._entities; + } + + appendChild(newChild) { + if (newChild.nodeType === xml.NodeType.Entity) { + this._entities.setNamedItem(newChild); + } + } + + createEntity(name) { + return new xml.Entity(this.ownerDocument, name); + } +}; + +xml.NamedNodeMap = class extends Array { + + getNamedItem(qualifiedName) { + for (let i = this.length - 1; i >= 0; i--) { + const node = this[i]; + const key = node.prefix ? node.prefix + ':' + node.localName : node.localName; + if (qualifiedName == key) { + return node; + } + } + return null; + } + + getNamedItemNS(namespaceURI, localName) { + for (let i = this.length - 1; i >= 0; i--) { + const node = this[i]; + if (localName === node.localName && namespaceURI == node.namespaceURI) { + return node; + } + } + return null; + } + + setNamedItem(node) { + const qualifiedName = node.prefix ? node.prefix + ':' + node.localName : node.localName; + for (let i = this.length - 1; i >= 0; i--) { + const node = this[i]; + const key = node.prefix ? node.prefix + ':' + node.localName : node.localName; + if (qualifiedName == key) { + const oldNode = this[i]; + this[i] = node; + return oldNode; + } + } + this.push(node); + return null; + } +}; + +xml.NodeType = { + None: 0, + Element: 1, + Attribute: 2, + Text: 3, + CDATA: 4, + EntityReference: 5, + Entity: 6, + ProcessingInstruction: 7, + Comment: 8, + Document: 9, + DocumentType: 10, + DocumentFragment: 11, + Notation: 12 +}; + +xml.Utility = class { + + static split(name) { + const index = name.indexOf(':'); + if (index < 0 || index === name.length - 1) { + return [ null, name ]; + } + const localName = name.substring(index + 1); + const c = localName.codePointAt(0); + if (localName.indexOf(':') !== -1 || !xml.Utility.nameStartCharRegExp.test(String.fromCodePoint(c)) && (c < 0x10000 || c > 0xEFFFF)) { + return [ null, name ]; + } + const prefix = name.substring(0, index); + return [ prefix, localName ]; + } +}; + +xml.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'XML Error'; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.TextReader = xml.TextReader; +} diff --git a/zip.js b/zip.js new file mode 100644 index 0000000..ac0e749 --- /dev/null +++ b/zip.js @@ -0,0 +1,713 @@ + +var zip = zip || {}; +var zlib = zlib || {}; + +zip.Archive = class { + + static open(data) { + const stream = data instanceof Uint8Array ? new zip.BinaryReader(data) : data; + if (stream.length > 2) { + const buffer = stream.peek(2); + if (buffer[0] === 0x78) { // zlib + const check = (buffer[0] << 8) + buffer[1]; + if (check % 31 === 0) { + return new zlib.Archive(stream); + } + } + const signature = buffer[0] === 0x50 && buffer[1] === 0x4B; + const position = stream.position; + const seek = (content) => { + let position = stream.length; + do { + position = Math.max(0, position - 66000); + stream.seek(position); + const length = Math.min(stream.length - position, 66000 + 4); + const buffer = stream.read(length); + for (let i = buffer.length - 4; i >= 0; i--) { + if (content[0] === buffer[i] && content[1] === buffer[i + 1] && content[2] === buffer[i + 2] && content[3] === buffer[i + 3]) { + stream.seek(position + i + 4); + return true; + } + } + if (!signature) { + break; + } + } + while (position > 0); + return false; + }; + if (!seek([ 0x50, 0x4B, 0x05, 0x06 ])) { + stream.seek(position); + if (!signature) { + return null; + } + throw new zip.Error('End of Zip central directory not found.'); + } + const reader = new zip.BinaryReader(stream.read(16)); + reader.skip(12); + let offset = reader.uint32(); // central directory offset + if (offset > stream.length) { + if (!seek([ 0x50, 0x4B, 0x06, 0x06 ])) { + stream.seek(position); + throw new zip.Error('End of Zip64 central directory not found.'); + } + const reader = new zip.BinaryReader(stream.read(52)); + reader.skip(44); + offset = reader.uint32(); + if (reader.uint32() !== 0) { + stream.seek(position); + throw new zip.Error('Zip 64-bit central directory offset not supported.'); + } + } + if (offset > stream.length) { + stream.seek(position); + throw new zip.Error('Invalid Zip central directory offset.'); + } + stream.seek(offset); + const archive = new zip.Archive(stream); + stream.seek(position); + return archive; + } + return null; + } + + constructor(stream) { + this._entries = new Map(); + const headers = []; + const signature = [ 0x50, 0x4B, 0x01, 0x02 ]; + while (stream.position + 4 < stream.length && stream.read(4).every((value, index) => value === signature[index])) { + const header = {}; + const reader = new zip.BinaryReader(stream.read(42)); + reader.uint16(); // version made by + reader.skip(2); // version needed to extract + const flags = reader.uint16(); + if ((flags & 1) == 1) { + throw new zip.Error('Encrypted Zip entries not supported.'); + } + header.encoding = flags & 0x800 ? 'utf-8' : 'ascii'; + header.compressionMethod = reader.uint16(); + reader.uint32(); // date + reader.uint32(); // crc32 + header.compressedSize = reader.uint32(); + header.size = reader.uint32(); + header.nameLength = reader.uint16(); // file name length + const extraDataLength = reader.uint16(); + const commentLength = reader.uint16(); + header.disk = reader.uint16(); // disk number start + reader.uint16(); // internal file attributes + reader.uint32(); // external file attributes + header.localHeaderOffset = reader.uint32(); + const nameBuffer = stream.read(header.nameLength); + const decoder = new TextDecoder(header.encoding); + header.name = decoder.decode(nameBuffer); + const extraData = stream.read(extraDataLength); + if (extraData.length > 0) { + const reader = new zip.BinaryReader(extraData); + while (reader.position < reader.length) { + const type = reader.uint16(); + const length = reader.uint16(); + switch (type) { + case 0x0001: + if (header.size === 0xffffffff) { + header.size = reader.uint32(); + if (reader.uint32() !== 0) { + throw new zip.Error('Zip 64-bit offset not supported.'); + } + } + if (header.compressedSize === 0xffffffff) { + header.compressedSize = reader.uint32(); + if (reader.uint32() !== 0) { + throw new zip.Error('Zip 64-bit offset not supported.'); + } + } + if (header.localHeaderOffset === 0xffffffff) { + header.localHeaderOffset = reader.uint32(); + if (reader.uint32() !== 0) { + throw new zip.Error('Zip 64-bit offset not supported.'); + } + } + if (header.disk === 0xffff) { + header.disk = reader.uint32(); + } + break; + default: + reader.skip(length); + break; + } + } + } + stream.read(commentLength); // comment + headers.push(header); + } + for (const header of headers) { + if (header.size === 0 && header.name.endsWith('/')) { + continue; + } + const entry = new zip.Entry(stream, header); + this._entries.set(entry.name, entry.stream); + } + } + + get entries() { + return this._entries; + } +}; + +zip.Entry = class { + + constructor(stream, header) { + stream.seek(header.localHeaderOffset); + const signature = [ 0x50, 0x4B, 0x03, 0x04 ]; + if (stream.position + 4 > stream.length || !stream.read(4).every((value, index) => value === signature[index])) { + throw new zip.Error('Invalid Zip local file header signature.'); + } + const reader = new zip.BinaryReader(stream.read(26)); + reader.skip(22); + header.nameLength = reader.uint16(); + const extraDataLength = reader.uint16(); + header.nameBuffer = stream.read(header.nameLength); + stream.skip(extraDataLength); + const decoder = new TextDecoder(header.encoding); + this._name = decoder.decode(header.nameBuffer); + this._stream = stream.stream(header.compressedSize); + switch (header.compressionMethod) { + case 0: { // stored + if (header.size !== header.compressedSize) { + throw new zip.Error('Invalid compression size.'); + } + break; + } + case 8: { // deflate + this._stream = new zip.InflaterStream(this._stream, header.size); + break; + } + default: + throw new zip.Error('Invalid compression method.'); + } + } + + get name() { + return this._name; + } + + get stream() { + return this._stream; + } +}; + +zip.Inflater = class { + + inflateRaw(data, length) { + let buffer = null; + if (typeof process === 'object' && typeof process.versions == 'object' && typeof process.versions.node !== 'undefined') { + buffer = require('zlib').inflateRawSync(data); + } + else { + const reader = new zip.BitReader(data); + const writer = length === undefined ? new zip.BlockWriter() : new zip.BufferWriter(length); + if (!zip.Inflater._staticLengthTree) { + zip.Inflater._codeLengths = new Uint8Array(19); + zip.Inflater._codeOrder = [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ]; + zip.Inflater._lengthBase = [ 24, 32, 40, 48, 56, 64, 72, 80, 89, 105, 121, 137, 154, 186, 218, 250, 283, 347, 411, 475, 540, 668, 796, 924, 1053, 1309, 1565, 1821, 2064, 7992, 7992, 7992 ]; + zip.Inflater._distanceBase = [ 16, 32, 48, 64, 81, 113, 146, 210, 275, 403, 532, 788, 1045, 1557, 2070, 3094, 4119, 6167, 8216, 12312, 16409, 24601, 32794, 49178, 65563, 98331, 131100, 196636, 262173, 393245, 1048560, 1048560 ]; + } + let type; + do { + type = reader.bits(3); + switch (type >>> 1) { + case 0: { // uncompressed block + this._copyUncompressedBlock(reader, writer); + break; + } + case 1: { // block with fixed huffman trees + if (!zip.Inflater._staticLengthTree) { + zip.Inflater._staticLengthTree = zip.HuffmanTree.create(new Uint8Array([].concat.apply([], [[144, 8], [112, 9], [24, 7], [8, 8]].map((x) => [...Array(x[0])].map(() => x[1]))))); + zip.Inflater._staticDistanceTree = zip.HuffmanTree.create(new Uint8Array([...Array(32)].map(() => 5))); + } + this._lengthTree = zip.Inflater._staticLengthTree; + this._distanceTree = zip.Inflater._staticDistanceTree; + this._inflateBlock(reader, writer); + break; + } + case 2: { // block with dynamic huffman trees + this._decodeTrees(reader); + this._inflateBlock(reader, writer); + break; + } + default: { + throw new zip.Error('Unknown block type.'); + } + } + } while ((type & 1) == 0); + if (length !== undefined && length !== writer.length) { + throw new zip.Error('Invalid uncompressed size.'); + } + buffer = writer.toBuffer(); + } + if (length !== undefined && length !== buffer.length) { + throw new zip.Error('Invalid uncompressed size.'); + } + return buffer; + } + + _copyUncompressedBlock(reader, writer) { + const length = reader.uint16(); + const inverseLength = reader.uint16(); + if (length !== (~inverseLength & 0xffff)) { + throw new zip.Error('Invalid uncompressed block length.'); + } + writer.write(reader.read(length)); + } + + _decodeTrees(reader) { + const hlit = reader.bits(5) + 257; + const hdist = reader.bits(5) + 1; + const hclen = reader.bits(4) + 4; + const codeLengths = zip.Inflater._codeLengths; + for (let i = 0; i < codeLengths.length; i++) { + codeLengths[i] = 0; + } + const codeOrder = zip.Inflater._codeOrder; + for (let i = 0; i < hclen; i++) { + codeLengths[codeOrder[i]] = reader.bits(3); + } + const codeTree = zip.HuffmanTree.create(codeLengths); + const codeMask = codeTree.length - 1; + const lengths = new Uint8Array(hlit + hdist); + let value = 0; + let length = 0; + for (let i = 0; i < hlit + hdist;) { + const code = codeTree[reader.bits16() & codeMask]; + reader.position += code & 0x0f; + const literal = code >>> 4; + switch (literal) { + case 16: length = reader.bits(2) + 3; break; + case 17: length = reader.bits(3) + 3; value = 0; break; + case 18: length = reader.bits(7) + 11; value = 0; break; + default: length = 1; value = literal; break; + } + for (; length > 0; length--) { + lengths[i++] = value; + } + } + this._lengthTree = zip.HuffmanTree.create(lengths.subarray(0, hlit)); + this._distanceTree = zip.HuffmanTree.create(lengths.subarray(hlit, hlit + hdist)); + } + + _inflateBlock(reader, writer) { + const lengthTree = this._lengthTree; + const distanceTree = this._distanceTree; + const lengthMask = lengthTree.length - 1; + const distanceMask = distanceTree.length - 1; + const buffer = writer.buffer; + const threshold = writer.threshold !== undefined ? writer.threshold : writer.length; + let position = writer.position; + for (;;) { + if (position > threshold) { + position = writer.push(position); + } + const code = lengthTree[reader.bits16() & lengthMask]; + reader.position += code & 0x0f; + const literal = code >>> 4; + if (literal < 256) { + buffer[position++] = literal; + } + else if (literal === 256) { + writer.push(position); + return; + } + else { + let length = literal - 254; + if (literal > 264) { + const lengthBase = zip.Inflater._lengthBase[literal - 257]; + length = (lengthBase >>> 3) + reader.bits(lengthBase & 0x07); + } + const code = distanceTree[reader.bits16() & distanceMask]; + reader.position += code & 0x0f; + const distanceBase = zip.Inflater._distanceBase[code >>> 4]; + const bits = distanceBase & 0x0f; + const distance = (distanceBase >>> 4) + (reader.bits16() & ((1 << bits) - 1)); + reader.position += bits; + let offset = position - distance; + for (let i = 0; i < length; i++) { + buffer[position++] = buffer[offset++]; + } + } + } + } +}; + +zip.HuffmanTree = class { + + static create(tree) { + let bits = tree[0]; + for (let i = 1; i < tree.length; ++i) { + if (tree[i] > bits) { + bits = tree[i]; + } + } + // Algorithm from https://github.com/photopea/UZIP.js + let rev15 = zip.HuffmanTree._rev15; + if (!rev15) { + const length = 1 << 15; + rev15 = new Uint16Array(length); + for (let i = 0; i < length; i++) { + let x = i; + x = (((x & 0xaaaaaaaa) >>> 1) | ((x & 0x55555555) << 1)); + x = (((x & 0xcccccccc) >>> 2) | ((x & 0x33333333) << 2)); + x = (((x & 0xf0f0f0f0) >>> 4) | ((x & 0x0f0f0f0f) << 4)); + x = (((x & 0xff00ff00) >>> 8) | ((x & 0x00ff00ff) << 8)); + rev15[i] = (((x >>> 16) | (x << 16))) >>> 17; + } + zip.HuffmanTree._rev15 = rev15; + zip.HuffmanTree._bitLengthCounts = new Uint16Array(16); + zip.HuffmanTree._nextCodes = new Uint16Array(16); + } + const length = tree.length; + const bitLengthCounts = zip.HuffmanTree._bitLengthCounts; + for (let i = 0; i < 16; i++) { + bitLengthCounts[i] = 0; + } + for (let i = 0; i < length; i++) { + bitLengthCounts[tree[i]]++; + } + const nextCodes = zip.HuffmanTree._nextCodes; + let code = 0; + bitLengthCounts[0] = 0; + for (let i = 0; i < bits; i++) { + code = (code + bitLengthCounts[i]) << 1; + nextCodes[i + 1] = code; + } + const codes = new Uint16Array(length); + for (let i = 0; i < length; i++) { + const index = tree[i]; + if (index !== 0) { + codes[i] = nextCodes[index]; + nextCodes[index]++; + } + } + const shift = 15 - bits; + const table = new Uint16Array(1 << bits); + for (let i = 0; i < length; i++) { + const c = tree[i]; + if (c !== 0) { + const value = (i << 4) | c; + const rest = bits - c; + let index = codes[i] << rest; + const max = index + (1 << rest); + for (; index != max; index++) { + table[rev15[index] >>> shift] = value; + } + } + } + return table; + } +}; + +zip.BitReader = class { + + constructor(buffer) { + this.buffer = buffer; + this.position = 0; + } + + bits(count) { + const offset = (this.position / 8) >> 0; + const shift = (this.position & 7); + this.position += count; + return ((this.buffer[offset] | (this.buffer[offset + 1] << 8)) >>> shift) & ((1 << count) - 1); + } + + bits16() { + const offset = (this.position / 8) >> 0; + return ((this.buffer[offset] | (this.buffer[offset + 1] << 8) | (this.buffer[offset + 2] << 16)) >>> (this.position & 7)); + } + + read(length) { + this.position = (this.position + 7) & ~7; // align + const offset = (this.position / 8) >> 0; + this.position += length * 8; + return this.buffer.subarray(offset, offset + length); + } + + uint16() { + this.position = (this.position + 7) & ~7; // align + const offset = (this.position / 8) >> 0; + this.position += 16; + return this.buffer[offset] | (this.buffer[offset + 1] << 8); + } +}; + +zip.BlockWriter = class { + + constructor() { + this.blocks = []; + this.buffer = new Uint8Array(65536); + this.position = 0; + this.length = 0; + this.threshold = 0xf400; + } + + push(position) { + this.blocks.push(new Uint8Array(this.buffer.subarray(this.position, position))); + this.length += position - this.position; + this.position = position; + return this._reset(); + } + + write(buffer) { + this.blocks.push(buffer); + const length = buffer.length; + this.length += length; + if (length > 32768) { + this.buffer.set(buffer.subarray(length - 32768, length), 0); + this.position = 32768; + } + else { + this._reset(); + this.buffer.set(buffer, this.position); + this.position += length; + } + } + + toBuffer() { + const buffer = new Uint8Array(this.length); + let offset = 0; + for (const block of this.blocks) { + buffer.set(block, offset); + offset += block.length; + } + return buffer; + } + + _reset() { + if (this.position > 32768) { + this.buffer.set(this.buffer.subarray(this.position - 32768, this.position), 0); + this.position = 32768; + } + return this.position; + } +}; + +zip.BufferWriter = class { + + constructor(length) { + this.buffer = new Uint8Array(length); + this.length = length; + this.position = 0; + } + + + push(position) { + this.position = position; + if (this.position > this.length) { + throw new zip.Error('Invalid size.'); + } + return this.position; + } + + write(buffer) { + this.buffer.set(buffer, this.position); + this.position += buffer.length; + if (this.position > this.length) { + throw new zip.Error('Invalid size.'); + } + return this.position; + } + + toBuffer() { + return this.buffer; + } +}; + +zip.InflaterStream = class { + + constructor(stream, length) { + this._stream = stream; + this._offset = this._stream.position; + this._position = 0; + this._length = length; + } + + get position() { + return this._position; + } + + get length() { + if (this._length === undefined) { + this._inflate(); + } + return this._length; + } + + seek(position) { + if (this._buffer === undefined) { + this._inflate(); + } + this._position = position >= 0 ? position : this._length + position; + } + + skip(offset) { + if (this._buffer === undefined) { + this._inflate(); + } + this._position += offset; + } + + peek(length) { + const position = this._position; + length = length !== undefined ? length : this._length - position; + this.skip(length); + const end = this._position; + this.seek(position); + if (position === 0 && length === this._length) { + return this._buffer; + } + return this._buffer.subarray(position, end); + } + + read(length) { + const position = this._position; + length = length !== undefined ? length : this._length - position; + this.skip(length); + if (position === 0 && length === this._length) { + return this._buffer; + } + return this._buffer.subarray(position, this._position); + } + + byte() { + const position = this._position; + this.skip(1); + return this._buffer[position]; + } + + _inflate() { + if (this._buffer === undefined) { + const position = this._stream.position; + this._stream.seek(this._offset); + const buffer = this._stream.peek(); + this._buffer = new zip.Inflater().inflateRaw(buffer, this._length); + this._length = this._buffer.length; + this._stream.seek(position); + delete this._stream; + } + } +}; + +zip.BinaryReader = class { + + constructor(buffer) { + this._buffer = buffer; + this._length = buffer.length; + this._position = 0; + this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); + } + + get position() { + return this._position; + } + + get length() { + return this._length; + } + + create(buffer) { + return new zip.BinaryReader(buffer); + } + + stream(length) { + return this.create(this.read(length)); + } + + seek(position) { + this._position = position >= 0 ? position : this._length + position; + } + + skip(offset) { + this._position += offset; + } + + peek(length) { + if (this._position === 0 && length === undefined) { + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + const end = this._position; + this.seek(position); + return this._buffer.subarray(position, end); + } + + read(length) { + if (this._position === 0 && length === undefined) { + this._position = this._length; + return this._buffer; + } + const position = this._position; + this.skip(length !== undefined ? length : this._length - this._position); + return this._buffer.subarray(position, this._position); + } + + byte() { + const position = this._position; + this.skip(1); + return this._buffer[position]; + } + + uint16() { + const position = this._position; + this.skip(2); + return this._view.getUint16(position, true); + } + + uint32() { + const position = this._position; + this.skip(4); + return this._view.getUint32(position, true); + } +}; + +zlib.Archive = class { + + constructor(stream) { + const position = stream.position; + stream.read(2); + const entry = new zlib.Entry(stream); + this._entries = new Map([ [ entry.name, entry.stream ] ]); + stream.seek(position); + } + + get entries() { + return this._entries; + } +}; + +zlib.Entry = class { + + constructor(stream) { + this._stream = new zip.InflaterStream(stream); + } + + get name() { + return ''; + } + + get stream() { + return this._stream; + } +}; + +zip.Error = class extends Error { + + constructor(message) { + super(message); + this.name = 'Zip Error'; + this.stack = undefined; + } +}; + +if (typeof module !== 'undefined' && typeof module.exports === 'object') { + module.exports.Archive = zip.Archive; + module.exports.Inflater = zip.Inflater; +} \ No newline at end of file