[ { "name": "Abs", "module": "ai.onnx", "version": 1, "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 = np.abs(x)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_abs\")" } ] }, { "name": "Abs", "module": "ai.onnx", "version": 6, "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 = np.abs(x)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_abs\")" } ] }, { "name": "Abs", "module": "ai.onnx", "version": 13, "description": "Absolute takes one input data (Tensor) and produces one output data\n(Tensor) where absolute value, 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 = np.abs(x)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_abs\")" } ] }, { "name": "Acos", "module": "ai.onnx", "version": 7, "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], 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], name=\"test_acos\")" } ] }, { "name": "Acos", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_acos\")" } ] }, { "name": "Acosh", "module": "ai.onnx", "version": 9, "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], 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], name=\"test_acosh\")" } ] }, { "name": "Acosh", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_acosh\")" } ] }, { "name": "Adagrad", "module": "ai.onnx.preview.training", "version": 1, "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(\n \"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(\n r, t, x, g, h, norm_coefficient, epsilon, decay_factor\n)\n\n# Check results.\nexpect(\n node,\n inputs=[r, t, x, g, h],\n outputs=[x_new, h_new],\n name=\"test_adagrad\",\n opset_imports=[\n onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)\n ],\n)" }, { "summary": "adagrad_multiple", "code": "# Define operator attributes.\nnorm_coefficient = 0.001\nepsilon = 1e-5\ndecay_factor = 0.1\n\nnode = onnx.helper.make_node(\n \"Adagrad\",\n inputs=[\"R\", \"T\", \"X1\", \"X2\", \"G1\", \"G2\", \"H1\", \"H2\"],\n outputs=[\"X1_new\", \"X2_new\", \"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(\n r, t, x1, g1, h1, norm_coefficient, epsilon, decay_factor\n)\nx2_new, h2_new = apply_adagrad(\n r, t, x2, g2, h2, norm_coefficient, epsilon, decay_factor\n)\n\n# Check results.\nexpect(\n node,\n inputs=[r, t, x1, x2, g1, g2, h1, h2],\n outputs=[x1_new, x2_new, h1_new, h2_new],\n name=\"test_adagrad_multiple\",\n opset_imports=[\n onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)\n ],\n)" } ] }, { "name": "Adam", "module": "ai.onnx.preview.training", "version": 1, "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(\n \"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(\n r, t, x, g, v, h, norm_coefficient, 0.0, alpha, beta, epsilon\n)\n\n# Check results.\nexpect(\n node,\n inputs=[r, t, x, g, v, h],\n outputs=[x_new, v_new, h_new],\n name=\"test_adam\",\n opset_imports=[\n onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)\n ],\n)" }, { "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(\n \"Adam\",\n 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 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\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(\n r, t, x1, g1, v1, h1, norm_coefficient, 0.0, alpha, beta, epsilon\n)\nx2_new, v2_new, h2_new = apply_adam(\n r, t, x2, g2, v2, h2, norm_coefficient, 0.0, alpha, beta, epsilon\n)\n\n# Check results.\nexpect(\n node,\n 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=[\n onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)\n ],\n)" } ] }, { "name": "Add", "module": "ai.onnx", "version": 1, "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], name=\"test_add\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int16\")\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], name=\"test_add_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint64\")" }, { "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], name=\"test_add_bcast\")" } ] }, { "name": "Add", "module": "ai.onnx", "version": 6, "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], name=\"test_add\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int16\")\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], name=\"test_add_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint64\")" }, { "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], name=\"test_add_bcast\")" } ] }, { "name": "Add", "module": "ai.onnx", "version": 7, "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], name=\"test_add\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int16\")\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], name=\"test_add_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint64\")" }, { "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], name=\"test_add_bcast\")" } ] }, { "name": "Add", "module": "ai.onnx", "version": 13, "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], name=\"test_add\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int16\")\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], name=\"test_add_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint64\")" }, { "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], name=\"test_add_bcast\")" } ] }, { "name": "Add", "module": "ai.onnx", "version": 14, "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], name=\"test_add\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_int16\")\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], name=\"test_add_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nexpect(node, inputs=[x, y], outputs=[x + y], name=\"test_add_uint64\")" }, { "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], name=\"test_add_bcast\")" } ] }, { "name": "Affine", "module": "ai.onnx", "version": 1, "description": "Affine takes one input data (Tensor) and produces one output data\n(Tensor) where the affine function, y = alpha * x + beta,\nis applied to the tensor elementwise.\n", "attributes": [ { "name": "alpha", "type": "float32", "required": false, "default": 1.0, "description": "Value of alpha" }, { "name": "beta", "type": "float32", "required": false, "description": "Value of beta" } ], "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)" ] } ] }, { "name": "Affine", "module": "ai.onnx", "version": 10, "description": "Affine takes one input data (Tensor) and produces one output data\n(Tensor) where the affine function, y = alpha * x + beta,\nis applied to the tensor elementwise.\n", "attributes": [ { "name": "alpha", "type": "float32", "required": false, "default": 1.0, "description": "Value of alpha" }, { "name": "beta", "type": "float32", "required": false, "description": "Value of beta" } ], "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)" ] } ] }, { "name": "AffineGrid", "module": "ai.onnx", "version": 20, "description": "Generates a 2D or 3D flow field (sampling grid), given a batch of affine matrices theta\n(https://pytorch.org/docs/stable/generated/torch.nn.functional.affine_grid.html).\nAn affine matrix `theta` is applied to a position tensor represented in its homogeneous expression. Here is an example in 3D:\n```\n[r00, r01, r02, t0] [x] [x']\n[r10, r11, r12, t1] * [y] = [y']\n[r20, r21, r22, t2] [z] [z']\n[0, 0, 0, 1 ] [1] [1 ]\n```\nwhere `(x, y, z)` is the position in the original space, `(x', y', z')` is the position in the output space.\nThe last row is always `[0, 0, 0, 1]` and is not stored in the affine matrix. Therefore we have `theta` of shape `(N, 2, 3)` for 2D or `(N, 3, 4)` for 3D.\n\nInput `size` is used to define grid of positions evenly spaced in the original 2D or 3D space, with dimensions ranging from `-1` to `1`.\nThe output `grid` contains positions in the output space.\n\nWhen `align_corners=1`, consider `-1` and `1` to refer to the centers of the corner pixels (mark `v` in illustration).\n```\nv v v v\n|-------------------|------------------|\n-1 0 1\n```\nWhen `align_corners=0`, consider `-1` and `1` to refer to the outer edge of the corner pixels.\n```\n v v v v\n|------------------|-------------------|\n-1 0 1\n```\n", "attributes": [ { "name": "align_corners", "type": "int64", "required": false, "description": "if align_corners=1, consider -1 and 1 to refer to the centers of the corner pixels. if align_corners=0, consider -1 and 1 to refer to the outer edge the corner pixels." } ], "inputs": [ { "name": "theta", "type": "T1", "description": "input batch of affine matrices with shape (N, 2, 3) for 2D or (N, 3, 4) for 3D" }, { "name": "size", "type": "T2", "description": "the target output image size (N, C, H, W) for 2D or (N, C, D, H, W) for 3D" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "grid", "type": "T1", "description": "output tensor of shape (N, H, W, 2) of 2D sample coordinates or (N, D, H, W, 3) of 3D sample coordinates." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain grid types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] }, { "description": "Constrain size's type to int64 tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int64)" ] } ], "examples": [ { "summary": "2d_no_reference_evaluator", "code": "theta_2d = create_theta_2d()\nN, C, H, W = len(theta_2d), 3, 5, 6\ndata_size = (H, W)\nfor align_corners in (0, 1):\n node = onnx.helper.make_node(\n \"AffineGrid\",\n inputs=[\"theta\", \"size\"],\n outputs=[\"grid\"],\n align_corners=align_corners,\n )\n\n original_grid = construct_original_grid(data_size, align_corners)\n grid = apply_affine_transform(theta_2d, original_grid)\n\n test_name = \"test_affine_grid_2d\"\n if align_corners == 1:\n test_name += \"_align_corners\"\n expect(\n node,\n inputs=[theta_2d, np.array([N, C, H, W], dtype=np.int64)],\n outputs=[grid],\n name=test_name,\n )" }, { "summary": "3d_no_reference_evaluator", "code": "theta_3d = create_theta_3d()\nN, C, D, H, W = len(theta_3d), 3, 4, 5, 6\ndata_size = (D, H, W)\nfor align_corners in (0, 1):\n node = onnx.helper.make_node(\n \"AffineGrid\",\n inputs=[\"theta\", \"size\"],\n outputs=[\"grid\"],\n align_corners=align_corners,\n )\n\n original_grid = construct_original_grid(data_size, align_corners)\n grid = apply_affine_transform(theta_3d, original_grid)\n\n test_name = \"test_affine_grid_3d\"\n if align_corners == 1:\n test_name += \"_align_corners\"\n expect(\n node,\n inputs=[theta_3d, np.array([N, C, D, H, W], dtype=np.int64)],\n outputs=[grid],\n name=test_name,\n )" } ] }, { "name": "And", "module": "ai.onnx", "version": 1, "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], 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], 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], 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], 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], 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], 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], 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], name=\"test_and_bcast4v4d\")" } ] }, { "name": "And", "module": "ai.onnx", "version": 7, "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], 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], 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], 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], 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], 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], 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], 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], name=\"test_and_bcast4v4d\")" } ] }, { "name": "ArgMax", "module": "ai.onnx", "version": 1, "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, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], keepdims=keepdims\n)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_random\",\n)" }, { "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\n# result: [[1, 1]]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_random_select_last_index\",\n)" }, { "summary": "keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node, inputs=[data], outputs=[result], name=\"test_argmax_keepdims_example\"\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmax_keepdims_random\"\n)" }, { "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)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_keepdims_random_select_last_index\",\n)" }, { "summary": "negative_axis_keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_random\",\n)" }, { "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)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_random_select_last_index\",\n)" }, { "summary": "no_keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [0, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_example\",\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmax_no_keepdims_random\"\n)" }, { "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)\n# result: [1, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_random_select_last_index\",\n)" } ] }, { "name": "ArgMax", "module": "ai.onnx", "version": 11, "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 input tensor must not be empty.\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, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], keepdims=keepdims\n)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_random\",\n)" }, { "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\n# result: [[1, 1]]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_random_select_last_index\",\n)" }, { "summary": "keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node, inputs=[data], outputs=[result], name=\"test_argmax_keepdims_example\"\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmax_keepdims_random\"\n)" }, { "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)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_keepdims_random_select_last_index\",\n)" }, { "summary": "negative_axis_keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_random\",\n)" }, { "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)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_random_select_last_index\",\n)" }, { "summary": "no_keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [0, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_example\",\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmax_no_keepdims_random\"\n)" }, { "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)\n# result: [1, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_random_select_last_index\",\n)" } ] }, { "name": "ArgMax", "module": "ai.onnx", "version": 12, "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, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], keepdims=keepdims\n)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_random\",\n)" }, { "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\n# result: [[1, 1]]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_random_select_last_index\",\n)" }, { "summary": "keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node, inputs=[data], outputs=[result], name=\"test_argmax_keepdims_example\"\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmax_keepdims_random\"\n)" }, { "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)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_keepdims_random_select_last_index\",\n)" }, { "summary": "negative_axis_keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_random\",\n)" }, { "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)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_random_select_last_index\",\n)" }, { "summary": "no_keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [0, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_example\",\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmax_no_keepdims_random\"\n)" }, { "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)\n# result: [1, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_random_select_last_index\",\n)" } ] }, { "name": "ArgMax", "module": "ai.onnx", "version": 13, "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, 2], [3, 10]], dtype=np.float32)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], keepdims=keepdims\n)\n\n# result: [[1, 1]]\nresult = argmax_use_numpy(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_random\",\n)" }, { "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\n# result: [[1, 1]]\nresult = argmax_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_default_axis_random_select_last_index\",\n)" }, { "summary": "keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node, inputs=[data], outputs=[result], name=\"test_argmax_keepdims_example\"\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmax_keepdims_random\"\n)" }, { "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)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_keepdims_random_select_last_index\",\n)" }, { "summary": "negative_axis_keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = -1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [[0], [1]]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_random\",\n)" }, { "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)\n# result: [[1], [1]]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_negative_axis_keepdims_random_select_last_index\",\n)" }, { "summary": "no_keepdims", "code": "data = np.array([[2, 2], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 0\nnode = onnx.helper.make_node(\n \"ArgMax\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# result: [0, 1]\nresult = argmax_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_example\",\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmax_no_keepdims_random\"\n)" }, { "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)\n# result: [1, 1]\nresult = argmax_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmax_no_keepdims_random_select_last_index\",\n)" } ] }, { "name": "ArgMin", "module": "ai.onnx", "version": 1, "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\", inputs=[\"data\"], outputs=[\"result\"], keepdims=keepdims\n)\n\n# The content of result is : [[0], [0]]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_random\",\n)" }, { "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\n# result: [[0, 0]]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_random_select_last_index\",\n)" }, { "summary": "keepdims", "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMin\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node, inputs=[data], outputs=[result], name=\"test_argmin_keepdims_example\"\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmin_keepdims_random\"\n)" }, { "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)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_keepdims_random_select_last_index\",\n)" }, { "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\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_random\",\n)" }, { "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)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_random_select_last_index\",\n)" }, { "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\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1, 0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_example\",\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmin_no_keepdims_random\"\n)" }, { "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)\n# result: [[1, 0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_random_select_last_index\",\n)" } ] }, { "name": "ArgMin", "module": "ai.onnx", "version": 11, "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 input tensor must not be empty.\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\", inputs=[\"data\"], outputs=[\"result\"], keepdims=keepdims\n)\n\n# The content of result is : [[0], [0]]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_random\",\n)" }, { "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\n# result: [[0, 0]]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_random_select_last_index\",\n)" }, { "summary": "keepdims", "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMin\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node, inputs=[data], outputs=[result], name=\"test_argmin_keepdims_example\"\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmin_keepdims_random\"\n)" }, { "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)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_keepdims_random_select_last_index\",\n)" }, { "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\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_random\",\n)" }, { "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)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_random_select_last_index\",\n)" }, { "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\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1, 0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_example\",\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmin_no_keepdims_random\"\n)" }, { "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)\n# result: [[1, 0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_random_select_last_index\",\n)" } ] }, { "name": "ArgMin", "module": "ai.onnx", "version": 12, "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\", inputs=[\"data\"], outputs=[\"result\"], keepdims=keepdims\n)\n\n# The content of result is : [[0], [0]]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_random\",\n)" }, { "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\n# result: [[0, 0]]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_random_select_last_index\",\n)" }, { "summary": "keepdims", "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMin\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node, inputs=[data], outputs=[result], name=\"test_argmin_keepdims_example\"\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmin_keepdims_random\"\n)" }, { "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)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_keepdims_random_select_last_index\",\n)" }, { "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\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_random\",\n)" }, { "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)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_random_select_last_index\",\n)" }, { "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\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1, 0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_example\",\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmin_no_keepdims_random\"\n)" }, { "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)\n# result: [[1, 0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_random_select_last_index\",\n)" } ] }, { "name": "ArgMin", "module": "ai.onnx", "version": 13, "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\", inputs=[\"data\"], outputs=[\"result\"], keepdims=keepdims\n)\n\n# The content of result is : [[0], [0]]\nresult = argmin_use_numpy(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_random\",\n)" }, { "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\n# result: [[0, 0]]\nresult = argmin_use_numpy_select_last_index(data, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_default_axis_random_select_last_index\",\n)" }, { "summary": "keepdims", "code": "data = np.array([[2, 1], [3, 10]], dtype=np.float32)\naxis = 1\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ArgMin\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node, inputs=[data], outputs=[result], name=\"test_argmin_keepdims_example\"\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmin_keepdims_random\"\n)" }, { "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)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_keepdims_random_select_last_index\",\n)" }, { "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\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1], [0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_random\",\n)" }, { "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)\n# result: [[1], [0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_negative_axis_keepdims_random_select_last_index\",\n)" }, { "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\", inputs=[\"data\"], outputs=[\"result\"], axis=axis, keepdims=keepdims\n)\n# The content of result is : [[1, 0]]\nresult = argmin_use_numpy(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_example\",\n)\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(\n node, inputs=[data], outputs=[result], name=\"test_argmin_no_keepdims_random\"\n)" }, { "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)\n# result: [[1, 0]]\nresult = argmin_use_numpy_select_last_index(data, axis=axis, keepdims=keepdims)\nexpect(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_example_select_last_index\",\n)\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(\n node,\n inputs=[data],\n outputs=[result],\n name=\"test_argmin_no_keepdims_random_select_last_index\",\n)" } ] }, { "name": "ArrayFeatureExtractor", "module": "ai.onnx.ml", "version": 1, "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)" ] } ], "examples": [ { "summary": "arrayfeatureextractor", "code": "node = onnx.helper.make_node(\n \"ArrayFeatureExtractor\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n domain=\"ai.onnx.ml\",\n)\n\nx = np.arange(12).reshape((3, 4)).astype(np.float32)\ny = np.array([0, 1], dtype=np.int64)\nz = np.array([[0, 4, 8], [1, 5, 9]], dtype=np.float32).T\nexpect(\n node,\n inputs=[x, y],\n outputs=[z],\n name=\"test_ai_onnx_ml_array_feature_extractor\",\n)" } ] }, { "name": "Asin", "module": "ai.onnx", "version": 7, "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], 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], name=\"test_asin\")" } ] }, { "name": "Asin", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_asin\")" } ] }, { "name": "Asinh", "module": "ai.onnx", "version": 9, "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], 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], name=\"test_asinh\")" } ] }, { "name": "Asinh", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_asinh\")" } ] }, { "name": "Atan", "module": "ai.onnx", "version": 7, "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], 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], name=\"test_atan\")" } ] }, { "name": "Atan", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_atan\")" } ] }, { "name": "Atanh", "module": "ai.onnx", "version": 9, "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], 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], name=\"test_atanh\")" } ] }, { "name": "Atanh", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_atanh\")" } ] }, { "name": "Attention", "module": "ai.onnx", "version": 23, "description": "Computes scaled dot product attention on query, key and value tensors, using an optional attention mask if passed.\n\nThis operator covers self and cross variants of the attention operation based on sequence lengths of K, Q and V.\n\nFor self attention, `kv_sequence_length` equals to `q_sequence_length`.\n\nFor cross attention, query and key might have different lengths.\n\nThis operator also covers the 3 following variants based on the number of heads:\n1) Multi-headed Attention (MHA): Described in the paper https://arxiv.org/pdf/1706.03762, `q_num_heads = kv_num_heads`.\n2) Group-query Attention (GQA): Described in the paper https://arxiv.org/pdf/2305.13245, `q_num_heads > kv_num_heads`, `q_num_heads % kv_num_heads == 0`.\n3) Multi-query Attention (MQA): Described in the paper https://arxiv.org/pdf/1911.02150, `q_num_heads > kv_num_heads`, `kv_num_heads=1`.\n\nAttention bias to be added is calculated based on `attn_mask` input and `is_causal` attribute:\n1) `attn_mask`: A boolean mask where a value of `True` indicates that the element should take part in attention or a float mask of the same type as query, key, value that is added to the attention score.\n2) If `is_causal` is set to `1`, causal masking is applied with bottom-right (offset-aware) alignment: query `i` attends key `j` iff `j <= i + past_sequence_length` (the count of cached keys in `past_key`); for a square Q/K this is the standard lower-triangular mask. The causal frontier is computed independently of the `attn_mask` input and is then composed with it additively, by summing their attention biases: a boolean `attn_mask` intersects the allowed set (its disallowed positions contribute `-inf` to the bias), while a float `attn_mask` is added to the attention scores rather than disabling positions. A fully-masked query row (every key's combined additive bias is `-inf`, e.g. an all-`False` boolean `attn_mask` row) produces a zero output row (matching prevailing runtime practice), not `NaN`.\n\nErrata (in-place behavioral correction, no opset bump): a fully-masked query row (e.g. an all-`False` boolean `attn_mask` row) now produces a zero output row instead of `NaN`, and the same zero-row guard applies to the mode-`3` `qk_matmul_output` debug output; this only replaces previously-`NaN` outputs. The mode-`3` `qk_matmul_output` is also now emitted at the operator's output precision (`T1`), matching the reference implementation, which affects only its dtype and only when `softmax_precision` differs from `T1`. No numerically useful, well-defined result of the released opset is otherwise changed.\n\nBoth past and present state key/values are optional. They shall be used together, and not allowed to use only one of them.\nThe following pattern is applied to the Q, K and V inputs after appropriate reshaping of K and V inputs based on sequence lengths and num heads provided:\n\n```\n The following pattern is applied by this operator:\n Q K V\n | | |\nQ*sqrt(scale) K*sqrt(scale) |\n | | |\n | Transpose |\n | | |\n ---MatMul--- |\n | |\n softcap (if provided) |\n | |\n at_mask---Add |\n | |\n Softmax |\n | |\n -----MatMul------\n |\n Y\n```\n\n", "attributes": [ { "name": "is_causal", "type": "int64", "required": false, "description": "If set to `1`, causal masking is applied with bottom-right (offset-aware) alignment: query `i` attends key `j` iff `j <= i + past_sequence_length` (the count of cached keys in `past_key`); for a square Q/K this is the standard lower-triangular mask." }, { "name": "kv_num_heads", "type": "int64", "required": false, "description": "Number of heads of key and value. Must be used with 3D inputs of Q, K and V. " }, { "name": "q_num_heads", "type": "int64", "required": false, "description": "Number of heads of query. Must be used with 3D inputs of Q, K and V. " }, { "name": "qk_matmul_output_mode", "type": "int64", "required": false, "description": "If set to `0`, qk_matmul_output is the output of qk matmul. If set to `1`, qk_matmul_output is the output after the softcap operation (before mask addition). If set to `2`, qk_matmul_output includes the attention mask and softcap (if provided) applied to the output of qk matmul. If set to `3`, qk_matmul_output is the output after the softmax operation. In mode `3`, a fully-masked query row (every key disallowed, e.g. an all-`False` boolean `attn_mask` row) is a zero row, consistent with the corresponding row of the primary output `Y`: the fully-masked-row guard is applied before this output is produced. The mode-`3` output is emitted at the operator's output precision (`T1`); when `softmax_precision` differs from `T1` this is a cast of the softmax result to `T1`. Default value is 0." }, { "name": "scale", "type": "float32", "required": false, "description": "Scaling factor applied to $Q*K^T$. Default value is `1/sqrt(head_size)`. To prevent [numerical overflow](https://tinyurl.com/sudb9s96), scale `Q`, `K` by `sqrt(scale)` before matmul." }, { "name": "softcap", "type": "float32", "required": false, "description": "Softcap value for attention weights. Default value is 0." }, { "name": "softmax_precision", "type": "int64", "required": false, "description": "The floating-point precision used in softmax computation. If softmax precision is not provided, the same precision as the input of softmax (Q and K) is used." } ], "inputs": [ { "name": "Q", "type": "T1", "description": "Query tensor. 4D tensor with shape `(batch_size, q_num_heads, q_sequence_length, head_size)` or 3D tensor with shape `(batch_size, q_sequence_length, q_hidden_size)`. For cases with a 3D input tensor, `q_hidden_size = q_num_heads * head_size`" }, { "name": "K", "type": "T1", "description": "Key tensor. 4D tensor with shape `(batch_size, kv_num_heads, kv_sequence_length, head_size)` or 3D tensor with shape `(batch_size, kv_sequence_length, k_hidden_size)`. For cases with a 3D input tensor, `k_hidden_size = kv_num_heads * head_size`" }, { "name": "V", "type": "T2", "description": "Value tensor. 4D tensor with shape `(batch_size, kv_num_heads, kv_sequence_length, v_head_size)` or 3D tensor with shape `(batch_size, kv_sequence_length, v_hidden_size)`. For cases with a 3D input tensor, `v_hidden_size = kv_num_heads * v_head_size`" }, { "name": "attn_mask", "type": "U", "option": "optional", "description": "Attention mask. Shape must be broadcastable to 4D tensor with shape `(batch_size, q_num_heads, q_sequence_length, total_sequence_length)` where `total_sequence_length = past_sequence_length + kv_sequence_length.` Two types of masks are supported. A boolean mask where a value of `True` indicates that the element should take part in attention. Also supports a float mask of the same type as query, key, value that is added to the attention score." }, { "name": "past_key", "type": "T1", "option": "optional", "description": "past state cache for key with shape `(batch_size, kv_num_heads, past_sequence_length, head_size)`" }, { "name": "past_value", "type": "T2", "option": "optional", "description": "past state cache for value with shape `(batch_size, kv_num_heads, past_sequence_length, v_head_size)`" } ], "min_input": 3, "max_input": 6, "outputs": [ { "name": "Y", "type": "T1", "description": "The output tensor . 4D tensor with shape `(batch_size, q_num_heads, q_sequence_length, v_head_size)` or 3D tensor with shape `(batch_size, q_sequence_length, hidden_size)`. For cases with a 3D input tensor, `hidden_size = q_num_heads * v_head_size`" }, { "name": "present_key", "type": "T1", "option": "optional", "description": "Updated key cache with shape `(batch_size, kv_num_heads, total_sequence_length, head_size)` where `total_sequence_length = past_sequence_length + kv_sequence_length`." }, { "name": "present_value", "type": "T2", "option": "optional", "description": "Updated value cache with shape `(batch_size, kv_num_heads, total_sequence_length, v_head_size)` where `total_sequence_length = past_sequence_length + kv_sequence_length`." }, { "name": "qk_matmul_output", "type": "T1", "option": "optional", "description": "The output of QK matmul. 4D tensor with shape `(batch_size, q_num_heads, q_sequence_length, total_sequence_length)` where `total_sequence_length = past_sequence_length + kv_sequence_length`." } ], "min_output": 1, "max_output": 4, "inputs_range": "3 - 6", "outputs_range": "1 - 4", "type_constraints": [ { "description": "Constrain Q and K inputs types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] }, { "description": "Constrain V input types to float tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] }, { "description": "Constrain output 'mask' types to boolean tensors and input types.", "type_param_str": "U", "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(bool)" ] } ], "examples": [ { "summary": "attention", "code": "node = onnx.helper.make_node(\"Attention\", inputs=[\"Q\", \"K\", \"V\"], outputs=[\"Y\"])\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_23_boolmask_fullymasked_row_nan_robustness", "code": "\"\"\"Opset-23 fully-masked boolean ``attn_mask`` row -> zero (not ``NaN``).\n\nThis locks the opset-23 / ``old.cc`` function-body fully-masked-row guard\nagainst future regressions. In opset 23 the only in-contract fully-masked\nrow comes from an all-``False`` boolean ``attn_mask`` row (``is_causal`` is\nnot set here): every key for that query is disallowed, so ``softmax`` over an\nall-``-inf`` bias row is ``NaN``. The guard zeros that row's probabilities\nbefore the ``P @ V`` contraction so the output row is exactly ``0``, while\nrows with at least one allowed key are unchanged.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(4)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(B, H, S, D).astype(np.float32)\nK = np.random.rand(B, H, S, D).astype(np.float32)\nV = np.random.rand(B, H, S, D).astype(np.float32)\n# Row 0: no key allowed -> fully masked (Bug-2 empty row). Row 1: both keys\n# allowed -> finite, unchanged by the guard.\nattn_mask = np.array([[False, False], [True, True]], dtype=np.bool_)\n\nY, _, _, _ = _compute_attention(Q, K, V, attn_mask=attn_mask)\n\n# Fully-masked row 0 is exactly zero (not NaN); every other cell is finite.\nassert np.all(np.isfinite(Y)), \"non-masked rows must be finite\"\nassert np.array_equal(Y[:, :, 0, :], np.zeros_like(Y[:, :, 0, :])), (\n \"fully-masked row must be zero (Bug-2)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_23_boolmask_fullymasked_row_nan_robustness\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_23_fullymasked_qk_matmul_output_mode3_zero", "code": "\"\"\"Opset-23 ``qk_matmul_output_mode=3`` fully-masked row is a zero row.\n\nMode ``3`` exposes the post-softmax matrix as the optional\n``qk_matmul_output``. For a fully-masked query row (all-``False`` boolean\n``attn_mask`` row), the fully-masked-row guard is applied before this output\nis produced, so the mode-3 row is zeroed, consistent with the primary output\n``Y`` row (both are ``0``). This pins the mandated agreement between the\nguarded primary output and the mode-3 output at opset 23.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(13)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=3,\n)\n\nQ = np.random.rand(B, H, S, D).astype(np.float32)\nK = np.random.rand(B, H, S, D).astype(np.float32)\nV = np.random.rand(B, H, S, D).astype(np.float32)\n# Row 0: no key allowed -> fully masked. Row 1: both keys allowed -> finite.\nattn_mask = np.array([[False, False], [True, True]], dtype=np.bool_)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=3,\n)\n\n# Primary output row 0 and the mode-3 row 0 are both guarded to zero.\nassert np.array_equal(Y[:, :, 0, :], np.zeros_like(Y[:, :, 0, :])), (\n \"fully-masked primary output row must be zero\"\n)\nassert np.array_equal(\n qk_matmul_output[:, :, 0, :], np.zeros_like(qk_matmul_output[:, :, 0, :])\n), \"mode-3 output row for a fully-masked query must be zero (consistent with Y)\"\nassert np.all(np.isfinite(qk_matmul_output)), (\n \"all mode-3 rows are finite (the fully-masked row is guarded to 0.0)\"\n)\nassert np.all(np.isfinite(Y)), (\n \"all Y rows are finite (the fully-masked row is guarded to 0.0)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_23_fullymasked_qk_matmul_output_mode3_zero\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_24_fullymasked_qk_matmul_output_mode3_zero", "code": "\"\"\"Opset-24 ``qk_matmul_output_mode=3`` fully-masked row is a zero row.\n\nThe opset-24 twin of\n``export_attention_23_fullymasked_qk_matmul_output_mode3_zero``. Mode ``3``\nexposes the post-softmax matrix as the optional ``qk_matmul_output``. For a\nfully-masked query row (all-``False`` boolean ``attn_mask`` row), the\nfully-masked-row guard is applied before this output is produced, so the\nmode-3 row is zeroed, consistent with the primary output ``Y`` row (both are\n``0``). This pins the mandated agreement between the guarded primary output\nand the mode-3 output at opset 24.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(13)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=3,\n)\n\nQ = np.random.rand(B, H, S, D).astype(np.float32)\nK = np.random.rand(B, H, S, D).astype(np.float32)\nV = np.random.rand(B, H, S, D).astype(np.float32)\n# Row 0: no key allowed -> fully masked. Row 1: both keys allowed -> finite.\nattn_mask = np.array([[False, False], [True, True]], dtype=np.bool_)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=3,\n)\n\n# Primary output row 0 and the mode-3 row 0 are both guarded to zero.\nassert np.array_equal(Y[:, :, 0, :], np.zeros_like(Y[:, :, 0, :])), (\n \"fully-masked primary output row must be zero\"\n)\nassert np.array_equal(\n qk_matmul_output[:, :, 0, :], np.zeros_like(qk_matmul_output[:, :, 0, :])\n), \"mode-3 output row for a fully-masked query must be zero (consistent with Y)\"\nassert np.all(np.isfinite(qk_matmul_output)), (\n \"all mode-3 rows are finite (the fully-masked row is guarded to 0.0)\"\n)\nassert np.all(np.isfinite(Y)), (\n \"all Y rows are finite (the fully-masked row is guarded to 0.0)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_24_fullymasked_qk_matmul_output_mode3_zero\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_24_qk_matmul_output_mode3_softmax_precision", "code": "\"\"\"Mode-3 ``qk_matmul_output`` is emitted at the output precision ``T1``.\n\n``qk_matmul_output_mode=3`` exposes the post-softmax probabilities. When\n``softmax_precision`` differs from the operator's output type ``T1`` (here\n``T1 = float16`` with softmax computed in ``float32``), the mode-3 output is\ncast back to ``T1`` -- matching the reference implementation, which casts the\nexposed matrix to ``Q.dtype``. This locks both the dtype contract and the\nfully-masked-row zeroing under a non-default ``softmax_precision``.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(17)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=3,\n softmax_precision=int(onnx.TensorProto.FLOAT),\n)\n\n# T1 = float16; softmax runs in float32, so the mode-3 output is cast back to\n# float16 on emission.\nQ = np.random.rand(B, H, S, D).astype(np.float16)\nK = np.random.rand(B, H, S, D).astype(np.float16)\nV = np.random.rand(B, H, S, D).astype(np.float16)\n# Row 0: fully masked. Row 1: both keys allowed -> finite.\nattn_mask = np.array([[False, False], [True, True]], dtype=np.bool_)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=3,\n softmax_precision=int(onnx.TensorProto.FLOAT),\n)\n\n# The mode-3 output is emitted at T1 (float16), not the float32 softmax\n# precision, matching the operator's output type.\nassert qk_matmul_output.dtype == np.float16, (\n \"mode-3 qk_matmul_output must be emitted at the output precision T1 (float16)\"\n)\n# The fully-masked row is still guarded to zero, consistent with Y.\nassert np.array_equal(\n qk_matmul_output[:, :, 0, :], np.zeros_like(qk_matmul_output[:, :, 0, :])\n), \"mode-3 output row for a fully-masked query must be zero (consistent with Y)\"\nassert np.all(np.isfinite(qk_matmul_output)), (\n \"all mode-3 rows are finite (the fully-masked row is guarded to 0.0)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_24_qk_matmul_output_mode3_softmax_precision\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_3d", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_attn_mask", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_3d_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_causal", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_attn_mask", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_causal", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_scaled", "code": "scale = 1e-2\nq_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_softcap", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_with_past_and_present", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 10).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_3d_diff_heads_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_gqa\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_attn_mask", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_3d_gqa_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_causal", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_gqa_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_scaled", "code": "scale = 1e-2\nq_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_gqa_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_softcap", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_gqa_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_with_past_and_present", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_3d_gqa_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_scaled", "code": "scale = 1e-2\nq_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_softcap", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_transpose_verification", "code": "\"\"\"Test case to verify correct 3D to 4D transpose behavior.\n\nThis test verifies that 3D inputs are correctly reshaped and transposed\naccording to the ONNX specification:\n[batch_size, seq_length, hidden_size] ->\n[batch_size, seq_length, num_heads, head_size] ->\n[batch_size, num_heads, seq_length, head_size]\n\"\"\"\nq_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\n# Test inputs that will clearly demonstrate the transpose behavior\nbatch_size = 1\nq_seq_length = 2\nkv_seq_length = 2\nhead_size = 4\nq_hidden_size = q_num_heads * head_size # 3 * 4 = 12\nkv_hidden_size = kv_num_heads * head_size # 3 * 4 = 12\n\n# Create structured inputs to verify correct transpose behavior\n# Q has a pattern where each position in hidden dimension has a specific value\nQ = np.zeros((batch_size, q_seq_length, q_hidden_size), dtype=np.float32)\n# Fill Q with pattern: head0=[1,1,1,1], head1=[2,2,2,2], head2=[3,3,3,3]\nfor head in range(q_num_heads):\n start_idx = head * head_size\n end_idx = start_idx + head_size\n Q[0, :, start_idx:end_idx] = float(head + 1)\n\nK = np.ones((batch_size, kv_seq_length, kv_hidden_size), dtype=np.float32) * 0.1\nV = np.ones((batch_size, kv_seq_length, kv_hidden_size), dtype=np.float32) * 0.1\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_transpose_verification\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_3d_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present_qk_matmul", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_3d_with_past_and_present_qk_matmul\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present_qk_matmul_bias", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n qk_matmul_output_mode=2,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_3d_with_past_and_present_qk_matmul_bias\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present_qk_matmul_softcap", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n softcap=2.0,\n qk_matmul_output_mode=1,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n softcap=2.0,\n qk_matmul_output_mode=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_3d_with_past_and_present_qk_matmul_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present_qk_matmul_softmax", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n qk_matmul_output_mode=3,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n qk_matmul_output_mode=3,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_3d_with_past_and_present_qk_matmul_softmax\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_4d_causal_nonpad_attn_mask_composition", "code": "\"\"\"Compose ``is_causal`` + ``nonpad_kv_seqlen`` + boolean ``attn_mask``.\n\nThe existing nonpad tests use no ``attn_mask`` and the existing mask tests\nuse no ``nonpad_kv_seqlen``; this is the first to activate all three\nconstraints together on the external-cache path with ``batch > 1``. The\nthree biases are summed additively and a key is attended only if allowed by\nall three. Crucially the inputs are designed so that **each constraint is\nindependently necessary** -- removing any one changes the golden -- to avoid a\ndegenerate test that a backend ignoring ``is_causal`` and/or\n``nonpad_kv_seqlen`` could still pass:\n\n* **``is_causal`` binds.** Each batch has a key that the boolean mask allows\n (``True``) but the bottom-right causal frontier disallows\n (``j > i + offset``); only ``is_causal`` masks it (batch 0 row 0 key 2,\n batch 1 row 0 key 3).\n* **``attn_mask`` binds.** Each batch has a key the causal frontier and the\n padding bound both allow but the boolean mask sets ``False`` (batch 0 row 2\n key 1, batch 1 row 2 key 2); only the mask masks it.\n* **``nonpad_kv_seqlen`` binds.** ``nonpad_kv_seqlen`` sets the per-batch\n causal *offset* (``offset = nonpad_kv_seqlen - q_sequence_length``), so\n dropping it collapses the frontier to top-left (``offset = 0``) and shifts\n which keys are attended. (Under ``is_causal=1`` the causal frontier already\n subsumes the ``j < nonpad`` padding bound, so ``nonpad_kv_seqlen`` binds\n through the offset it induces rather than through a redundant padding cut.)\n\nThe mask is chosen to leave at least one allowed key on every query row, so\nthis exercises the *intersection* of the three constraints with finite outputs\n(the fully-masked-row guard is covered by\n``test_attention_4d_causal_nonpad_negative_offset_structural_empty`` and\n``test_attention_24_fullymasked_qk_matmul_output_mode3_zero``).\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(11)\nB, H, L, D = 2, 2, 6, 8\nS_q = 3\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, L, D).astype(np.float32)\nV = np.random.rand(B, H, L, D).astype(np.float32)\nnonpad_kv_seqlen = np.array([4, 5], dtype=np.int64) # offsets [1, 2]\n# Per-batch (B, 1, S_q, L) bool mask. Each batch is laid out so all three\n# constraints uniquely bind (see the docstring): a causal-only-masked key\n# (mask True, j > i + offset), a mask-only-masked key (mask False, causal +\n# nonpad allow it), and >=1 allowed key per row.\nattn_mask = np.array(\n [\n [\n [\n [True, True, True, False, False, False],\n [True, True, True, False, False, False],\n [True, False, True, True, False, False],\n ]\n ],\n [\n [\n [True, True, True, True, False, False],\n [True, True, True, True, False, False],\n [True, True, False, True, True, False],\n ]\n ],\n ],\n dtype=np.bool_,\n)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\n# The chosen mask leaves >=1 allowed key per row, so the composition stays\n# finite (no fully-masked row in this case).\nassert np.all(np.isfinite(Y)), \"composed-constraint output must be finite\"\n\n# Non-degeneracy: each constraint is independently necessary. Removing any one\n# of the three (is_causal, attn_mask, nonpad_kv_seqlen) must change the result,\n# so a backend that ignores is_causal or nonpad_kv_seqlen cannot reproduce the\n# golden by applying only the most restrictive mask.\ny_no_causal, _, _, _ = _compute_attention(\n Q, K, V, attn_mask=attn_mask, nonpad_kv_seqlen=nonpad_kv_seqlen, is_causal=0\n)\ny_no_mask, _, _, _ = _compute_attention(\n Q, K, V, nonpad_kv_seqlen=nonpad_kv_seqlen, is_causal=1\n)\ny_no_nonpad, _, _, _ = _compute_attention(\n Q, K, V, attn_mask=attn_mask, is_causal=1\n)\nassert not np.allclose(Y, y_no_causal, equal_nan=True), (\n \"is_causal must bind: dropping it changes the result\"\n)\nassert not np.allclose(Y, y_no_mask, equal_nan=True), (\n \"attn_mask must bind: dropping it changes the result\"\n)\nassert not np.allclose(Y, y_no_nonpad, equal_nan=True), (\n \"nonpad_kv_seqlen must bind (via the causal offset): dropping it changes the result\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_causal_nonpad_attn_mask_composition\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_causal_nonpad_batch_prefill", "code": "\"\"\"Batch>1 continued prefill with distinct per-batch bottom-right offsets.\n\nThe batched generalization of the ``batch == 1`` continued-prefill case: with\n``nonpad_kv_seqlen = [4, 5, 6]`` and ``S_q = 2`` the per-batch bottom-right\noffsets are ``[2, 3, 4]`` (all ``>= 0``), so each batch realigns its causal\nfrontier to its own valid-key prefix. This pins that the per-batch offset is\napplied independently across the batch dimension.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(12)\nB, H, L, D = 3, 2, 6, 8\nS_q = 2\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, L, D).astype(np.float32)\nV = np.random.rand(B, H, L, D).astype(np.float32)\nnonpad_kv_seqlen = np.array([4, 5, 6], dtype=np.int64) # offsets [2, 3, 4]\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\nassert np.all(np.isfinite(Y)), \"per-batch prefill output must be finite\"\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_causal_nonpad_batch_prefill\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_causal_nonpad_continued_prefill", "code": "\"\"\"Continued / chunked prefill (S_q=2) into a partially-filled static cache.\n\nWith ``nonpad_kv_seqlen = [4]`` and ``S_q = 2`` the bottom-right offset is\n``4 - 2 = 2``: query 0 attends keys ``{0,1,2}`` and query 1 attends\n``{0,1,2,3}``. The old top-left alignment would mask everything past the\ndiagonal (``{0}`` and ``{0,1}``), so this test fails pre-fix.\n\"\"\"\nnp.random.seed(1)\nB, H, L, D = 1, 2, 4, 8\nS_q = 2\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, L, D).astype(np.float32)\nV = np.random.rand(B, H, L, D).astype(np.float32)\nnonpad_kv_seqlen = np.array([4], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_causal_nonpad_continued_prefill\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_causal_nonpad_negative_offset_structural_empty", "code": "\"\"\"Negative bottom-right offset: structurally-empty early query rows -> zero.\n\nThis is the onnx-node twin of the ORT gtest\n``Attention_Causal_NonPadKVSeqLen_StructuralEmptyRow_Zero`` /\n``StructuralEmptyRows_Zero_CUDA``. With ``nonpad_kv_seqlen = [2]`` and\n``S_q = 4`` the bottom-right offset is ``2 - 4 = -2``: query row ``sq``\nattends keys ``0..(sq - 2)``, so rows 0 and 1 have an empty key set. Their\n``softmax`` over an all-``-inf`` bias row is ``NaN``; the fully-masked-row\nguard zeros those rows before the ``P @ V`` contraction so the output rows are\nexactly ``0``, while rows 2 and 3 (attending keys ``{0}`` and ``{0,1}``) stay finite\nand nonzero. A ``nonpad_kv_seqlen[b] < q_sequence_length`` input is out of\nthe contract's intended use, but its result is still well-defined (zeroed\nrows) rather than ``NaN``; this test pins that defined behavior.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(7)\nB, H, L, D = 1, 2, 4, 8\nS_q = 4\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, L, D).astype(np.float32)\nV = np.random.rand(B, H, L, D).astype(np.float32)\n# offset = nonpad - S_q = 2 - 4 = -2 -> rows 0,1 structurally empty.\nnonpad_kv_seqlen = np.array([2], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\n# Structurally-empty early rows are exactly zero (not NaN); later rows finite.\nassert np.all(np.isfinite(Y)), \"all output rows must be finite\"\nassert np.array_equal(Y[:, :, 0, :], np.zeros_like(Y[:, :, 0, :])), (\n \"structurally-empty row 0 must be zero\"\n)\nassert np.array_equal(Y[:, :, 1, :], np.zeros_like(Y[:, :, 1, :])), (\n \"structurally-empty row 1 must be zero\"\n)\nassert np.any(Y[:, :, 2, :] != 0) and np.any(Y[:, :, 3, :] != 0), (\n \"rows with a non-empty key set must be nonzero\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_causal_nonpad_negative_offset_structural_empty\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_causal_with_past_and_present", "code": "\"\"\"Regression guard: internal (past_key) cache + is_causal.\n\nThis exercises the unchanged scalar bottom-right path (offset =\npast_sequence_length). Its golden output must remain identical to the\npre-fix behavior, proving the external-cache change does not touch the\npast_key path.\n\"\"\"\nnp.random.seed(2)\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n is_causal=1,\n)\n\npast_sequence_length = 3\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 4, 8).astype(np.float32)\nV = np.random.rand(2, 3, 4, 8).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n past_key=past_key,\n past_value=past_value,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_causal_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_diff_heads_mask4d_padded_kv", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 4).astype(np.float32)\nnonpad_kv_seqlen = np.array([3, 4], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_mask4d_padded_kv\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_gqa_causal_nonpad_decode", "code": "\"\"\"External/static-cache decode (S_q=1) with per-batch valid lengths.\n\nK/V are the full static cache buffer; ``nonpad_kv_seqlen`` marks how many\nleading keys are valid per batch. With bottom-right (offset-aware) causal\nmasking the single decode query attends keys ``0..nonpad[b]-1``. Under the\nold top-left alignment it would attend only key 0, so this test fails\npre-fix and passes post-fix.\n\"\"\"\nnp.random.seed(0)\nB, H_q, H_kv, L, D = 2, 4, 2, 8, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H_q, 1, D).astype(np.float32)\nK = np.random.rand(B, H_kv, L, D).astype(np.float32)\nV = np.random.rand(B, H_kv, L, D).astype(np.float32)\n# Batch 0 has all 8 keys valid, batch 1 only the first 5.\nnonpad_kv_seqlen = np.array([8, 5], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_gqa_causal_nonpad_decode\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_gqa_causal_nonpad_decode_fp16", "code": "\"\"\"fp16 variant of the external-cache decode case (locks -inf dtype handling).\"\"\"\nnp.random.seed(0)\nB, H_q, H_kv, L, D = 2, 4, 2, 8, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H_q, 1, D).astype(np.float16)\nK = np.random.rand(B, H_kv, L, D).astype(np.float16)\nV = np.random.rand(B, H_kv, L, D).astype(np.float16)\nnonpad_kv_seqlen = np.array([8, 5], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_gqa_causal_nonpad_decode_fp16\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_attn_3d_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_3d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_3d_mask_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_3d_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_4d_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_4d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_4d_mask_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_4d_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_mask_bool", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(bool)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_bool\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_mask_bool_4d", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6).astype(bool)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_bool_4d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, is_causal=1)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_causal_boolmask_nan_robustness", "code": "\"\"\"Composed ``is_causal`` + boolean ``attn_mask`` NaN-robustness.\n\nThe causal frontier (lower-triangular here, offset 0) and the boolean\n``attn_mask`` are intersected: a key is attended only if allowed by both.\nThis exercises two pre-fix NaN sources on the same forward pass:\n\n* **Bug-1 (allowed cells stay finite).** Query 0 is allowed key 0 by both\n the causal frontier (``{0}``) and the mask (``True`` at key 0). The old\n ``(1 - attn_mask) * -inf`` conversion computes ``0 * -inf = NaN`` at that\n allowed cell, poisoning the row. The select conversion\n ``where(attn_mask, 0, -inf)`` keeps it finite.\n* **Bug-2 (fully-masked row -> 0).** Query 1 is allowed keys ``{0, 1}`` by\n the causal frontier but the mask is ``False`` at both, so the combined\n constraint allows no key. ``softmax`` of an all-``-inf`` row is ``NaN``;\n the fully-masked-row guard zeros it before the ``P @ V`` contraction so\n the output row is ``0``.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(3)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S, D).astype(np.float32)\nK = np.random.rand(B, H, S, D).astype(np.float32)\nV = np.random.rand(B, H, S, D).astype(np.float32)\n# Row 0: key 0 allowed (Bug-1 allowed cell). Row 1: no key allowed -> fully\n# masked once intersected with the causal frontier (Bug-2 empty row).\nattn_mask = np.array([[True, False], [False, False]], dtype=np.bool_)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n is_causal=1,\n)\n\n# Bug-1: allowed cells are finite (no NaN anywhere). Bug-2: the fully-masked\n# query row is exactly zero, not NaN.\nassert np.all(np.isfinite(Y)), \"allowed cells must be finite (Bug-1)\"\nassert np.array_equal(Y[:, :, 1, :], np.zeros_like(Y[:, :, 1, :])), (\n \"fully-masked row must be zero (Bug-2)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_causal_boolmask_nan_robustness\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_diff_head_sizes", "code": "node = onnx.helper.make_node(\"Attention\", inputs=[\"Q\", \"K\", \"V\"], outputs=[\"Y\"])\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_attn_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_scaled", "code": "scale = 1e-2\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, scale=scale)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_softcap", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=2.0,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n softcap=2.0,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_with_past_and_present", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 10).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_diff_heads_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_with_past_and_present_mask3D", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 10).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_diff_heads_with_past_and_present_mask3d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_with_past_and_present_mask4D", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 10).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_diff_heads_with_past_and_present_mask4d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_fp16", "code": "node = onnx.helper.make_node(\"Attention\", inputs=[\"Q\", \"K\", \"V\"], outputs=[\"Y\"])\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float16)\nK = np.random.rand(2, 3, 6, 8).astype(np.float16)\nV = np.random.rand(2, 3, 6, 8).astype(np.float16)\n\nY, _, _, _ = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_fp16\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa", "code": "node = onnx.helper.make_node(\"Attention\", inputs=[\"Q\", \"K\", \"V\"], outputs=[\"Y\"])\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_gqa\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_attn_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_gqa_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, is_causal=1)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_gqa_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_scaled", "code": "scale = 1e-2\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n)\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, scale=scale)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_gqa_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_softcap", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=2.0,\n)\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, softcap=2.0)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_gqa_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_with_past_and_present", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_gqa_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_with_past_and_present_fp16", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 9, 4, 8).astype(np.float16)\nK = np.random.rand(2, 3, 6, 8).astype(np.float16)\nV = np.random.rand(2, 3, 6, 8).astype(np.float16)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float16)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float16)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float16)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_gqa_with_past_and_present_fp16\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_scaled", "code": "scale = 1e-2\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, scale=scale)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_softcap", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=2.0,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, softcap=2.0)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_softcap_with_neginf_mask", "code": "\"\"\"Softcap + -inf mask: verifies softcap is applied BEFORE mask/bias.\n\nIf ordering were wrong (mask then softcap), tanh(-inf/softcap) = -1,\nso softcap * tanh(-inf/softcap) = -softcap (finite). That leaks\nprobability to masked positions. With correct ordering (softcap then\nmask), the -inf mask values survive to softmax and yield zero weight.\n\"\"\"\nnp.random.seed(42)\nB, H, S_q, S_kv, D = 1, 1, 4, 6, 8\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, S_kv, D).astype(np.float32)\nV = np.random.rand(B, H, S_kv, D).astype(np.float32)\n\n# All Q positions are blocked from KV positions 4 and 5.\nattn_mask = np.zeros((S_q, S_kv), dtype=np.float32)\nattn_mask[:, 4:] = -np.inf\n\nsoftcap = 0.5\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n softcap=softcap,\n)\n\nY, _, _, _ = _compute_attention(Q, K, V, attn_mask=attn_mask, softcap=softcap)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_softcap_neginf_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_softcap_with_neginf_mask_poison", "code": "\"\"\"Softcap + -inf mask + poison values at masked KV positions.\n\nV has value 1000 at the masked positions (4 and 5). With correct\nordering the output stays in [0, 1] because the mask zeros out those\npositions. With wrong ordering the output explodes (> 50), making\nthe failure obvious even with loose tolerances.\n\"\"\"\nnp.random.seed(42)\nB, H, S_q, S_kv, D = 1, 1, 4, 6, 8\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, S_kv, D).astype(np.float32)\nV = np.random.rand(B, H, S_kv, D).astype(np.float32)\n\n# Block all Q positions from KV positions 4 and 5.\nattn_mask = np.zeros((S_q, S_kv), dtype=np.float32)\nattn_mask[:, 4:] = -np.inf\n\n# Poison: if attention leaks to masked positions, output >> 1.\nV[:, :, 4:, :] = 1000.0\n\nsoftcap = 0.5\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n softcap=softcap,\n)\n\nY, _, _, _ = _compute_attention(Q, K, V, attn_mask=attn_mask, softcap=softcap)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_softcap_neginf_mask_poison\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias_3d_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias_3d_mask_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n is_causal=1,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias_4d_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias_4d_mask_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n is_causal=1,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_qk_matmul", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, qk_matmul_output = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_4d_with_qk_matmul\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_qk_matmul_bias", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_4d_with_qk_matmul_bias\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_qk_matmul_softcap", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n softcap=2.0,\n qk_matmul_output_mode=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n softcap=2.0,\n qk_matmul_output_mode=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_4d_with_qk_matmul_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_qk_matmul_softmax", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=3,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=3,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_4d_with_qk_matmul_softmax\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" } ], "category": "Attention" }, { "name": "Attention", "module": "ai.onnx", "version": 24, "description": "Computes scaled dot product attention on query, key and value tensors, using an optional attention mask if passed.\n\nThis operator covers self and cross variants of the attention operation based on sequence lengths of K, Q and V.\n\nFor self attention, `kv_sequence_length` equals to `q_sequence_length`.\n\nFor cross attention, query and key might have different lengths.\n\nThis operator also covers the 3 following variants based on the number of heads:\n1) Multi-headed Attention (MHA): Described in the paper https://arxiv.org/pdf/1706.03762, `q_num_heads = kv_num_heads`.\n2) Group-query Attention (GQA): Described in the paper https://arxiv.org/pdf/2305.13245, `q_num_heads > kv_num_heads`, `q_num_heads % kv_num_heads == 0`.\n3) Multi-query Attention (MQA): Described in the paper https://arxiv.org/pdf/1911.02150, `q_num_heads > kv_num_heads`, `kv_num_heads=1`.\n\nAttention bias to be added is calculated based on `attn_mask` input and `is_causal` attribute:\n1) `attn_mask`: A boolean mask where a value of `True` indicates that the element should take part in attention or a float mask of the same type as query, key, value that is added to the attention score.\n2) If `is_causal` is set to `1`, causal masking is applied with bottom-right (offset-aware) alignment: query `i` attends key `j` iff `j <= i + offset`, as illustrated below.\n\n```\n 2D causal mask for Attention (PR onnx/onnx#8068)\n S_q=4 queries, S_k=8 keys\n Rule: query i attends key j iff j <= i + offset\n offset = nonpad_kv_seqlen - S_q\n\n nonpad_kv_seqlen=4, offset=4-4=0\n\n k0 k1 k2 k3 k4 k5 k6 k7\n +----+----+----+----+----+----+----+----+\n q0 | ## | | | | | | | |\n +----+----+----+----+----+----+----+----+\n q1 | ## | ## | | | | | | |\n +----+----+----+----+----+----+----+----+\n q2 | ## | ## | ## | | | | | |\n +----+----+----+----+----+----+----+----+\n q3 | ## | ## | ## | ## | | | | |\n +----+----+----+----+----+----+----+----+\n\n\n nonpad_kv_seqlen=8, offset=8-4=4\n\n k0 k1 k2 k3 k4 k5 k6 k7\n +----+----+----+----+----+----+----+----+\n q0 | ## | ## | ## | ## | ## | | | |\n +----+----+----+----+----+----+----+----+\n q1 | ## | ## | ## | ## | ## | ## | | |\n +----+----+----+----+----+----+----+----+\n q2 | ## | ## | ## | ## | ## | ## | ## | |\n +----+----+----+----+----+----+----+----+\n q3 | ## | ## | ## | ## | ## | ## | ## | ## |\n +----+----+----+----+----+----+----+----+\n```\n\nWith `nonpad_kv_seqlen=4` (offset=0), the mask is the standard lower-triangular. With `nonpad_kv_seqlen=8` (offset=4), the diagonal shifts right by 4, so each query sees the 4 additional valid cached keys.\n\n`offset` is the count of valid keys preceding the current query block: `offset = past_sequence_length` when `past_key` is provided; `offset = nonpad_kv_seqlen - q_sequence_length` (per batch) when an external cache is indicated by `nonpad_kv_seqlen` without `past_key`; `offset = 0` when neither is provided (the no-cache case, which reduces to the standard lower-triangular mask). When `offset < 0` (`nonpad_kv_seqlen < q_sequence_length`, i.e. more query tokens than cached keys) the leading query rows have an empty key set (no key satisfies `j <= i + offset`) and are fully masked. The causal frontier is computed independently of `attn_mask` and is then composed with it additively: a boolean `attn_mask` intersects the allowed set (its disallowed positions contribute `-inf` to the bias), while a float `attn_mask` is added to the attention scores rather than disabling positions. A fully-masked query row (no key attended, including the negative-offset leading rows) produces a zero output row, not `NaN`, for both `Y` and the mode-`3` `qk_matmul_output` debug output; the mode-`3` `qk_matmul_output` is emitted at the operator's output precision (`T1`).\n\nErrata (in-place behavioral correction, no opset bump): the reference implementation and backend tests were incorrect when `nonpad_kv_seqlen != q_sequence_length` (nonzero bottom-right offset, top-left instead of bottom-right causal alignment) and produced `NaN` for fully-masked rows; corrected in version 1.23. This fixed three behaviors described above: external-cache bottom-right causal alignment (`offset = nonpad_kv_seqlen - q_sequence_length`), zero (non-`NaN`) output for fully-masked rows including the mode-`3` `qk_matmul_output`, and the mode-`3` `qk_matmul_output` precision (`T1`).\n\nWith respect to KV cache update, this operator allows the following two use cases:\n\n1) Cache update happens inside the Attention operator. In this case, the `K` and `V` inputs contain only the incoming\ntokens for the current autoregressive step, and the four optional inputs/outputs past and present key and value are\nall needed. The Attention op performs a Concat operation on the past and incoming key and value to form the present\nkey and value, respectively. Note that this only works correctly for the special case where the past key and value\ndo not contain padded tokens.\n2) Cache update happens outside the Attention operator (for example, through the `TensorScatter` operator). In this\ncase, the `K` and `V` inputs correspond to the entire cache tensor, so the four optional inputs/outputs past and\npresent key and value should not be used. An additional input `nonpad_kv_seqlen` of shape (batch_size,) may be\nprovided to indicate the number of non-padding tokens in each sample of the batch to save unnecessary computation.\nHere, the kv_sequence dimension of `attn_mask` can be shorter than `K` and `V`, but still needs to be at least as long\nas the maximum value of `nonpad_kv_seqlen`.\n\nBoth past and present state key/values are optional. They shall be used together, and not allowed to use only one of them.\nThe following pattern is applied to the Q, K and V inputs after appropriate reshaping of K and V inputs based on sequence lengths and num heads provided:\n\n```\n The following pattern is applied by this operator:\n Q K V\n | | |\nQ*sqrt(scale) K*sqrt(scale) |\n | | |\n | Transpose |\n | | |\n ---MatMul--- |\n | |\n softcap (if provided) |\n | |\n at_mask---Add |\n | |\n Softmax |\n | |\n -----MatMul------\n |\n Y\n```\n\n", "attributes": [ { "name": "is_causal", "type": "int64", "required": false, "description": "If set to `1`, causal masking is applied. For a square Q/K (no cache offset) this is a lower-triangular matrix. In general the mask is bottom-right (offset-aware): query in-block index `i` attends key `j` iff `j <= i + offset`, where `offset` is the count of valid keys preceding the query block (`past_sequence_length` for an internal `past_key` cache, or `nonpad_kv_seqlen - q_sequence_length` per batch for an external cache). When `offset = 0` this reduces to the lower-triangular (top-left) mask." }, { "name": "kv_num_heads", "type": "int64", "required": false, "description": "Number of heads of key and value. Must be used with 3D inputs of Q, K and V. " }, { "name": "q_num_heads", "type": "int64", "required": false, "description": "Number of heads of query. Must be used with 3D inputs of Q, K and V. " }, { "name": "qk_matmul_output_mode", "type": "int64", "required": false, "description": "If set to `0`, qk_matmul_output is the output of qk matmul. If set to `1`, qk_matmul_output is the output after the softcap operation (before mask addition). If set to `2`, qk_matmul_output includes the attention mask and softcap (if provided) applied to the output of qk matmul. If set to `3`, qk_matmul_output is the output after the softmax operation. In mode `3`, a fully-masked query row (every key disallowed) is a zero row, consistent with the corresponding row of the primary output `Y`: the fully-masked-row guard is applied before this output is produced. The mode-`3` output is emitted at the operator's output precision (`T1`); when `softmax_precision` differs from `T1` this is a cast of the softmax result to `T1`. Default value is 0." }, { "name": "scale", "type": "float32", "required": false, "description": "Scaling factor applied to $Q*K^T$. Default value is `1/sqrt(head_size)`. To prevent [numerical overflow](https://tinyurl.com/sudb9s96), scale `Q`, `K` by `sqrt(scale)` before matmul." }, { "name": "softcap", "type": "float32", "required": false, "description": "Softcap value for attention weights. Default value is 0." }, { "name": "softmax_precision", "type": "int64", "required": false, "description": "The floating-point precision used in softmax computation. If softmax precision is not provided, the same precision as the input of softmax (Q and K) is used." } ], "inputs": [ { "name": "Q", "type": "T1", "description": "Query tensor. 4D tensor with shape `(batch_size, q_num_heads, q_sequence_length, head_size)` or 3D tensor with shape `(batch_size, q_sequence_length, q_hidden_size)`. For cases with a 3D input tensor, `q_hidden_size = q_num_heads * head_size`" }, { "name": "K", "type": "T1", "description": "Key tensor. 4D tensor with shape `(batch_size, kv_num_heads, kv_sequence_length, head_size)` or 3D tensor with shape `(batch_size, kv_sequence_length, k_hidden_size)`. For cases with a 3D input tensor, `k_hidden_size = kv_num_heads * head_size`" }, { "name": "V", "type": "T2", "description": "Value tensor. 4D tensor with shape `(batch_size, kv_num_heads, kv_sequence_length, v_head_size)` or 3D tensor with shape `(batch_size, kv_sequence_length, v_hidden_size)`. For cases with a 3D input tensor, `v_hidden_size = kv_num_heads * v_head_size`" }, { "name": "attn_mask", "type": "U", "option": "optional", "description": "Attention mask. Shape must be broadcastable to `(batch_size, q_num_heads, q_sequence_length, total_sequence_length)` where `total_sequence_length = past_sequence_length + kv_sequence_length.` The last dimension can also be shorter than `total_sequence_length` and will be padded to `total_sequence_length` with negative infinity. Two types of masks are supported: a boolean mask where a value of `True` indicates that the element should take part in attention, or a float mask of the same type as query, key, value that is added to the attention score." }, { "name": "past_key", "type": "T1", "option": "optional", "description": "past state cache for key with shape `(batch_size, kv_num_heads, past_sequence_length, head_size)`" }, { "name": "past_value", "type": "T2", "option": "optional", "description": "past state cache for value with shape `(batch_size, kv_num_heads, past_sequence_length, v_head_size)`" }, { "name": "nonpad_kv_seqlen", "type": "tensor(int64)", "option": "optional", "description": "A vector of integers of shape `(batch_size,)` that indicates the number of valid (ie, non-padding) tokens in each sample. A padding mask can be derived from this. This should not be used together with `past_key` and `past_value` inputs or `present_key` and `present_value` outputs (See the KV cache use cases in the operator description)." } ], "min_input": 3, "max_input": 7, "outputs": [ { "name": "Y", "type": "T1", "description": "The output tensor . 4D tensor with shape `(batch_size, q_num_heads, q_sequence_length, v_head_size)` or 3D tensor with shape `(batch_size, q_sequence_length, hidden_size)`. For cases with a 3D input tensor, `hidden_size = q_num_heads * v_head_size`" }, { "name": "present_key", "type": "T1", "option": "optional", "description": "Updated key cache with shape `(batch_size, kv_num_heads, total_sequence_length, head_size)` where `total_sequence_length = past_sequence_length + kv_sequence_length`." }, { "name": "present_value", "type": "T2", "option": "optional", "description": "Updated value cache with shape `(batch_size, kv_num_heads, total_sequence_length, v_head_size)` where `total_sequence_length = past_sequence_length + kv_sequence_length`." }, { "name": "qk_matmul_output", "type": "T1", "option": "optional", "description": "The output of QK matmul. 4D tensor with shape `(batch_size, q_num_heads, q_sequence_length, total_sequence_length)` where `total_sequence_length = past_sequence_length + kv_sequence_length`." } ], "min_output": 1, "max_output": 4, "inputs_range": "3 - 7", "outputs_range": "1 - 4", "type_constraints": [ { "description": "Constrain Q and K inputs types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] }, { "description": "Constrain V input types to float tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] }, { "description": "Constrain output 'mask' types to boolean tensors and input types.", "type_param_str": "U", "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(bool)" ] } ], "examples": [ { "summary": "attention", "code": "node = onnx.helper.make_node(\"Attention\", inputs=[\"Q\", \"K\", \"V\"], outputs=[\"Y\"])\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_23_boolmask_fullymasked_row_nan_robustness", "code": "\"\"\"Opset-23 fully-masked boolean ``attn_mask`` row -> zero (not ``NaN``).\n\nThis locks the opset-23 / ``old.cc`` function-body fully-masked-row guard\nagainst future regressions. In opset 23 the only in-contract fully-masked\nrow comes from an all-``False`` boolean ``attn_mask`` row (``is_causal`` is\nnot set here): every key for that query is disallowed, so ``softmax`` over an\nall-``-inf`` bias row is ``NaN``. The guard zeros that row's probabilities\nbefore the ``P @ V`` contraction so the output row is exactly ``0``, while\nrows with at least one allowed key are unchanged.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(4)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(B, H, S, D).astype(np.float32)\nK = np.random.rand(B, H, S, D).astype(np.float32)\nV = np.random.rand(B, H, S, D).astype(np.float32)\n# Row 0: no key allowed -> fully masked (Bug-2 empty row). Row 1: both keys\n# allowed -> finite, unchanged by the guard.\nattn_mask = np.array([[False, False], [True, True]], dtype=np.bool_)\n\nY, _, _, _ = _compute_attention(Q, K, V, attn_mask=attn_mask)\n\n# Fully-masked row 0 is exactly zero (not NaN); every other cell is finite.\nassert np.all(np.isfinite(Y)), \"non-masked rows must be finite\"\nassert np.array_equal(Y[:, :, 0, :], np.zeros_like(Y[:, :, 0, :])), (\n \"fully-masked row must be zero (Bug-2)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_23_boolmask_fullymasked_row_nan_robustness\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_23_fullymasked_qk_matmul_output_mode3_zero", "code": "\"\"\"Opset-23 ``qk_matmul_output_mode=3`` fully-masked row is a zero row.\n\nMode ``3`` exposes the post-softmax matrix as the optional\n``qk_matmul_output``. For a fully-masked query row (all-``False`` boolean\n``attn_mask`` row), the fully-masked-row guard is applied before this output\nis produced, so the mode-3 row is zeroed, consistent with the primary output\n``Y`` row (both are ``0``). This pins the mandated agreement between the\nguarded primary output and the mode-3 output at opset 23.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(13)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=3,\n)\n\nQ = np.random.rand(B, H, S, D).astype(np.float32)\nK = np.random.rand(B, H, S, D).astype(np.float32)\nV = np.random.rand(B, H, S, D).astype(np.float32)\n# Row 0: no key allowed -> fully masked. Row 1: both keys allowed -> finite.\nattn_mask = np.array([[False, False], [True, True]], dtype=np.bool_)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=3,\n)\n\n# Primary output row 0 and the mode-3 row 0 are both guarded to zero.\nassert np.array_equal(Y[:, :, 0, :], np.zeros_like(Y[:, :, 0, :])), (\n \"fully-masked primary output row must be zero\"\n)\nassert np.array_equal(\n qk_matmul_output[:, :, 0, :], np.zeros_like(qk_matmul_output[:, :, 0, :])\n), \"mode-3 output row for a fully-masked query must be zero (consistent with Y)\"\nassert np.all(np.isfinite(qk_matmul_output)), (\n \"all mode-3 rows are finite (the fully-masked row is guarded to 0.0)\"\n)\nassert np.all(np.isfinite(Y)), (\n \"all Y rows are finite (the fully-masked row is guarded to 0.0)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_23_fullymasked_qk_matmul_output_mode3_zero\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_24_fullymasked_qk_matmul_output_mode3_zero", "code": "\"\"\"Opset-24 ``qk_matmul_output_mode=3`` fully-masked row is a zero row.\n\nThe opset-24 twin of\n``export_attention_23_fullymasked_qk_matmul_output_mode3_zero``. Mode ``3``\nexposes the post-softmax matrix as the optional ``qk_matmul_output``. For a\nfully-masked query row (all-``False`` boolean ``attn_mask`` row), the\nfully-masked-row guard is applied before this output is produced, so the\nmode-3 row is zeroed, consistent with the primary output ``Y`` row (both are\n``0``). This pins the mandated agreement between the guarded primary output\nand the mode-3 output at opset 24.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(13)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=3,\n)\n\nQ = np.random.rand(B, H, S, D).astype(np.float32)\nK = np.random.rand(B, H, S, D).astype(np.float32)\nV = np.random.rand(B, H, S, D).astype(np.float32)\n# Row 0: no key allowed -> fully masked. Row 1: both keys allowed -> finite.\nattn_mask = np.array([[False, False], [True, True]], dtype=np.bool_)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=3,\n)\n\n# Primary output row 0 and the mode-3 row 0 are both guarded to zero.\nassert np.array_equal(Y[:, :, 0, :], np.zeros_like(Y[:, :, 0, :])), (\n \"fully-masked primary output row must be zero\"\n)\nassert np.array_equal(\n qk_matmul_output[:, :, 0, :], np.zeros_like(qk_matmul_output[:, :, 0, :])\n), \"mode-3 output row for a fully-masked query must be zero (consistent with Y)\"\nassert np.all(np.isfinite(qk_matmul_output)), (\n \"all mode-3 rows are finite (the fully-masked row is guarded to 0.0)\"\n)\nassert np.all(np.isfinite(Y)), (\n \"all Y rows are finite (the fully-masked row is guarded to 0.0)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_24_fullymasked_qk_matmul_output_mode3_zero\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_24_qk_matmul_output_mode3_softmax_precision", "code": "\"\"\"Mode-3 ``qk_matmul_output`` is emitted at the output precision ``T1``.\n\n``qk_matmul_output_mode=3`` exposes the post-softmax probabilities. When\n``softmax_precision`` differs from the operator's output type ``T1`` (here\n``T1 = float16`` with softmax computed in ``float32``), the mode-3 output is\ncast back to ``T1`` -- matching the reference implementation, which casts the\nexposed matrix to ``Q.dtype``. This locks both the dtype contract and the\nfully-masked-row zeroing under a non-default ``softmax_precision``.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(17)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=3,\n softmax_precision=int(onnx.TensorProto.FLOAT),\n)\n\n# T1 = float16; softmax runs in float32, so the mode-3 output is cast back to\n# float16 on emission.\nQ = np.random.rand(B, H, S, D).astype(np.float16)\nK = np.random.rand(B, H, S, D).astype(np.float16)\nV = np.random.rand(B, H, S, D).astype(np.float16)\n# Row 0: fully masked. Row 1: both keys allowed -> finite.\nattn_mask = np.array([[False, False], [True, True]], dtype=np.bool_)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=3,\n softmax_precision=int(onnx.TensorProto.FLOAT),\n)\n\n# The mode-3 output is emitted at T1 (float16), not the float32 softmax\n# precision, matching the operator's output type.\nassert qk_matmul_output.dtype == np.float16, (\n \"mode-3 qk_matmul_output must be emitted at the output precision T1 (float16)\"\n)\n# The fully-masked row is still guarded to zero, consistent with Y.\nassert np.array_equal(\n qk_matmul_output[:, :, 0, :], np.zeros_like(qk_matmul_output[:, :, 0, :])\n), \"mode-3 output row for a fully-masked query must be zero (consistent with Y)\"\nassert np.all(np.isfinite(qk_matmul_output)), (\n \"all mode-3 rows are finite (the fully-masked row is guarded to 0.0)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_24_qk_matmul_output_mode3_softmax_precision\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_3d", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_attn_mask", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_3d_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_causal", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_attn_mask", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_causal", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_scaled", "code": "scale = 1e-2\nq_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_softcap", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_diff_heads_sizes_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_diff_head_sizes_with_past_and_present", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 30).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 10).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_3d_diff_heads_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_gqa\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_attn_mask", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_3d_gqa_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_causal", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n is_causal=1,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_gqa_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_scaled", "code": "scale = 1e-2\nq_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_gqa_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_softcap", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_gqa_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_gqa_with_past_and_present", "code": "q_num_heads, kv_num_heads = 9, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 72).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_3d_gqa_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_scaled", "code": "scale = 1e-2\nq_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n scale=scale,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_softcap", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n softcap=3.0,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_transpose_verification", "code": "\"\"\"Test case to verify correct 3D to 4D transpose behavior.\n\nThis test verifies that 3D inputs are correctly reshaped and transposed\naccording to the ONNX specification:\n[batch_size, seq_length, hidden_size] ->\n[batch_size, seq_length, num_heads, head_size] ->\n[batch_size, num_heads, seq_length, head_size]\n\"\"\"\nq_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\n# Test inputs that will clearly demonstrate the transpose behavior\nbatch_size = 1\nq_seq_length = 2\nkv_seq_length = 2\nhead_size = 4\nq_hidden_size = q_num_heads * head_size # 3 * 4 = 12\nkv_hidden_size = kv_num_heads * head_size # 3 * 4 = 12\n\n# Create structured inputs to verify correct transpose behavior\n# Q has a pattern where each position in hidden dimension has a specific value\nQ = np.zeros((batch_size, q_seq_length, q_hidden_size), dtype=np.float32)\n# Fill Q with pattern: head0=[1,1,1,1], head1=[2,2,2,2], head2=[3,3,3,3]\nfor head in range(q_num_heads):\n start_idx = head * head_size\n end_idx = start_idx + head_size\n Q[0, :, start_idx:end_idx] = float(head + 1)\n\nK = np.ones((batch_size, kv_seq_length, kv_hidden_size), dtype=np.float32) * 0.1\nV = np.ones((batch_size, kv_seq_length, kv_hidden_size), dtype=np.float32) * 0.1\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_3d_transpose_verification\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_3d_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present_qk_matmul", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_3d_with_past_and_present_qk_matmul\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present_qk_matmul_bias", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n qk_matmul_output_mode=2,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_3d_with_past_and_present_qk_matmul_bias\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present_qk_matmul_softcap", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n softcap=2.0,\n qk_matmul_output_mode=1,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n softcap=2.0,\n qk_matmul_output_mode=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_3d_with_past_and_present_qk_matmul_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_3d_with_past_and_present_qk_matmul_softmax", "code": "q_num_heads, kv_num_heads = 3, 3\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n qk_matmul_output_mode=3,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 4, 24).astype(np.float32)\nK = np.random.rand(2, 6, 24).astype(np.float32)\nV = np.random.rand(2, 6, 24).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n q_num_heads=q_num_heads,\n kv_num_heads=kv_num_heads,\n qk_matmul_output_mode=3,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_3d_with_past_and_present_qk_matmul_softmax\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_4d_causal_nonpad_attn_mask_composition", "code": "\"\"\"Compose ``is_causal`` + ``nonpad_kv_seqlen`` + boolean ``attn_mask``.\n\nThe existing nonpad tests use no ``attn_mask`` and the existing mask tests\nuse no ``nonpad_kv_seqlen``; this is the first to activate all three\nconstraints together on the external-cache path with ``batch > 1``. The\nthree biases are summed additively and a key is attended only if allowed by\nall three. Crucially the inputs are designed so that **each constraint is\nindependently necessary** -- removing any one changes the golden -- to avoid a\ndegenerate test that a backend ignoring ``is_causal`` and/or\n``nonpad_kv_seqlen`` could still pass:\n\n* **``is_causal`` binds.** Each batch has a key that the boolean mask allows\n (``True``) but the bottom-right causal frontier disallows\n (``j > i + offset``); only ``is_causal`` masks it (batch 0 row 0 key 2,\n batch 1 row 0 key 3).\n* **``attn_mask`` binds.** Each batch has a key the causal frontier and the\n padding bound both allow but the boolean mask sets ``False`` (batch 0 row 2\n key 1, batch 1 row 2 key 2); only the mask masks it.\n* **``nonpad_kv_seqlen`` binds.** ``nonpad_kv_seqlen`` sets the per-batch\n causal *offset* (``offset = nonpad_kv_seqlen - q_sequence_length``), so\n dropping it collapses the frontier to top-left (``offset = 0``) and shifts\n which keys are attended. (Under ``is_causal=1`` the causal frontier already\n subsumes the ``j < nonpad`` padding bound, so ``nonpad_kv_seqlen`` binds\n through the offset it induces rather than through a redundant padding cut.)\n\nThe mask is chosen to leave at least one allowed key on every query row, so\nthis exercises the *intersection* of the three constraints with finite outputs\n(the fully-masked-row guard is covered by\n``test_attention_4d_causal_nonpad_negative_offset_structural_empty`` and\n``test_attention_24_fullymasked_qk_matmul_output_mode3_zero``).\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(11)\nB, H, L, D = 2, 2, 6, 8\nS_q = 3\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, L, D).astype(np.float32)\nV = np.random.rand(B, H, L, D).astype(np.float32)\nnonpad_kv_seqlen = np.array([4, 5], dtype=np.int64) # offsets [1, 2]\n# Per-batch (B, 1, S_q, L) bool mask. Each batch is laid out so all three\n# constraints uniquely bind (see the docstring): a causal-only-masked key\n# (mask True, j > i + offset), a mask-only-masked key (mask False, causal +\n# nonpad allow it), and >=1 allowed key per row.\nattn_mask = np.array(\n [\n [\n [\n [True, True, True, False, False, False],\n [True, True, True, False, False, False],\n [True, False, True, True, False, False],\n ]\n ],\n [\n [\n [True, True, True, True, False, False],\n [True, True, True, True, False, False],\n [True, True, False, True, True, False],\n ]\n ],\n ],\n dtype=np.bool_,\n)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\n# The chosen mask leaves >=1 allowed key per row, so the composition stays\n# finite (no fully-masked row in this case).\nassert np.all(np.isfinite(Y)), \"composed-constraint output must be finite\"\n\n# Non-degeneracy: each constraint is independently necessary. Removing any one\n# of the three (is_causal, attn_mask, nonpad_kv_seqlen) must change the result,\n# so a backend that ignores is_causal or nonpad_kv_seqlen cannot reproduce the\n# golden by applying only the most restrictive mask.\ny_no_causal, _, _, _ = _compute_attention(\n Q, K, V, attn_mask=attn_mask, nonpad_kv_seqlen=nonpad_kv_seqlen, is_causal=0\n)\ny_no_mask, _, _, _ = _compute_attention(\n Q, K, V, nonpad_kv_seqlen=nonpad_kv_seqlen, is_causal=1\n)\ny_no_nonpad, _, _, _ = _compute_attention(\n Q, K, V, attn_mask=attn_mask, is_causal=1\n)\nassert not np.allclose(Y, y_no_causal, equal_nan=True), (\n \"is_causal must bind: dropping it changes the result\"\n)\nassert not np.allclose(Y, y_no_mask, equal_nan=True), (\n \"attn_mask must bind: dropping it changes the result\"\n)\nassert not np.allclose(Y, y_no_nonpad, equal_nan=True), (\n \"nonpad_kv_seqlen must bind (via the causal offset): dropping it changes the result\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_causal_nonpad_attn_mask_composition\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_causal_nonpad_batch_prefill", "code": "\"\"\"Batch>1 continued prefill with distinct per-batch bottom-right offsets.\n\nThe batched generalization of the ``batch == 1`` continued-prefill case: with\n``nonpad_kv_seqlen = [4, 5, 6]`` and ``S_q = 2`` the per-batch bottom-right\noffsets are ``[2, 3, 4]`` (all ``>= 0``), so each batch realigns its causal\nfrontier to its own valid-key prefix. This pins that the per-batch offset is\napplied independently across the batch dimension.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(12)\nB, H, L, D = 3, 2, 6, 8\nS_q = 2\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, L, D).astype(np.float32)\nV = np.random.rand(B, H, L, D).astype(np.float32)\nnonpad_kv_seqlen = np.array([4, 5, 6], dtype=np.int64) # offsets [2, 3, 4]\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\nassert np.all(np.isfinite(Y)), \"per-batch prefill output must be finite\"\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_causal_nonpad_batch_prefill\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_causal_nonpad_continued_prefill", "code": "\"\"\"Continued / chunked prefill (S_q=2) into a partially-filled static cache.\n\nWith ``nonpad_kv_seqlen = [4]`` and ``S_q = 2`` the bottom-right offset is\n``4 - 2 = 2``: query 0 attends keys ``{0,1,2}`` and query 1 attends\n``{0,1,2,3}``. The old top-left alignment would mask everything past the\ndiagonal (``{0}`` and ``{0,1}``), so this test fails pre-fix.\n\"\"\"\nnp.random.seed(1)\nB, H, L, D = 1, 2, 4, 8\nS_q = 2\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, L, D).astype(np.float32)\nV = np.random.rand(B, H, L, D).astype(np.float32)\nnonpad_kv_seqlen = np.array([4], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_causal_nonpad_continued_prefill\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_causal_nonpad_negative_offset_structural_empty", "code": "\"\"\"Negative bottom-right offset: structurally-empty early query rows -> zero.\n\nThis is the onnx-node twin of the ORT gtest\n``Attention_Causal_NonPadKVSeqLen_StructuralEmptyRow_Zero`` /\n``StructuralEmptyRows_Zero_CUDA``. With ``nonpad_kv_seqlen = [2]`` and\n``S_q = 4`` the bottom-right offset is ``2 - 4 = -2``: query row ``sq``\nattends keys ``0..(sq - 2)``, so rows 0 and 1 have an empty key set. Their\n``softmax`` over an all-``-inf`` bias row is ``NaN``; the fully-masked-row\nguard zeros those rows before the ``P @ V`` contraction so the output rows are\nexactly ``0``, while rows 2 and 3 (attending keys ``{0}`` and ``{0,1}``) stay finite\nand nonzero. A ``nonpad_kv_seqlen[b] < q_sequence_length`` input is out of\nthe contract's intended use, but its result is still well-defined (zeroed\nrows) rather than ``NaN``; this test pins that defined behavior.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(7)\nB, H, L, D = 1, 2, 4, 8\nS_q = 4\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, L, D).astype(np.float32)\nV = np.random.rand(B, H, L, D).astype(np.float32)\n# offset = nonpad - S_q = 2 - 4 = -2 -> rows 0,1 structurally empty.\nnonpad_kv_seqlen = np.array([2], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\n# Structurally-empty early rows are exactly zero (not NaN); later rows finite.\nassert np.all(np.isfinite(Y)), \"all output rows must be finite\"\nassert np.array_equal(Y[:, :, 0, :], np.zeros_like(Y[:, :, 0, :])), (\n \"structurally-empty row 0 must be zero\"\n)\nassert np.array_equal(Y[:, :, 1, :], np.zeros_like(Y[:, :, 1, :])), (\n \"structurally-empty row 1 must be zero\"\n)\nassert np.any(Y[:, :, 2, :] != 0) and np.any(Y[:, :, 3, :] != 0), (\n \"rows with a non-empty key set must be nonzero\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_causal_nonpad_negative_offset_structural_empty\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_causal_with_past_and_present", "code": "\"\"\"Regression guard: internal (past_key) cache + is_causal.\n\nThis exercises the unchanged scalar bottom-right path (offset =\npast_sequence_length). Its golden output must remain identical to the\npre-fix behavior, proving the external-cache change does not touch the\npast_key path.\n\"\"\"\nnp.random.seed(2)\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n is_causal=1,\n)\n\npast_sequence_length = 3\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 4, 8).astype(np.float32)\nV = np.random.rand(2, 3, 4, 8).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n past_key=past_key,\n past_value=past_value,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_causal_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_diff_heads_mask4d_padded_kv", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 4).astype(np.float32)\nnonpad_kv_seqlen = np.array([3, 4], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_mask4d_padded_kv\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_gqa_causal_nonpad_decode", "code": "\"\"\"External/static-cache decode (S_q=1) with per-batch valid lengths.\n\nK/V are the full static cache buffer; ``nonpad_kv_seqlen`` marks how many\nleading keys are valid per batch. With bottom-right (offset-aware) causal\nmasking the single decode query attends keys ``0..nonpad[b]-1``. Under the\nold top-left alignment it would attend only key 0, so this test fails\npre-fix and passes post-fix.\n\"\"\"\nnp.random.seed(0)\nB, H_q, H_kv, L, D = 2, 4, 2, 8, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H_q, 1, D).astype(np.float32)\nK = np.random.rand(B, H_kv, L, D).astype(np.float32)\nV = np.random.rand(B, H_kv, L, D).astype(np.float32)\n# Batch 0 has all 8 keys valid, batch 1 only the first 5.\nnonpad_kv_seqlen = np.array([8, 5], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_gqa_causal_nonpad_decode\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_4d_gqa_causal_nonpad_decode_fp16", "code": "\"\"\"fp16 variant of the external-cache decode case (locks -inf dtype handling).\"\"\"\nnp.random.seed(0)\nB, H_q, H_kv, L, D = 2, 4, 2, 8, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"\", \"\", \"\", \"nonpad_kv_seqlen\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H_q, 1, D).astype(np.float16)\nK = np.random.rand(B, H_kv, L, D).astype(np.float16)\nV = np.random.rand(B, H_kv, L, D).astype(np.float16)\nnonpad_kv_seqlen = np.array([8, 5], dtype=np.int64)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n nonpad_kv_seqlen=nonpad_kv_seqlen,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, nonpad_kv_seqlen],\n outputs=[Y],\n name=\"test_attention_4d_gqa_causal_nonpad_decode_fp16\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_attn_3d_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_3d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_3d_mask_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_3d_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_4d_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_4d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_4d_mask_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_4d_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_mask_bool", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(bool)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_bool\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_attn_mask_bool_4d", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6).astype(bool)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_attn_mask_bool_4d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, is_causal=1)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_causal_boolmask_nan_robustness", "code": "\"\"\"Composed ``is_causal`` + boolean ``attn_mask`` NaN-robustness.\n\nThe causal frontier (lower-triangular here, offset 0) and the boolean\n``attn_mask`` are intersected: a key is attended only if allowed by both.\nThis exercises two pre-fix NaN sources on the same forward pass:\n\n* **Bug-1 (allowed cells stay finite).** Query 0 is allowed key 0 by both\n the causal frontier (``{0}``) and the mask (``True`` at key 0). The old\n ``(1 - attn_mask) * -inf`` conversion computes ``0 * -inf = NaN`` at that\n allowed cell, poisoning the row. The select conversion\n ``where(attn_mask, 0, -inf)`` keeps it finite.\n* **Bug-2 (fully-masked row -> 0).** Query 1 is allowed keys ``{0, 1}`` by\n the causal frontier but the mask is ``False`` at both, so the combined\n constraint allows no key. ``softmax`` of an all-``-inf`` row is ``NaN``;\n the fully-masked-row guard zeros it before the ``P @ V`` contraction so\n the output row is ``0``.\n\n4D Q/K/V is used so ``q_num_heads``/``kv_num_heads`` are omitted (passing\nthem would make the function body treat the input as 3D).\n\"\"\"\nnp.random.seed(3)\nB, H, S, D = 1, 2, 2, 8\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(B, H, S, D).astype(np.float32)\nK = np.random.rand(B, H, S, D).astype(np.float32)\nV = np.random.rand(B, H, S, D).astype(np.float32)\n# Row 0: key 0 allowed (Bug-1 allowed cell). Row 1: no key allowed -> fully\n# masked once intersected with the causal frontier (Bug-2 empty row).\nattn_mask = np.array([[True, False], [False, False]], dtype=np.bool_)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n is_causal=1,\n)\n\n# Bug-1: allowed cells are finite (no NaN anywhere). Bug-2: the fully-masked\n# query row is exactly zero, not NaN.\nassert np.all(np.isfinite(Y)), \"allowed cells must be finite (Bug-1)\"\nassert np.array_equal(Y[:, :, 1, :], np.zeros_like(Y[:, :, 1, :])), (\n \"fully-masked row must be zero (Bug-2)\"\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_causal_boolmask_nan_robustness\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" }, { "summary": "attention_diff_head_sizes", "code": "node = onnx.helper.make_node(\"Attention\", inputs=[\"Q\", \"K\", \"V\"], outputs=[\"Y\"])\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_attn_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_scaled", "code": "scale = 1e-2\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, scale=scale)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_softcap", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=2.0,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n softcap=2.0,\n)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_diff_heads_sizes_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_with_past_and_present", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 10).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_diff_heads_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_with_past_and_present_mask3D", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 10).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_diff_heads_with_past_and_present_mask3d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_diff_head_sizes_with_past_and_present_mask4D", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 10).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 10).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_diff_heads_with_past_and_present_mask4d\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_fp16", "code": "node = onnx.helper.make_node(\"Attention\", inputs=[\"Q\", \"K\", \"V\"], outputs=[\"Y\"])\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float16)\nK = np.random.rand(2, 3, 6, 8).astype(np.float16)\nV = np.random.rand(2, 3, 6, 8).astype(np.float16)\n\nY, _, _, _ = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_fp16\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa", "code": "node = onnx.helper.make_node(\"Attention\", inputs=[\"Q\", \"K\", \"V\"], outputs=[\"Y\"])\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_gqa\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_attn_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n)\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_gqa_attn_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n is_causal=1,\n)\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, is_causal=1)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_gqa_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_scaled", "code": "scale = 1e-2\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n)\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, scale=scale)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_gqa_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_softcap", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=2.0,\n)\n\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, softcap=2.0)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_gqa_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_with_past_and_present", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 9, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_gqa_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_gqa_with_past_and_present_fp16", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 9, 4, 8).astype(np.float16)\nK = np.random.rand(2, 3, 6, 8).astype(np.float16)\nV = np.random.rand(2, 3, 6, 8).astype(np.float16)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float16)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float16)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float16)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_gqa_with_past_and_present_fp16\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_scaled", "code": "scale = 1e-2\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, scale=scale)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_scaled\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_softcap", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n softcap=2.0,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, _ = _compute_attention(Q, K, V, softcap=2.0)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_attention_4d_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_softcap_with_neginf_mask", "code": "\"\"\"Softcap + -inf mask: verifies softcap is applied BEFORE mask/bias.\n\nIf ordering were wrong (mask then softcap), tanh(-inf/softcap) = -1,\nso softcap * tanh(-inf/softcap) = -softcap (finite). That leaks\nprobability to masked positions. With correct ordering (softcap then\nmask), the -inf mask values survive to softmax and yield zero weight.\n\"\"\"\nnp.random.seed(42)\nB, H, S_q, S_kv, D = 1, 1, 4, 6, 8\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, S_kv, D).astype(np.float32)\nV = np.random.rand(B, H, S_kv, D).astype(np.float32)\n\n# All Q positions are blocked from KV positions 4 and 5.\nattn_mask = np.zeros((S_q, S_kv), dtype=np.float32)\nattn_mask[:, 4:] = -np.inf\n\nsoftcap = 0.5\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n softcap=softcap,\n)\n\nY, _, _, _ = _compute_attention(Q, K, V, attn_mask=attn_mask, softcap=softcap)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_softcap_neginf_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_softcap_with_neginf_mask_poison", "code": "\"\"\"Softcap + -inf mask + poison values at masked KV positions.\n\nV has value 1000 at the masked positions (4 and 5). With correct\nordering the output stays in [0, 1] because the mask zeros out those\npositions. With wrong ordering the output explodes (> 50), making\nthe failure obvious even with loose tolerances.\n\"\"\"\nnp.random.seed(42)\nB, H, S_q, S_kv, D = 1, 1, 4, 6, 8\n\nQ = np.random.rand(B, H, S_q, D).astype(np.float32)\nK = np.random.rand(B, H, S_kv, D).astype(np.float32)\nV = np.random.rand(B, H, S_kv, D).astype(np.float32)\n\n# Block all Q positions from KV positions 4 and 5.\nattn_mask = np.zeros((S_q, S_kv), dtype=np.float32)\nattn_mask[:, 4:] = -np.inf\n\n# Poison: if attention leaks to masked positions, output >> 1.\nV[:, :, 4:, :] = 1000.0\n\nsoftcap = 0.5\n\nnode = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\"],\n softcap=softcap,\n)\n\nY, _, _, _ = _compute_attention(Q, K, V, attn_mask=attn_mask, softcap=softcap)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y],\n name=\"test_attention_4d_softcap_neginf_mask_poison\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, _ = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value],\n name=\"test_attention_4d_with_past_and_present\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias_3d_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias_3d_mask_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n is_causal=1,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 1, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias_4d_mask", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_past_and_present_qk_matmul_bias_4d_mask_causal", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\", \"past_key\", \"past_value\"],\n outputs=[\"Y\", \"present_key\", \"present_value\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n is_causal=1,\n)\n\npast_sequence_length = 12\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(2, 3, 4, 6 + past_sequence_length).astype(np.float32)\npast_key = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\npast_value = np.random.rand(2, 3, past_sequence_length, 8).astype(np.float32)\n\nY, present_key, present_value, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n past_key=past_key,\n past_value=past_value,\n qk_matmul_output_mode=2,\n is_causal=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask, past_key, past_value],\n outputs=[Y, present_key, present_value, qk_matmul_output],\n name=\"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_causal\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_qk_matmul", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\n\nY, _, _, qk_matmul_output = _compute_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_4d_with_qk_matmul\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_qk_matmul_bias", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=2,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=2,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_4d_with_qk_matmul_bias\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_qk_matmul_softcap", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n softcap=2.0,\n qk_matmul_output_mode=1,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n softcap=2.0,\n qk_matmul_output_mode=1,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_4d_with_qk_matmul_softcap\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" }, { "summary": "attention_with_qk_matmul_softmax", "code": "node = onnx.helper.make_node(\n \"Attention\",\n inputs=[\"Q\", \"K\", \"V\", \"attn_mask\"],\n outputs=[\"Y\", \"\", \"\", \"qk_matmul_output\"],\n qk_matmul_output_mode=3,\n)\n\nQ = np.random.rand(2, 3, 4, 8).astype(np.float32)\nK = np.random.rand(2, 3, 6, 8).astype(np.float32)\nV = np.random.rand(2, 3, 6, 8).astype(np.float32)\nattn_mask = np.random.rand(4, 6).astype(np.float32)\n\nY, _, _, qk_matmul_output = _compute_attention(\n Q,\n K,\n V,\n attn_mask=attn_mask,\n qk_matmul_output_mode=3,\n)\n\nexpect(\n node,\n inputs=[Q, K, V, attn_mask],\n outputs=[Y, qk_matmul_output],\n name=\"test_attention_4d_with_qk_matmul_softmax\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 23)],\n)" } ], "category": "Attention" }, { "name": "Attention", "module": "com.microsoft", "version": 1, "description": "Multi-Head Attention that can be either unidirectional (like GPT-2) or bidirectional (like BERT).\n\nThe weights for input projection of Q, K and V are merged. The data is stacked on the second dimension. Its shape\nis (input_hidden_size, hidden_size + hidden_size + v_hidden_size). Here hidden_size is the hidden dimension of Q and K,\nand v_hidden_size is that of V.\n\nThe mask_index is optional. Besides raw attention mask with shape (batch_size, total_sequence_length)\nor (batch_size, sequence_length, total_sequence_length) with value 0 for masked and 1 otherwise,\nwe support other two formats: When input has right-side padding, mask_index is one dimension with shape (batch_size),\nwhere value is actual sequence length excluding padding. When input has left-side padding, mask_index has\nshape (2 * batch_size), where the values are the exclusive end positions followed by the inclusive start positions.\n\nWhen unidirectional is 1, each token only attends to previous tokens.\n\nBoth past and present state are optional. They shall be used together, and not allowed to use only one of them.\nThe qkv_hidden_sizes is required only when K and V have different hidden sizes.\n\nWhen there is past state, hidden dimension for Q, K and V shall be the same.\n\nThe total_sequence_length is past_sequence_length + kv_sequence_length. Here kv_sequence_length is the length of K or V.\nFor self attention, kv_sequence_length equals to sequence_length (sequence length of Q).\nFor cross attention, query and key might have different lengths.\n", "attributes": [ { "name": "do_rotary", "type": "int64", "required": false, "description": "Whether to use rotary position embedding. Default value is 0." }, { "name": "mask_filter_value", "type": "float32", "required": false, "description": "The value to be filled in the attention mask. Default value is -10000.0f" }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "past_present_share_buffer", "type": "int64", "required": false, "description": "Corresponding past and present are same tensor, its size is (2, batch_size, num_heads, max_sequence_length, head_size)" }, { "name": "qkv_hidden_sizes", "type": "int64[]", "required": false, "description": "Hidden dimension of Q, K, V: hidden_size, hidden_size and v_hidden_size" }, { "name": "rotary_embedding_dim", "type": "int64", "required": false, "description": "Dimension of rotary embedding. Limited to 32, 64 or 128. Default value is head_size" }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1/sqrt(head_size)" }, { "name": "unidirectional", "type": "int64", "required": false, "description": "Whether every token can only attend to previous tokens. Default value is 0." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor with shape (batch_size, sequence_length, input_hidden_size)" }, { "name": "weights", "type": "T", "description": "Merged Q/K/V weights with shape (input_hidden_size, hidden_size + hidden_size + v_hidden_size)" }, { "name": "bias", "type": "T", "option": "optional", "description": "Bias tensor with shape (hidden_size + hidden_size + v_hidden_size) for input projection" }, { "name": "mask_index", "type": "M", "option": "optional", "description": "Attention mask with shape (batch_size, 1, max_sequence_length, max_sequence_length), (batch_size, total_sequence_length) or (batch_size, sequence_length, total_sequence_length), or index with shape (batch_size) or (2 * batch_size) or (3 * batch_size + 2)" }, { "name": "past", "type": "T", "option": "optional", "description": "past state for key and value with shape (2, batch_size, num_heads, past_sequence_length, head_size)When past_present_share_buffer is set, its shape is (2, batch_size, num_heads, max_sequence_length, head_size)" }, { "name": "attention_bias", "type": "T", "option": "optional", "description": "additional add to QxK' with shape (batch_size or 1, num_heads or 1, sequence_length, total_sequence_length)" }, { "name": "past_sequence_length", "type": "M", "option": "optional", "description": "When past_present_share_buffer is used, it is required to specify past_sequence_length (could be 0)." } ], "min_input": 2, "max_input": 7, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, v_hidden_size)" }, { "name": "present", "type": "T", "option": "optional", "description": "past state for key and value with shape (2, batch_size, num_heads, total_sequence_length, head_size). If past_present_share_buffer is set, its shape is (2, batch_size, num_heads, max_sequence_length, head_size), while effective_seq_length = (past_sequence_length + kv_sequence_length)." } ], "min_output": 1, "max_output": 2, "inputs_range": "2 - 7", "outputs_range": "1 - 2", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain mask index to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "AttnLSTM", "module": "com.microsoft", "version": 1, "description": "Computes an one-layer RNN where its RNN Cell is an AttentionWrapper wrapped a LSTM Cell. The RNN layer\ncontains following basic component: LSTM Cell, Bahdanau Attention Mechanism, AttentionWrapp.\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\n Softmax(x) - exp(x) / sum(exp(x))\n\nBahdanau Attention Mechanism:\n `M` - Memory tensor.\n\n `VALUES` - masked Memory by its real sequence length.\n\n `MW` - Memory layer weight.\n\n `KEYS` - Processed memory tensor by the memory layer.\n KEYS = M * MW\n\n `Query` - Query tensor, normally at specific time step in sequence.\n\n `QW` - Query layer weight in the attention mechanism\n\n `PQ` - processed query, = `Query` * `QW`\n\n `V' - attention vector\n\n `ALIGN` - calculated alignment based on Query and KEYS\n ALIGN = softmax(reduce_sum(`V` * Tanh(`KEYS` + `PQ`)))\n\n `CONTEXT` - context based on `ALIGN` and `VALUES`\n CONTEXT = `ALIGN` * `VALUES`\n\n\nLSTM Cell:\n `X` - input tensor concat with attention state in the attention wrapper\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\n Equations (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)\n\n\nAttentionWrapp Notations:\n `lstm()' - wrapped inner cell.\n Ht, Ct = lstm(concat(Xt, ATTNt-1), Ct-1)\n\n `am()` - attention mechanism the wrapper used.\n CONTEXTt, ALIGNt = am(Ht, ALIGNt-1)\n\n `AW` - attention layer weights, optional.\n\n `ATTN` - attention state, initial is zero. If `AW` provided, it is the output of the attention layer,\n ATTNt = concat(Ht, CONTEXTt) * AW\n otherwise,\n ATTNt = CONTEXTt\n\nRNN layer output:\n `Y` - if needed is the sequence of Ht from lstm cell.\n\n `Y_h` - is the last valid H from lstm cell.\n\n `Y_c` - is the last valid C from lstm cell.\n\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." } ], "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." }, { "name": "QW", "type": "T", "option": "optional", "description": "The weight tensor of the query layer in the attention mechanism. Should be of shape `[num_directions, am_query_depth(hidden_size of lstm), am_attn_size]` " }, { "name": "MW", "type": "T", "option": "optional", "description": "The weight tensor of the memory layer in the attention mechanism. Should be of shape `[num_directions, memory_depth, am_attn_size]` " }, { "name": "V", "type": "T", "option": "optional", "description": "The attention_v tensor in the attention mechanism. Should be of shape `[num_directions, am_attn_size]` " }, { "name": "M", "type": "T", "option": "optional", "description": "The sequence of the memory (input) for attention mechanism. Should be of `[batch_size, max_memory_step, memory_depth]` " }, { "name": "memory_seq_lens", "type": "T1", "option": "optional", "description": "The sequence length of the input memory for the attention mechanism. Should be of `[batch_size]` " }, { "name": "AW", "type": "T", "option": "optional", "description": "The weights of attention layer in the attention wrapper. If exists, should be of shape `[num_directions, memory_depth+hidden_size, aw_attn_size]. Please note that attention mechanism context depth is also memory_depth in the attention mechanism.` " } ], "min_input": 3, "max_input": 14, "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 - 14", "outputs_range": "0 - 3", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(double)" ] }, { "description": "Constrain seq_lens to integral tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "AveragePool", "module": "ai.onnx", "version": 1, "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_1d_default\")" }, { "summary": "averagepool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[6, 7.5], [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_ceil\")" }, { "summary": "averagepool_2d_ceil_last_window_starts_on_pad", "code": "\"\"\"input_shape: [1, 3, 2, 2]\noutput_shape: [1, 3, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n strides=[3, 3],\n pads=[1, 1, 1, 1],\n ceil_mode=True,\n count_include_pad=1,\n)\nx = np.array(\n [\n [\n [[0.8580, 0.0786], [0.2692, 0.1537]],\n [[0.8816, 0.4353], [0.5772, 0.6623]],\n [[0.9067, 0.9483], [0.5970, 0.7630]],\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[0.1511]], [[0.2841]], [[0.3572]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_ceil_last_window_starts_on_pad\",\n)" }, { "summary": "averagepool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_default\")" }, { "summary": "averagepool_2d_dilations", "code": "\"\"\"input_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=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4]\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[6, 7], [10, 11]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_dilations\")" }, { "summary": "averagepool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=np.nan,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_pads\")" }, { "summary": "averagepool_2d_pads_count_include_pad", "code": "\"\"\"input_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)\ndilations = (1, 1)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, dilations, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n count_include_pad=1,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_pads", "code": "\"\"\"input_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)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_precomputed_pads\"\n)" }, { "summary": "averagepool_2d_precomputed_pads_count_include_pad", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 5.5, 7], [11.5, 13, 14.5], [19, 20.5, 22]]]]).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_same_upper\",\n)" }, { "summary": "averagepool_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 6], [14, 16]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_strides\",\n)" }, { "summary": "averagepool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_lower\")" }, { "summary": "averagepool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_upper\")" }, { "summary": "averagepool_2d_strides", "code": "\"\"\"input_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, pads = get_output_shape_explicit_padding(\n None, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = x\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=None,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_strides\")" }, { "summary": "averagepool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_default\")" }, { "summary": "averagepool_3d_dilations", "code": "\"\"\"input_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=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4, 4]\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[[6, 7], [10, 11]], [[6, 7], [10, 11]]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_dilations_small\"\n)" }, { "summary": "averagepool_3d_dilations_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\ncount_include_pad = 0\n\nfor count_include_pad in (0, 1):\n for ceil_mode in (True, False):\n node = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n count_include_pad=count_include_pad,\n ceil_mode=ceil_mode,\n )\n\n x = np.random.randn(1, 1, *x_shape).astype(np.float32)\n out_shape, extra_pads = get_output_shape_explicit_padding(\n None,\n x_shape,\n kernel_shape,\n strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n )\n padded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[3]),\n (extra_pads[1], extra_pads[4]),\n (extra_pads[2], extra_pads[5]),\n ),\n mode=\"constant\",\n constant_values=0 if count_include_pad == 1 else np.nan,\n )\n y = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=None,\n dilations=dilations,\n count_include_pad=count_include_pad,\n )\n\n test_name = f\"test_averagepool_3d_dilations_large_count_include_pad_is_{count_include_pad}_ceil_mode_is_{ceil_mode}\"\n expect(node, inputs=[x], outputs=[y], name=test_name)" } ], "category": "Pool" }, { "name": "AveragePool", "module": "ai.onnx", "version": 7, "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_1d_default\")" }, { "summary": "averagepool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[6, 7.5], [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_ceil\")" }, { "summary": "averagepool_2d_ceil_last_window_starts_on_pad", "code": "\"\"\"input_shape: [1, 3, 2, 2]\noutput_shape: [1, 3, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n strides=[3, 3],\n pads=[1, 1, 1, 1],\n ceil_mode=True,\n count_include_pad=1,\n)\nx = np.array(\n [\n [\n [[0.8580, 0.0786], [0.2692, 0.1537]],\n [[0.8816, 0.4353], [0.5772, 0.6623]],\n [[0.9067, 0.9483], [0.5970, 0.7630]],\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[0.1511]], [[0.2841]], [[0.3572]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_ceil_last_window_starts_on_pad\",\n)" }, { "summary": "averagepool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_default\")" }, { "summary": "averagepool_2d_dilations", "code": "\"\"\"input_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=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4]\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[6, 7], [10, 11]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_dilations\")" }, { "summary": "averagepool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=np.nan,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_pads\")" }, { "summary": "averagepool_2d_pads_count_include_pad", "code": "\"\"\"input_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)\ndilations = (1, 1)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, dilations, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n count_include_pad=1,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_pads", "code": "\"\"\"input_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)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_precomputed_pads\"\n)" }, { "summary": "averagepool_2d_precomputed_pads_count_include_pad", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 5.5, 7], [11.5, 13, 14.5], [19, 20.5, 22]]]]).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_same_upper\",\n)" }, { "summary": "averagepool_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 6], [14, 16]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_strides\",\n)" }, { "summary": "averagepool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_lower\")" }, { "summary": "averagepool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_upper\")" }, { "summary": "averagepool_2d_strides", "code": "\"\"\"input_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, pads = get_output_shape_explicit_padding(\n None, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = x\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=None,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_strides\")" }, { "summary": "averagepool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_default\")" }, { "summary": "averagepool_3d_dilations", "code": "\"\"\"input_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=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4, 4]\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[[6, 7], [10, 11]], [[6, 7], [10, 11]]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_dilations_small\"\n)" }, { "summary": "averagepool_3d_dilations_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\ncount_include_pad = 0\n\nfor count_include_pad in (0, 1):\n for ceil_mode in (True, False):\n node = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n count_include_pad=count_include_pad,\n ceil_mode=ceil_mode,\n )\n\n x = np.random.randn(1, 1, *x_shape).astype(np.float32)\n out_shape, extra_pads = get_output_shape_explicit_padding(\n None,\n x_shape,\n kernel_shape,\n strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n )\n padded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[3]),\n (extra_pads[1], extra_pads[4]),\n (extra_pads[2], extra_pads[5]),\n ),\n mode=\"constant\",\n constant_values=0 if count_include_pad == 1 else np.nan,\n )\n y = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=None,\n dilations=dilations,\n count_include_pad=count_include_pad,\n )\n\n test_name = f\"test_averagepool_3d_dilations_large_count_include_pad_is_{count_include_pad}_ceil_mode_is_{ceil_mode}\"\n expect(node, inputs=[x], outputs=[y], name=test_name)" } ], "category": "Pool" }, { "name": "AveragePool", "module": "ai.onnx", "version": 10, "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_1d_default\")" }, { "summary": "averagepool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[6, 7.5], [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_ceil\")" }, { "summary": "averagepool_2d_ceil_last_window_starts_on_pad", "code": "\"\"\"input_shape: [1, 3, 2, 2]\noutput_shape: [1, 3, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n strides=[3, 3],\n pads=[1, 1, 1, 1],\n ceil_mode=True,\n count_include_pad=1,\n)\nx = np.array(\n [\n [\n [[0.8580, 0.0786], [0.2692, 0.1537]],\n [[0.8816, 0.4353], [0.5772, 0.6623]],\n [[0.9067, 0.9483], [0.5970, 0.7630]],\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[0.1511]], [[0.2841]], [[0.3572]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_ceil_last_window_starts_on_pad\",\n)" }, { "summary": "averagepool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_default\")" }, { "summary": "averagepool_2d_dilations", "code": "\"\"\"input_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=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4]\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[6, 7], [10, 11]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_dilations\")" }, { "summary": "averagepool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=np.nan,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_pads\")" }, { "summary": "averagepool_2d_pads_count_include_pad", "code": "\"\"\"input_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)\ndilations = (1, 1)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, dilations, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n count_include_pad=1,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_pads", "code": "\"\"\"input_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)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_precomputed_pads\"\n)" }, { "summary": "averagepool_2d_precomputed_pads_count_include_pad", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 5.5, 7], [11.5, 13, 14.5], [19, 20.5, 22]]]]).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_same_upper\",\n)" }, { "summary": "averagepool_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 6], [14, 16]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_strides\",\n)" }, { "summary": "averagepool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_lower\")" }, { "summary": "averagepool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_upper\")" }, { "summary": "averagepool_2d_strides", "code": "\"\"\"input_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, pads = get_output_shape_explicit_padding(\n None, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = x\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=None,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_strides\")" }, { "summary": "averagepool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_default\")" }, { "summary": "averagepool_3d_dilations", "code": "\"\"\"input_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=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4, 4]\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[[6, 7], [10, 11]], [[6, 7], [10, 11]]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_dilations_small\"\n)" }, { "summary": "averagepool_3d_dilations_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\ncount_include_pad = 0\n\nfor count_include_pad in (0, 1):\n for ceil_mode in (True, False):\n node = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n count_include_pad=count_include_pad,\n ceil_mode=ceil_mode,\n )\n\n x = np.random.randn(1, 1, *x_shape).astype(np.float32)\n out_shape, extra_pads = get_output_shape_explicit_padding(\n None,\n x_shape,\n kernel_shape,\n strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n )\n padded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[3]),\n (extra_pads[1], extra_pads[4]),\n (extra_pads[2], extra_pads[5]),\n ),\n mode=\"constant\",\n constant_values=0 if count_include_pad == 1 else np.nan,\n )\n y = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=None,\n dilations=dilations,\n count_include_pad=count_include_pad,\n )\n\n test_name = f\"test_averagepool_3d_dilations_large_count_include_pad_is_{count_include_pad}_ceil_mode_is_{ceil_mode}\"\n expect(node, inputs=[x], outputs=[y], name=test_name)" } ], "category": "Pool" }, { "name": "AveragePool", "module": "ai.onnx", "version": 11, "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] - 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 when ceil_mode is enabled:\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 ```\nor when ceil_mode is disabled:\n ```\n VALID: output_spatial_shape[i] = floor((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] = floor(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\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 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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_1d_default\")" }, { "summary": "averagepool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[6, 7.5], [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_ceil\")" }, { "summary": "averagepool_2d_ceil_last_window_starts_on_pad", "code": "\"\"\"input_shape: [1, 3, 2, 2]\noutput_shape: [1, 3, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n strides=[3, 3],\n pads=[1, 1, 1, 1],\n ceil_mode=True,\n count_include_pad=1,\n)\nx = np.array(\n [\n [\n [[0.8580, 0.0786], [0.2692, 0.1537]],\n [[0.8816, 0.4353], [0.5772, 0.6623]],\n [[0.9067, 0.9483], [0.5970, 0.7630]],\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[0.1511]], [[0.2841]], [[0.3572]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_ceil_last_window_starts_on_pad\",\n)" }, { "summary": "averagepool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_default\")" }, { "summary": "averagepool_2d_dilations", "code": "\"\"\"input_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=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4]\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[6, 7], [10, 11]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_dilations\")" }, { "summary": "averagepool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=np.nan,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_pads\")" }, { "summary": "averagepool_2d_pads_count_include_pad", "code": "\"\"\"input_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)\ndilations = (1, 1)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, dilations, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n count_include_pad=1,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_pads", "code": "\"\"\"input_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)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_precomputed_pads\"\n)" }, { "summary": "averagepool_2d_precomputed_pads_count_include_pad", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 5.5, 7], [11.5, 13, 14.5], [19, 20.5, 22]]]]).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_same_upper\",\n)" }, { "summary": "averagepool_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 6], [14, 16]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_strides\",\n)" }, { "summary": "averagepool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_lower\")" }, { "summary": "averagepool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_upper\")" }, { "summary": "averagepool_2d_strides", "code": "\"\"\"input_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, pads = get_output_shape_explicit_padding(\n None, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = x\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=None,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_strides\")" }, { "summary": "averagepool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_default\")" }, { "summary": "averagepool_3d_dilations", "code": "\"\"\"input_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=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4, 4]\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[[6, 7], [10, 11]], [[6, 7], [10, 11]]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_dilations_small\"\n)" }, { "summary": "averagepool_3d_dilations_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\ncount_include_pad = 0\n\nfor count_include_pad in (0, 1):\n for ceil_mode in (True, False):\n node = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n count_include_pad=count_include_pad,\n ceil_mode=ceil_mode,\n )\n\n x = np.random.randn(1, 1, *x_shape).astype(np.float32)\n out_shape, extra_pads = get_output_shape_explicit_padding(\n None,\n x_shape,\n kernel_shape,\n strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n )\n padded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[3]),\n (extra_pads[1], extra_pads[4]),\n (extra_pads[2], extra_pads[5]),\n ),\n mode=\"constant\",\n constant_values=0 if count_include_pad == 1 else np.nan,\n )\n y = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=None,\n dilations=dilations,\n count_include_pad=count_include_pad,\n )\n\n test_name = f\"test_averagepool_3d_dilations_large_count_include_pad_is_{count_include_pad}_ceil_mode_is_{ceil_mode}\"\n expect(node, inputs=[x], outputs=[y], name=test_name)" } ], "category": "Pool" }, { "name": "AveragePool", "module": "ai.onnx", "version": 19, "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 is calculated differently\n depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized.\n With explicit padding (https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html?highlight=maxpool#torch.nn.MaxPool2d):\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled. `pad_shape[i]` is the sum of pads along axis `i`.\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following when ceil_mode is enabled:\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 or when ceil_mode is disabled (https://www.tensorflow.org/api_docs/python/tf/keras/layers/AveragePooling2D):\n ```\n VALID: output_spatial_shape[i] = floor((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i]) + 1\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = floor((input_spatial_shape[i] - 1) / strides_spatial_shape[i]) + 1\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 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": "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": "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_1d_default\")" }, { "summary": "averagepool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[6, 7.5], [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_ceil\")" }, { "summary": "averagepool_2d_ceil_last_window_starts_on_pad", "code": "\"\"\"input_shape: [1, 3, 2, 2]\noutput_shape: [1, 3, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n strides=[3, 3],\n pads=[1, 1, 1, 1],\n ceil_mode=True,\n count_include_pad=1,\n)\nx = np.array(\n [\n [\n [[0.8580, 0.0786], [0.2692, 0.1537]],\n [[0.8816, 0.4353], [0.5772, 0.6623]],\n [[0.9067, 0.9483], [0.5970, 0.7630]],\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[0.1511]], [[0.2841]], [[0.3572]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_ceil_last_window_starts_on_pad\",\n)" }, { "summary": "averagepool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_default\")" }, { "summary": "averagepool_2d_dilations", "code": "\"\"\"input_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=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4]\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[6, 7], [10, 11]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_dilations\")" }, { "summary": "averagepool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=np.nan,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_pads\")" }, { "summary": "averagepool_2d_pads_count_include_pad", "code": "\"\"\"input_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)\ndilations = (1, 1)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, dilations, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n count_include_pad=1,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_pads", "code": "\"\"\"input_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)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_precomputed_pads\"\n)" }, { "summary": "averagepool_2d_precomputed_pads_count_include_pad", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 5.5, 7], [11.5, 13, 14.5], [19, 20.5, 22]]]]).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_same_upper\",\n)" }, { "summary": "averagepool_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 6], [14, 16]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_strides\",\n)" }, { "summary": "averagepool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_lower\")" }, { "summary": "averagepool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_upper\")" }, { "summary": "averagepool_2d_strides", "code": "\"\"\"input_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, pads = get_output_shape_explicit_padding(\n None, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = x\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=None,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_strides\")" }, { "summary": "averagepool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_default\")" }, { "summary": "averagepool_3d_dilations", "code": "\"\"\"input_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=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4, 4]\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[[6, 7], [10, 11]], [[6, 7], [10, 11]]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_dilations_small\"\n)" }, { "summary": "averagepool_3d_dilations_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\ncount_include_pad = 0\n\nfor count_include_pad in (0, 1):\n for ceil_mode in (True, False):\n node = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n count_include_pad=count_include_pad,\n ceil_mode=ceil_mode,\n )\n\n x = np.random.randn(1, 1, *x_shape).astype(np.float32)\n out_shape, extra_pads = get_output_shape_explicit_padding(\n None,\n x_shape,\n kernel_shape,\n strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n )\n padded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[3]),\n (extra_pads[1], extra_pads[4]),\n (extra_pads[2], extra_pads[5]),\n ),\n mode=\"constant\",\n constant_values=0 if count_include_pad == 1 else np.nan,\n )\n y = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=None,\n dilations=dilations,\n count_include_pad=count_include_pad,\n )\n\n test_name = f\"test_averagepool_3d_dilations_large_count_include_pad_is_{count_include_pad}_ceil_mode_is_{ceil_mode}\"\n expect(node, inputs=[x], outputs=[y], name=test_name)" } ], "category": "Pool" }, { "name": "AveragePool", "module": "ai.onnx", "version": 22, "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 is calculated differently\n depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized.\n With explicit padding (https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html?highlight=maxpool#torch.nn.MaxPool2d):\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled. `pad_shape[i]` is the sum of pads along axis `i`. Sliding windows that would start in the right padded region are ignored.\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following when ceil_mode is enabled:\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 or when ceil_mode is disabled (https://www.tensorflow.org/api_docs/python/tf/keras/layers/AveragePooling2D):\n ```\n VALID: output_spatial_shape[i] = floor((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i]) + 1\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = floor((input_spatial_shape[i] - 1) / strides_spatial_shape[i]) + 1\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 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": "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": "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "averagepool_1d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_1d_default\")" }, { "summary": "averagepool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[6, 7.5], [12, 13.5]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_ceil\")" }, { "summary": "averagepool_2d_ceil_last_window_starts_on_pad", "code": "\"\"\"input_shape: [1, 3, 2, 2]\noutput_shape: [1, 3, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n strides=[3, 3],\n pads=[1, 1, 1, 1],\n ceil_mode=True,\n count_include_pad=1,\n)\nx = np.array(\n [\n [\n [[0.8580, 0.0786], [0.2692, 0.1537]],\n [[0.8816, 0.4353], [0.5772, 0.6623]],\n [[0.9067, 0.9483], [0.5970, 0.7630]],\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[0.1511]], [[0.2841]], [[0.3572]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_ceil_last_window_starts_on_pad\",\n)" }, { "summary": "averagepool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_default\")" }, { "summary": "averagepool_2d_dilations", "code": "\"\"\"input_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=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4]\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[6, 7], [10, 11]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_dilations\")" }, { "summary": "averagepool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=np.nan,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_pads\")" }, { "summary": "averagepool_2d_pads_count_include_pad", "code": "\"\"\"input_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)\ndilations = (1, 1)\nkernel_shape = (3, 3)\nstrides = (1, 1)\npad_bottom = 2\npad_top = 2\npad_right = 2\npad_left = 2\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides, dilations, ceil_mode=False\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=pads,\n count_include_pad=1,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_pads", "code": "\"\"\"input_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)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_precomputed_pads\"\n)" }, { "summary": "averagepool_2d_precomputed_pads_count_include_pad", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_pads_count_include_pad\",\n)" }, { "summary": "averagepool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 5.5, 7], [11.5, 13, 14.5], [19, 20.5, 22]]]]).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_same_upper\",\n)" }, { "summary": "averagepool_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[4, 6], [14, 16]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_averagepool_2d_precomputed_strides\",\n)" }, { "summary": "averagepool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_lower\")" }, { "summary": "averagepool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = (pad_top, pad_left, pad_bottom, pad_right)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_same_upper\")" }, { "summary": "averagepool_2d_strides", "code": "\"\"\"input_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, pads = get_output_shape_explicit_padding(\n None, x_shape[2:], kernel_shape, strides, ceil_mode=False\n)\npadded = x\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=pads,\n pads=None,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_2d_strides\")" }, { "summary": "averagepool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"AVG\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_default\")" }, { "summary": "averagepool_3d_dilations", "code": "\"\"\"input_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=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n ceil_mode=True,\n)\n\n# input shape: [1, 1, 4, 4, 4]\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array([[[[[6, 7], [10, 11]], [[6, 7], [10, 11]]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_averagepool_3d_dilations_small\"\n)" }, { "summary": "averagepool_3d_dilations_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\ncount_include_pad = 0\n\nfor count_include_pad in (0, 1):\n for ceil_mode in (True, False):\n node = onnx.helper.make_node(\n \"AveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n count_include_pad=count_include_pad,\n ceil_mode=ceil_mode,\n )\n\n x = np.random.randn(1, 1, *x_shape).astype(np.float32)\n out_shape, extra_pads = get_output_shape_explicit_padding(\n None,\n x_shape,\n kernel_shape,\n strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n )\n padded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[3]),\n (extra_pads[1], extra_pads[4]),\n (extra_pads[2], extra_pads[5]),\n ),\n mode=\"constant\",\n constant_values=0 if count_include_pad == 1 else np.nan,\n )\n y = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"AVG\",\n pads_required=extra_pads,\n pads=None,\n dilations=dilations,\n count_include_pad=count_include_pad,\n )\n\n test_name = f\"test_averagepool_3d_dilations_large_count_include_pad_is_{count_include_pad}_ceil_mode_is_{ceil_mode}\"\n expect(node, inputs=[x], outputs=[y], name=test_name)" } ], "category": "Pool" }, { "name": "AveragePool", "module": "com.microsoft.nchwc", "version": 1, "attributes": [ { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "name": "ceil_mode", "type": "int64", "required": false }, { "name": "count_include_pad", "type": "int64", "required": false }, { "name": "dilations", "type": "int64[]", "required": false }, { "name": "kernel_shape", "type": "int64[]", "required": true }, { "name": "pads", "type": "int64[]", "required": false }, { "name": "strides", "type": "int64[]", "required": false } ], "inputs": [ { "name": "X", "type": "T" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T" } ], "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(float)" ] } ], "category": "Pool" }, { "name": "AveragePool", "module": "com.ms.internal.nhwc", "version": 7, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Pool" }, { "name": "AveragePool", "module": "com.ms.internal.nhwc", "version": 10, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Pool" }, { "name": "AveragePool", "module": "com.ms.internal.nhwc", "version": 11, "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] - 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 when ceil_mode is enabled:\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 ```\nor when ceil_mode is disabled:\n ```\n VALID: output_spatial_shape[i] = floor((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] = floor(input_spatial_shape[i] / strides_spatial_shape[i])\n ```\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 divided by the number of elements (exclude pad when attribute count_include_pad is zero).\n ", "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Pool" }, { "name": "AveragePool", "module": "com.ms.internal.nhwc", "version": 19, "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 is calculated differently\n depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized.\n With explicit padding (https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html?highlight=maxpool#torch.nn.MaxPool2d):\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled. `pad_shape[i]` is the sum of pads along axis `i`.\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following when ceil_mode is enabled:\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 or when ceil_mode is disabled (https://www.tensorflow.org/api_docs/python/tf/keras/layers/AveragePooling2D):\n ```\n VALID: output_spatial_shape[i] = floor((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i]) + 1\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = floor((input_spatial_shape[i] - 1) / strides_spatial_shape[i]) + 1\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 divided by the number of elements (exclude pad when attribute count_include_pad is zero).\n ", "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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": "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": "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)" ] } ], "category": "Pool" }, { "name": "BatchNormalization", "module": "ai.onnx", "version": 1, "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_example\",\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_epsilon\",\n)" }, { "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_example_training_mode\",\n)\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(\n x, s, bias, mean, var, momentum, epsilon\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_epsilon_training_mode\",\n)" } ], "category": "Normalization" }, { "name": "BatchNormalization", "module": "ai.onnx", "version": 6, "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_example\",\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_epsilon\",\n)" }, { "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_example_training_mode\",\n)\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(\n x, s, bias, mean, var, momentum, epsilon\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_epsilon_training_mode\",\n)" } ], "category": "Normalization" }, { "name": "BatchNormalization", "module": "ai.onnx", "version": 7, "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_example\",\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_epsilon\",\n)" }, { "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_example_training_mode\",\n)\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(\n x, s, bias, mean, var, momentum, epsilon\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_epsilon_training_mode\",\n)" } ], "category": "Normalization" }, { "name": "BatchNormalization", "module": "ai.onnx", "version": 9, "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_example\",\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_epsilon\",\n)" }, { "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_example_training_mode\",\n)\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(\n x, s, bias, mean, var, momentum, epsilon\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_epsilon_training_mode\",\n)" } ], "category": "Normalization" }, { "name": "BatchNormalization", "module": "ai.onnx", "version": 14, "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_example\",\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_epsilon\",\n)" }, { "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_example_training_mode\",\n)\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(\n x, s, bias, mean, var, momentum, epsilon\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_epsilon_training_mode\",\n)" } ], "category": "Normalization" }, { "name": "BatchNormalization", "module": "ai.onnx", "version": 15, "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\n* Output case #1: Y, running_mean, running_var (training_mode=True)\n* Output 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\n`sum(sqrd(x_i - x_avg)) / N`\nwhere `N` is the population size (this formula does not use sample size `N - 1`).\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 and 2 are to be computed." } ], "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_example\",\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y],\n name=\"test_batchnorm_epsilon\",\n)" }, { "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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_example_training_mode\",\n)\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(\n x, s, bias, mean, var, momentum, epsilon\n)\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(\n node,\n inputs=[x, s, bias, mean, var],\n outputs=[y, output_mean, output_var],\n name=\"test_batchnorm_epsilon_training_mode\",\n)" } ], "category": "Normalization" }, { "name": "BatchNormalization", "module": "com.ms.internal.nhwc", "version": 7, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Normalization" }, { "name": "BatchNormalization", "module": "com.ms.internal.nhwc", "version": 9, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Normalization" }, { "name": "BatchNormalization", "module": "com.ms.internal.nhwc", "version": 14, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Normalization" }, { "name": "BatchNormalization", "module": "com.ms.internal.nhwc", "version": 15, "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\n* Output case #1: Y, running_mean, running_var (training_mode=True)\n* Output 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\n`sum(sqrd(x_i - x_avg)) / N`\nwhere `N` is the population size (this formula does not use sample size `N - 1`).\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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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 and 2 are to be computed." } ], "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)" ] } ], "category": "Normalization" }, { "name": "BeamSearch", "module": "com.microsoft", "version": 1, "description": "Beam Search for text generation. Supports GPT-2 decoder.", "attributes": [ { "name": "decoder", "type": "graph", "required": true, "description": "Decoder subgraph to execute in a loop." }, { "name": "decoder_start_token_id", "type": "int64", "required": false, "default": -1, "description": "The id of the token that indicates decoding starts." }, { "name": "early_stopping", "type": "int64", "required": false, "description": "early stop or not" }, { "name": "encoder", "type": "graph", "required": false, "description": "The subgraph for initialization of encoder and decoder. It will be called once before decoder subgraph." }, { "name": "eos_token_id", "type": "int64", "required": true, "description": "The id of the end-of-sequence token" }, { "name": "init_decoder", "type": "graph", "required": false, "description": "The subgraph for the first decoding run. It will be called once before `decoder` subgraph. This is relevant only for the GPT2 model. If this attribute is missing, the `decoder` subgraph will be used for all decoding runs" }, { "name": "model_type", "type": "int64", "required": false, "description": "model type: 0 for GPT-2; 1 for encoder decoder like T5" }, { "name": "no_repeat_ngram_size", "type": "int64", "required": false, "description": "no repeat ngrams size" }, { "name": "pad_token_id", "type": "int64", "required": true, "description": "The id of the padding token" }, { "name": "vocab_size", "type": "int64", "required": false, "default": -1, "description": "Size of the vocabulary. If not provided, it will be inferred from the decoder subgraph's output shape" } ], "inputs": [ { "name": "input_ids", "type": "F", "description": "The sequence used as a prompt for the generation in the encoder subgraph. Shape is (batch_size, sequence_length)" }, { "name": "max_length", "type": "I", "description": "The maximum length of the sequence to be generated. Shape is (1)" }, { "name": "min_length", "type": "I", "option": "optional", "description": "The minimum length below which the score of eos_token_id is set to -Inf. Shape is (1)" }, { "name": "num_beams", "type": "I", "description": "Number of beams for beam search. 1 means no beam search. Shape is (1)" }, { "name": "num_return_sequences", "type": "I", "description": "The number of returned sequences in the batch. Shape is (1)" }, { "name": "length_penalty", "type": "T", "option": "optional", "description": "Exponential penalty to the length. Default value 1.0 means no penalty.Value > 1.0 encourages longer sequences, while values < 1.0 produces shorter sequences.Shape is (1,)" }, { "name": "repetition_penalty", "type": "T", "option": "optional", "description": "The parameter for repetition penalty. Default value 1.0 means no penalty. Accepts value > 0.0. Shape is (1)" }, { "name": "vocab_mask", "type": "M", "option": "optional", "description": "Mask of vocabulary. Words that masked with 0 are not allowed to be generated, and 1 is allowed. Shape is (vocab_size)" }, { "name": "prefix_vocab_mask", "type": "M", "option": "optional", "description": "Mask of vocabulary for first step. Words that masked with 0 are not allowed to be generated, and 1 is allowed. Shape is (batch_size, vocab_size)" }, { "name": "attention_mask", "type": "I", "option": "optional", "description": "Custom attention mask. Shape is (batch_size, sequence_length)" }, { "name": "decoder_input_ids", "type": "I", "option": "optional", "description": "The forced input id sequence for the decoder subgraph. Shape is (batch_size, initial_sequence_length)" }, { "name": "logits_processor", "type": "I", "option": "optional", "description": "Specific logits processor for different types of beamsearch models. Default value 0 means no specific logit processor. Accepts value >= 0. Shape is (1)" } ], "min_input": 5, "max_input": 12, "outputs": [ { "name": "sequences", "type": "I", "description": "Word IDs of generated sequences. Shape is (batch_size, num_return_sequences, max_sequence_length)" }, { "name": "sequences_scores", "type": "T", "option": "optional", "description": "Final beam score of the generated sequences. Shape is (batch_size, num_return_sequences)" }, { "name": "scores", "type": "T", "option": "optional", "description": "Processed beam scores for each vocabulary token at each generation step.Beam scores consisting of log softmax scores for each vocabulary token and sum of log softmax of previously generated tokens in this beam.Shape is (max_length - sequence_length, batch_size, num_beams, vocab_size)" } ], "min_output": 1, "max_output": 3, "inputs_range": "5 - 12", "outputs_range": "1 - 3", "type_constraints": [ { "description": "Constrain to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain input type to float or int tensors.", "type_param_str": "F", "allowed_type_strs": [ "tensor(float)", "tensor(int32)", "tensor(float16)" ] }, { "description": "Constrain to integer types", "type_param_str": "I", "allowed_type_strs": [ "tensor(int32)" ] }, { "description": "Constrain mask to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "Bernoulli", "module": "ai.onnx", "version": 15, "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, float)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bernoulli_double\")" }, { "summary": "bernoulli_with_seed", "code": "seed = 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(float)\ny = bernoulli_reference_implementation(x, float)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bernoulli\")" } ] }, { "name": "Bernoulli", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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(uint8)", "tensor(uint16)", "tensor(uint32)", "tensor(uint64)", "tensor(int8)", "tensor(int16)", "tensor(int32)", "tensor(int64)", "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)", "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, float)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bernoulli_double\")" }, { "summary": "bernoulli_with_seed", "code": "seed = 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(float)\ny = bernoulli_reference_implementation(x, float)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bernoulli\")" } ] }, { "name": "BiasAdd", "module": "com.microsoft", "version": 1, "description": "Add input with bias, then add residual inputs.\n", "inputs": [ { "name": "X", "type": "T", "description": "Input tensor. Dimensions are (N, S, C), where N is the batch size, S is image size H*W, and C is number of channels" }, { "name": "bias", "type": "T", "description": "Bias tensor. Dimensions are (C)" }, { "name": "skip", "type": "T", "description": "Residual tensor. Dimensions are (N, S, C)" } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "Y", "type": "T", "description": "The output tensor with dimensions (N, S, C)" } ], "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)" ] } ] }, { "name": "BiasDropout", "module": "com.microsoft", "version": 1, "description": "output, dropout_mask = Dropout(data + bias, ratio) + residual, Intended to specialize the dropout pattern commonly found in transformer models.", "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": "bias", "type": "T", "description": "The bias input, a vector with the same shape as last dim of data OR same shape with data" }, { "name": "residual", "type": "T", "option": "optional", "description": "The residual input, must have the same shape as data" }, { "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": 2, "max_input": 5, "outputs": [ { "name": "output", "type": "T", "description": "The output." }, { "name": "mask", "type": "T2", "option": "optional", "description": "The output mask of dropout." } ], "min_output": 1, "max_output": 2, "inputs_range": "2 - 5", "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)", "tensor(bfloat16)" ] }, { "description": "Constrain output 'mask' types to boolean tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(bool)" ] } ] }, { "name": "BiasGelu", "module": "com.microsoft", "version": 1, "description": "Bias Gelu.\nIt's an extension of Gelu. It takes the sum of input A and bias input B as the input of Gelu activation. ", "inputs": [ { "name": "A", "type": "T", "description": "The normal input data." }, { "name": "B", "type": "T", "description": "The bias input data that is a 1D tensor." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "C", "type": "T", "description": "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)", "tensor(bfloat16)" ] } ] }, { "name": "BiasSoftmax", "module": "com.microsoft", "version": 1, "description": "Y = softmax(scores + bias)) with simple broadcast on bias. Intended to specialize softmax(scores + additive_mask) commonly found in transformer models.", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "apply softmax to elements for dimensions axis or higher" }, { "name": "is_inner_broadcast", "type": "int64", "required": true, "description": "true if broadcast bias across input for dimensions broadcast_axis to axis-1, otherwise broadcast bias across input for dimensions 0 to broadcast_axis - 1" } ], "inputs": [ { "name": "data", "type": "T", "description": "The input data as Tensor." }, { "name": "bias", "type": "T", "description": "The bias (or mask) as Tensor." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "output", "type": "T", "description": "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)" ] } ] }, { "name": "BiasSplitGelu", "module": "com.microsoft", "version": 1, "description": "A fusion used in diffusion model that after adding bias, hidden state is sliced into two tensors of same size, then left\ntensor multiplies the Gelu activation result of right tensor.\n", "inputs": [ { "name": "X", "type": "T", "description": "Input tensor. Dimensions are (N, S, D), where N is the batch size, S are image size, and D is hidden dimension" }, { "name": "bias", "type": "T", "description": "Bias tensor. Dimensions are (D), where D is the same hidden dimension as input tensor" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "T", "description": "The output tensor with dimensions (N, S, D/2)" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input X and output Y types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] } ] }, { "name": "BifurcationDetector", "module": "com.microsoft", "version": 1, "description": "Component for aggressive decoding. Find the bifurcation index of predicted tokens, between source tokens,\nstarting from previous suffix match index, and predicted tokens.\nConcat predicted tokens, starting from bifurcation index, to the back\nof current tokens. This forms the output tokens.\nDetect suffix match index in source tokens, between source tokens and output tokens.\nDetection is based on finding the appearances of last n-gram in output tokens\nin source tokens.\nA match is considered found if source tokens contain a single matching n-gram.\nReturn the index of the start of the n-gram in source tokens.\nNo matching if found if src tokens contain multiple or zero matching n-grams. Return -1.\n", "attributes": [ { "name": "max_ngram_size", "type": "int64", "required": false, "default": 3, "description": "The maximum NGram size for suffix matching." }, { "name": "min_ngram_size", "type": "int64", "required": false, "default": 1, "description": "The minimum NGram size for suffix matching." } ], "inputs": [ { "name": "src_tokens", "type": "T", "description": "Encoder input ids." }, { "name": "cur_tokens", "type": "T", "description": "Decoder input ids." }, { "name": "prev_suffix_match_idx", "type": "T", "description": "Previous suffix match index" }, { "name": "pred_tokens", "type": "T", "option": "optional", "description": "Predicted token ids from aggressive decoding" } ], "min_input": 3, "max_input": 4, "outputs": [ { "name": "tokens", "type": "T", "description": "Decoder input ids after merging predicted tokens" }, { "name": "suffix_match_idx", "type": "T", "description": "new suffix match index" } ], "min_output": 2, "max_output": 2, "inputs_range": "3 - 4", "type_constraints": [ { "description": "Constrain to integer types.", "type_param_str": "T", "allowed_type_strs": [ "tensor(int64)" ] } ] }, { "name": "Binarizer", "module": "ai.onnx.ml", "version": 1, "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)" ] } ], "examples": [ { "summary": "binarizer", "code": "threshold = 1.0\nnode = onnx.helper.make_node(\n \"Binarizer\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n threshold=threshold,\n domain=\"ai.onnx.ml\",\n)\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = compute_binarizer(x, threshold)[0]\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_ai_onnx_ml_binarizer\")" } ] }, { "name": "BitCast", "module": "ai.onnx", "version": 26, "description": "Reinterprets the binary representation of a tensor as a different data type,\nspecified by the 'to' attribute. Unlike Cast, BitCast preserves the exact bit\npattern without any value conversion.\n\nThe target data type must have the same bit-width as the input data type.\nThe output tensor has the same shape as the input tensor.\nAll types except string are supported. Implementations must treat the\nunderlying bytes as little endian.\n", "attributes": [ { "name": "to", "type": "int64", "required": true, "description": "The data type to which the input tensor is bitwise reinterpreted. Must be one of the non-string types from DataType enum in TensorProto. The target type must have the same bit-width as the input type." } ], "inputs": [ { "name": "input", "type": "T1", "description": "Input tensor to be bitcast." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T2", "description": "Output tensor with the same shape as the input." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input types. Bitcasting from string is not supported.", "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(bool)", "tensor(complex64)", "tensor(complex128)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] }, { "description": "Constrain output types. Bitcasting to string is not supported.", "type_param_str": "T2", "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(bool)", "tensor(complex64)", "tensor(complex128)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "examples": [ { "summary": "bitcast_2d_float32_to_int32", "code": "\"\"\"Test bitcasting 2D array from float32 to int32.\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.INT32,\n)\nx = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32)\ny = x.view(np.int32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitcast_2d_float32_to_int32\")" }, { "summary": "bitcast_bool_to_uint8", "code": "\"\"\"Test bitcasting from bool to uint8 (same size).\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.UINT8,\n)\nx = np.array([True, False, True, False], dtype=np.bool_)\ny = x.view(np.uint8)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitcast_bool_to_uint8\")" }, { "summary": "bitcast_float32_to_int32", "code": "\"\"\"Test bitcasting from float32 to int32 (same size).\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.INT32,\n)\nx = np.array([1.0, -2.5, 3.75], dtype=np.float32)\ny = x.view(np.int32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitcast_float32_to_int32\")" }, { "summary": "bitcast_float64_to_int64", "code": "\"\"\"Test bitcasting from float64 to int64 (same size).\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.INT64,\n)\nx = np.array([1.0, -2.5, 3.75], dtype=np.float64)\ny = x.view(np.int64)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitcast_float64_to_int64\")" }, { "summary": "bitcast_int32_to_float32", "code": "\"\"\"Test bitcasting from int32 to float32 (same size).\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.FLOAT,\n)\nx = np.array([1065353216, -1071644672, 1081081856], dtype=np.int32)\ny = x.view(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitcast_int32_to_float32\")" }, { "summary": "bitcast_int64_to_float64", "code": "\"\"\"Test bitcasting from int64 to float64 (same size).\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.DOUBLE,\n)\nx = np.array(\n [4607182418800017408, -4611686018427387904, 4614256656552045184],\n dtype=np.int64,\n)\ny = x.view(np.float64)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitcast_int64_to_float64\")" }, { "summary": "bitcast_int8_to_uint8", "code": "\"\"\"Test bitcasting from int8 to uint8 (same size, different signedness).\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.UINT8,\n)\nx = np.array([-1, -128, 127, 0], dtype=np.int8)\ny = x.view(np.uint8)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitcast_int8_to_uint8\")" }, { "summary": "bitcast_scalar_float32_to_int32", "code": "\"\"\"Test bitcasting scalar from float32 to int32.\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.INT32,\n)\nx = np.array(1.0, dtype=np.float32)\ny = x.view(np.int32)\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_bitcast_scalar_float32_to_int32\"\n)" }, { "summary": "bitcast_uint16_to_int16", "code": "\"\"\"Test bitcasting from uint16 to int16 (same size, different signedness).\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.INT16,\n)\nx = np.array([1, 32768, 65535], dtype=np.uint16)\ny = x.view(np.int16)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitcast_uint16_to_int16\")" }, { "summary": "bitcast_uint32_to_int32", "code": "\"\"\"Test bitcasting from uint32 to int32 (same size, different signedness).\"\"\"\nnode = onnx.helper.make_node(\n \"BitCast\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n to=onnx.TensorProto.INT32,\n)\nx = np.array([4294967295, 2147483648, 2147483647], dtype=np.uint32)\ny = x.view(np.int32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitcast_uint32_to_int32\")" } ] }, { "name": "BitShift", "module": "ai.onnx", "version": 11, "description": "Bitwise shift operator performs element-wise operation. For each input element, if the\nattribute \"direction\" is \"RIGHT\", this operator moves its binary representation toward\nthe right side so that the input value is effectively decreased. If the attribute \"direction\"\nis \"LEFT\", bits of binary representation moves toward the left side, which results the\nincrease of its actual value. The input X is the tensor to be shifted and another input\nY specifies the amounts of shifting. For example, if \"direction\" is \"Right\", X is [1, 4],\nand S is [1, 1], the corresponding output Z would be [0, 2]. If \"direction\" is \"LEFT\" with\nX=[1, 2] and S=[1, 2], the corresponding output Y would be [2, 8].\n\nBecause this operator supports Numpy-style broadcasting, X's and Y's shapes are\nnot 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\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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], name=\"test_bitshift_left_uint16\")" }, { "summary": "left_unit32", "code": "node = onnx.helper.make_node(\n \"BitShift\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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], name=\"test_bitshift_left_uint32\")" }, { "summary": "left_unit64", "code": "node = onnx.helper.make_node(\n \"BitShift\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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], name=\"test_bitshift_left_uint64\")" }, { "summary": "left_unit8", "code": "node = onnx.helper.make_node(\n \"BitShift\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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], name=\"test_bitshift_left_uint8\")" }, { "summary": "right_unit16", "code": "node = onnx.helper.make_node(\n \"BitShift\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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], name=\"test_bitshift_right_uint16\")" }, { "summary": "right_unit32", "code": "node = onnx.helper.make_node(\n \"BitShift\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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], name=\"test_bitshift_right_uint32\")" }, { "summary": "right_unit64", "code": "node = onnx.helper.make_node(\n \"BitShift\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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], name=\"test_bitshift_right_uint64\")" }, { "summary": "right_unit8", "code": "node = onnx.helper.make_node(\n \"BitShift\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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], name=\"test_bitshift_right_uint8\")" } ] }, { "name": "BitmaskBiasDropout", "module": "com.microsoft", "version": 1, "description": "output, dropout_bitmask = Dropout(data + bias, ratio) + residual, Intended to specialize the dropout pattern commonly found in transformer models.", "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": "bias", "type": "T", "description": "The bias input, a vector with the same shape as last dim of data OR same shape with data" }, { "name": "residual", "type": "T", "option": "optional", "description": "The residual input, must have the same shape as data" }, { "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": 2, "max_input": 5, "outputs": [ { "name": "output", "type": "T", "description": "The output." }, { "name": "mask", "type": "T3", "option": "optional", "description": "The output mask of dropout." } ], "min_output": 1, "max_output": 2, "inputs_range": "2 - 5", "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)", "tensor(bfloat16)" ] }, { "description": "Constrain input 'training_mode' types to boolean tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(bool)" ] }, { "description": "Constrain output 'mask' types to uint32 tensors.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(uint32)" ] } ] }, { "name": "BitmaskDropout", "module": "com.microsoft", "version": 1, "description": "BitmaskDropout takes an input floating-point tensor, an optional input ratio (floating-point scalar) and an optional input training_mode (boolean scalar).\nIt produces two tensor outputs: output (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, the 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```\n\nThis op functions in much the same was as Dropout-11 and Dropout-13 do, except that the mask is output as a bit-packed uint32 tensor, instead of a boolean tensor.\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": "T3", "option": "optional", "description": "The bit-packed 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)", "tensor(bfloat16)" ] }, { "description": "Constrain 'training_mode' to boolean tensor.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(bool)" ] }, { "description": "Constrain output 'mask' types to bit-packed uint32 tensor.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(uint32)" ] } ] }, { "name": "BitwiseAnd", "module": "ai.onnx", "version": 18, "description": "Returns the tensor resulting from performing the bitwise `and` 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 bitwise operator." }, { "name": "B", "type": "T", "description": "Second input operand for the bitwise operator." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "C", "type": "T", "description": "Result tensor." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input to integer tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(uint16)", "tensor(uint32)", "tensor(uint64)", "tensor(int8)", "tensor(int16)", "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "bitwiseand", "code": "node = onnx.helper.make_node(\n \"BitwiseAnd\",\n inputs=[\"x\", \"y\"],\n outputs=[\"bitwiseand\"],\n)\n\n# 2d\nx = create_random_int((3, 4), np.int32)\ny = create_random_int((3, 4), np.int32)\nz = np.bitwise_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_and_i32_2d\")\n\n# 3d\nx = create_random_int((3, 4, 5), np.int16)\ny = create_random_int((3, 4, 5), np.int16)\nz = np.bitwise_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_and_i16_3d\")" }, { "summary": "bitwiseand_broadcast", "code": "node = onnx.helper.make_node(\n \"BitwiseAnd\",\n inputs=[\"x\", \"y\"],\n outputs=[\"bitwiseand\"],\n)\n\n# 3d vs 1d\nx = create_random_int((3, 4, 5), np.uint64)\ny = create_random_int((5,), np.uint64)\nz = np.bitwise_and(x, y)\nexpect(\n node, inputs=[x, y], outputs=[z], name=\"test_bitwise_and_ui64_bcast_3v1d\"\n)\n\n# 4d vs 3d\nx = create_random_int((3, 4, 5, 6), np.uint8)\ny = create_random_int((4, 5, 6), np.uint8)\nz = np.bitwise_and(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_and_ui8_bcast_4v3d\")" } ] }, { "name": "BitwiseNot", "module": "ai.onnx", "version": 18, "description": "Returns the bitwise not 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 integer tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(uint16)", "tensor(uint32)", "tensor(uint64)", "tensor(int8)", "tensor(int16)", "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "bitwisenot", "code": "node = onnx.helper.make_node(\n \"BitwiseNot\",\n inputs=[\"x\"],\n outputs=[\"bitwise_not\"],\n)\n\n# 2d\nx = create_random_int((3, 4), np.int32)\ny = np.bitwise_not(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitwise_not_2d\")\n\n# 3d\nx = create_random_int((3, 4, 5), np.uint16)\ny = np.bitwise_not(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitwise_not_3d\")\n\n# 4d\nx = create_random_int((3, 4, 5, 6), np.uint8)\ny = np.bitwise_not(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_bitwise_not_4d\")" } ] }, { "name": "BitwiseOr", "module": "ai.onnx", "version": 18, "description": "Returns the tensor resulting from performing the bitwise `or` 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 bitwise operator." }, { "name": "B", "type": "T", "description": "Second input operand for the bitwise operator." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "C", "type": "T", "description": "Result tensor." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input to integer tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(uint16)", "tensor(uint32)", "tensor(uint64)", "tensor(int8)", "tensor(int16)", "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "bitwiseor", "code": "node = onnx.helper.make_node(\n \"BitwiseOr\",\n inputs=[\"x\", \"y\"],\n outputs=[\"bitwiseor\"],\n)\n# 2d\nx = create_random_int((3, 4), np.int32)\ny = create_random_int((3, 4), np.int32)\nz = np.bitwise_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_or_i32_2d\")\n\n# 4d\nx = create_random_int((3, 4, 5, 6), np.int8)\ny = create_random_int((3, 4, 5, 6), np.int8)\nz = np.bitwise_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_or_i16_4d\")" }, { "summary": "bitwiseor_broadcast", "code": "node = onnx.helper.make_node(\n \"BitwiseOr\",\n inputs=[\"x\", \"y\"],\n outputs=[\"bitwiseor\"],\n)\n\n# 3d vs 1d\nx = create_random_int((3, 4, 5), np.uint64)\ny = create_random_int((5,), np.uint64)\nz = np.bitwise_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_or_ui64_bcast_3v1d\")\n\n# 4d vs 3d\nx = create_random_int((3, 4, 5, 6), np.uint8)\ny = create_random_int((4, 5, 6), np.uint8)\nz = np.bitwise_or(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_or_ui8_bcast_4v3d\")" } ] }, { "name": "BitwiseXor", "module": "ai.onnx", "version": 18, "description": "Returns the tensor resulting from performing the bitwise `xor` 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 bitwise operator." }, { "name": "B", "type": "T", "description": "Second input operand for the bitwise operator." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "C", "type": "T", "description": "Result tensor." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input to integer tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(uint16)", "tensor(uint32)", "tensor(uint64)", "tensor(int8)", "tensor(int16)", "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "bitwiseor_broadcast", "code": "node = onnx.helper.make_node(\n \"BitwiseXor\",\n inputs=[\"x\", \"y\"],\n outputs=[\"bitwisexor\"],\n)\n\n# 3d vs 1d\nx = create_random_int((3, 4, 5), np.uint64)\ny = create_random_int((5,), np.uint64)\nz = np.bitwise_xor(x, y)\nexpect(\n node, inputs=[x, y], outputs=[z], name=\"test_bitwise_xor_ui64_bcast_3v1d\"\n)\n\n# 4d vs 3d\nx = create_random_int((3, 4, 5, 6), np.uint8)\ny = create_random_int((4, 5, 6), np.uint8)\nz = np.bitwise_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_xor_ui8_bcast_4v3d\")" }, { "summary": "bitwisexor", "code": "node = onnx.helper.make_node(\n \"BitwiseXor\",\n inputs=[\"x\", \"y\"],\n outputs=[\"bitwisexor\"],\n)\n\n# 2d\nx = create_random_int((3, 4), np.int32)\ny = create_random_int((3, 4), np.int32)\nz = np.bitwise_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_xor_i32_2d\")\n\n# 3d\nx = create_random_int((3, 4, 5), np.int16)\ny = create_random_int((3, 4, 5), np.int16)\nz = np.bitwise_xor(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_bitwise_xor_i16_3d\")" } ] }, { "name": "BlackmanWindow", "module": "ai.onnx", "version": 17, "description": "Generates a Blackman window as described in the paper https://ieeexplore.ieee.org/document/1455106.\n", "attributes": [ { "name": "output_datatype", "type": "int64", "required": false, "default": 1, "description": "The data type of the output tensor. Strictly must be one of the values from DataType enum in TensorProto whose values correspond to T2. The default value is 1 = FLOAT. " }, { "name": "periodic", "type": "int64", "required": false, "default": 1, "description": "If 1, returns a window to be used as periodic function. If 0, return a symmetric window. When 'periodic' is specified, hann computes a window of length size + 1 and returns the first size points. The default value is 1. " } ], "inputs": [ { "name": "size", "type": "T1", "description": "A scalar value indicating the length of the window." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T2", "description": "A Blackman window with length: size. The output has the shape: [size]." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain the input size to int32_t or int64_t.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] }, { "description": "Constrain output types to numeric tensors.", "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)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "blackmanwindow", "code": "# Test periodic window\nnode = onnx.helper.make_node(\n \"BlackmanWindow\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\nsize = np.int32(10)\na0 = 0.42\na1 = -0.5\na2 = 0.08\ny = a0\ny += a1 * np.cos(2 * np.pi * np.arange(0, size, 1, dtype=np.float32) / size)\ny += a2 * np.cos(4 * np.pi * np.arange(0, size, 1, dtype=np.float32) / size)\nexpect(\n node,\n inputs=[size],\n outputs=[y.astype(np.float32)],\n name=\"test_blackmanwindow\",\n)\n\n# Test symmetric window\nnode = onnx.helper.make_node(\n \"BlackmanWindow\", inputs=[\"x\"], outputs=[\"y\"], periodic=0\n)\nsize = np.int32(10)\na0 = 0.42\na1 = -0.5\na2 = 0.08\ny = a0\ny += a1 * np.cos(\n 2 * np.pi * np.arange(0, size, 1, dtype=np.float32) / (size - 1)\n)\ny += a2 * np.cos(\n 4 * np.pi * np.arange(0, size, 1, dtype=np.float32) / (size - 1)\n)\nexpect(\n node,\n inputs=[size],\n outputs=[y.astype(np.float32)],\n name=\"test_blackmanwindow_symmetric\",\n)" } ] }, { "name": "CDist", "module": "com.microsoft", "version": 1, "attributes": [ { "name": "metric", "type": "string", "required": false, "default": "sqeuclidean", "description": "The distance metric to use. If a string, the distance function can be \"braycurtis\", \"canberra\", \"chebyshev\", \"cityblock\", \"correlation\", \"cosine\", \"dice\", \"euclidean\", \"hamming\", \"jaccard\", \"jensenshannon\", \"kulsinski\", \"mahalanobis\", \"matching\", \"minkowski\", \"rogerstanimoto\", \"russellrao\", \"seuclidean\", \"sokalmichener\", \"sokalsneath\", \"sqeuclidean\", \"wminkowski\", \"yule\"." } ], "inputs": [ { "name": "A", "type": "T", "description": "2D matrix with shape (M,N)" }, { "name": "B", "type": "T", "description": "2D matrix with shape (K,N)" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "C", "type": "T", "description": "A 2D Matrix that represents the distance between each pair of the two collections of inputs." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrains input to only numeric types.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(double)" ] } ] }, { "name": "Cast", "module": "ai.onnx", "version": 1, "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": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in F8_TYPES or to_type in F8_TYPES:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "e8m0", "code": "np_fp32 = np.array(\n [\n \"0.0\",\n \"0.124\",\n \"0.25\",\n \"0.5\",\n \"1.1\",\n \"2.0\",\n \"4.0\",\n \"8.0\",\n ],\n dtype=np.float32,\n)\ntest_cases = [\n (\"FLOAT\", \"FLOAT8E8M0\"),\n (\"FLOAT16\", \"FLOAT8E8M0\"),\n (\"FLOAT8E8M0\", \"FLOAT\"),\n (\"FLOAT8E8M0\", \"FLOAT16\"),\n]\nfor from_type, to_type in test_cases:\n if from_type == \"FLOAT\":\n input_np = np_fp32\n output_np = to_float8e8m0(np_fp32)\n elif from_type == \"FLOAT16\":\n input_np = np_fp32.astype(np.float16)\n output_np = to_float8e8m0(input_np)\n elif from_type == \"FLOAT8E8M0\":\n input_np = to_float8e8m0(np_fp32)\n if to_type == \"FLOAT\":\n output_np = input_np.astype(np.float32)\n elif to_type == \"FLOAT16\":\n output_np = input_np.astype(np.float16)\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n input = make_tensor(\n \"input\",\n getattr(TensorProto, from_type),\n [2, 4],\n input_np,\n raw=True,\n )\n output = make_tensor(\n \"output\",\n getattr(TensorProto, to_type),\n [2, 4],\n output_np,\n raw=True,\n )\n if to_type == \"FLOAT8E8M0\":\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n saturate=1,\n round_mode=\"up\",\n )\n else:\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n )\n\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_e8m0_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n saturate=0,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "Cast", "module": "ai.onnx", "version": 6, "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": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in F8_TYPES or to_type in F8_TYPES:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "e8m0", "code": "np_fp32 = np.array(\n [\n \"0.0\",\n \"0.124\",\n \"0.25\",\n \"0.5\",\n \"1.1\",\n \"2.0\",\n \"4.0\",\n \"8.0\",\n ],\n dtype=np.float32,\n)\ntest_cases = [\n (\"FLOAT\", \"FLOAT8E8M0\"),\n (\"FLOAT16\", \"FLOAT8E8M0\"),\n (\"FLOAT8E8M0\", \"FLOAT\"),\n (\"FLOAT8E8M0\", \"FLOAT16\"),\n]\nfor from_type, to_type in test_cases:\n if from_type == \"FLOAT\":\n input_np = np_fp32\n output_np = to_float8e8m0(np_fp32)\n elif from_type == \"FLOAT16\":\n input_np = np_fp32.astype(np.float16)\n output_np = to_float8e8m0(input_np)\n elif from_type == \"FLOAT8E8M0\":\n input_np = to_float8e8m0(np_fp32)\n if to_type == \"FLOAT\":\n output_np = input_np.astype(np.float32)\n elif to_type == \"FLOAT16\":\n output_np = input_np.astype(np.float16)\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n input = make_tensor(\n \"input\",\n getattr(TensorProto, from_type),\n [2, 4],\n input_np,\n raw=True,\n )\n output = make_tensor(\n \"output\",\n getattr(TensorProto, to_type),\n [2, 4],\n output_np,\n raw=True,\n )\n if to_type == \"FLOAT8E8M0\":\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n saturate=1,\n round_mode=\"up\",\n )\n else:\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n )\n\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_e8m0_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n saturate=0,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "Cast", "module": "ai.onnx", "version": 9, "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\nyield result 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": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in F8_TYPES or to_type in F8_TYPES:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "e8m0", "code": "np_fp32 = np.array(\n [\n \"0.0\",\n \"0.124\",\n \"0.25\",\n \"0.5\",\n \"1.1\",\n \"2.0\",\n \"4.0\",\n \"8.0\",\n ],\n dtype=np.float32,\n)\ntest_cases = [\n (\"FLOAT\", \"FLOAT8E8M0\"),\n (\"FLOAT16\", \"FLOAT8E8M0\"),\n (\"FLOAT8E8M0\", \"FLOAT\"),\n (\"FLOAT8E8M0\", \"FLOAT16\"),\n]\nfor from_type, to_type in test_cases:\n if from_type == \"FLOAT\":\n input_np = np_fp32\n output_np = to_float8e8m0(np_fp32)\n elif from_type == \"FLOAT16\":\n input_np = np_fp32.astype(np.float16)\n output_np = to_float8e8m0(input_np)\n elif from_type == \"FLOAT8E8M0\":\n input_np = to_float8e8m0(np_fp32)\n if to_type == \"FLOAT\":\n output_np = input_np.astype(np.float32)\n elif to_type == \"FLOAT16\":\n output_np = input_np.astype(np.float16)\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n input = make_tensor(\n \"input\",\n getattr(TensorProto, from_type),\n [2, 4],\n input_np,\n raw=True,\n )\n output = make_tensor(\n \"output\",\n getattr(TensorProto, to_type),\n [2, 4],\n output_np,\n raw=True,\n )\n if to_type == \"FLOAT8E8M0\":\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n saturate=1,\n round_mode=\"up\",\n )\n else:\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n )\n\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_e8m0_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n saturate=0,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "Cast", "module": "ai.onnx", "version": 13, "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\nyield result 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\nIn more detail, the conversion among numerical types should follow these rules:\n\n* Casting from floating point to:\n * floating point: +/- infinity if OOR (out of range).\n * fixed point: undefined if OOR.\n * bool: +/- 0.0 to False; all else to True.\n* Casting from fixed point to:\n * floating point: +/- infinity if OOR. (+ infinity in the case of uint)\n * fixed point: when OOR, discard higher bits and reinterpret (with respect to two's complement representation for\n signed types). For example, 200 (int16) -> -56 (int8).\n * bool: zero to False; nonzero to True.\n* Casting from bool to:\n * floating point: `{1.0, 0.0}`.\n * fixed point: `{1, 0}`.\n * bool: no change.\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": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in F8_TYPES or to_type in F8_TYPES:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "e8m0", "code": "np_fp32 = np.array(\n [\n \"0.0\",\n \"0.124\",\n \"0.25\",\n \"0.5\",\n \"1.1\",\n \"2.0\",\n \"4.0\",\n \"8.0\",\n ],\n dtype=np.float32,\n)\ntest_cases = [\n (\"FLOAT\", \"FLOAT8E8M0\"),\n (\"FLOAT16\", \"FLOAT8E8M0\"),\n (\"FLOAT8E8M0\", \"FLOAT\"),\n (\"FLOAT8E8M0\", \"FLOAT16\"),\n]\nfor from_type, to_type in test_cases:\n if from_type == \"FLOAT\":\n input_np = np_fp32\n output_np = to_float8e8m0(np_fp32)\n elif from_type == \"FLOAT16\":\n input_np = np_fp32.astype(np.float16)\n output_np = to_float8e8m0(input_np)\n elif from_type == \"FLOAT8E8M0\":\n input_np = to_float8e8m0(np_fp32)\n if to_type == \"FLOAT\":\n output_np = input_np.astype(np.float32)\n elif to_type == \"FLOAT16\":\n output_np = input_np.astype(np.float16)\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n input = make_tensor(\n \"input\",\n getattr(TensorProto, from_type),\n [2, 4],\n input_np,\n raw=True,\n )\n output = make_tensor(\n \"output\",\n getattr(TensorProto, to_type),\n [2, 4],\n output_np,\n raw=True,\n )\n if to_type == \"FLOAT8E8M0\":\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n saturate=1,\n round_mode=\"up\",\n )\n else:\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n )\n\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_e8m0_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n saturate=0,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "Cast", "module": "ai.onnx", "version": 19, "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\nyield result 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\nIn more detail, the conversion among numerical types should follow these rules\nif the destination type is not a float 8 type.\n\n* Casting from floating point to:\n * floating point: +/- infinity if OOR (out of range).\n * fixed point: undefined if OOR.\n * bool: +/- 0.0 to False; all else to True.\n* Casting from fixed point to:\n * floating point: +/- infinity if OOR. (+ infinity in the case of uint)\n * fixed point: when OOR, discard higher bits and reinterpret (with respect to two's complement representation for\n signed types). For example, 200 (int16) -> -56 (int8).\n * bool: zero to False; nonzero to True.\n* Casting from bool to:\n * floating point: `{1.0, 0.0}`.\n * fixed point: `{1, 0}`.\n * bool: no change.\n\nFloat 8 type were introduced to speed up the training of\ndeep models. By default the conversion of a float *x* obeys\nto the following rules. `[x]` means the value rounded to\nthe target mantissa width.\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | -------- | -------- | -------- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| Inf | FLT_MAX | NaN | FLT_MAX | NaN |\n| -Inf | -FLT_MAX | NaN | -FLT_MAX | NaN |\n| \\[x\\] > FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX |\n| \\[x\\] \\< -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX |\n| else | RNE | RNE | RNE | RNE |\n\nThe behavior changes if the parameter 'saturate' is set to False.\nThe rules then become:\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | ------ | -------- | ---- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| -NaN | -NaN | NaN | -NaN | NaN |\n| Inf | NaN | NaN | Inf | NaN |\n| -Inf | -NaN | NaN | -Inf | NaN |\n| \\[x\\] > FLT_MAX | NaN | NaN | Inf | NaN |\n| \\[x\\] \\< -FLT_MAX | NaN | NaN | -Inf | NaN |\n| else | RNE | RNE | RNE | RNE |\n", "attributes": [ { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. All cases are fully described in two tables inserted in the operator description." }, { "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] } ], "examples": [ { "summary": "cast", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in F8_TYPES or to_type in F8_TYPES:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "e8m0", "code": "np_fp32 = np.array(\n [\n \"0.0\",\n \"0.124\",\n \"0.25\",\n \"0.5\",\n \"1.1\",\n \"2.0\",\n \"4.0\",\n \"8.0\",\n ],\n dtype=np.float32,\n)\ntest_cases = [\n (\"FLOAT\", \"FLOAT8E8M0\"),\n (\"FLOAT16\", \"FLOAT8E8M0\"),\n (\"FLOAT8E8M0\", \"FLOAT\"),\n (\"FLOAT8E8M0\", \"FLOAT16\"),\n]\nfor from_type, to_type in test_cases:\n if from_type == \"FLOAT\":\n input_np = np_fp32\n output_np = to_float8e8m0(np_fp32)\n elif from_type == \"FLOAT16\":\n input_np = np_fp32.astype(np.float16)\n output_np = to_float8e8m0(input_np)\n elif from_type == \"FLOAT8E8M0\":\n input_np = to_float8e8m0(np_fp32)\n if to_type == \"FLOAT\":\n output_np = input_np.astype(np.float32)\n elif to_type == \"FLOAT16\":\n output_np = input_np.astype(np.float16)\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n input = make_tensor(\n \"input\",\n getattr(TensorProto, from_type),\n [2, 4],\n input_np,\n raw=True,\n )\n output = make_tensor(\n \"output\",\n getattr(TensorProto, to_type),\n [2, 4],\n output_np,\n raw=True,\n )\n if to_type == \"FLOAT8E8M0\":\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n saturate=1,\n round_mode=\"up\",\n )\n else:\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n )\n\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_e8m0_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n saturate=0,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "Cast", "module": "ai.onnx", "version": 21, "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\nyield result 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\nIn more detail, the conversion among numerical types should follow these rules\nif the destination type is not a float 8 type.\n\n* Casting from floating point to:\n * floating point: +/- infinity if OOR (out of range).\n * fixed point: undefined if OOR.\n * bool: +/- 0.0 to False; all else to True.\n* Casting from fixed point to:\n * floating point: +/- infinity if OOR. (+ infinity in the case of uint)\n * fixed point: when OOR, discard higher bits and reinterpret (with respect to two's complement representation for\n signed types). For example, 200 (int16) -> -56 (int8).\n * bool: zero to False; nonzero to True.\n* Casting from bool to:\n * floating point: `{1.0, 0.0}`.\n * fixed point: `{1, 0}`.\n * bool: no change.\n\nFloat 8 type were introduced to speed up the training of\ndeep models. By default the conversion of a float *x* obeys\nto the following rules. `[x]` means the value rounded to\nthe target mantissa width.\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | -------- | -------- | -------- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| Inf | FLT_MAX | NaN | FLT_MAX | NaN |\n| -Inf | -FLT_MAX | NaN | -FLT_MAX | NaN |\n| \\[x\\] > FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX |\n| \\[x\\] \\< -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX |\n| else | RNE | RNE | RNE | RNE |\n\nThe behavior changes if the parameter 'saturate' is set to False.\nThe rules then become:\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | ------ | -------- | ---- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| -NaN | -NaN | NaN | -NaN | NaN |\n| Inf | NaN | NaN | Inf | NaN |\n| -Inf | -NaN | NaN | -Inf | NaN |\n| \\[x\\] > FLT_MAX | NaN | NaN | Inf | NaN |\n| \\[x\\] \\< -FLT_MAX | NaN | NaN | -Inf | NaN |\n| else | RNE | RNE | RNE | RNE |\n", "attributes": [ { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. All cases are fully described in two tables inserted in the operator description." }, { "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] }, { "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "examples": [ { "summary": "cast", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in F8_TYPES or to_type in F8_TYPES:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "e8m0", "code": "np_fp32 = np.array(\n [\n \"0.0\",\n \"0.124\",\n \"0.25\",\n \"0.5\",\n \"1.1\",\n \"2.0\",\n \"4.0\",\n \"8.0\",\n ],\n dtype=np.float32,\n)\ntest_cases = [\n (\"FLOAT\", \"FLOAT8E8M0\"),\n (\"FLOAT16\", \"FLOAT8E8M0\"),\n (\"FLOAT8E8M0\", \"FLOAT\"),\n (\"FLOAT8E8M0\", \"FLOAT16\"),\n]\nfor from_type, to_type in test_cases:\n if from_type == \"FLOAT\":\n input_np = np_fp32\n output_np = to_float8e8m0(np_fp32)\n elif from_type == \"FLOAT16\":\n input_np = np_fp32.astype(np.float16)\n output_np = to_float8e8m0(input_np)\n elif from_type == \"FLOAT8E8M0\":\n input_np = to_float8e8m0(np_fp32)\n if to_type == \"FLOAT\":\n output_np = input_np.astype(np.float32)\n elif to_type == \"FLOAT16\":\n output_np = input_np.astype(np.float16)\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n input = make_tensor(\n \"input\",\n getattr(TensorProto, from_type),\n [2, 4],\n input_np,\n raw=True,\n )\n output = make_tensor(\n \"output\",\n getattr(TensorProto, to_type),\n [2, 4],\n output_np,\n raw=True,\n )\n if to_type == \"FLOAT8E8M0\":\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n saturate=1,\n round_mode=\"up\",\n )\n else:\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n )\n\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_e8m0_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n saturate=0,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "Cast", "module": "ai.onnx", "version": 23, "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\nyield result 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\nIn more detail, the conversion among numerical types should follow these rules\nif the destination type is not a float 8 type.\n\n* Casting from floating point to:\n * floating point: +/- infinity if OOR (out of range).\n * fixed point: undefined if OOR.\n * bool: +/- 0.0 to False; all else to True.\n* Casting from fixed point to:\n * floating point: +/- infinity if OOR. (+ infinity in the case of uint)\n * fixed point: when OOR, discard higher bits and reinterpret (with respect to two's complement representation for\n signed types). For example, 200 (int16) -> -56 (int8).\n * bool: zero to False; nonzero to True.\n* Casting from bool to:\n * floating point: `{1.0, 0.0}`.\n * fixed point: `{1, 0}`.\n * bool: no change.\n\nFloat 8 type were introduced to speed up the training of\ndeep models. By default the conversion of a float *x* obeys\nto the following rules. `[x]` means the value rounded to\nthe target mantissa width.\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | -------- | -------- | -------- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| Inf | FLT_MAX | NaN | FLT_MAX | NaN |\n| -Inf | -FLT_MAX | NaN | -FLT_MAX | NaN |\n| \\[x\\] > FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX |\n| \\[x\\] \\< -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX |\n| else | RNE | RNE | RNE | RNE |\n\nThe behavior changes if the parameter 'saturate' is set to False.\nThe rules then become:\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | ------ | -------- | ---- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| -NaN | -NaN | NaN | -NaN | NaN |\n| Inf | NaN | NaN | Inf | NaN |\n| -Inf | -NaN | NaN | -Inf | NaN |\n| \\[x\\] > FLT_MAX | NaN | NaN | Inf | NaN |\n| \\[x\\] \\< -FLT_MAX | NaN | NaN | -Inf | NaN |\n| else | RNE | RNE | RNE | RNE |\n", "attributes": [ { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. All cases are fully described in two tables inserted in the operator description." }, { "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(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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] }, { "description": "Constrain output types. Casting to complex is not supported.", "type_param_str": "T2", "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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "examples": [ { "summary": "cast", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in F8_TYPES or to_type in F8_TYPES:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "e8m0", "code": "np_fp32 = np.array(\n [\n \"0.0\",\n \"0.124\",\n \"0.25\",\n \"0.5\",\n \"1.1\",\n \"2.0\",\n \"4.0\",\n \"8.0\",\n ],\n dtype=np.float32,\n)\ntest_cases = [\n (\"FLOAT\", \"FLOAT8E8M0\"),\n (\"FLOAT16\", \"FLOAT8E8M0\"),\n (\"FLOAT8E8M0\", \"FLOAT\"),\n (\"FLOAT8E8M0\", \"FLOAT16\"),\n]\nfor from_type, to_type in test_cases:\n if from_type == \"FLOAT\":\n input_np = np_fp32\n output_np = to_float8e8m0(np_fp32)\n elif from_type == \"FLOAT16\":\n input_np = np_fp32.astype(np.float16)\n output_np = to_float8e8m0(input_np)\n elif from_type == \"FLOAT8E8M0\":\n input_np = to_float8e8m0(np_fp32)\n if to_type == \"FLOAT\":\n output_np = input_np.astype(np.float32)\n elif to_type == \"FLOAT16\":\n output_np = input_np.astype(np.float16)\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n input = make_tensor(\n \"input\",\n getattr(TensorProto, from_type),\n [2, 4],\n input_np,\n raw=True,\n )\n output = make_tensor(\n \"output\",\n getattr(TensorProto, to_type),\n [2, 4],\n output_np,\n raw=True,\n )\n if to_type == \"FLOAT8E8M0\":\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n saturate=1,\n round_mode=\"up\",\n )\n else:\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n )\n\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_e8m0_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n saturate=0,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "Cast", "module": "ai.onnx", "version": 24, "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\nyield result 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\nIn more detail, the conversion among numerical types should follow these rules\nif the destination type is not a float 8 type.\n\n* Casting from floating point to:\n * floating point: +/- infinity if OOR (out of range).\n * fixed point: undefined if OOR.\n * bool: +/- 0.0 to False; all else to True.\n* Casting from fixed point to:\n * floating point: +/- infinity if OOR. (+ infinity in the case of uint)\n * fixed point: when OOR, discard higher bits and reinterpret (with respect to two's complement representation for\n signed types). For example, 200 (int16) -> -56 (int8).\n * bool: zero to False; nonzero to True.\n* Casting from bool to:\n * floating point: `{1.0, 0.0}`.\n * fixed point: `{1, 0}`.\n * bool: no change.\n\nFloat 8 types (E4M3FN, E4M3FNUZ, E5M2, E5M2FNUZ) were introduced to speed up the training of\ndeep models. By default the conversion of a float *x* obeys\nto the following rules. `[x]` means the value rounded to\nthe target mantissa width.\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | -------- | -------- | -------- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| Inf | FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX |\n| -Inf | -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX |\n| \\[x\\] > FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX |\n| \\[x\\] \\< -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX |\n| else | RNE | RNE | RNE | RNE |\n\nThe behavior changes if the parameter 'saturate' is set to False.\nThe rules then become:\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | ------ | -------- | ---- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| -NaN | -NaN | NaN | -NaN | NaN |\n| Inf | NaN | NaN | Inf | NaN |\n| -Inf | -NaN | NaN | -Inf | NaN |\n| \\[x\\] > FLT_MAX | NaN | NaN | Inf | NaN |\n| \\[x\\] \\< -FLT_MAX | NaN | NaN | -Inf | NaN |\n| else | RNE | RNE | RNE | RNE |\n\nFLOAT8E8M0 type was introduced to enable [Microscaling (MX) formats](https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf).\nWhen casting to FLOAT8E8M0, the rounding behavior can be specified using the `round_mode` and `saturate` attributes.\nThe current CUDA behavior is to round up and saturate. Casting negative values to FLOAT8E8M0 gives undefined behavior.\nThe following table describes the casting behavior of special values to FLOAT8E8M0 in the two most common cases.\n\n| x | saturate + up | non-saturate + nearest |\n| ----------------- | ------------- | --------------------- |\n| 0 | 0 | NaN |\n| -0 | Unspecified | Unspecified |\n| NaN | NaN | NaN |\n| Inf | E8M0_MAX | NaN |\n| x > E8M0_MAX | E8M0_MAX | NaN |\n| x < E8M0_MIN | E8M0_MIN | NaN |\n| x < 0 | Unspecified | Unspecified |\n", "attributes": [ { "name": "round_mode", "type": "string", "required": false, "default": "up", "description": "Rounding mode for conversion to float8e8m0. It only applies to casting to float8e8m0 and is `up` by default. `up`: round to nearest value away from zero, `down`: round to nearest value towards zero, `nearest`: round to nearest value and ties round up." }, { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz, float8e8m0). It is true by default. All cases are fully described in the tables inserted in the operator description." }, { "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(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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] }, { "description": "Constrain output types. Casting to complex is not supported.", "type_param_str": "T2", "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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "examples": [ { "summary": "cast", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in F8_TYPES or to_type in F8_TYPES:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "e8m0", "code": "np_fp32 = np.array(\n [\n \"0.0\",\n \"0.124\",\n \"0.25\",\n \"0.5\",\n \"1.1\",\n \"2.0\",\n \"4.0\",\n \"8.0\",\n ],\n dtype=np.float32,\n)\ntest_cases = [\n (\"FLOAT\", \"FLOAT8E8M0\"),\n (\"FLOAT16\", \"FLOAT8E8M0\"),\n (\"FLOAT8E8M0\", \"FLOAT\"),\n (\"FLOAT8E8M0\", \"FLOAT16\"),\n]\nfor from_type, to_type in test_cases:\n if from_type == \"FLOAT\":\n input_np = np_fp32\n output_np = to_float8e8m0(np_fp32)\n elif from_type == \"FLOAT16\":\n input_np = np_fp32.astype(np.float16)\n output_np = to_float8e8m0(input_np)\n elif from_type == \"FLOAT8E8M0\":\n input_np = to_float8e8m0(np_fp32)\n if to_type == \"FLOAT\":\n output_np = input_np.astype(np.float32)\n elif to_type == \"FLOAT16\":\n output_np = input_np.astype(np.float16)\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n input = make_tensor(\n \"input\",\n getattr(TensorProto, from_type),\n [2, 4],\n input_np,\n raw=True,\n )\n output = make_tensor(\n \"output\",\n getattr(TensorProto, to_type),\n [2, 4],\n output_np,\n raw=True,\n )\n if to_type == \"FLOAT8E8M0\":\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n saturate=1,\n round_mode=\"up\",\n )\n else:\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n )\n\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_e8m0_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n saturate=0,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "Cast", "module": "ai.onnx", "version": 25, "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\nyield result 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\nIn more detail, the conversion among numerical types should follow these rules\nif the destination type is not a float 8 type.\n\n* Casting from floating point to:\n * floating point: +/- infinity if OOR (out of range).\n * fixed point: undefined if OOR.\n * bool: +/- 0.0 to False; all else to True.\n* Casting from fixed point to:\n * floating point: +/- infinity if OOR. (+ infinity in the case of uint)\n * fixed point: when OOR, discard higher bits and reinterpret (with respect to two's complement representation for\n signed types). For example, 200 (int16) -> -56 (int8).\n * bool: zero to False; nonzero to True.\n* Casting from bool to:\n * floating point: `{1.0, 0.0}`.\n * fixed point: `{1, 0}`.\n * bool: no change.\n\nFloat 8 types (E4M3FN, E4M3FNUZ, E5M2, E5M2FNUZ) were introduced to speed up the training of\ndeep models. By default the conversion of a float *x* obeys\nto the following rules. `[x]` means the value rounded to\nthe target mantissa width.\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | -------- | -------- | -------- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| Inf | FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX |\n| -Inf | -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX |\n| \\[x\\] > FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX | FLT_MAX |\n| \\[x\\] \\< -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX | -FLT_MAX |\n| else | RNE | RNE | RNE | RNE |\n\nThe behavior changes if the parameter 'saturate' is set to False.\nThe rules then become:\n\n| x | E4M3FN | E4M3FNUZ | E5M2 | E5M2FNUZ |\n| ----------------- | ------ | -------- | ---- | -------- |\n| 0 | 0 | 0 | 0 | 0 |\n| -0 | -0 | 0 | -0 | 0 |\n| NaN | NaN | NaN | NaN | NaN |\n| -NaN | -NaN | NaN | -NaN | NaN |\n| Inf | NaN | NaN | Inf | NaN |\n| -Inf | -NaN | NaN | -Inf | NaN |\n| \\[x\\] > FLT_MAX | NaN | NaN | Inf | NaN |\n| \\[x\\] \\< -FLT_MAX | NaN | NaN | -Inf | NaN |\n| else | RNE | RNE | RNE | RNE |\n\nFLOAT8E8M0 type was introduced to enable [Microscaling (MX) formats](https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf).\nWhen casting to FLOAT8E8M0, the rounding behavior can be specified using the `round_mode` and `saturate` attributes.\nThe current CUDA behavior is to round up and saturate. Casting negative values to FLOAT8E8M0 gives undefined behavior.\nThe following table describes the casting behavior of special values to FLOAT8E8M0 in the two most common cases.\n\n| x | saturate + up | non-saturate + nearest |\n| ----------------- | ------------- | --------------------- |\n| 0 | 0 | NaN |\n| -0 | Unspecified | Unspecified |\n| NaN | NaN | NaN |\n| Inf | E8M0_MAX | NaN |\n| x > E8M0_MAX | E8M0_MAX | NaN |\n| x < E8M0_MIN | E8M0_MIN | NaN |\n| x < 0 | Unspecified | Unspecified |\n", "attributes": [ { "name": "round_mode", "type": "string", "required": false, "default": "up", "description": "Rounding mode for conversion to float8e8m0. It only applies to casting to float8e8m0 and is `up` by default. `up`: round to nearest value away from zero, `down`: round to nearest value towards zero, `nearest`: round to nearest value and ties round up." }, { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz, float8e8m0). It is true by default. All cases are fully described in the tables inserted in the operator description." }, { "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(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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] }, { "description": "Constrain output types. Casting to complex is not supported.", "type_param_str": "T2", "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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "examples": [ { "summary": "cast", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in F8_TYPES or to_type in F8_TYPES:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "e8m0", "code": "np_fp32 = np.array(\n [\n \"0.0\",\n \"0.124\",\n \"0.25\",\n \"0.5\",\n \"1.1\",\n \"2.0\",\n \"4.0\",\n \"8.0\",\n ],\n dtype=np.float32,\n)\ntest_cases = [\n (\"FLOAT\", \"FLOAT8E8M0\"),\n (\"FLOAT16\", \"FLOAT8E8M0\"),\n (\"FLOAT8E8M0\", \"FLOAT\"),\n (\"FLOAT8E8M0\", \"FLOAT16\"),\n]\nfor from_type, to_type in test_cases:\n if from_type == \"FLOAT\":\n input_np = np_fp32\n output_np = to_float8e8m0(np_fp32)\n elif from_type == \"FLOAT16\":\n input_np = np_fp32.astype(np.float16)\n output_np = to_float8e8m0(input_np)\n elif from_type == \"FLOAT8E8M0\":\n input_np = to_float8e8m0(np_fp32)\n if to_type == \"FLOAT\":\n output_np = input_np.astype(np.float32)\n elif to_type == \"FLOAT16\":\n output_np = input_np.astype(np.float16)\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n else:\n raise ValueError(\n f\"Conversion from {from_type} to {to_type} is not tested.\"\n )\n input = make_tensor(\n \"input\",\n getattr(TensorProto, from_type),\n [2, 4],\n input_np,\n raw=True,\n )\n output = make_tensor(\n \"output\",\n getattr(TensorProto, to_type),\n [2, 4],\n output_np,\n raw=True,\n )\n if to_type == \"FLOAT8E8M0\":\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n saturate=1,\n round_mode=\"up\",\n )\n else:\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=getattr(TensorProto, to_type),\n )\n\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_e8m0_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n node = onnx.helper.make_node(\n \"Cast\",\n inputs=[\"input\"],\n outputs=[\"output\"],\n to=to_dtype,\n saturate=0,\n )\n expect(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_cast_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "CastLike", "module": "ai.onnx", "version": 15, "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": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nf8_types = {\"FLOAT8E4M3FN\", \"FLOAT8E4M3FNUZ\", \"FLOAT8E5M2\", \"FLOAT8E5M2FNUZ\"}\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in f8_types or to_type in f8_types:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n saturate=0,\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "CastLike", "module": "ai.onnx", "version": 19, "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", "attributes": [ { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. Please refer to operator Cast description for further details." } ], "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] } ], "examples": [ { "summary": "castlike", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nf8_types = {\"FLOAT8E4M3FN\", \"FLOAT8E4M3FNUZ\", \"FLOAT8E5M2\", \"FLOAT8E5M2FNUZ\"}\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in f8_types or to_type in f8_types:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n saturate=0,\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "CastLike", "module": "ai.onnx", "version": 21, "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", "attributes": [ { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. Please refer to operator Cast description for further details." } ], "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(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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] }, { "description": "Constrain output types. Casting to complex is not supported.", "type_param_str": "T2", "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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "examples": [ { "summary": "castlike", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nf8_types = {\"FLOAT8E4M3FN\", \"FLOAT8E4M3FNUZ\", \"FLOAT8E5M2\", \"FLOAT8E5M2FNUZ\"}\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in f8_types or to_type in f8_types:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n saturate=0,\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "CastLike", "module": "ai.onnx", "version": 23, "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", "attributes": [ { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. Please refer to operator Cast description for further details." } ], "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(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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] }, { "description": "Constrain output types. Casting to complex is not supported.", "type_param_str": "T2", "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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "examples": [ { "summary": "castlike", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nf8_types = {\"FLOAT8E4M3FN\", \"FLOAT8E4M3FNUZ\", \"FLOAT8E5M2\", \"FLOAT8E5M2FNUZ\"}\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in f8_types or to_type in f8_types:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n saturate=0,\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "CastLike", "module": "ai.onnx", "version": 24, "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", "attributes": [ { "name": "round_mode", "type": "string", "required": false, "default": "up", "description": "Rounding mode for conversion to float8e8m0. It only applies to casting to float8e8m0 and is `up` by default. `up`: round to nearest value away from zero, `down`: round to nearest value towards zero, `nearest`: round to nearest value and ties round up. Please refer to operator Cast description for further details." }, { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz, float8e8m0). It is true by default. Please refer to operator Cast description for further details." } ], "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(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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] }, { "description": "Constrain output types. Casting to complex is not supported.", "type_param_str": "T2", "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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "examples": [ { "summary": "castlike", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nf8_types = {\"FLOAT8E4M3FN\", \"FLOAT8E4M3FNUZ\", \"FLOAT8E5M2\", \"FLOAT8E5M2FNUZ\"}\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in f8_types or to_type in f8_types:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n saturate=0,\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "CastLike", "module": "ai.onnx", "version": 25, "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", "attributes": [ { "name": "round_mode", "type": "string", "required": false, "default": "up", "description": "Rounding mode for conversion to float8e8m0. It only applies to casting to float8e8m0 and is `up` by default. `up`: round to nearest value away from zero, `down`: round to nearest value towards zero, `nearest`: round to nearest value and ties round up. Please refer to operator Cast description for further details." }, { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz, float8e8m0). It is true by default. Please refer to operator Cast description for further details." } ], "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(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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] }, { "description": "Constrain output types. Casting to complex is not supported.", "type_param_str": "T2", "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(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "examples": [ { "summary": "castlike", "code": "test_cases = [\n (\"FLOAT\", \"FLOAT16\"),\n (\"FLOAT\", \"DOUBLE\"),\n (\"FLOAT16\", \"FLOAT\"),\n (\"FLOAT16\", \"DOUBLE\"),\n (\"DOUBLE\", \"FLOAT\"),\n (\"DOUBLE\", \"FLOAT16\"),\n (\"FLOAT\", \"BFLOAT16\"),\n (\"BFLOAT16\", \"FLOAT\"),\n (\"FLOAT\", \"FLOAT8E4M3FN\"),\n (\"FLOAT16\", \"FLOAT8E4M3FN\"),\n (\"FLOAT\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E4M3FNUZ\"),\n (\"FLOAT8E4M3FN\", \"FLOAT\"),\n (\"FLOAT8E4M3FN\", \"FLOAT16\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT\"),\n (\"FLOAT8E4M3FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT8E5M2\"),\n (\"FLOAT16\", \"FLOAT8E5M2\"),\n (\"FLOAT\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT16\", \"FLOAT8E5M2FNUZ\"),\n (\"FLOAT8E5M2\", \"FLOAT\"),\n (\"FLOAT8E5M2\", \"FLOAT16\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT\"),\n (\"FLOAT8E5M2FNUZ\", \"FLOAT16\"),\n (\"FLOAT\", \"UINT4\"),\n (\"FLOAT16\", \"UINT4\"),\n (\"FLOAT\", \"INT4\"),\n (\"FLOAT16\", \"INT4\"),\n (\"UINT4\", \"FLOAT\"),\n (\"UINT4\", \"FLOAT16\"),\n (\"UINT4\", \"UINT8\"),\n (\"INT4\", \"FLOAT\"),\n (\"INT4\", \"FLOAT16\"),\n (\"INT4\", \"INT8\"),\n (\"FLOAT4E2M1\", \"FLOAT\"),\n (\"FLOAT4E2M1\", \"FLOAT16\"),\n (\"FLOAT\", \"FLOAT4E2M1\"),\n (\"FLOAT16\", \"FLOAT4E2M1\"),\n (\"FLOAT\", \"UINT2\"),\n (\"FLOAT16\", \"UINT2\"),\n (\"FLOAT\", \"INT2\"),\n (\"FLOAT16\", \"INT2\"),\n (\"UINT2\", \"FLOAT\"),\n (\"UINT2\", \"FLOAT16\"),\n (\"UINT2\", \"UINT8\"),\n (\"INT2\", \"FLOAT\"),\n (\"INT2\", \"FLOAT16\"),\n (\"INT2\", \"INT8\"),\n]\n\nf8_types = {\"FLOAT8E4M3FN\", \"FLOAT8E4M3FNUZ\", \"FLOAT8E5M2\", \"FLOAT8E5M2FNUZ\"}\n\nfor from_type, to_type in test_cases:\n if from_type == to_type:\n # Skip cases where from_type and to_type are the same\n continue\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n\n if from_type == \"BFLOAT16\" or to_type == \"BFLOAT16\":\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 4)\n\n elif from_type in f8_types or to_type in f8_types:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n elif from_type in (\"UINT4\", \"INT4\") or to_type in (\"UINT4\", \"INT4\"):\n np_fp32 = np.arange(-9, 16).astype(np.float32)\n input_shape = (5, 5)\n elif from_type in (\"UINT2\", \"INT2\") or to_type in (\"UINT2\", \"INT2\"):\n np_fp32 = np.arange(-3, 4).astype(np.float32)\n input_shape = (7, 1)\n elif from_type == \"FLOAT4E2M1\" or to_type == \"FLOAT4E2M1\":\n np_fp32 = np.array(\n [\n \"0.48\",\n \"0.25\",\n \"1.05\",\n \"-3.5\",\n \"-8\",\n \"9\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-4\",\n \"0.01\",\n \"-0.0\",\n ],\n dtype=np.float32,\n )\n input_shape = (3, 5)\n\n else:\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.816468\",\n \"0.21087195\",\n \"0.7229038\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n ],\n dtype=np.float32,\n ).reshape([3, 4])\n input_shape = (3, 4)\n\n if from_type in F8_TYPES:\n np_from = onnx.numpy_helper.saturate_cast(np_fp32, from_np_dtype)\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_from,\n raw=True,\n )\n elif from_type in FOUR_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_4bitx2(np_from)\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif from_type in TWO_BIT_TYPES:\n np_from = np_fp32.astype(from_np_dtype)\n packed = onnx.numpy_helper._pack_2bitx4(np_from)\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n np_from = np_fp32.astype(from_np_dtype)\n input = make_tensor(\n \"input\", from_dtype, input_shape, vals=np_from, raw=True\n )\n\n if to_type in F8_TYPES:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=onnx.numpy_helper.saturate_cast(np_from, to_np_dtype),\n raw=True,\n )\n elif to_type in FOUR_BIT_TYPES:\n packed = onnx.numpy_helper._pack_4bitx2(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_4bitx2()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n elif to_type in TWO_BIT_TYPES:\n packed = onnx.numpy_helper._pack_2bitx4(np_from.astype(to_np_dtype))\n # No byteswap needed on big-endian machines as _pack_2bitx4()\n # returns a numpy array with uint8 datatype.\n output = make_tensor(\n \"output\", to_dtype, input_shape, vals=packed.tobytes(), raw=True\n )\n else:\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_from.astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_\" + from_type + \"_to_\" + to_type,\n )" }, { "summary": "saturate_false", "code": "test_cases = itertools.product(\n [\n \"FLOAT\",\n \"FLOAT16\",\n ],\n [\n \"FLOAT8E4M3FN\",\n \"FLOAT8E4M3FNUZ\",\n \"FLOAT8E5M2\",\n \"FLOAT8E5M2FNUZ\",\n ],\n)\ninput_shape = (3, 5)\nfor from_type, to_type in test_cases:\n from_dtype = getattr(TensorProto, from_type)\n to_dtype = getattr(TensorProto, to_type)\n from_np_dtype = tensor_dtype_to_np_dtype(from_dtype)\n to_np_dtype = tensor_dtype_to_np_dtype(to_dtype)\n np_fp32 = np.array(\n [\n \"0.47892547\",\n \"0.48033667\",\n \"0.49968487\",\n \"0.81910545\",\n \"0.47031248\",\n \"0.7229038\",\n \"1000000\",\n \"1e-7\",\n \"NaN\",\n \"INF\",\n \"+INF\",\n \"-INF\",\n \"-0.0000001\",\n \"0.0000001\",\n \"-1000000\",\n ],\n dtype=np.float32,\n )\n\n input = make_tensor(\n \"input\",\n from_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype),\n raw=True,\n )\n output = make_tensor(\n \"output\",\n to_dtype,\n input_shape,\n vals=np_fp32.astype(from_np_dtype).astype(to_np_dtype),\n raw=True,\n )\n\n like = make_tensor(\"like\", to_dtype, (0,), vals=[])\n\n node = onnx.helper.make_node(\n \"CastLike\",\n inputs=[\"input\", \"like\"],\n outputs=[\"output\"],\n saturate=0,\n )\n\n expect(\n node,\n inputs=[input, like],\n outputs=[output],\n name=\"test_castlike_no_saturate_\" + from_type + \"_to_\" + to_type,\n )" } ] }, { "name": "CastMap", "module": "ai.onnx.ml", "version": 1, "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, "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": "CausalConvWithState", "module": "ai.onnx", "version": 27, "description": "Stateful causal 1D depthwise convolution.\n\nUsed by Gated DeltaNet (Qwen3.5) and Mamba (Jamba, FalconMamba) as a preprocessing step.\nReplaces the 3-op pattern (Concat + Conv + Slice) with a single fused operation.\n\nThe convolution is causal (looks only at current and past positions) and depthwise\n(each channel is convolved independently with its own kernel).\n\nThe input, weight, past_state, output, and present_state tensors are rank-3 with\nshape (batch_size, channels, length). The optional bias input is rank-1 with\nshape (channels). For higher-dimensional data, use Reshape nodes before and\nafter this operator to pack extra dimensions into the batch or channel axis.\n\nWeight layout: (channels, 1, k) for depthwise convolution.\nThe carry state stores the last (k-1) positions for incremental decode.\n\nThe optional activation attribute supports fused SiLU/Swish activation.\n\n", "attributes": [ { "name": "activation", "type": "string", "required": false, "default": "none", "description": "Fused activation function. One of: 'silu', 'swish', 'none'. Default is 'none'." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor with shape (batch_size, channels, length). Channels-first layout." }, { "name": "weight", "type": "T", "description": "Depthwise convolution kernel with shape (channels, 1, k) where k is the kernel size. The middle dim of size 1 follows the ONNX `Conv` weight layout `(M, C/group, k1, ..., kn)`: since this op is always depthwise, `group = channels`, so `C/group = 1`. Keeping this layout makes the weight tensor a drop-in for a depthwise `Conv(group=channels)` weight, so `Conv` <-> `CausalConvWithState` rewrites require no reshape." }, { "name": "bias", "type": "T", "option": "optional", "description": "Optional per-channel bias with shape (channels)." }, { "name": "past_state", "type": "T", "option": "optional", "description": "Carry state from previous step with shape (batch_size, channels, k - 1). If not provided, padding is zero." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Convolution output with same shape as input." }, { "name": "present_state", "type": "T", "description": "Updated carry state with shape (batch_size, channels, k - 1). Contains the last (k - 1) values of the effective padded/concatenated sequence along the causal axis, including any values from past_state or zero-padding when the current input is shorter than k - 1." } ], "min_output": 2, "max_output": 2, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "b1_c1_degenerate", "code": "# Mamba/GDN inner-head edge case: B=1, C=1.\nnode = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\"],\n outputs=[\"output\", \"present_state\"],\n)\n\nbatch_size, channels, length, k = 1, 1, 6, 4\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\n\noutput, present_state = _compute(input_, weight)\n\nexpect(\n node,\n inputs=[input_, weight],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_b1_c1_degenerate\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "basic", "code": "node = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\"],\n outputs=[\"output\", \"present_state\"],\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 4\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\n\noutput, present_state = _compute(input_, weight)\n\nexpect(\n node,\n inputs=[input_, weight],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_basic\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "decode_step", "code": "node = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\", \"bias\", \"past_state\"],\n outputs=[\"output\", \"present_state\"],\n)\n\nbatch_size, channels, length, k = 2, 4, 1, 4\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\nbias = np.random.randn(channels).astype(np.float32)\npast_state = np.random.randn(batch_size, channels, k - 1).astype(np.float32)\n\noutput, present_state = _compute(\n input_, weight, bias=bias, past_state=past_state\n)\n\nexpect(\n node,\n inputs=[input_, weight, bias, past_state],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_decode_step\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "fp16", "code": "node = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\"],\n outputs=[\"output\", \"present_state\"],\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 4\ninput_ = np.random.rand(batch_size, channels, length).astype(np.float16)\nweight = np.random.rand(channels, 1, k).astype(np.float16)\n\noutput, present_state = _compute(input_, weight)\n\nexpect(\n node,\n inputs=[input_, weight],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_fp16\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "kernel_size_one", "code": "node = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\"],\n outputs=[\"output\", \"present_state\"],\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 1\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\n\noutput, present_state = _compute(input_, weight)\n\nexpect(\n node,\n inputs=[input_, weight],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_kernel_size_one\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "short_input_no_past_state", "code": "# L < k-1 with no past_state: zero-pad is wider than the input.\nnode = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\"],\n outputs=[\"output\", \"present_state\"],\n)\n\nbatch_size, channels, length, k = 2, 4, 2, 5\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\n\noutput, present_state = _compute(input_, weight)\n\nexpect(\n node,\n inputs=[input_, weight],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_short_input_no_past_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "silu", "code": "node = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\"],\n outputs=[\"output\", \"present_state\"],\n activation=\"silu\",\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 4\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\n\noutput, present_state = _compute(input_, weight, activation=\"silu\")\n\nexpect(\n node,\n inputs=[input_, weight],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_silu\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "silu_fp16", "code": "# fp16 + SiLU: the reference upcasts Sigmoid/Mul to float32, so the\n# function-body expansion must do the same to stay numerically faithful.\nnode = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\"],\n outputs=[\"output\", \"present_state\"],\n activation=\"silu\",\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 4\ninput_ = np.random.rand(batch_size, channels, length).astype(np.float16)\nweight = np.random.rand(channels, 1, k).astype(np.float16)\n\noutput, present_state = _compute(input_, weight, activation=\"silu\")\n\nexpect(\n node,\n inputs=[input_, weight],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_silu_fp16\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "silu_with_past_state", "code": "# Fused activation combined with concat-from-past variant of PaddedInput.\nnode = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\", \"\", \"past_state\"],\n outputs=[\"output\", \"present_state\"],\n activation=\"silu\",\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 4\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\npast_state = np.random.randn(batch_size, channels, k - 1).astype(np.float32)\n\noutput, present_state = _compute(\n input_, weight, past_state=past_state, activation=\"silu\"\n)\n\nexpect(\n node,\n inputs=[input_, weight, past_state],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_silu_with_past_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "swish_alias", "code": "node = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\"],\n outputs=[\"output\", \"present_state\"],\n activation=\"swish\",\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 4\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\n\noutput, present_state = _compute(input_, weight, activation=\"swish\")\n\nexpect(\n node,\n inputs=[input_, weight],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_swish_alias\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "with_bias", "code": "node = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\", \"bias\"],\n outputs=[\"output\", \"present_state\"],\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 4\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\nbias = np.random.randn(channels).astype(np.float32)\n\noutput, present_state = _compute(input_, weight, bias=bias)\n\nexpect(\n node,\n inputs=[input_, weight, bias],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_with_bias\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "with_bias_and_past_state", "code": "# Multi-token (T>1) path through Concat(past, input) -> Conv(+bias).\nnode = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\", \"bias\", \"past_state\"],\n outputs=[\"output\", \"present_state\"],\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 4\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\nbias = np.random.randn(channels).astype(np.float32)\npast_state = np.random.randn(batch_size, channels, k - 1).astype(np.float32)\n\noutput, present_state = _compute(\n input_, weight, bias=bias, past_state=past_state\n)\n\nexpect(\n node,\n inputs=[input_, weight, bias, past_state],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_with_bias_and_past_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" }, { "summary": "with_past_state", "code": "node = onnx.helper.make_node(\n \"CausalConvWithState\",\n inputs=[\"input\", \"weight\", \"\", \"past_state\"],\n outputs=[\"output\", \"present_state\"],\n)\n\nbatch_size, channels, length, k = 2, 4, 8, 4\ninput_ = np.random.randn(batch_size, channels, length).astype(np.float32)\nweight = np.random.randn(channels, 1, k).astype(np.float32)\npast_state = np.random.randn(batch_size, channels, k - 1).astype(np.float32)\n\noutput, present_state = _compute(input_, weight, past_state=past_state)\n\nexpect(\n node,\n inputs=[input_, weight, past_state],\n outputs=[output, present_state],\n name=\"test_causal_conv_with_state_with_past_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 27)],\n)" } ] }, { "name": "CausalConvWithState", "module": "com.microsoft", "version": 1, "description": "Stateful causal depthwise convolution, generalized to N spatial dimensions.\n\nUsed by Gated DeltaNet (Qwen3.5) and Mamba (Jamba, FalconMamba) as a preprocessing step.\nReplaces the 3-op pattern (Concat + Conv + Slice) with a single fused operation.\n\nThe convolution is causal (looks only at current and past positions along the last\nspatial dimension) and depthwise (each channel is convolved independently with its own kernel).\n\nInput layout is channels-first: (batch_size, channels, ...).\nWeight layout: (channels, 1, k_1, ...) for depthwise convolution.\nThe carry state stores the last (k-1) positions along the causal axis for incremental decode.\n\nThe ndim attribute generalizes the op to 1D, 2D, or 3D spatial dimensions. Causality is\nenforced on the last spatial dimension only.\n\nThe optional activation attribute supports fused SiLU/Swish activation.\n", "attributes": [ { "name": "activation", "type": "string", "required": false, "default": "none", "description": "Fused activation function. One of: 'silu', 'swish', 'none'. Default is 'none'." }, { "name": "ndim", "type": "int64", "required": false, "default": 1, "description": "Spatial dimensionality: 1, 2, or 3. Default is 1." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor with shape (batch_size, channels, ...). Channels-first layout. Spatial dims: 1D: (L,); 2D: (H, W); 3D: (D, H, W)." }, { "name": "weight", "type": "T", "description": "Depthwise convolution kernel with shape (channels, 1, k_1, ...). Spatial kernel sizes: (k_1, ..., k_ndim)." }, { "name": "bias", "type": "T", "option": "optional", "description": "Optional per-channel bias with shape (channels)." }, { "name": "past_state", "type": "T", "option": "optional", "description": "Carry state from previous step. For ndim=1: (batch_size, channels, k_1 - 1). If not provided, padding is zero." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Convolution output with same shape as input." }, { "name": "present_state", "type": "T", "description": "Updated carry state. For ndim=1: (batch_size, channels, k_1 - 1). Contains the last (k-1) values from the virtual input along the causal axis." } ], "min_output": 2, "max_output": 2, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ] }, { "name": "Ceil", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_ceil\")" } ] }, { "name": "Ceil", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_ceil\")" } ] }, { "name": "Ceil", "module": "ai.onnx", "version": 13, "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. If x is integral, +0, -0, NaN, or infinite, x itself is returned.\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], 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], name=\"test_ceil\")" } ] }, { "name": "Celu", "module": "ai.onnx", "version": 12, "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(\n [\n [\n [[0.8439683], [0.5665144], [0.05836735]],\n [[0.02916367], [0.12964272], [0.5060197]],\n [[0.79538304], [0.9411346], [0.9546573]],\n ],\n [\n [[0.17730942], [0.46192095], [0.26480448]],\n [[0.6746842], [0.01665257], [0.62473077]],\n [[0.9240844], [0.9722341], [0.11965699]],\n ],\n [\n [[0.41356155], [0.9129373], [0.59330076]],\n [[0.81929934], [0.7862604], [0.11799799]],\n [[0.69248444], [0.54119414], [0.07513223]],\n ],\n ],\n dtype=np.float32,\n)\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], name=\"test_celu\")" }, { "summary": "celu_bfloat16", "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([-3.0, -0.5, 0.0, 0.5, 3.0], dtype=ml_dtypes.bfloat16)\n\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).astype(ml_dtypes.bfloat16)\n\nexpect(\n node,\n inputs=[input_data],\n outputs=[expected_output],\n name=\"test_celu_bfloat16\",\n)" }, { "summary": "celu_float16", "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([-3.0, -0.5, 0.0, 0.5, 3.0], dtype=np.float16)\n\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).astype(np.float16)\n\nexpect(\n node,\n inputs=[input_data],\n outputs=[expected_output],\n name=\"test_celu_float16\",\n)" } ] }, { "name": "Celu", "module": "ai.onnx", "version": 28, "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 float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "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(\n [\n [\n [[0.8439683], [0.5665144], [0.05836735]],\n [[0.02916367], [0.12964272], [0.5060197]],\n [[0.79538304], [0.9411346], [0.9546573]],\n ],\n [\n [[0.17730942], [0.46192095], [0.26480448]],\n [[0.6746842], [0.01665257], [0.62473077]],\n [[0.9240844], [0.9722341], [0.11965699]],\n ],\n [\n [[0.41356155], [0.9129373], [0.59330076]],\n [[0.81929934], [0.7862604], [0.11799799]],\n [[0.69248444], [0.54119414], [0.07513223]],\n ],\n ],\n dtype=np.float32,\n)\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], name=\"test_celu\")" }, { "summary": "celu_bfloat16", "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([-3.0, -0.5, 0.0, 0.5, 3.0], dtype=ml_dtypes.bfloat16)\n\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).astype(ml_dtypes.bfloat16)\n\nexpect(\n node,\n inputs=[input_data],\n outputs=[expected_output],\n name=\"test_celu_bfloat16\",\n)" }, { "summary": "celu_float16", "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([-3.0, -0.5, 0.0, 0.5, 3.0], dtype=np.float16)\n\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).astype(np.float16)\n\nexpect(\n node,\n inputs=[input_data],\n outputs=[expected_output],\n name=\"test_celu_float16\",\n)" } ] }, { "name": "CenterCropPad", "module": "ai.onnx", "version": 18, "description": "Center crop or pad an input to given dimensions.\n\nThe crop/pad dimensions can be specified for a subset of the `axes`; unspecified dimensions will remain unchanged.\n\nIf the input dimensions are larger than the target crop dimensions, a centered cropping window will be extracted\nfrom the input. The starting value for the cropping window is rounded down, which means that if the difference\nbetween the input shape and the crop shape is odd, the cropping window will be shifted half a pixel to the left\nof the input center.\n\nIf the input dimensions are smaller than the target crop dimensions, the input will be padded equally on both sides\nto center it in the output. In cases where the total number of padding pixels is odd, an additional pixel will be\nadded to the right side.\n\nThe padding value used is zero.\n", "attributes": [ { "name": "axes", "type": "int64[]", "required": false, "description": "If provided, it specifies a subset of axes that 'shape' refer to. If not provided, all axes are assumed [0, 1, ..., r-1], where r = rank(data). 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." } ], "inputs": [ { "name": "input_data", "type": "T", "description": "Input to extract the centered crop from." }, { "name": "shape", "type": "Tind", "description": "1-D tensor representing the cropping window dimensions." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "output_data", "type": "T", "description": "Output 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)" ] }, { "description": "Constrain indices to integer types", "type_param_str": "Tind", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "center_crop_pad_crop", "code": "node = onnx.helper.make_node(\n \"CenterCropPad\",\n inputs=[\"x\", \"shape\"],\n outputs=[\"y\"],\n)\n\n# First dim is even diff, second is uneven\nx = np.random.randn(20, 10, 3).astype(np.float32)\nshape = np.array([10, 7, 3], dtype=np.int64)\ny = x[5:15, 1:8, :]\n\nexpect(node, inputs=[x, shape], outputs=[y], name=\"test_center_crop_pad_crop\")" }, { "summary": "center_crop_pad_crop_and_pad", "code": "node = onnx.helper.make_node(\n \"CenterCropPad\",\n inputs=[\"x\", \"shape\"],\n outputs=[\"y\"],\n)\n\n# Cropping on first dim, padding on second, third stays the same\nx = np.random.randn(20, 8, 3).astype(np.float32)\nshape = np.array([10, 10, 3], dtype=np.int64)\ny = np.zeros([10, 10, 3], dtype=np.float32)\ny[:, 1:9, :] = x[5:15, :, :]\n\nexpect(\n node,\n inputs=[x, shape],\n outputs=[y],\n name=\"test_center_crop_pad_crop_and_pad\",\n)" }, { "summary": "center_crop_pad_crop_axes_chw", "code": "node = onnx.helper.make_node(\n \"CenterCropPad\",\n inputs=[\"x\", \"shape\"],\n outputs=[\"y\"],\n axes=[1, 2],\n)\n\n# Cropping on second dim, padding on third, first stays the same\nx = np.random.randn(3, 20, 8).astype(np.float32)\nshape = np.array([10, 9], dtype=np.int64)\ny = np.zeros([3, 10, 9], dtype=np.float32)\ny[:, :, :8] = x[:, 5:15, :]\n\nexpect(\n node,\n inputs=[x, shape],\n outputs=[y],\n name=\"test_center_crop_pad_crop_axes_chw\",\n)" }, { "summary": "center_crop_pad_crop_axes_hwc", "code": "node = onnx.helper.make_node(\n \"CenterCropPad\",\n inputs=[\"x\", \"shape\"],\n outputs=[\"y\"],\n axes=[0, 1],\n)\n\n# Cropping on first dim, padding on second, third stays the same\nx = np.random.randn(20, 8, 3).astype(np.float32)\nshape = np.array([10, 9], dtype=np.int64)\ny = np.zeros([10, 9, 3], dtype=np.float32)\ny[:, :8, :] = x[5:15, :, :]\n\nexpect(\n node,\n inputs=[x, shape],\n outputs=[y],\n name=\"test_center_crop_pad_crop_axes_hwc\",\n)" }, { "summary": "center_crop_pad_crop_negative_axes_hwc", "code": "node = onnx.helper.make_node(\n \"CenterCropPad\",\n inputs=[\"x\", \"shape\"],\n outputs=[\"y\"],\n axes=[-3, -2],\n)\n\n# Cropping on first dim, padding on second, third stays the same\nx = np.random.randn(20, 8, 3).astype(np.float32)\nshape = np.array([10, 9], dtype=np.int64)\ny = np.zeros([10, 9, 3], dtype=np.float32)\ny[:, :8, :] = x[5:15, :, :]\n\nexpect(\n node,\n inputs=[x, shape],\n outputs=[y],\n name=\"test_center_crop_pad_crop_negative_axes_hwc\",\n)" }, { "summary": "center_crop_pad_pad", "code": "node = onnx.helper.make_node(\n \"CenterCropPad\",\n inputs=[\"x\", \"shape\"],\n outputs=[\"y\"],\n)\n\n# First dim is even diff, second is uneven\nx = np.random.randn(10, 7, 3).astype(np.float32)\nshape = np.array([20, 10, 3], dtype=np.int64)\ny = np.zeros([20, 10, 3], dtype=np.float32)\ny[5:15, 1:8, :] = x\n\nexpect(node, inputs=[x, shape], outputs=[y], name=\"test_center_crop_pad_pad\")" } ] }, { "name": "Clip", "module": "ai.onnx", "version": 1, "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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_example\"\n)\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], 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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_inbounds\"\n)\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_outbounds\"\n)\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_splitbounds\",\n)\n\nx = np.array([-2, 0, 6]).astype(np.float32)\ny = np.array([1, 1, 1]).astype(np.float32)\nmin_val = np.float32(2)\nmax_val = np.float32(1)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_min_greater_than_max\",\n)" }, { "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], 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], 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], 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(\n node, inputs=[x, min_val], outputs=[y], name=\"test_clip_default_int8_min\"\n)\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(\n node, inputs=[x, max_val], outputs=[y], name=\"test_clip_default_int8_max\"\n)\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], name=\"test_clip_default_int8_inbounds\")" } ], "category": "Activation" }, { "name": "Clip", "module": "ai.onnx", "version": 6, "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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_example\"\n)\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], 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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_inbounds\"\n)\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_outbounds\"\n)\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_splitbounds\",\n)\n\nx = np.array([-2, 0, 6]).astype(np.float32)\ny = np.array([1, 1, 1]).astype(np.float32)\nmin_val = np.float32(2)\nmax_val = np.float32(1)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_min_greater_than_max\",\n)" }, { "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], 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], 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], 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(\n node, inputs=[x, min_val], outputs=[y], name=\"test_clip_default_int8_min\"\n)\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(\n node, inputs=[x, max_val], outputs=[y], name=\"test_clip_default_int8_max\"\n)\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], name=\"test_clip_default_int8_inbounds\")" } ], "category": "Activation" }, { "name": "Clip", "module": "ai.onnx", "version": 11, "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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_example\"\n)\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], 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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_inbounds\"\n)\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_outbounds\"\n)\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_splitbounds\",\n)\n\nx = np.array([-2, 0, 6]).astype(np.float32)\ny = np.array([1, 1, 1]).astype(np.float32)\nmin_val = np.float32(2)\nmax_val = np.float32(1)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_min_greater_than_max\",\n)" }, { "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], 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], 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], 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(\n node, inputs=[x, min_val], outputs=[y], name=\"test_clip_default_int8_min\"\n)\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(\n node, inputs=[x, max_val], outputs=[y], name=\"test_clip_default_int8_max\"\n)\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], name=\"test_clip_default_int8_inbounds\")" } ], "category": "Activation" }, { "name": "Clip", "module": "ai.onnx", "version": 12, "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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_example\"\n)\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], 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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_inbounds\"\n)\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_outbounds\"\n)\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_splitbounds\",\n)\n\nx = np.array([-2, 0, 6]).astype(np.float32)\ny = np.array([1, 1, 1]).astype(np.float32)\nmin_val = np.float32(2)\nmax_val = np.float32(1)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_min_greater_than_max\",\n)" }, { "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], 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], 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], 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(\n node, inputs=[x, min_val], outputs=[y], name=\"test_clip_default_int8_min\"\n)\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(\n node, inputs=[x, max_val], outputs=[y], name=\"test_clip_default_int8_max\"\n)\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], name=\"test_clip_default_int8_inbounds\")" } ], "category": "Activation" }, { "name": "Clip", "module": "ai.onnx", "version": 13, "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.\nWhen 'min' is greater than 'max', the clip operator sets all the 'input' values to\nthe value of 'max'. Thus, this is equivalent to 'Min(max, Max(input, min))'.\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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_example\"\n)\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], 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(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_inbounds\"\n)\n\nx = np.array([-6, 0, 6]).astype(np.float32)\ny = np.array([-5, 0, 5]).astype(np.float32)\nexpect(\n node, inputs=[x, min_val, max_val], outputs=[y], name=\"test_clip_outbounds\"\n)\n\nx = np.array([-1, 0, 6]).astype(np.float32)\ny = np.array([-1, 0, 5]).astype(np.float32)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_splitbounds\",\n)\n\nx = np.array([-2, 0, 6]).astype(np.float32)\ny = np.array([1, 1, 1]).astype(np.float32)\nmin_val = np.float32(2)\nmax_val = np.float32(1)\nexpect(\n node,\n inputs=[x, min_val, max_val],\n outputs=[y],\n name=\"test_clip_min_greater_than_max\",\n)" }, { "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], 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], 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], 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(\n node, inputs=[x, min_val], outputs=[y], name=\"test_clip_default_int8_min\"\n)\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(\n node, inputs=[x, max_val], outputs=[y], name=\"test_clip_default_int8_max\"\n)\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], name=\"test_clip_default_int8_inbounds\")" } ], "category": "Activation" }, { "name": "Col2Im", "module": "ai.onnx", "version": 18, "description": "The operator rearranges column blocks back into a multidimensional image\n\nCol2Im behaves similarly to PyTorch's fold https://pytorch.org/docs/stable/generated/torch.nn.Fold.html,\nbut it only supports *batched* multi-dimensional image tensors.\nAnother implementation in Python with N-dimension support can be found at https://github.com/f-dangel/unfoldNd/.\n\nNOTE:\n Although specifying image_shape looks redundant because it could be calculated from\n convolution formulas, it is required as input for more advanced scenarios as explained\n at PyTorch's implementation (https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/Col2Im.cpp#L10)\n", "attributes": [ { "name": "dilations", "type": "int64[]", "required": false, "description": "1-dimensional tensor with dilation value along each spatial axis of the image. If not present, the dilation defaults to 1 along each spatial axis of the image." }, { "name": "pads", "type": "int64[]", "required": false, "description": "1-dimensional tensor with padding value 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 is the number of pixels added at the beginning of axis `i` and xi_end is the number of pixels added at the end of axis `i`. If not present, the padding defaults to 0 along start and end of each spatial axis." }, { "name": "strides", "type": "int64[]", "required": false, "description": "1-dimensional tensor with stride value along each spatial axis. If not present, the stride defaults to 1 along each spatial axis." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input data tensor to be rearranged from column blocks back into an image. This is a 3-dimensional tensor containing [N, C * n-ary-product(block_shape), L], where N is batch dimension, C is image channel dimension and L is number of blocks.The blocks are enumerated in increasing lexicographic-order of their indices.For example, with an image-size 10*20 and block-size 9*18, there would be 2*3 blocks, enumerated in the order block(0, 0), block(0, 1), block(0, 2), block(1, 0), block(1, 1), block(1, 2)." }, { "name": "image_shape", "type": "tensor(int64)", "description": "The shape of the spatial dimensions of the image after rearranging the column blocks.This is a 1-dimensional tensor with size of at least 2, containing the value [H_img, W_img] for a 2-D image or [dim_i1, dim_i2, ..., dim_iN] for a N-D image." }, { "name": "block_shape", "type": "tensor(int64)", "description": "The shape of the block to apply on the input.This is a 1-dimensional tensor of size of at least 2, containing the value [H_block, W_block] for a 2-D image or [dim_b1, dim_b2, ..., dim_bN] for a N-D block.This is the block-shape before dilation is applied to it." } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "output", "type": "T", "description": "Output tensor produced by rearranging blocks into an image." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to all numeric 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": "col2im", "code": "input = np.array(\n [\n [\n [1.0, 6.0, 11.0, 16.0, 21.0], # (1, 5, 5)\n [2.0, 7.0, 12.0, 17.0, 22.0],\n [3.0, 8.0, 13.0, 18.0, 23.0],\n [4.0, 9.0, 14.0, 19.0, 24.0],\n [5.0, 0.0, 15.0, 20.0, 25.0],\n ]\n ]\n).astype(np.float32)\n\nimage_shape = np.array([5, 5]).astype(np.int64)\nblock_shape = np.array([1, 5]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Col2Im\", [\"input\", \"image_shape\", \"block_shape\"], [\"output\"]\n)\n\noutput = np.array(\n [\n [\n [\n [1.0, 2.0, 3.0, 4.0, 5.0], # (1, 1, 5, 5)\n [6.0, 7.0, 8.0, 9.0, 0.0],\n [11.0, 12.0, 13.0, 14.0, 15.0],\n [16.0, 17.0, 18.0, 19.0, 20.0],\n [21.0, 22.0, 23.0, 24.0, 25.0],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[input, image_shape, block_shape],\n outputs=[output],\n name=\"test_col2im\",\n)" }, { "summary": "col2im_5d", "code": "input = np.array(\n [\n [\n [1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56], # (1, 10, 12)\n [2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57],\n [3, 8, 13, 18, 23, 28, 33, 38, 43, 48, 53, 58],\n [4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59],\n [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],\n [61, 66, 71, 76, 81, 86, 91, 96, 101, 106, 111, 116],\n [62, 67, 72, 77, 82, 87, 92, 97, 102, 107, 112, 117],\n [63, 68, 73, 78, 83, 88, 93, 98, 103, 108, 113, 118],\n [64, 69, 74, 79, 84, 89, 94, 99, 104, 109, 114, 119],\n [65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120],\n ]\n ]\n).astype(np.float32)\nimage_shape = np.array([3, 4, 5]).astype(np.int64)\nblock_shape = np.array([1, 1, 5]).astype(np.int64)\n\noutput = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4, 5], # (1, 2, 3, 4, 5)\n [6, 7, 8, 9, 10],\n [11, 12, 13, 14, 15],\n [16, 17, 18, 19, 20],\n ],\n [\n [21, 22, 23, 24, 25],\n [26, 27, 28, 29, 30],\n [31, 32, 33, 34, 35],\n [36, 37, 38, 39, 40],\n ],\n [\n [41, 42, 43, 44, 45],\n [46, 47, 48, 49, 50],\n [51, 52, 53, 54, 55],\n [56, 57, 58, 59, 60],\n ],\n ],\n [\n [\n [61, 62, 63, 64, 65],\n [66, 67, 68, 69, 70],\n [71, 72, 73, 74, 75],\n [76, 77, 78, 79, 80],\n ],\n [\n [81, 82, 83, 84, 85],\n [86, 87, 88, 89, 90],\n [91, 92, 93, 94, 95],\n [96, 97, 98, 99, 100],\n ],\n [\n [101, 102, 103, 104, 105],\n [106, 107, 108, 109, 110],\n [111, 112, 113, 114, 115],\n [116, 117, 118, 119, 120],\n ],\n ],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Col2Im\", [\"input\", \"image_shape\", \"block_shape\"], [\"output\"]\n)\nexpect(\n node,\n inputs=[input, image_shape, block_shape],\n outputs=[output],\n name=\"test_col2im_5d\",\n)" }, { "summary": "col2im_dilations", "code": "input = np.array(\n [\n [\n [1.0, 5.0, 9.0, 13.0, 17], # (1, 4, 5)\n [2.0, 6.0, 10.0, 14.0, 18],\n [3.0, 7.0, 11.0, 15.0, 19],\n [4.0, 8.0, 12.0, 16.0, 20],\n ]\n ]\n).astype(np.float32)\nimage_shape = np.array([6, 6]).astype(np.int64)\nblock_shape = np.array([2, 2]).astype(np.int64)\n\noutput = np.array(\n [\n [\n [\n [1.0, 0.0, 0.0, 0.0, 0.0, 2.0], # (1, 1, 6, 6)\n [8.0, 0.0, 0.0, 0.0, 0.0, 10.0],\n [16.0, 0.0, 0.0, 0.0, 0.0, 18.0],\n [24.0, 0.0, 0.0, 0.0, 0.0, 26.0],\n [32.0, 0.0, 0.0, 0.0, 0.0, 34.0],\n [19.0, 0.0, 0.0, 0.0, 0.0, 20.0],\n ]\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Col2Im\",\n [\"input\", \"image_shape\", \"block_shape\"],\n [\"output\"],\n dilations=[1, 5],\n)\nexpect(\n node,\n inputs=[input, image_shape, block_shape],\n outputs=[output],\n name=\"test_col2im_dilations\",\n)" }, { "summary": "col2im_pads", "code": "input = np.array(\n [\n [\n [\n 1.0,\n 6.0,\n 11.0,\n 16.0,\n 21.0,\n 26,\n 31,\n 36,\n 41,\n 46,\n 51,\n 56,\n 61,\n 66,\n 71,\n ], # (1, 5, 15)\n [\n 2.0,\n 7.0,\n 12.0,\n 17.0,\n 22.0,\n 27,\n 32,\n 37,\n 42,\n 47,\n 52,\n 57,\n 62,\n 67,\n 72,\n ],\n [\n 3.0,\n 8.0,\n 13.0,\n 18.0,\n 23.0,\n 28,\n 33,\n 38,\n 43,\n 48,\n 53,\n 58,\n 63,\n 68,\n 73,\n ],\n [\n 4.0,\n 9.0,\n 14.0,\n 19.0,\n 24.0,\n 29,\n 34,\n 39,\n 44,\n 49,\n 54,\n 59,\n 64,\n 69,\n 74,\n ],\n [\n 5.0,\n 10.0,\n 15.0,\n 20.0,\n 25.0,\n 30,\n 35,\n 40,\n 45,\n 50,\n 55,\n 60,\n 65,\n 70,\n 75,\n ],\n ]\n ]\n).astype(np.float32)\nimage_shape = np.array([5, 5]).astype(np.int64)\nblock_shape = np.array([1, 5]).astype(np.int64)\n\noutput = np.array(\n [\n [\n [\n [8.0, 21.0, 24.0, 27.0, 24.0], # (1, 1, 5, 5)\n [38.0, 66.0, 69.0, 72.0, 54.0],\n [68.0, 111.0, 114.0, 117.0, 84.0],\n [98.0, 156.0, 159.0, 162.0, 114.0],\n [128.0, 201.0, 204.0, 207.0, 144.0],\n ]\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Col2Im\",\n [\"input\", \"image_shape\", \"block_shape\"],\n [\"output\"],\n pads=[0, 1, 0, 1],\n)\nexpect(\n node,\n inputs=[input, image_shape, block_shape],\n outputs=[output],\n name=\"test_col2im_pads\",\n)" }, { "summary": "col2im_strides", "code": "input = np.array(\n [\n [\n [0.0, 0.0, 0.0, 0.0], # (1, 9, 4)\n [1.0, 1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0, 1.0],\n [0.0, 0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0, 0.0],\n [1.0, 1.0, 1.0, 1.0],\n [0.0, 0.0, 0.0, 0.0],\n ]\n ]\n).astype(np.float32)\nimage_shape = np.array([5, 5]).astype(np.int64)\nblock_shape = np.array([3, 3]).astype(np.int64)\n\noutput = np.array(\n [\n [\n [\n [0.0, 1.0, 1.0, 1.0, 1.0], # (1, 1, 5, 5)\n [1.0, 0.0, 1.0, 0.0, 0.0],\n [0.0, 2.0, 1.0, 2.0, 1.0],\n [1.0, 0.0, 1.0, 0.0, 0.0],\n [0.0, 1.0, 0.0, 1.0, 0.0],\n ]\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Col2Im\",\n [\"input\", \"image_shape\", \"block_shape\"],\n [\"output\"],\n strides=[2, 2],\n)\nexpect(\n node,\n inputs=[input, image_shape, block_shape],\n outputs=[output],\n name=\"test_col2im_strides\",\n)" } ] }, { "name": "ComplexMul", "module": "com.microsoft", "version": 1, "inputs": [ { "name": "A", "type": "T", "description": "input_0" }, { "name": "B", "type": "T", "description": "input_1" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "C", "type": "T", "description": "output tensor" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to float or half tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(double)", "tensor(float16)" ] } ] }, { "name": "ComplexMulConj", "module": "com.microsoft", "version": 1, "inputs": [ { "name": "A", "type": "T", "description": "input_0" }, { "name": "B", "type": "T", "description": "input_1" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "C", "type": "T", "description": "output tensor" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to float or half tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(double)", "tensor(float16)" ] } ] }, { "name": "Compress", "module": "ai.onnx", "version": 9, "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(\n node,\n inputs=[input, condition.astype(bool)],\n outputs=[output],\n name=\"test_compress_0\",\n)" }, { "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(\n node,\n inputs=[input, condition.astype(bool)],\n outputs=[output],\n name=\"test_compress_1\",\n)" }, { "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(\n node,\n inputs=[input, condition.astype(bool)],\n outputs=[output],\n name=\"test_compress_default_axis\",\n)" }, { "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(\n node,\n inputs=[input, condition.astype(bool)],\n outputs=[output],\n name=\"test_compress_negative_axis\",\n)" } ] }, { "name": "Compress", "module": "ai.onnx", "version": 11, "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(\n node,\n inputs=[input, condition.astype(bool)],\n outputs=[output],\n name=\"test_compress_0\",\n)" }, { "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(\n node,\n inputs=[input, condition.astype(bool)],\n outputs=[output],\n name=\"test_compress_1\",\n)" }, { "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(\n node,\n inputs=[input, condition.astype(bool)],\n outputs=[output],\n name=\"test_compress_default_axis\",\n)" }, { "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(\n node,\n inputs=[input, condition.astype(bool)],\n outputs=[output],\n name=\"test_compress_negative_axis\",\n)" } ] }, { "name": "Concat", "module": "ai.onnx", "version": 1, "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[str, Sequence[Any]] = {\n \"1d\": ([1, 2], [3, 4]),\n \"2d\": ([[1, 2], [3, 4]], [[5, 6], [7, 8]]),\n \"3d\": (\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]]],\n [[[9, 10], [11, 12]], [[13, 14], [15, 16]]],\n ),\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\", inputs=list(in_args), outputs=[\"output\"], axis=i\n )\n output = np.concatenate(values, i)\n expect(\n node,\n inputs=list(values),\n outputs=[output],\n name=\"test_concat_\" + test_case + \"_axis_\" + str(i),\n )\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\", inputs=list(in_args), outputs=[\"output\"], axis=i\n )\n output = np.concatenate(values, i)\n expect(\n node,\n inputs=list(values),\n outputs=[output],\n name=\"test_concat_\" + test_case + \"_axis_negative_\" + str(abs(i)),\n )" } ], "category": "Tensor" }, { "name": "Concat", "module": "ai.onnx", "version": 4, "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[str, Sequence[Any]] = {\n \"1d\": ([1, 2], [3, 4]),\n \"2d\": ([[1, 2], [3, 4]], [[5, 6], [7, 8]]),\n \"3d\": (\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]]],\n [[[9, 10], [11, 12]], [[13, 14], [15, 16]]],\n ),\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\", inputs=list(in_args), outputs=[\"output\"], axis=i\n )\n output = np.concatenate(values, i)\n expect(\n node,\n inputs=list(values),\n outputs=[output],\n name=\"test_concat_\" + test_case + \"_axis_\" + str(i),\n )\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\", inputs=list(in_args), outputs=[\"output\"], axis=i\n )\n output = np.concatenate(values, i)\n expect(\n node,\n inputs=list(values),\n outputs=[output],\n name=\"test_concat_\" + test_case + \"_axis_negative_\" + str(abs(i)),\n )" } ], "category": "Tensor" }, { "name": "Concat", "module": "ai.onnx", "version": 11, "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[str, Sequence[Any]] = {\n \"1d\": ([1, 2], [3, 4]),\n \"2d\": ([[1, 2], [3, 4]], [[5, 6], [7, 8]]),\n \"3d\": (\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]]],\n [[[9, 10], [11, 12]], [[13, 14], [15, 16]]],\n ),\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\", inputs=list(in_args), outputs=[\"output\"], axis=i\n )\n output = np.concatenate(values, i)\n expect(\n node,\n inputs=list(values),\n outputs=[output],\n name=\"test_concat_\" + test_case + \"_axis_\" + str(i),\n )\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\", inputs=list(in_args), outputs=[\"output\"], axis=i\n )\n output = np.concatenate(values, i)\n expect(\n node,\n inputs=list(values),\n outputs=[output],\n name=\"test_concat_\" + test_case + \"_axis_negative_\" + str(abs(i)),\n )" } ], "category": "Tensor" }, { "name": "Concat", "module": "ai.onnx", "version": 13, "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[str, Sequence[Any]] = {\n \"1d\": ([1, 2], [3, 4]),\n \"2d\": ([[1, 2], [3, 4]], [[5, 6], [7, 8]]),\n \"3d\": (\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]]],\n [[[9, 10], [11, 12]], [[13, 14], [15, 16]]],\n ),\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\", inputs=list(in_args), outputs=[\"output\"], axis=i\n )\n output = np.concatenate(values, i)\n expect(\n node,\n inputs=list(values),\n outputs=[output],\n name=\"test_concat_\" + test_case + \"_axis_\" + str(i),\n )\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\", inputs=list(in_args), outputs=[\"output\"], axis=i\n )\n output = np.concatenate(values, i)\n expect(\n node,\n inputs=list(values),\n outputs=[output],\n name=\"test_concat_\" + test_case + \"_axis_negative_\" + str(abs(i)),\n )" } ], "category": "Tensor" }, { "name": "ConcatFromSequence", "module": "ai.onnx", "version": 11, "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, "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "Constant", "module": "ai.onnx", "version": 9, "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "Constant", "module": "ai.onnx", "version": 11, "description": "A constant tensor. Exactly one of the two attributes, either value or sparse_value,\nmust be specified.\n", "attributes": [ { "name": "sparse_value", "type": "sparse_tensor", "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "Constant", "module": "ai.onnx", "version": 12, "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", "type": "sparse_tensor", "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "Constant", "module": "ai.onnx", "version": 13, "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", "type": "sparse_tensor", "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "Constant", "module": "ai.onnx", "version": 19, "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", "type": "sparse_tensor", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] } ], "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "Constant", "module": "ai.onnx", "version": 21, "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", "type": "sparse_tensor", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "Constant", "module": "ai.onnx", "version": 23, "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", "type": "sparse_tensor", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "Constant", "module": "ai.onnx", "version": 24, "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", "type": "sparse_tensor", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "Constant", "module": "ai.onnx", "version": 25, "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", "type": "sparse_tensor", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "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], name=\"test_constant\")" } ], "category": "Constant" }, { "name": "ConstantOfShape", "module": "ai.onnx", "version": 9, "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(\n \"value\", onnx.TensorProto.FLOAT, [1], [1]\n)\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], name=\"test_constantofshape_float_ones\")" }, { "summary": "int32_shape_zero", "code": "x = np.array(\n [\n 0,\n ]\n).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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(\n node, inputs=[x], outputs=[y], name=\"test_constantofshape_int_shape_zero\"\n)" }, { "summary": "int32_zeros", "code": "x = np.array([10, 6]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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], name=\"test_constantofshape_int_zeros\")" } ] }, { "name": "ConstantOfShape", "module": "ai.onnx", "version": 20, "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)", "tensor(bfloat16)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] } ], "examples": [ { "summary": "float_ones", "code": "x = np.array([4, 3, 2]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.FLOAT, [1], [1]\n)\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], name=\"test_constantofshape_float_ones\")" }, { "summary": "int32_shape_zero", "code": "x = np.array(\n [\n 0,\n ]\n).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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(\n node, inputs=[x], outputs=[y], name=\"test_constantofshape_int_shape_zero\"\n)" }, { "summary": "int32_zeros", "code": "x = np.array([10, 6]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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], name=\"test_constantofshape_int_zeros\")" } ] }, { "name": "ConstantOfShape", "module": "ai.onnx", "version": 21, "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 or boolean.", "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(uint4)", "tensor(int4)", "tensor(bool)", "tensor(bfloat16)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] } ], "examples": [ { "summary": "float_ones", "code": "x = np.array([4, 3, 2]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.FLOAT, [1], [1]\n)\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], name=\"test_constantofshape_float_ones\")" }, { "summary": "int32_shape_zero", "code": "x = np.array(\n [\n 0,\n ]\n).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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(\n node, inputs=[x], outputs=[y], name=\"test_constantofshape_int_shape_zero\"\n)" }, { "summary": "int32_zeros", "code": "x = np.array([10, 6]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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], name=\"test_constantofshape_int_zeros\")" } ] }, { "name": "ConstantOfShape", "module": "ai.onnx", "version": 23, "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 or boolean.", "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(uint4)", "tensor(int4)", "tensor(bool)", "tensor(bfloat16)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(float4e2m1)" ] } ], "examples": [ { "summary": "float_ones", "code": "x = np.array([4, 3, 2]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.FLOAT, [1], [1]\n)\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], name=\"test_constantofshape_float_ones\")" }, { "summary": "int32_shape_zero", "code": "x = np.array(\n [\n 0,\n ]\n).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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(\n node, inputs=[x], outputs=[y], name=\"test_constantofshape_int_shape_zero\"\n)" }, { "summary": "int32_zeros", "code": "x = np.array([10, 6]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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], name=\"test_constantofshape_int_zeros\")" } ] }, { "name": "ConstantOfShape", "module": "ai.onnx", "version": 24, "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 or boolean.", "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(uint4)", "tensor(int4)", "tensor(bool)", "tensor(bfloat16)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "examples": [ { "summary": "float_ones", "code": "x = np.array([4, 3, 2]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.FLOAT, [1], [1]\n)\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], name=\"test_constantofshape_float_ones\")" }, { "summary": "int32_shape_zero", "code": "x = np.array(\n [\n 0,\n ]\n).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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(\n node, inputs=[x], outputs=[y], name=\"test_constantofshape_int_shape_zero\"\n)" }, { "summary": "int32_zeros", "code": "x = np.array([10, 6]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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], name=\"test_constantofshape_int_zeros\")" } ] }, { "name": "ConstantOfShape", "module": "ai.onnx", "version": 25, "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 or boolean.", "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(uint4)", "tensor(int4)", "tensor(bool)", "tensor(bfloat16)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "examples": [ { "summary": "float_ones", "code": "x = np.array([4, 3, 2]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.FLOAT, [1], [1]\n)\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], name=\"test_constantofshape_float_ones\")" }, { "summary": "int32_shape_zero", "code": "x = np.array(\n [\n 0,\n ]\n).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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(\n node, inputs=[x], outputs=[y], name=\"test_constantofshape_int_shape_zero\"\n)" }, { "summary": "int32_zeros", "code": "x = np.array([10, 6]).astype(np.int64)\ntensor_value = onnx.helper.make_tensor(\n \"value\", onnx.TensorProto.INT32, [1], [0]\n)\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], name=\"test_constantofshape_int_zeros\")" } ] }, { "name": "Conv", "module": "ai.onnx", "version": 1, "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": "x = np.array(\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 5, 5) input tensor\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n [20.0, 21.0, 22.0, 23.0, 24.0],\n ]\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 1, 3, 3) tensor for convolution weights\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ]\n ]\n ]\n).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(\n [\n [\n [\n [12.0, 21.0, 27.0, 33.0, 24.0], # (1, 1, 5, 5) output tensor\n [33.0, 54.0, 63.0, 72.0, 51.0],\n [63.0, 99.0, 108.0, 117.0, 81.0],\n [93.0, 144.0, 153.0, 162.0, 111.0],\n [72.0, 111.0, 117.0, 123.0, 84.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_padding,\n inputs=[x, W],\n outputs=[y_with_padding],\n name=\"test_basic_conv_with_padding\",\n)\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(\n [\n [\n [\n [54.0, 63.0, 72.0], # (1, 1, 3, 3) output tensor\n [99.0, 108.0, 117.0],\n [144.0, 153.0, 162.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_without_padding,\n inputs=[x, W],\n outputs=[y_without_padding],\n name=\"test_basic_conv_without_padding\",\n)" }, { "summary": "conv_with_autopad_same", "code": "x = np.array(\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 5, 5) input tensor\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n [20.0, 21.0, 22.0, 23.0, 24.0],\n ]\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 1, 3, 3) tensor for convolution weights\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ]\n ]\n ]\n).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(\n [[[[12.0, 27.0, 24.0], [63.0, 108.0, 81.0], [72.0, 117.0, 84.0]]]]\n).astype(np.float32)\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_conv_with_autopad_same\")" }, { "summary": "conv_with_strides", "code": "x = np.array(\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 7, 5) input tensor\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n [20.0, 21.0, 22.0, 23.0, 24.0],\n [25.0, 26.0, 27.0, 28.0, 29.0],\n [30.0, 31.0, 32.0, 33.0, 34.0],\n ]\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 1, 3, 3) tensor for convolution weights\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ]\n ]\n ]\n).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=[\n 2,\n 2,\n ], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_padding = np.array(\n [\n [\n [\n [12.0, 27.0, 24.0], # (1, 1, 4, 3) output tensor\n [63.0, 108.0, 81.0],\n [123.0, 198.0, 141.0],\n [112.0, 177.0, 124.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_padding,\n inputs=[x, W],\n outputs=[y_with_padding],\n name=\"test_conv_with_strides_padding\",\n)\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=[\n 2,\n 2,\n ], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_without_padding = np.array(\n [\n [\n [\n [54.0, 72.0], # (1, 1, 3, 2) output tensor\n [144.0, 162.0],\n [234.0, 252.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_without_padding,\n inputs=[x, W],\n outputs=[y_without_padding],\n name=\"test_conv_with_strides_no_padding\",\n)\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=[\n 2,\n 2,\n ], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_asymmetric_padding = np.array(\n [\n [\n [\n [21.0, 33.0], # (1, 1, 4, 2) output tensor\n [99.0, 117.0],\n [189.0, 207.0],\n [171.0, 183.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_asymmetric_padding,\n inputs=[x, W],\n outputs=[y_with_asymmetric_padding],\n name=\"test_conv_with_strides_and_asymmetric_padding\",\n)" } ], "category": "Layer" }, { "name": "Conv", "module": "ai.onnx", "version": 11, "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": "x = np.array(\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 5, 5) input tensor\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n [20.0, 21.0, 22.0, 23.0, 24.0],\n ]\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 1, 3, 3) tensor for convolution weights\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ]\n ]\n ]\n).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(\n [\n [\n [\n [12.0, 21.0, 27.0, 33.0, 24.0], # (1, 1, 5, 5) output tensor\n [33.0, 54.0, 63.0, 72.0, 51.0],\n [63.0, 99.0, 108.0, 117.0, 81.0],\n [93.0, 144.0, 153.0, 162.0, 111.0],\n [72.0, 111.0, 117.0, 123.0, 84.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_padding,\n inputs=[x, W],\n outputs=[y_with_padding],\n name=\"test_basic_conv_with_padding\",\n)\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(\n [\n [\n [\n [54.0, 63.0, 72.0], # (1, 1, 3, 3) output tensor\n [99.0, 108.0, 117.0],\n [144.0, 153.0, 162.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_without_padding,\n inputs=[x, W],\n outputs=[y_without_padding],\n name=\"test_basic_conv_without_padding\",\n)" }, { "summary": "conv_with_autopad_same", "code": "x = np.array(\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 5, 5) input tensor\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n [20.0, 21.0, 22.0, 23.0, 24.0],\n ]\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 1, 3, 3) tensor for convolution weights\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ]\n ]\n ]\n).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(\n [[[[12.0, 27.0, 24.0], [63.0, 108.0, 81.0], [72.0, 117.0, 84.0]]]]\n).astype(np.float32)\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_conv_with_autopad_same\")" }, { "summary": "conv_with_strides", "code": "x = np.array(\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 7, 5) input tensor\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n [20.0, 21.0, 22.0, 23.0, 24.0],\n [25.0, 26.0, 27.0, 28.0, 29.0],\n [30.0, 31.0, 32.0, 33.0, 34.0],\n ]\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 1, 3, 3) tensor for convolution weights\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ]\n ]\n ]\n).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=[\n 2,\n 2,\n ], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_padding = np.array(\n [\n [\n [\n [12.0, 27.0, 24.0], # (1, 1, 4, 3) output tensor\n [63.0, 108.0, 81.0],\n [123.0, 198.0, 141.0],\n [112.0, 177.0, 124.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_padding,\n inputs=[x, W],\n outputs=[y_with_padding],\n name=\"test_conv_with_strides_padding\",\n)\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=[\n 2,\n 2,\n ], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_without_padding = np.array(\n [\n [\n [\n [54.0, 72.0], # (1, 1, 3, 2) output tensor\n [144.0, 162.0],\n [234.0, 252.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_without_padding,\n inputs=[x, W],\n outputs=[y_without_padding],\n name=\"test_conv_with_strides_no_padding\",\n)\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=[\n 2,\n 2,\n ], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_asymmetric_padding = np.array(\n [\n [\n [\n [21.0, 33.0], # (1, 1, 4, 2) output tensor\n [99.0, 117.0],\n [189.0, 207.0],\n [171.0, 183.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_asymmetric_padding,\n inputs=[x, W],\n outputs=[y_with_asymmetric_padding],\n name=\"test_conv_with_strides_and_asymmetric_padding\",\n)" } ], "category": "Layer" }, { "name": "Conv", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "conv", "code": "x = np.array(\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 5, 5) input tensor\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n [20.0, 21.0, 22.0, 23.0, 24.0],\n ]\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 1, 3, 3) tensor for convolution weights\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ]\n ]\n ]\n).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(\n [\n [\n [\n [12.0, 21.0, 27.0, 33.0, 24.0], # (1, 1, 5, 5) output tensor\n [33.0, 54.0, 63.0, 72.0, 51.0],\n [63.0, 99.0, 108.0, 117.0, 81.0],\n [93.0, 144.0, 153.0, 162.0, 111.0],\n [72.0, 111.0, 117.0, 123.0, 84.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_padding,\n inputs=[x, W],\n outputs=[y_with_padding],\n name=\"test_basic_conv_with_padding\",\n)\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(\n [\n [\n [\n [54.0, 63.0, 72.0], # (1, 1, 3, 3) output tensor\n [99.0, 108.0, 117.0],\n [144.0, 153.0, 162.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_without_padding,\n inputs=[x, W],\n outputs=[y_without_padding],\n name=\"test_basic_conv_without_padding\",\n)" }, { "summary": "conv_with_autopad_same", "code": "x = np.array(\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 5, 5) input tensor\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n [20.0, 21.0, 22.0, 23.0, 24.0],\n ]\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 1, 3, 3) tensor for convolution weights\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ]\n ]\n ]\n).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(\n [[[[12.0, 27.0, 24.0], [63.0, 108.0, 81.0], [72.0, 117.0, 84.0]]]]\n).astype(np.float32)\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_conv_with_autopad_same\")" }, { "summary": "conv_with_strides", "code": "x = np.array(\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 7, 5) input tensor\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n [20.0, 21.0, 22.0, 23.0, 24.0],\n [25.0, 26.0, 27.0, 28.0, 29.0],\n [30.0, 31.0, 32.0, 33.0, 34.0],\n ]\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 1, 3, 3) tensor for convolution weights\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ]\n ]\n ]\n).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=[\n 2,\n 2,\n ], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_padding = np.array(\n [\n [\n [\n [12.0, 27.0, 24.0], # (1, 1, 4, 3) output tensor\n [63.0, 108.0, 81.0],\n [123.0, 198.0, 141.0],\n [112.0, 177.0, 124.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_padding,\n inputs=[x, W],\n outputs=[y_with_padding],\n name=\"test_conv_with_strides_padding\",\n)\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=[\n 2,\n 2,\n ], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_without_padding = np.array(\n [\n [\n [\n [54.0, 72.0], # (1, 1, 3, 2) output tensor\n [144.0, 162.0],\n [234.0, 252.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_without_padding,\n inputs=[x, W],\n outputs=[y_without_padding],\n name=\"test_conv_with_strides_no_padding\",\n)\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=[\n 2,\n 2,\n ], # Default values for other attributes: dilations=[1, 1], groups=1\n)\ny_with_asymmetric_padding = np.array(\n [\n [\n [\n [21.0, 33.0], # (1, 1, 4, 2) output tensor\n [99.0, 117.0],\n [189.0, 207.0],\n [171.0, 183.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_asymmetric_padding,\n inputs=[x, W],\n outputs=[y_with_asymmetric_padding],\n name=\"test_conv_with_strides_and_asymmetric_padding\",\n)" } ], "category": "Layer" }, { "name": "Conv", "module": "com.microsoft.nchwc", "version": 1, "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "name": "dilations", "type": "int64[]", "required": false }, { "name": "group", "type": "int64", "required": false, "default": 1 }, { "name": "kernel_shape", "type": "int64[]", "required": false }, { "name": "pads", "type": "int64[]", "required": false }, { "name": "strides", "type": "int64[]", "required": false } ], "inputs": [ { "name": "X", "type": "T" }, { "name": "W", "type": "T" }, { "name": "B", "type": "T", "option": "optional" }, { "name": "Sum", "type": "T", "option": "optional" } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "Y", "type": "T" } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain input and output types to float tensors", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)" ] } ], "category": "Layer" }, { "name": "Conv", "module": "com.ms.internal.nhwc", "version": 1, "description": "The convolution operator consumes an input tensor and a filter, and\ncomputes the output.", "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Layer" }, { "name": "Conv", "module": "com.ms.internal.nhwc", "version": 11, "description": "The convolution operator consumes an input tensor and a filter, and\ncomputes the output.", "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Layer" }, { "name": "ConvInteger", "module": "ai.onnx", "version": 10, "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": "x = (\n np.array([2, 3, 4, 5, 6, 7, 8, 9, 10])\n .astype(np.uint8)\n .reshape((1, 1, 3, 3))\n)\nx_zero_point = np.uint8(1)\nw_zero_points = np.array([0, 1], dtype=np.uint8)\nw = np.array([1, 1, 1, 1, 1, 1, 1, 1]).astype(np.uint8).reshape((2, 1, 2, 2))\n\ny = (\n np.array(\n [\n 1,\n 3,\n 5,\n 3,\n 5,\n 12,\n 16,\n 9,\n 11,\n 24,\n 28,\n 15,\n 7,\n 15,\n 17,\n 9,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n 0,\n ]\n )\n .astype(np.int32)\n .reshape((1, 2, 4, 4))\n)\n\n# ConvInteger with padding\nconvinteger_node_with_padding = onnx.helper.make_node(\n \"ConvInteger\",\n inputs=[\"x\", \"w\", \"x_zero_point\", \"w_zero_points\"],\n outputs=[\"y\"],\n pads=[1, 1, 1, 1],\n)\n\nexpect(\n convinteger_node_with_padding,\n inputs=[x, w, x_zero_point, w_zero_points],\n outputs=[y],\n name=\"test_convinteger_with_padding\",\n)" }, { "summary": "without_padding", "code": "x = (\n np.array([2, 3, 4, 5, 6, 7, 8, 9, 10])\n .astype(np.uint8)\n .reshape((1, 1, 3, 3))\n)\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(\n \"ConvInteger\", inputs=[\"x\", \"w\", \"x_zero_point\"], outputs=[\"y\"]\n)\n\nexpect(\n convinteger_node,\n inputs=[x, w, x_zero_point],\n outputs=[y],\n name=\"test_convinteger_without_padding\",\n)" } ], "category": "Layer" }, { "name": "ConvTranspose", "module": "ai.onnx", "version": 1, "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(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0], # (1, 2, 5, 5)\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose\")" }, { "summary": "convtranspose_1d", "code": "x = np.array([[[0.0, 1.0, 2.0]]]).astype(np.float32) # (1, 1, 3)\n\nW = np.array([[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]]).astype( # (1, 2, 3)\n np.float32\n)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array(\n [[[0.0, 1.0, 3.0, 3.0, 2.0], [0.0, 1.0, 3.0, 3.0, 2.0]]] # (1, 2, 5)\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_1d\")" }, { "summary": "convtranspose_3d", "code": "x = np.array(\n [\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 3, 4, 5)\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n ],\n [\n [20.0, 21.0, 22.0, 23.0, 24.0],\n [25.0, 26.0, 27.0, 28.0, 29.0],\n [30.0, 31.0, 32.0, 33.0, 34.0],\n [35.0, 36.0, 37.0, 38.0, 39.0],\n ],\n [\n [40.0, 41.0, 42.0, 43.0, 44.0],\n [45.0, 46.0, 47.0, 48.0, 49.0],\n [50.0, 51.0, 52.0, 53.0, 54.0],\n [55.0, 56.0, 57.0, 58.0, 59.0],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 2, 3, 3, 3)\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array(\n [\n [\n [\n [\n [0.0, 1.0, 3.0, 6.0, 9.0, 7.0, 4.0], # (1, 2, 5, 6, 7)\n [5.0, 12.0, 21.0, 27.0, 33.0, 24.0, 13.0],\n [15.0, 33.0, 54.0, 63.0, 72.0, 51.0, 27.0],\n [30.0, 63.0, 99.0, 108.0, 117.0, 81.0, 42.0],\n [25.0, 52.0, 81.0, 87.0, 93.0, 64.0, 33.0],\n [15.0, 31.0, 48.0, 51.0, 54.0, 37.0, 19.0],\n ],\n [\n [20.0, 42.0, 66.0, 72.0, 78.0, 54.0, 28.0],\n [50.0, 104.0, 162.0, 174.0, 186.0, 128.0, 66.0],\n [90.0, 186.0, 288.0, 306.0, 324.0, 222.0, 114.0],\n [120.0, 246.0, 378.0, 396.0, 414.0, 282.0, 144.0],\n [90.0, 184.0, 282.0, 294.0, 306.0, 208.0, 106.0],\n [50.0, 102.0, 156.0, 162.0, 168.0, 114.0, 58.0],\n ],\n [\n [60.0, 123.0, 189.0, 198.0, 207.0, 141.0, 72.0],\n [135.0, 276.0, 423.0, 441.0, 459.0, 312.0, 159.0],\n [225.0, 459.0, 702.0, 729.0, 756.0, 513.0, 261.0],\n [270.0, 549.0, 837.0, 864.0, 891.0, 603.0, 306.0],\n [195.0, 396.0, 603.0, 621.0, 639.0, 432.0, 219.0],\n [105.0, 213.0, 324.0, 333.0, 342.0, 231.0, 117.0],\n ],\n [\n [60.0, 122.0, 186.0, 192.0, 198.0, 134.0, 68.0],\n [130.0, 264.0, 402.0, 414.0, 426.0, 288.0, 146.0],\n [210.0, 426.0, 648.0, 666.0, 684.0, 462.0, 234.0],\n [240.0, 486.0, 738.0, 756.0, 774.0, 522.0, 264.0],\n [170.0, 344.0, 522.0, 534.0, 546.0, 368.0, 186.0],\n [90.0, 182.0, 276.0, 282.0, 288.0, 194.0, 98.0],\n ],\n [\n [40.0, 81.0, 123.0, 126.0, 129.0, 87.0, 44.0],\n [85.0, 172.0, 261.0, 267.0, 273.0, 184.0, 93.0],\n [135.0, 273.0, 414.0, 423.0, 432.0, 291.0, 147.0],\n [150.0, 303.0, 459.0, 468.0, 477.0, 321.0, 162.0],\n [105.0, 212.0, 321.0, 327.0, 333.0, 224.0, 113.0],\n [55.0, 111.0, 168.0, 171.0, 174.0, 117.0, 59.0],\n ],\n ],\n [\n [\n [0.0, 1.0, 3.0, 6.0, 9.0, 7.0, 4.0],\n [5.0, 12.0, 21.0, 27.0, 33.0, 24.0, 13.0],\n [15.0, 33.0, 54.0, 63.0, 72.0, 51.0, 27.0],\n [30.0, 63.0, 99.0, 108.0, 117.0, 81.0, 42.0],\n [25.0, 52.0, 81.0, 87.0, 93.0, 64.0, 33.0],\n [15.0, 31.0, 48.0, 51.0, 54.0, 37.0, 19.0],\n ],\n [\n [20.0, 42.0, 66.0, 72.0, 78.0, 54.0, 28.0],\n [50.0, 104.0, 162.0, 174.0, 186.0, 128.0, 66.0],\n [90.0, 186.0, 288.0, 306.0, 324.0, 222.0, 114.0],\n [120.0, 246.0, 378.0, 396.0, 414.0, 282.0, 144.0],\n [90.0, 184.0, 282.0, 294.0, 306.0, 208.0, 106.0],\n [50.0, 102.0, 156.0, 162.0, 168.0, 114.0, 58.0],\n ],\n [\n [60.0, 123.0, 189.0, 198.0, 207.0, 141.0, 72.0],\n [135.0, 276.0, 423.0, 441.0, 459.0, 312.0, 159.0],\n [225.0, 459.0, 702.0, 729.0, 756.0, 513.0, 261.0],\n [270.0, 549.0, 837.0, 864.0, 891.0, 603.0, 306.0],\n [195.0, 396.0, 603.0, 621.0, 639.0, 432.0, 219.0],\n [105.0, 213.0, 324.0, 333.0, 342.0, 231.0, 117.0],\n ],\n [\n [60.0, 122.0, 186.0, 192.0, 198.0, 134.0, 68.0],\n [130.0, 264.0, 402.0, 414.0, 426.0, 288.0, 146.0],\n [210.0, 426.0, 648.0, 666.0, 684.0, 462.0, 234.0],\n [240.0, 486.0, 738.0, 756.0, 774.0, 522.0, 264.0],\n [170.0, 344.0, 522.0, 534.0, 546.0, 368.0, 186.0],\n [90.0, 182.0, 276.0, 282.0, 288.0, 194.0, 98.0],\n ],\n [\n [40.0, 81.0, 123.0, 126.0, 129.0, 87.0, 44.0],\n [85.0, 172.0, 261.0, 267.0, 273.0, 184.0, 93.0],\n [135.0, 273.0, 414.0, 423.0, 432.0, 291.0, 147.0],\n [150.0, 303.0, 459.0, 468.0, 477.0, 321.0, 162.0],\n [105.0, 212.0, 321.0, 327.0, 333.0, 224.0, 113.0],\n [55.0, 111.0, 168.0, 171.0, 174.0, 117.0, 59.0],\n ],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_3d\")" }, { "summary": "convtranspose_attributes", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\ny = np.array(\n [\n [\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0], # (1, 2, 10, 8)\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n ],\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], strides=[3, 2], output_shape=[10, 8]\n)\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_output_shape\")\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], strides=[3, 2], output_padding=[1, 1]\n)\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_pad\")\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\",\n [\"X\", \"W\"],\n [\"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], name=\"test_convtranspose_kernel_shape\")" }, { "summary": "convtranspose_autopad_same", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], auto_pad=\"SAME_UPPER\", strides=[2, 2]\n)\n\ny = np.array(\n [\n [\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [3.0, 3.0, 8.0, 5.0, 12.0, 7.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0],\n [9.0, 9.0, 20.0, 11.0, 24.0, 13.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0],\n ],\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [3.0, 3.0, 8.0, 5.0, 12.0, 7.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0],\n [9.0, 9.0, 20.0, 11.0, 24.0, 13.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_autopad_same\")" }, { "summary": "convtranspose_dilations", "code": "x = np.array(\n [[[[3.0, 8.0, 1.0], [9.0, 5.0, 7.0], [3.0, 2.0, 6.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\nW = np.array([[[[7.0, 2.0], [1.0, 9.0]]]]).astype(np.float32) # (1, 1, 2, 2)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], dilations=[2, 2]\n)\n\ny = np.array(\n [\n [\n [\n [21.0, 56.0, 13.0, 16.0, 2.0], # [1, 1, 5, 5]\n [63.0, 35.0, 67.0, 10.0, 14.0],\n [24.0, 22.0, 76.0, 76.0, 21.0],\n [9.0, 5.0, 88.0, 45.0, 63.0],\n [3.0, 2.0, 33.0, 18.0, 54.0],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_dilations\")" }, { "summary": "convtranspose_group_2", "code": "x = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], group=2)\n\ny = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_group_2\")" }, { "summary": "convtranspose_group_2_image_3", "code": "x = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ],\n [\n [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0], [24.0, 25.0, 26.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ],\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ],\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], group=2)\n\ny = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ],\n [\n [\n [18.0, 37.0, 57.0, 39.0, 20.0],\n [39.0, 80.0, 123.0, 84.0, 43.0],\n [63.0, 129.0, 198.0, 135.0, 69.0],\n [45.0, 92.0, 141.0, 96.0, 49.0],\n [24.0, 49.0, 75.0, 51.0, 26.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ],\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ],\n ]\n).astype(np.float32)\n\nexpect(\n node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_group_2_image_3\"\n)" }, { "summary": "convtranspose_pads", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], strides=[3, 2], pads=[1, 2, 1, 2]\n)\n\ny = np.array(\n [\n [\n [\n [1.0, 1.0, 3.0], # (1, 2, 7, 3)\n [1.0, 1.0, 3.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [13.0, 7.0, 15.0],\n [13.0, 7.0, 15.0],\n ],\n [\n [1.0, 1.0, 3.0],\n [1.0, 1.0, 3.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [13.0, 7.0, 15.0],\n [13.0, 7.0, 15.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_pads\")" } ], "category": "Layer" }, { "name": "ConvTranspose", "module": "ai.onnx", "version": 11, "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. Note that the output_shape attribute value should not include dimensions for batch size and channels, which are automatically inferred." }, { "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(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0], # (1, 2, 5, 5)\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose\")" }, { "summary": "convtranspose_1d", "code": "x = np.array([[[0.0, 1.0, 2.0]]]).astype(np.float32) # (1, 1, 3)\n\nW = np.array([[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]]).astype( # (1, 2, 3)\n np.float32\n)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array(\n [[[0.0, 1.0, 3.0, 3.0, 2.0], [0.0, 1.0, 3.0, 3.0, 2.0]]] # (1, 2, 5)\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_1d\")" }, { "summary": "convtranspose_3d", "code": "x = np.array(\n [\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 3, 4, 5)\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n ],\n [\n [20.0, 21.0, 22.0, 23.0, 24.0],\n [25.0, 26.0, 27.0, 28.0, 29.0],\n [30.0, 31.0, 32.0, 33.0, 34.0],\n [35.0, 36.0, 37.0, 38.0, 39.0],\n ],\n [\n [40.0, 41.0, 42.0, 43.0, 44.0],\n [45.0, 46.0, 47.0, 48.0, 49.0],\n [50.0, 51.0, 52.0, 53.0, 54.0],\n [55.0, 56.0, 57.0, 58.0, 59.0],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 2, 3, 3, 3)\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array(\n [\n [\n [\n [\n [0.0, 1.0, 3.0, 6.0, 9.0, 7.0, 4.0], # (1, 2, 5, 6, 7)\n [5.0, 12.0, 21.0, 27.0, 33.0, 24.0, 13.0],\n [15.0, 33.0, 54.0, 63.0, 72.0, 51.0, 27.0],\n [30.0, 63.0, 99.0, 108.0, 117.0, 81.0, 42.0],\n [25.0, 52.0, 81.0, 87.0, 93.0, 64.0, 33.0],\n [15.0, 31.0, 48.0, 51.0, 54.0, 37.0, 19.0],\n ],\n [\n [20.0, 42.0, 66.0, 72.0, 78.0, 54.0, 28.0],\n [50.0, 104.0, 162.0, 174.0, 186.0, 128.0, 66.0],\n [90.0, 186.0, 288.0, 306.0, 324.0, 222.0, 114.0],\n [120.0, 246.0, 378.0, 396.0, 414.0, 282.0, 144.0],\n [90.0, 184.0, 282.0, 294.0, 306.0, 208.0, 106.0],\n [50.0, 102.0, 156.0, 162.0, 168.0, 114.0, 58.0],\n ],\n [\n [60.0, 123.0, 189.0, 198.0, 207.0, 141.0, 72.0],\n [135.0, 276.0, 423.0, 441.0, 459.0, 312.0, 159.0],\n [225.0, 459.0, 702.0, 729.0, 756.0, 513.0, 261.0],\n [270.0, 549.0, 837.0, 864.0, 891.0, 603.0, 306.0],\n [195.0, 396.0, 603.0, 621.0, 639.0, 432.0, 219.0],\n [105.0, 213.0, 324.0, 333.0, 342.0, 231.0, 117.0],\n ],\n [\n [60.0, 122.0, 186.0, 192.0, 198.0, 134.0, 68.0],\n [130.0, 264.0, 402.0, 414.0, 426.0, 288.0, 146.0],\n [210.0, 426.0, 648.0, 666.0, 684.0, 462.0, 234.0],\n [240.0, 486.0, 738.0, 756.0, 774.0, 522.0, 264.0],\n [170.0, 344.0, 522.0, 534.0, 546.0, 368.0, 186.0],\n [90.0, 182.0, 276.0, 282.0, 288.0, 194.0, 98.0],\n ],\n [\n [40.0, 81.0, 123.0, 126.0, 129.0, 87.0, 44.0],\n [85.0, 172.0, 261.0, 267.0, 273.0, 184.0, 93.0],\n [135.0, 273.0, 414.0, 423.0, 432.0, 291.0, 147.0],\n [150.0, 303.0, 459.0, 468.0, 477.0, 321.0, 162.0],\n [105.0, 212.0, 321.0, 327.0, 333.0, 224.0, 113.0],\n [55.0, 111.0, 168.0, 171.0, 174.0, 117.0, 59.0],\n ],\n ],\n [\n [\n [0.0, 1.0, 3.0, 6.0, 9.0, 7.0, 4.0],\n [5.0, 12.0, 21.0, 27.0, 33.0, 24.0, 13.0],\n [15.0, 33.0, 54.0, 63.0, 72.0, 51.0, 27.0],\n [30.0, 63.0, 99.0, 108.0, 117.0, 81.0, 42.0],\n [25.0, 52.0, 81.0, 87.0, 93.0, 64.0, 33.0],\n [15.0, 31.0, 48.0, 51.0, 54.0, 37.0, 19.0],\n ],\n [\n [20.0, 42.0, 66.0, 72.0, 78.0, 54.0, 28.0],\n [50.0, 104.0, 162.0, 174.0, 186.0, 128.0, 66.0],\n [90.0, 186.0, 288.0, 306.0, 324.0, 222.0, 114.0],\n [120.0, 246.0, 378.0, 396.0, 414.0, 282.0, 144.0],\n [90.0, 184.0, 282.0, 294.0, 306.0, 208.0, 106.0],\n [50.0, 102.0, 156.0, 162.0, 168.0, 114.0, 58.0],\n ],\n [\n [60.0, 123.0, 189.0, 198.0, 207.0, 141.0, 72.0],\n [135.0, 276.0, 423.0, 441.0, 459.0, 312.0, 159.0],\n [225.0, 459.0, 702.0, 729.0, 756.0, 513.0, 261.0],\n [270.0, 549.0, 837.0, 864.0, 891.0, 603.0, 306.0],\n [195.0, 396.0, 603.0, 621.0, 639.0, 432.0, 219.0],\n [105.0, 213.0, 324.0, 333.0, 342.0, 231.0, 117.0],\n ],\n [\n [60.0, 122.0, 186.0, 192.0, 198.0, 134.0, 68.0],\n [130.0, 264.0, 402.0, 414.0, 426.0, 288.0, 146.0],\n [210.0, 426.0, 648.0, 666.0, 684.0, 462.0, 234.0],\n [240.0, 486.0, 738.0, 756.0, 774.0, 522.0, 264.0],\n [170.0, 344.0, 522.0, 534.0, 546.0, 368.0, 186.0],\n [90.0, 182.0, 276.0, 282.0, 288.0, 194.0, 98.0],\n ],\n [\n [40.0, 81.0, 123.0, 126.0, 129.0, 87.0, 44.0],\n [85.0, 172.0, 261.0, 267.0, 273.0, 184.0, 93.0],\n [135.0, 273.0, 414.0, 423.0, 432.0, 291.0, 147.0],\n [150.0, 303.0, 459.0, 468.0, 477.0, 321.0, 162.0],\n [105.0, 212.0, 321.0, 327.0, 333.0, 224.0, 113.0],\n [55.0, 111.0, 168.0, 171.0, 174.0, 117.0, 59.0],\n ],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_3d\")" }, { "summary": "convtranspose_attributes", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\ny = np.array(\n [\n [\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0], # (1, 2, 10, 8)\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n ],\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], strides=[3, 2], output_shape=[10, 8]\n)\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_output_shape\")\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], strides=[3, 2], output_padding=[1, 1]\n)\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_pad\")\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\",\n [\"X\", \"W\"],\n [\"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], name=\"test_convtranspose_kernel_shape\")" }, { "summary": "convtranspose_autopad_same", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], auto_pad=\"SAME_UPPER\", strides=[2, 2]\n)\n\ny = np.array(\n [\n [\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [3.0, 3.0, 8.0, 5.0, 12.0, 7.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0],\n [9.0, 9.0, 20.0, 11.0, 24.0, 13.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0],\n ],\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [3.0, 3.0, 8.0, 5.0, 12.0, 7.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0],\n [9.0, 9.0, 20.0, 11.0, 24.0, 13.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_autopad_same\")" }, { "summary": "convtranspose_dilations", "code": "x = np.array(\n [[[[3.0, 8.0, 1.0], [9.0, 5.0, 7.0], [3.0, 2.0, 6.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\nW = np.array([[[[7.0, 2.0], [1.0, 9.0]]]]).astype(np.float32) # (1, 1, 2, 2)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], dilations=[2, 2]\n)\n\ny = np.array(\n [\n [\n [\n [21.0, 56.0, 13.0, 16.0, 2.0], # [1, 1, 5, 5]\n [63.0, 35.0, 67.0, 10.0, 14.0],\n [24.0, 22.0, 76.0, 76.0, 21.0],\n [9.0, 5.0, 88.0, 45.0, 63.0],\n [3.0, 2.0, 33.0, 18.0, 54.0],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_dilations\")" }, { "summary": "convtranspose_group_2", "code": "x = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], group=2)\n\ny = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_group_2\")" }, { "summary": "convtranspose_group_2_image_3", "code": "x = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ],\n [\n [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0], [24.0, 25.0, 26.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ],\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ],\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], group=2)\n\ny = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ],\n [\n [\n [18.0, 37.0, 57.0, 39.0, 20.0],\n [39.0, 80.0, 123.0, 84.0, 43.0],\n [63.0, 129.0, 198.0, 135.0, 69.0],\n [45.0, 92.0, 141.0, 96.0, 49.0],\n [24.0, 49.0, 75.0, 51.0, 26.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ],\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ],\n ]\n).astype(np.float32)\n\nexpect(\n node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_group_2_image_3\"\n)" }, { "summary": "convtranspose_pads", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], strides=[3, 2], pads=[1, 2, 1, 2]\n)\n\ny = np.array(\n [\n [\n [\n [1.0, 1.0, 3.0], # (1, 2, 7, 3)\n [1.0, 1.0, 3.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [13.0, 7.0, 15.0],\n [13.0, 7.0, 15.0],\n ],\n [\n [1.0, 1.0, 3.0],\n [1.0, 1.0, 3.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [13.0, 7.0, 15.0],\n [13.0, 7.0, 15.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_pads\")" } ], "category": "Layer" }, { "name": "ConvTranspose", "module": "ai.onnx", "version": 22, "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. Note that the output_shape attribute value should not include dimensions for batch size and channels, which are automatically inferred." }, { "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "convtranspose", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0], # (1, 2, 5, 5)\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose\")" }, { "summary": "convtranspose_1d", "code": "x = np.array([[[0.0, 1.0, 2.0]]]).astype(np.float32) # (1, 1, 3)\n\nW = np.array([[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]]).astype( # (1, 2, 3)\n np.float32\n)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array(\n [[[0.0, 1.0, 3.0, 3.0, 2.0], [0.0, 1.0, 3.0, 3.0, 2.0]]] # (1, 2, 5)\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_1d\")" }, { "summary": "convtranspose_3d", "code": "x = np.array(\n [\n [\n [\n [\n [0.0, 1.0, 2.0, 3.0, 4.0], # (1, 1, 3, 4, 5)\n [5.0, 6.0, 7.0, 8.0, 9.0],\n [10.0, 11.0, 12.0, 13.0, 14.0],\n [15.0, 16.0, 17.0, 18.0, 19.0],\n ],\n [\n [20.0, 21.0, 22.0, 23.0, 24.0],\n [25.0, 26.0, 27.0, 28.0, 29.0],\n [30.0, 31.0, 32.0, 33.0, 34.0],\n [35.0, 36.0, 37.0, 38.0, 39.0],\n ],\n [\n [40.0, 41.0, 42.0, 43.0, 44.0],\n [45.0, 46.0, 47.0, 48.0, 49.0],\n [50.0, 51.0, 52.0, 53.0, 54.0],\n [55.0, 56.0, 57.0, 58.0, 59.0],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [\n [\n [1.0, 1.0, 1.0], # (1, 2, 3, 3, 3)\n [1.0, 1.0, 1.0],\n [1.0, 1.0, 1.0],\n ],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"])\n\ny = np.array(\n [\n [\n [\n [\n [0.0, 1.0, 3.0, 6.0, 9.0, 7.0, 4.0], # (1, 2, 5, 6, 7)\n [5.0, 12.0, 21.0, 27.0, 33.0, 24.0, 13.0],\n [15.0, 33.0, 54.0, 63.0, 72.0, 51.0, 27.0],\n [30.0, 63.0, 99.0, 108.0, 117.0, 81.0, 42.0],\n [25.0, 52.0, 81.0, 87.0, 93.0, 64.0, 33.0],\n [15.0, 31.0, 48.0, 51.0, 54.0, 37.0, 19.0],\n ],\n [\n [20.0, 42.0, 66.0, 72.0, 78.0, 54.0, 28.0],\n [50.0, 104.0, 162.0, 174.0, 186.0, 128.0, 66.0],\n [90.0, 186.0, 288.0, 306.0, 324.0, 222.0, 114.0],\n [120.0, 246.0, 378.0, 396.0, 414.0, 282.0, 144.0],\n [90.0, 184.0, 282.0, 294.0, 306.0, 208.0, 106.0],\n [50.0, 102.0, 156.0, 162.0, 168.0, 114.0, 58.0],\n ],\n [\n [60.0, 123.0, 189.0, 198.0, 207.0, 141.0, 72.0],\n [135.0, 276.0, 423.0, 441.0, 459.0, 312.0, 159.0],\n [225.0, 459.0, 702.0, 729.0, 756.0, 513.0, 261.0],\n [270.0, 549.0, 837.0, 864.0, 891.0, 603.0, 306.0],\n [195.0, 396.0, 603.0, 621.0, 639.0, 432.0, 219.0],\n [105.0, 213.0, 324.0, 333.0, 342.0, 231.0, 117.0],\n ],\n [\n [60.0, 122.0, 186.0, 192.0, 198.0, 134.0, 68.0],\n [130.0, 264.0, 402.0, 414.0, 426.0, 288.0, 146.0],\n [210.0, 426.0, 648.0, 666.0, 684.0, 462.0, 234.0],\n [240.0, 486.0, 738.0, 756.0, 774.0, 522.0, 264.0],\n [170.0, 344.0, 522.0, 534.0, 546.0, 368.0, 186.0],\n [90.0, 182.0, 276.0, 282.0, 288.0, 194.0, 98.0],\n ],\n [\n [40.0, 81.0, 123.0, 126.0, 129.0, 87.0, 44.0],\n [85.0, 172.0, 261.0, 267.0, 273.0, 184.0, 93.0],\n [135.0, 273.0, 414.0, 423.0, 432.0, 291.0, 147.0],\n [150.0, 303.0, 459.0, 468.0, 477.0, 321.0, 162.0],\n [105.0, 212.0, 321.0, 327.0, 333.0, 224.0, 113.0],\n [55.0, 111.0, 168.0, 171.0, 174.0, 117.0, 59.0],\n ],\n ],\n [\n [\n [0.0, 1.0, 3.0, 6.0, 9.0, 7.0, 4.0],\n [5.0, 12.0, 21.0, 27.0, 33.0, 24.0, 13.0],\n [15.0, 33.0, 54.0, 63.0, 72.0, 51.0, 27.0],\n [30.0, 63.0, 99.0, 108.0, 117.0, 81.0, 42.0],\n [25.0, 52.0, 81.0, 87.0, 93.0, 64.0, 33.0],\n [15.0, 31.0, 48.0, 51.0, 54.0, 37.0, 19.0],\n ],\n [\n [20.0, 42.0, 66.0, 72.0, 78.0, 54.0, 28.0],\n [50.0, 104.0, 162.0, 174.0, 186.0, 128.0, 66.0],\n [90.0, 186.0, 288.0, 306.0, 324.0, 222.0, 114.0],\n [120.0, 246.0, 378.0, 396.0, 414.0, 282.0, 144.0],\n [90.0, 184.0, 282.0, 294.0, 306.0, 208.0, 106.0],\n [50.0, 102.0, 156.0, 162.0, 168.0, 114.0, 58.0],\n ],\n [\n [60.0, 123.0, 189.0, 198.0, 207.0, 141.0, 72.0],\n [135.0, 276.0, 423.0, 441.0, 459.0, 312.0, 159.0],\n [225.0, 459.0, 702.0, 729.0, 756.0, 513.0, 261.0],\n [270.0, 549.0, 837.0, 864.0, 891.0, 603.0, 306.0],\n [195.0, 396.0, 603.0, 621.0, 639.0, 432.0, 219.0],\n [105.0, 213.0, 324.0, 333.0, 342.0, 231.0, 117.0],\n ],\n [\n [60.0, 122.0, 186.0, 192.0, 198.0, 134.0, 68.0],\n [130.0, 264.0, 402.0, 414.0, 426.0, 288.0, 146.0],\n [210.0, 426.0, 648.0, 666.0, 684.0, 462.0, 234.0],\n [240.0, 486.0, 738.0, 756.0, 774.0, 522.0, 264.0],\n [170.0, 344.0, 522.0, 534.0, 546.0, 368.0, 186.0],\n [90.0, 182.0, 276.0, 282.0, 288.0, 194.0, 98.0],\n ],\n [\n [40.0, 81.0, 123.0, 126.0, 129.0, 87.0, 44.0],\n [85.0, 172.0, 261.0, 267.0, 273.0, 184.0, 93.0],\n [135.0, 273.0, 414.0, 423.0, 432.0, 291.0, 147.0],\n [150.0, 303.0, 459.0, 468.0, 477.0, 321.0, 162.0],\n [105.0, 212.0, 321.0, 327.0, 333.0, 224.0, 113.0],\n [55.0, 111.0, 168.0, 171.0, 174.0, 117.0, 59.0],\n ],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_3d\")" }, { "summary": "convtranspose_attributes", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\ny = np.array(\n [\n [\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0], # (1, 2, 10, 8)\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n ],\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],\n [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], strides=[3, 2], output_shape=[10, 8]\n)\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_output_shape\")\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], strides=[3, 2], output_padding=[1, 1]\n)\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_pad\")\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\",\n [\"X\", \"W\"],\n [\"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], name=\"test_convtranspose_kernel_shape\")" }, { "summary": "convtranspose_autopad_same", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], auto_pad=\"SAME_UPPER\", strides=[2, 2]\n)\n\ny = np.array(\n [\n [\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [3.0, 3.0, 8.0, 5.0, 12.0, 7.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0],\n [9.0, 9.0, 20.0, 11.0, 24.0, 13.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0],\n ],\n [\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],\n [3.0, 3.0, 8.0, 5.0, 12.0, 7.0],\n [3.0, 3.0, 7.0, 4.0, 9.0, 5.0],\n [9.0, 9.0, 20.0, 11.0, 24.0, 13.0],\n [6.0, 6.0, 13.0, 7.0, 15.0, 8.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_autopad_same\")" }, { "summary": "convtranspose_dilations", "code": "x = np.array(\n [[[[3.0, 8.0, 1.0], [9.0, 5.0, 7.0], [3.0, 2.0, 6.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\nW = np.array([[[[7.0, 2.0], [1.0, 9.0]]]]).astype(np.float32) # (1, 1, 2, 2)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], dilations=[2, 2]\n)\n\ny = np.array(\n [\n [\n [\n [21.0, 56.0, 13.0, 16.0, 2.0], # [1, 1, 5, 5]\n [63.0, 35.0, 67.0, 10.0, 14.0],\n [24.0, 22.0, 76.0, 76.0, 21.0],\n [9.0, 5.0, 88.0, 45.0, 63.0],\n [3.0, 2.0, 33.0, 18.0, 54.0],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_dilations\")" }, { "summary": "convtranspose_group_2", "code": "x = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ]\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], group=2)\n\ny = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_group_2\")" }, { "summary": "convtranspose_group_2_image_3", "code": "x = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ],\n [\n [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0], [24.0, 25.0, 26.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ],\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0], [15.0, 16.0, 17.0]],\n ],\n ]\n).astype(np.float32)\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], group=2)\n\ny = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ],\n [\n [\n [18.0, 37.0, 57.0, 39.0, 20.0],\n [39.0, 80.0, 123.0, 84.0, 43.0],\n [63.0, 129.0, 198.0, 135.0, 69.0],\n [45.0, 92.0, 141.0, 96.0, 49.0],\n [24.0, 49.0, 75.0, 51.0, 26.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ],\n [\n [\n [0.0, 1.0, 3.0, 3.0, 2.0],\n [3.0, 8.0, 15.0, 12.0, 7.0],\n [9.0, 21.0, 36.0, 27.0, 15.0],\n [9.0, 20.0, 33.0, 24.0, 13.0],\n [6.0, 13.0, 21.0, 15.0, 8.0],\n ],\n [\n [9.0, 19.0, 30.0, 21.0, 11.0],\n [21.0, 44.0, 69.0, 48.0, 25.0],\n [36.0, 75.0, 117.0, 81.0, 42.0],\n [27.0, 56.0, 87.0, 60.0, 31.0],\n [15.0, 31.0, 48.0, 33.0, 17.0],\n ],\n ],\n ]\n).astype(np.float32)\n\nexpect(\n node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_group_2_image_3\"\n)" }, { "summary": "convtranspose_pads", "code": "x = np.array(\n [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]] # (1, 1, 3, 3)\n).astype(np.float32)\n\nW = np.array(\n [\n [\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], # (1, 2, 3, 3)\n [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],\n ]\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"ConvTranspose\", [\"X\", \"W\"], [\"Y\"], strides=[3, 2], pads=[1, 2, 1, 2]\n)\n\ny = np.array(\n [\n [\n [\n [1.0, 1.0, 3.0], # (1, 2, 7, 3)\n [1.0, 1.0, 3.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [13.0, 7.0, 15.0],\n [13.0, 7.0, 15.0],\n ],\n [\n [1.0, 1.0, 3.0],\n [1.0, 1.0, 3.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [7.0, 4.0, 9.0],\n [13.0, 7.0, 15.0],\n [13.0, 7.0, 15.0],\n ],\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x, W], outputs=[y], name=\"test_convtranspose_pads\")" } ], "category": "Layer" }, { "name": "ConvTranspose", "module": "com.ms.internal.nhwc", "version": 1, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Layer" }, { "name": "ConvTranspose", "module": "com.ms.internal.nhwc", "version": 11, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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. Note that the output_shape attribute value should not include dimensions for batch size and channels, which are automatically inferred." }, { "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)" ] } ], "category": "Layer" }, { "name": "ConvTransposeWithDynamicPads", "module": "com.microsoft", "version": 1, "attributes": [ { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "name": "dilations", "type": "int64[]", "required": false }, { "name": "group", "type": "int64", "required": false, "default": 1 }, { "name": "kernel_shape", "type": "int64[]", "required": false }, { "name": "output_padding", "type": "int64[]", "required": false }, { "name": "strides", "type": "int64[]", "required": false } ], "inputs": [ { "name": "X", "type": "T" }, { "name": "W", "type": "T" }, { "name": "Pads", "type": "tensor(int64)", "option": "optional" }, { "name": "B", "type": "T", "option": "optional" } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "Y", "type": "T" } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain input and output types to float tensors", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)" ] } ] }, { "name": "Cos", "module": "ai.onnx", "version": 7, "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], 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], name=\"test_cos\")" } ] }, { "name": "Cos", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_cos\")" } ] }, { "name": "Cosh", "module": "ai.onnx", "version": 9, "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], 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], name=\"test_cosh\")" } ] }, { "name": "Cosh", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_cosh\")" } ] }, { "name": "Crop", "module": "ai.onnx", "version": 1, "description": "Crop and image to the specified spatial dimensions. If scale is given,\nthen optionally start the crop offset by the left/top border amounts.\nIf scale is not provided, crop the borders as provided.", "attributes": [ { "name": "border", "type": "int64[]", "required": false, "description": "A 1-D values of (leftBorder, topBorder, rightBorder, bottomBorder)." }, { "name": "scale", "type": "int64[]", "required": false, "description": "A 1-D values of (height, width)." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor of shape [N,C,H,W]" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "Result, has same type as input, with H and W dimensions reduced." } ], "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": "Data" }, { "name": "Crop", "module": "ai.onnx", "version": 10, "description": "Crop and image to the specified spatial dimensions. If scale is given,\nthen optionally start the crop offset by the left/top border amounts.\nIf scale is not provided, crop the borders as provided.", "attributes": [ { "name": "border", "type": "int64[]", "required": true, "description": "A 1-D values of (leftBorder, topBorder, rightBorder, bottomBorder)." }, { "name": "scale", "type": "int64[]", "required": false, "description": "A 1-D values of (height, width)." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor of shape [N,C,H,W]" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "Result, has same type as input, with H and W dimensions reduced." } ], "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": "Data" }, { "name": "CropAndResize", "module": "com.microsoft", "version": 1, "description": "Extracts crops from the input image tensor and resizes them using bilinear sampling or nearest neighbor sampling\n (possibly with aspect ratio change) to a common output size specified by crop_height and crop_width.\n Returns a tensor with crops from the input image at positions defined at the bounding box locations in boxes.\n The cropped boxes are all resized (with bilinear or nearest neighbor interpolation) to\n a fixed size = [crop_height, crop_width]. The result is a 4-D tensor [num_boxes, crop_height, crop_width, depth].\n The resizing is corner aligned.", "attributes": [ { "name": "extrapolation_value", "type": "float32", "required": false, "description": "Value used for extrapolation, when applicable. Default is 0.0f. " }, { "name": "mode", "type": "string", "required": false, "default": "bilinear", "description": "The pooling method. Two modes are supported: 'bilinear' and 'nearest'. Default is 'bilinear'." } ], "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 [[y1, x1, y2, x2], ...]. The RoIs' coordinates are normalized 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." }, { "name": "crop_size", "type": "T2", "description": "1-D tensor of 2 elements: [crop_height, crop_width]. All cropped image patches are resized to this size. Both crop_height and crop_width need to be positive." } ], "min_input": 4, "max_input": 4, "outputs": [ { "name": "Y", "type": "T1", "description": "RoI pooled output, 4-D tensor of shape (num_rois, C, crop_height, crop_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(int32)" ] } ] }, { "name": "CumProd", "module": "ai.onnx", "version": 26, "description": "Performs cumulative product of the input elements along the given axis.\nBy default, it will do the product 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 product 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, 2, 6]\nexclusive=1\noutput = [1, 1, 2]\nexclusive=0\nreverse=1\noutput = [6, 6, 3]\nexclusive=1\nreverse=1\noutput = [6, 3, 1]\n```\n ", "attributes": [ { "name": "exclusive", "type": "int64", "required": false, "description": "If set to 1 will return exclusive product in which the top element is not included. In other terms, if set to 1, the j-th output element would be the product of the first (j-1) elements. Otherwise, it would be the product of the first j elements." }, { "name": "reverse", "type": "int64", "required": false, "description": "If set to 1 will perform the products 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 products of the x's elements" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to 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": "cumprod_1d", "code": "node = onnx.helper.make_node(\"CumProd\", inputs=[\"x\", \"axis\"], outputs=[\"y\"])\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.array(0, dtype=np.int32)\ny = np.array([1.0, 2.0, 6.0, 24.0, 120.0]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumprod_1d\")" }, { "summary": "cumprod_1d_exclusive", "code": "node = onnx.helper.make_node(\n \"CumProd\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], exclusive=1\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.array(0, dtype=np.int32)\ny = np.array([1.0, 1.0, 2.0, 6.0, 24.0]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumprod_1d_exclusive\")" }, { "summary": "cumprod_1d_int32_exclusive", "code": "node = onnx.helper.make_node(\n \"CumProd\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], exclusive=1\n)\nx = np.array([1, 2, 3, 4, 5]).astype(np.int32)\naxis = np.array(0, dtype=np.int32)\ny = np.array([1, 1, 2, 6, 24]).astype(np.int32)\nexpect(\n node, inputs=[x, axis], outputs=[y], name=\"test_cumprod_1d_int32_exclusive\"\n)" }, { "summary": "cumprod_1d_reverse", "code": "node = onnx.helper.make_node(\n \"CumProd\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], reverse=1\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.array(0, dtype=np.int32)\ny = np.array([120.0, 120.0, 60.0, 20.0, 5.0]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumprod_1d_reverse\")" }, { "summary": "cumprod_1d_reverse_exclusive", "code": "node = onnx.helper.make_node(\n \"CumProd\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], reverse=1, exclusive=1\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.array(0, dtype=np.int32)\ny = np.array([120.0, 60.0, 20.0, 5.0, 1.0]).astype(np.float64)\nexpect(\n node,\n inputs=[x, axis],\n outputs=[y],\n name=\"test_cumprod_1d_reverse_exclusive\",\n)" }, { "summary": "cumprod_2d_axis_0", "code": "node = onnx.helper.make_node(\n \"CumProd\",\n inputs=[\"x\", \"axis\"],\n outputs=[\"y\"],\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))\naxis = np.array(0, dtype=np.int32)\ny = (\n np.array([1.0, 2.0, 3.0, 4.0, 10.0, 18.0])\n .astype(np.float64)\n .reshape((2, 3))\n)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumprod_2d_axis_0\")" }, { "summary": "cumprod_2d_axis_1", "code": "node = onnx.helper.make_node(\n \"CumProd\",\n inputs=[\"x\", \"axis\"],\n outputs=[\"y\"],\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))\naxis = np.array(1, dtype=np.int32)\ny = (\n np.array([1.0, 2.0, 6.0, 4.0, 20.0, 120.0])\n .astype(np.float64)\n .reshape((2, 3))\n)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumprod_2d_axis_1\")" }, { "summary": "cumprod_2d_int32", "code": "node = onnx.helper.make_node(\n \"CumProd\",\n inputs=[\"x\", \"axis\"],\n outputs=[\"y\"],\n)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.int32).reshape((2, 3))\naxis = np.array(0, dtype=np.int32)\ny = np.array([1, 2, 3, 4, 10, 18]).astype(np.int32).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumprod_2d_int32\")" }, { "summary": "cumprod_2d_negative_axis", "code": "node = onnx.helper.make_node(\n \"CumProd\",\n inputs=[\"x\", \"axis\"],\n outputs=[\"y\"],\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))\naxis = np.array(-1, dtype=np.int32)\ny = (\n np.array([1.0, 2.0, 6.0, 4.0, 20.0, 120.0])\n .astype(np.float64)\n .reshape((2, 3))\n)\nexpect(\n node, inputs=[x, axis], outputs=[y], name=\"test_cumprod_2d_negative_axis\"\n)" } ] }, { "name": "CumSum", "module": "ai.onnx", "version": 11, "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(\"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"])\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([1.0, 3.0, 6.0, 10.0, 15.0]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d\")" }, { "summary": "cumsum_1d_exclusive", "code": "node = onnx.helper.make_node(\n \"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], exclusive=1\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([0.0, 1.0, 3.0, 6.0, 10.0]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d_exclusive\")" }, { "summary": "cumsum_1d_int32_exclusive", "code": "node = onnx.helper.make_node(\n \"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], exclusive=1\n)\nx = np.array([1, 2, 3, 4, 5]).astype(np.int32)\naxis = np.int32(0)\ny = np.array([0, 1, 3, 6, 10]).astype(np.int32)\nexpect(\n node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d_int32_exclusive\"\n)" }, { "summary": "cumsum_1d_reverse", "code": "node = onnx.helper.make_node(\n \"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], reverse=1\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([15.0, 14.0, 12.0, 9.0, 5.0]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d_reverse\")" }, { "summary": "cumsum_1d_reverse_exclusive", "code": "node = onnx.helper.make_node(\n \"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], reverse=1, exclusive=1\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([14.0, 12.0, 9.0, 5.0, 0.0]).astype(np.float64)\nexpect(\n node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d_reverse_exclusive\"\n)" }, { "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.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))\naxis = np.int32(0)\ny = np.array([1.0, 2.0, 3.0, 5.0, 7.0, 9.0]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y], 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.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))\naxis = np.int32(1)\ny = np.array([1.0, 3.0, 6.0, 4.0, 9.0, 15.0]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_2d_axis_1\")" }, { "summary": "cumsum_2d_int32", "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.int32).reshape((2, 3))\naxis = np.int32(0)\ny = np.array([1, 2, 3, 5, 7, 9]).astype(np.int32).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_2d_int32\")" }, { "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.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))\naxis = np.int32(-1)\ny = np.array([1.0, 3.0, 6.0, 4.0, 9.0, 15.0]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_2d_negative_axis\")" } ] }, { "name": "CumSum", "module": "ai.onnx", "version": 14, "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 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(\"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"])\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([1.0, 3.0, 6.0, 10.0, 15.0]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d\")" }, { "summary": "cumsum_1d_exclusive", "code": "node = onnx.helper.make_node(\n \"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], exclusive=1\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([0.0, 1.0, 3.0, 6.0, 10.0]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d_exclusive\")" }, { "summary": "cumsum_1d_int32_exclusive", "code": "node = onnx.helper.make_node(\n \"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], exclusive=1\n)\nx = np.array([1, 2, 3, 4, 5]).astype(np.int32)\naxis = np.int32(0)\ny = np.array([0, 1, 3, 6, 10]).astype(np.int32)\nexpect(\n node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d_int32_exclusive\"\n)" }, { "summary": "cumsum_1d_reverse", "code": "node = onnx.helper.make_node(\n \"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], reverse=1\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([15.0, 14.0, 12.0, 9.0, 5.0]).astype(np.float64)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d_reverse\")" }, { "summary": "cumsum_1d_reverse_exclusive", "code": "node = onnx.helper.make_node(\n \"CumSum\", inputs=[\"x\", \"axis\"], outputs=[\"y\"], reverse=1, exclusive=1\n)\nx = np.array([1.0, 2.0, 3.0, 4.0, 5.0]).astype(np.float64)\naxis = np.int32(0)\ny = np.array([14.0, 12.0, 9.0, 5.0, 0.0]).astype(np.float64)\nexpect(\n node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_1d_reverse_exclusive\"\n)" }, { "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.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))\naxis = np.int32(0)\ny = np.array([1.0, 2.0, 3.0, 5.0, 7.0, 9.0]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y], 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.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))\naxis = np.int32(1)\ny = np.array([1.0, 3.0, 6.0, 4.0, 9.0, 15.0]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_2d_axis_1\")" }, { "summary": "cumsum_2d_int32", "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.int32).reshape((2, 3))\naxis = np.int32(0)\ny = np.array([1, 2, 3, 5, 7, 9]).astype(np.int32).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_2d_int32\")" }, { "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.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float64).reshape((2, 3))\naxis = np.int32(-1)\ny = np.array([1.0, 3.0, 6.0, 4.0, 9.0, 15.0]).astype(np.float64).reshape((2, 3))\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_cumsum_2d_negative_axis\")" } ] }, { "name": "DFT", "module": "ai.onnx", "version": 17, "description": "Computes the discrete Fourier transform of input.", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "The axis on which to perform the DFT. By default this value is set to 1, which corresponds to the first dimension after the batch index. Negative value means counting dimensions from the back. Accepted range is $[-r, -2] \\cup [0, r-2]$ where `r = rank(input)`. The last dimension is for representing complex numbers and thus is an invalid axis." }, { "name": "inverse", "type": "int64", "required": false, "description": "Whether to perform the inverse discrete fourier transform. By default this value is set to 0, which corresponds to false." }, { "name": "onesided", "type": "int64", "required": false, "description": "If onesided is 1, only values for w in [0, 1, 2, ..., floor(n_fft/2) + 1] are used or returned because the real-to-complex Fourier transform satisfies the conjugate symmetry, i.e., X[m, w] = X[m, n_fft-w]*. When onesided=1 and inverse=0 (forward DFT), only real input is supported and a one-sided complex spectrum is returned (RFFT). When onesided=1 and inverse=1 (inverse DFT), only complex input is supported and a full real signal is returned (IRFFT). When invoked with real or complex valued input, the default value is 0. Values can be 0 or 1." } ], "inputs": [ { "name": "input", "type": "T1", "description": "For real input, the following shape is expected: [batch_idx][signal_dim1][signal_dim2]...[signal_dimN][1]. For complex input, the following shape is expected: [batch_idx][signal_dim1][signal_dim2]...[signal_dimN][2]. The first dimension is the batch dimension. The following N dimensions correspond to the signal's dimensions. The final dimension represents the real and imaginary parts of the value in that order." }, { "name": "dft_length", "type": "T2", "option": "optional", "description": "The length of the signal as a scalar. If greater than the axis dimension, the signal will be zero-padded up to dft_length. If less than the axis dimension, only the first dft_length values will be used as the signal. If not provided, the default dft_length = signal_dim_axis, except for the IRFFT case (onesided=1, inverse=1), in which case the default dft_length is 2 * (signal_dim_axis - 1). It's an optional value." } ], "min_input": 1, "max_input": 2, "outputs": [ { "name": "output", "type": "T1", "description": "The Fourier Transform of the input vector. For standard DFT (onesided=0), the output shape is: [batch_idx][signal_dim1][signal_dim2]...[signal_dimN][2] (complex), with signal_dim_axis = dft_length. For RFFT (onesided=1, inverse=0), the output shape is: [batch_idx][signal_dim1][signal_dim2]...[signal_dimN][2] (one-sided complex), with signal_dim_axis = floor(dft_length/2) + 1. For IRFFT (onesided=1, inverse=1), the output shape is: [batch_idx][signal_dim1][signal_dim2]...[signal_dimN][1] (real), where signal_dim_axis = dft_length." } ], "min_output": 1, "max_output": 1, "inputs_range": "1 - 2", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] }, { "description": "Constrain scalar length types to int64_t.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "dft", "code": "node = onnx.helper.make_node(\"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"])\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\naxis = np.array(1, dtype=np.int64)\ny = np.fft.fft(x, axis=0)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft\")\n\nnode = onnx.helper.make_node(\"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"])\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\naxis = np.array(2, dtype=np.int64)\ny = np.fft.fft(x, axis=1)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft_axis\")\n\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"], inverse=1\n)\nx = np.arange(0, 100, dtype=np.complex64).reshape(10, 10)\naxis = np.array(1, dtype=np.int64)\ny = np.fft.ifft(x, axis=0)\n\nx = np.stack((x.real, x.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft_inverse\")\n\n# Test RFFT (Real FFT): real input -> one-sided complex output\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"], onesided=1\n)\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\naxis = np.array(1, dtype=np.int64)\ny = np.fft.rfft(x, axis=0)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 6, 10, 2)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft_rfft\")\n\n# Test IRFFT (Inverse Real FFT): one-sided complex input -> real output\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"], onesided=1, inverse=1\n)\n# Create one-sided complex input (6 bins for signal length 10)\nx = np.fft.rfft(np.arange(0, 100).reshape(10, 10), axis=0).astype(np.complex64)\naxis = np.array(1, dtype=np.int64)\ny = np.fft.irfft(x, n=10, axis=0)\n\nx = np.stack((x.real, x.imag), axis=2).astype(np.float32).reshape(1, 6, 10, 2)\ny = y.reshape(1, 10, 10, 1).astype(np.float32)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft_irfft\")" }, { "summary": "opset19", "code": "node = onnx.helper.make_node(\"DFT\", inputs=[\"x\"], outputs=[\"y\"], axis=1)\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\ny = np.fft.fft(x, axis=0)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)\n\nnode = onnx.helper.make_node(\"DFT\", inputs=[\"x\"], outputs=[\"y\"], axis=2)\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\ny = np.fft.fft(x, axis=1)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_axis_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)\n\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\"], outputs=[\"y\"], inverse=1, axis=1\n)\nx = np.arange(0, 100, dtype=np.complex64).reshape(\n 10,\n 10,\n)\ny = np.fft.ifft(x, axis=0)\n\nx = np.stack((x.real, x.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_inverse_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)\n\n# Test RFFT (Real FFT): real input -> one-sided complex output\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\"], outputs=[\"y\"], onesided=1, axis=1\n)\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\ny = np.fft.rfft(x, axis=0)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 6, 10, 2)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_rfft_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)\n\n# Test IRFFT (Inverse Real FFT): one-sided complex input -> real output\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\"], outputs=[\"y\"], onesided=1, inverse=1, axis=1\n)\n# Create one-sided complex input (6 bins for signal length 10)\nx = np.fft.rfft(np.arange(0, 100).reshape(10, 10), axis=0).astype(np.complex64)\ny = np.fft.irfft(x, n=10, axis=0)\n\nx = np.stack((x.real, x.imag), axis=2).astype(np.float32).reshape(1, 6, 10, 2)\ny = y.reshape(1, 10, 10, 1).astype(np.float32)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_irfft_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)" } ] }, { "name": "DFT", "module": "ai.onnx", "version": 20, "description": "Computes the discrete Fourier Transform (DFT) of the input.\n\nAssuming the input has shape `[M, N]`, where `N` is the dimension over which the\nDFT is computed and `M` denotes the conceptual \"all other dimensions,\"\nthe DFT `y[m, k]` of shape `[M, N]` is defined as\n\n$$y[m, k] = \\sum_{n=0}^{N-1} e^{-2 \\pi j \\frac{k n}{N} } x[m, n] ,$$\n\nand the inverse transform is defined as\n\n$$x[m, n] = \\frac{1}{N} \\sum_{k=0}^{N-1} e^{2 \\pi j \\frac{k n}{N} } y[m, k] ,$$\n\nwhere $j$ is the imaginary unit.\n\nThe actual shape of the output is specified in the \"output\" section.\n\nReference: https://docs.scipy.org/doc/scipy/tutorial/fft.html\n", "attributes": [ { "name": "inverse", "type": "int64", "required": false, "description": "Whether to perform the inverse discrete Fourier Transform. Default is 0, which corresponds to `false`." }, { "name": "onesided", "type": "int64", "required": false, "description": "If `onesided` is `1`, only values for `k` in `[0, 1, 2, ..., floor(n_fft/2) + 1]` are used or returned because the real-to-complex Fourier transform satisfies the conjugate symmetry, i.e., `X[m, k] = X[m, n_fft-k]*`, where `m` denotes \"all other dimensions\" DFT was not applied on. When `onesided=1` and `inverse=0` (forward DFT), only real input is supported and a one-sided complex spectrum is returned (RFFT). When `onesided=1` and `inverse=1` (inverse DFT), only complex input is supported and a full real signal is returned (IRFFT). Value can be `0` or `1`. Default is `0`." } ], "inputs": [ { "name": "input", "type": "T1", "description": "For real input, the following shape is expected: `[signal_dim0][signal_dim1][signal_dim2]...[signal_dimN][1]`. For complex input, the following shape is expected: `[signal_dim0][signal_dim1][signal_dim2]...[signal_dimN][2]`. The final dimension represents the real and imaginary parts of the value in that order." }, { "name": "dft_length", "type": "T2", "option": "optional", "description": "The length of the signal as a scalar. If greater than the axis dimension, the signal will be zero-padded up to `dft_length`. If less than the axis dimension, only the first `dft_length` values will be used as the signal. If not provided, the default `dft_length = signal_dim_axis`, except for the IRFFT case (`onesided=1`, `inverse=1`), in which case the default dft_length is `2 * (signal_dim_axis - 1)`." }, { "name": "axis", "type": "tensor(int64)", "option": "optional", "description": "The axis as a scalar on which to perform the DFT. Default is `-2` (last signal axis). Negative value means counting dimensions from the back. Accepted range is $[-r, -2] \\cup [0, r-2]$ where `r = rank(input)`. The last dimension is for representing complex numbers and thus is an invalid axis." } ], "min_input": 1, "max_input": 3, "outputs": [ { "name": "output", "type": "T1", "description": "The Fourier Transform of the input vector. For standard DFT (`onesided=0`), the output shape is: `[signal_dim0][signal_dim1][signal_dim2]...[signal_dimN][2]` (complex), with `signal_dim_axis = dft_length`. For RFFT (`onesided=1`, `inverse=0`), the output shape is: `[signal_dim0][signal_dim1][signal_dim2]...[signal_dimN][2]` (one-sided complex), with `signal_dim_axis = floor(dft_length/2) + 1`. For IRFFT (`onesided=1`, `inverse=1`), the output shape is: `[signal_dim0][signal_dim1][signal_dim2]...[signal_dimN][1]` (real), where `signal_dim_axis = dft_length`." } ], "min_output": 1, "max_output": 1, "inputs_range": "1 - 3", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] }, { "description": "Constrain scalar length types to integers.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "dft", "code": "node = onnx.helper.make_node(\"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"])\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\naxis = np.array(1, dtype=np.int64)\ny = np.fft.fft(x, axis=0)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft\")\n\nnode = onnx.helper.make_node(\"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"])\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\naxis = np.array(2, dtype=np.int64)\ny = np.fft.fft(x, axis=1)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft_axis\")\n\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"], inverse=1\n)\nx = np.arange(0, 100, dtype=np.complex64).reshape(10, 10)\naxis = np.array(1, dtype=np.int64)\ny = np.fft.ifft(x, axis=0)\n\nx = np.stack((x.real, x.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft_inverse\")\n\n# Test RFFT (Real FFT): real input -> one-sided complex output\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"], onesided=1\n)\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\naxis = np.array(1, dtype=np.int64)\ny = np.fft.rfft(x, axis=0)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 6, 10, 2)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft_rfft\")\n\n# Test IRFFT (Inverse Real FFT): one-sided complex input -> real output\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\", \"\", \"axis\"], outputs=[\"y\"], onesided=1, inverse=1\n)\n# Create one-sided complex input (6 bins for signal length 10)\nx = np.fft.rfft(np.arange(0, 100).reshape(10, 10), axis=0).astype(np.complex64)\naxis = np.array(1, dtype=np.int64)\ny = np.fft.irfft(x, n=10, axis=0)\n\nx = np.stack((x.real, x.imag), axis=2).astype(np.float32).reshape(1, 6, 10, 2)\ny = y.reshape(1, 10, 10, 1).astype(np.float32)\nexpect(node, inputs=[x, axis], outputs=[y], name=\"test_dft_irfft\")" }, { "summary": "opset19", "code": "node = onnx.helper.make_node(\"DFT\", inputs=[\"x\"], outputs=[\"y\"], axis=1)\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\ny = np.fft.fft(x, axis=0)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)\n\nnode = onnx.helper.make_node(\"DFT\", inputs=[\"x\"], outputs=[\"y\"], axis=2)\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\ny = np.fft.fft(x, axis=1)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_axis_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)\n\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\"], outputs=[\"y\"], inverse=1, axis=1\n)\nx = np.arange(0, 100, dtype=np.complex64).reshape(\n 10,\n 10,\n)\ny = np.fft.ifft(x, axis=0)\n\nx = np.stack((x.real, x.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 10, 10, 2)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_inverse_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)\n\n# Test RFFT (Real FFT): real input -> one-sided complex output\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\"], outputs=[\"y\"], onesided=1, axis=1\n)\nx = np.arange(0, 100).reshape(10, 10).astype(np.float32)\ny = np.fft.rfft(x, axis=0)\n\nx = x.reshape(1, 10, 10, 1)\ny = np.stack((y.real, y.imag), axis=2).astype(np.float32).reshape(1, 6, 10, 2)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_rfft_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)\n\n# Test IRFFT (Inverse Real FFT): one-sided complex input -> real output\nnode = onnx.helper.make_node(\n \"DFT\", inputs=[\"x\"], outputs=[\"y\"], onesided=1, inverse=1, axis=1\n)\n# Create one-sided complex input (6 bins for signal length 10)\nx = np.fft.rfft(np.arange(0, 100).reshape(10, 10), axis=0).astype(np.complex64)\ny = np.fft.irfft(x, n=10, axis=0)\n\nx = np.stack((x.real, x.imag), axis=2).astype(np.float32).reshape(1, 6, 10, 2)\ny = y.reshape(1, 10, 10, 1).astype(np.float32)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dft_irfft_opset19\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 19)],\n)" } ] }, { "name": "DecoderAttention", "module": "com.microsoft", "version": 1, "description": "This DecoderAttention supports self attention and cross attention, key and value cache, and key_padding_mask. The attention mask is not support at the moment.\nSome boolean parameters are passed by runtime input for generic purpose\n", "attributes": [ { "name": "mask_filter_value", "type": "float32", "required": false, "description": "The value to be filled in the attention mask. Default value is -10000.0f" }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" } ], "inputs": [ { "name": "query", "type": "T", "description": "3D input tensor with shape (sequence_length, batch_size, hidden_size), hidden_size = num_heads * head_size" }, { "name": "key", "type": "T", "description": "3D input tensor with shape (total_sequence_length, batch_size, hidden_size)" }, { "name": "q_weight", "type": "T", "description": "2D input tensor with shape (hidden_size, hidden_size)" }, { "name": "kv_weight", "type": "T", "description": "2D input tensor with shape (hidden_size, 2 * hidden_size)" }, { "name": "bias", "type": "T", "description": "1D input tensor with shape (3 * hidden_size)" }, { "name": "key_padding_mask", "type": "B", "option": "optional", "description": "2D input tensor with shape (batch_size, total_sequence_length)" }, { "name": "key_cache", "type": "T", "option": "optional", "description": "input tensor with shape (batch_size, num_heads, sequence_length or total_sequence_length, head_size)" }, { "name": "value_cache", "type": "T", "option": "optional", "description": "input tensor with shape (batch_size, num_heads, sequence_length or total_sequence_length, head_size)" }, { "name": "static_kv", "type": "B", "description": "If static_kv = true, cross-attention; else self-attention" }, { "name": "use_past", "type": "B", "description": "If use_past = true, use cache; else no cache" }, { "name": "has_layer_state", "type": "B", "description": "If has_layer_state = true, layer_state = {} or [a,b]; else layer_state = None" }, { "name": "has_key_padding_mask", "type": "B", "description": "has_key_padding_mask or not" } ], "min_input": 12, "max_input": 12, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (sequence_length, batch_size, hidden_size)" }, { "name": "new_key_cache", "type": "T", "option": "optional", "description": "output tensor with shape (batch_size, num_heads, new sequence_length, head_size)" }, { "name": "new_value_cache", "type": "T", "option": "optional", "description": "output tensor with shape (batch_size, num_heads, new sequence_length, head_size)" } ], "min_output": 1, "max_output": 3, "outputs_range": "1 - 3", "type_constraints": [ { "description": "Constrain input and output types to float and float16 tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain key_padding_mask to bool tensors.", "type_param_str": "B", "allowed_type_strs": [ "tensor(bool)" ] } ], "category": "Attention" }, { "name": "DecoderMaskedMultiHeadAttention", "module": "com.microsoft", "version": 1, "description": "Multihead attention that supports input sequence length of 1.\nSimilar to DecoderMaskedSelfAttention but this op excludes QKV MatMul and Bias.\nThis op supports both Self and Cross Attention.\n", "attributes": [ { "name": "mask_filter_value", "type": "float32", "required": false, "description": "The value to be filled in the attention mask. Default value is -10000.0f" }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "output_qk", "type": "int64", "required": false, "description": "Need output the cross attention MatMul(Q, K)" }, { "name": "past_present_share_buffer", "type": "int64", "required": false, "description": "Corresponding past and present are same tensor, its size is (batch_size, num_heads, max_sequence_length, head_size)" }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1/sqrt(head_size)" } ], "inputs": [ { "name": "query", "type": "T", "description": "Query with shape (batch_size, 1, hidden_size) or packed QKV with shape (batch_size, 1, 2 * hidden_size + v_hidden_size)" }, { "name": "key", "type": "T", "option": "optional", "description": "Key with shape (batch_size, 1, hidden_size) for self attention or past_key with shape (batch_size, num_heads, kv_sequence_length, head_size) for cross attention" }, { "name": "value", "type": "T", "option": "optional", "description": "Value with shape (batch_size, 1, v_hidden_size) for self attention or past_value with shape (batch_size, num_heads, kv_sequence_length, head_size) for cross attention" }, { "name": "mask_index", "type": "M", "option": "optional", "description": "Mask values of shape (batch_size, total_sequence_length) or (batch_size, kv_sequence_length)" }, { "name": "attention_bias", "type": "T", "option": "optional", "description": "additional add to QxK' with shape (batch_size or 1, num_heads or 1, sequence_length, total_sequence_length)" }, { "name": "past_key", "type": "T", "option": "optional", "description": "past state for key with shape (batch_size, num_heads, past_sequence_length, head_size) for self attentionWhen past_present_share_buffer is set, its shape is (batch_size, num_heads, max_sequence_length, head_size). The keys buffer is re-ordered in such a way that its virtual sub-tensor of shape (batch_size, num_heads, max_sequence_length, head_size) which may be perceived as being of shape (batch_size, num_heads, max_sequence_length, head_size / x, x) is reordered to become (batch_size, num_heads, head_size / x, max_sequence_length, x) where `x = 16 / sizeof(T)`." }, { "name": "past_value", "type": "T", "option": "optional", "description": "past state for value with shape (batch_size, num_heads, past_sequence_length, head_size) for self attentionWhen past_present_share_buffer is set, its shape is (batch_size, num_heads, max_sequence_length, head_size). " }, { "name": "past_sequence_length", "type": "M", "option": "optional", "description": "When past_present_share_buffer is used, it is required to specify past_sequence_length (could be 0).Cross Attention doesn't need this input." }, { "name": "beam_width", "type": "M", "option": "optional", "description": "The beam width that is being used while decoding. If not provided, the beam width will be assumed to be 1." }, { "name": "cache_indirection", "type": "M", "option": "optional", "description": "A buffer of shape [batch_size, beam_width, max_output_length] where an `[i, j, k]` entry specifies which beam the `k`-th token came from for the `j`-th beam for batch `i` in the current iteration" }, { "name": "bias", "type": "T", "option": "optional", "description": "Bias tensor with shape (hidden_size + hidden_size + v_hidden_size) from input projection" } ], "min_input": 1, "max_input": 11, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, v_hidden_size)" }, { "name": "present_key", "type": "T", "option": "optional", "description": "present state for key with shape (batch_size, num_heads, total_sequence_length, head_size). If past_present_share_buffer is set, its shape is (batch_size, num_heads, max_sequence_length, head_size), while effective_seq_length = (past_sequence_length + kv_sequence_length)." }, { "name": "present_value", "type": "T", "option": "optional", "description": "present state for value with shape (batch_size, num_heads, total_sequence_length, head_size). If past_present_share_buffer is set, its shape is (batch_size, num_heads, max_sequence_length, head_size), while effective_seq_length = (past_sequence_length + kv_sequence_length)." }, { "name": "qk", "type": "QK", "option": "optional", "description": "normalized Q * K, of shape (batch_size, num_heads, 1, total_sequence_length). " } ], "min_output": 1, "max_output": 4, "inputs_range": "1 - 11", "outputs_range": "1 - 4", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain QK output to float32 or float16 tensors, independent of input type or output type.", "type_param_str": "QK", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain mask index to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "DecoderMaskedSelfAttention", "module": "com.microsoft", "version": 1, "description": "Self attention that supports input sequence length of 1.\n\nThe weights for input projection of Q, K and V are merged. The data is stacked on the second dimension. Its shape\nis (input_hidden_size, hidden_size + hidden_size + v_hidden_size). Here hidden_size is the hidden dimension of Q and K,\nand v_hidden_size is that of V.\n\nThe mask_index is optional. If it is provided, only raw attention mask with shape (batch_size, total_sequence_length) is supported currently.\n\nBoth past and present state need to be provided.\n\nThe qkv_hidden_sizes is required only when K and V have different hidden sizes.\n\nThe total_sequence_length is past_sequence_length + kv_sequence_length. Here kv_sequence_length is the length of K or V.\nCurrently, only self attention is supported which means that kv_sequence_length equals to sequence_length (sequence length of Q).\n", "attributes": [ { "name": "do_rotary", "type": "int64", "required": false, "description": "Whether to use rotary position embedding. Default value is 0." }, { "name": "mask_filter_value", "type": "float32", "required": false, "description": "The value to be filled in the attention mask. Default value is -10000.0f" }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "past_present_share_buffer", "type": "int64", "required": false, "description": "Corresponding past and present are same tensor, its size is (2, batch_size, num_heads, max_sequence_length, head_size)" }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1/sqrt(head_size)" } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor with shape (batch_size, 1, input_hidden_size)" }, { "name": "weights", "type": "T", "description": "Merged Q/K/V weights with shape (input_hidden_size, hidden_size + hidden_size + v_hidden_size)" }, { "name": "bias", "type": "T", "description": "Bias tensor with shape (hidden_size + hidden_size + v_hidden_size) for input projection" }, { "name": "mask_index", "type": "M", "option": "optional", "description": "Mask values of shape (batch_size, total_sequence_length)" }, { "name": "past", "type": "T", "description": "past state for key and value with shape (2, batch_size, num_heads, past_sequence_length, head_size)When past_present_share_buffer is set, its shape is (2, batch_size, num_heads, max_sequence_length, head_size). The first `batch_size * num_heads * max_sequence_length * head_size` elements correspond to keys and the next `batch_size * num_heads * max_sequence_length * head_size` elements correspond to values. The keys buffer is re-ordered in such a way that its virtual sub-tensor of shape (batch_size, num_heads, max_sequence_length, head_size) which may be perceived as being of shape (batch_size, num_heads, max_sequence_length, head_size / x, x) is reordered to become (batch_size, num_heads, head_size / x, max_sequence_length, x) where `x = 16 / sizeof(T)`." }, { "name": "attention_bias", "type": "T", "option": "optional", "description": "additional add to QxK' with shape (batch_size or 1, num_heads or 1, sequence_length, total_sequence_length)" }, { "name": "past_sequence_length", "type": "M", "description": "When past_present_share_buffer is used, it is required to specify past_sequence_length (could be 0)." }, { "name": "beam_width", "type": "M", "option": "optional", "description": "The beam width that is being used while decoding. If not provided, the beam width will be assumed to be 1." }, { "name": "cache_indirection", "type": "M", "option": "optional", "description": "A buffer of shape [batch_size, beam_width, max_output_length] where an `[i, j, k]` entry specifies which beam the `k`-th token came from for the `j`-th beam for batch `i` in the current iteration" } ], "min_input": 7, "max_input": 9, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, v_hidden_size)" }, { "name": "present", "type": "T", "description": "past state for key and value with shape (2, batch_size, num_heads, total_sequence_length, head_size). If past_present_share_buffer is set, its shape is (2, batch_size, num_heads, max_sequence_length, head_size), while effective_seq_length = (past_sequence_length + kv_sequence_length)." } ], "min_output": 2, "max_output": 2, "inputs_range": "7 - 9", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain mask index to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "DeformConv", "module": "ai.onnx", "version": 19, "description": "Performs deformable convolution as described in https://arxiv.org/abs/1703.06211 and https://arxiv.org/abs/1811.11168.\nThis operator specification supports the general N-D case. Note that most common use cases have 2D or 3D data.\n", "attributes": [ { "name": "dilations", "type": "int64[]", "required": false, "description": "Dilation value along each spatial axis of the kernel. Default is 1 along each axis." }, { "name": "group", "type": "int64", "required": false, "default": 1, "description": "Number of groups the input and output channels, C and oC, are divided into. C and oC must both be divisible by group. Default is 1." }, { "name": "kernel_shape", "type": "int64[]", "required": false, "description": "Shape of the convolution kernel. If not present, it is inferred from the shape of input W." }, { "name": "offset_group", "type": "int64", "required": false, "default": 1, "description": "Number of groups of offset. C must be divisible by offset_group. Default is 1." }, { "name": "pads", "type": "int64[]", "required": false, "description": "Padding for the beginning and end along each spatial axis. The values represent the number of pixels added to the beginning and end of the corresponding axis and can take any nonnegative value. The format should be as follows: [x1_begin, x2_begin, ..., x1_end, x2_end, ...], where xi_begin is the number of pixels added at the beginning of axis `i` and xi_end is the number of pixels added at the end of axis `i`. Default is 0 along each axis." }, { "name": "strides", "type": "int64[]", "required": false, "description": "Stride along each spatial axis. Default is 1 along each axis." } ], "inputs": [ { "name": "X", "type": "T", "description": "Input data tensor. For 2D image data, it has shape (N, C, H, W) where N is the batch size, C is the number of input channels, and H and W are the height and width. In general, the shape is (N, C, D1, D2, ... , Dn) for n-dimensional data, where D1 to Dn are the spatial dimension sizes. Most common use cases have n = 2 or 3." }, { "name": "W", "type": "T", "description": "Weight tensor that will be used in the convolutions. It has shape (oC, C/group, kH, kW), where oC is the number of output channels and kH and kW are the kernel height and width. For more than 2 dimensions, it has shape (oC, C/group, k1, k2, ... , kn)." }, { "name": "offset", "type": "T", "description": "Offset tensor denoting the offset for the sampling locations in the convolution kernel. It has shape (N, offset_group * kH * kW * 2, oH, oW) for 2D data or (N, offset_group * k1 * k2 * ... * kn * n, o1, o2, ... , on) for nD data. Use linear interpolationfor fractional offset values. Sampling locations outside of the padded input tensor gives zero." }, { "name": "B", "type": "T", "option": "optional", "description": "Optional 1D bias of length oC to be added to the convolution. Default is a tensor of zeros." }, { "name": "mask", "type": "T", "option": "optional", "description": "The mask tensor to be applied to each position in the convolution kernel. It has shape (N, offset_group * kH * kW, oH, oW) for 2D data or (N, offset_group * k1 * k2 * ... * kn * n, o1, o2, ... , on) for nD data. Default is a tensor of ones." } ], "min_input": 3, "max_input": 5, "outputs": [ { "name": "Y", "type": "T", "description": "Output data tensor that contains the result of convolution. It has shape (N, oC, oH, oW) for 2D data or (N, oC, o1, o2, ..., on) for nD data" } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - 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": "deformconv", "code": "X = np.arange(9).astype(np.float32)\nX.shape = (1, 1, 3, 3)\nW = np.ones((1, 1, 2, 2), dtype=np.float32)\n\n# Convolution with padding\noffset_with_padding = np.zeros((1, 8, 4, 4), dtype=np.float32)\n# h-coord of [0, 0] element of kernel, at output position [0, 0]\noffset_with_padding[0, 0, 0, 0] = 0.5\n# w-coord of [1, 0] element of kernel, at output position [1, 2]\noffset_with_padding[0, 5, 1, 2] = -0.1\n\nnode_with_padding = onnx.helper.make_node(\n \"DeformConv\",\n inputs=[\"X\", \"W\", \"offset_with_padding\"],\n outputs=[\"Y_with_padding\"],\n kernel_shape=[2, 2],\n pads=[1, 1, 1, 1],\n)\nY_with_padding = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 2.0], # (1, 1, 4, 4) output tensor\n [3.0, 8.0, 11.9, 7.0],\n [9.0, 20.0, 24.0, 13.0],\n [6.0, 13.0, 15.0, 8.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_padding,\n inputs=[X, W, offset_with_padding],\n outputs=[Y_with_padding],\n name=\"test_basic_deform_conv_with_padding\",\n)\n\n# Convolution without padding\noffset_without_padding = np.zeros((1, 8, 2, 2), dtype=np.float32)\n# h-coord of [0, 0] element of kernel, at output position [0, 0]\noffset_without_padding[0, 0, 0, 0] = 0.5\n# w-coord of [1, 0] element of kernel, at output position [0, 1]\noffset_without_padding[0, 5, 0, 1] = -0.1\n\nnode_without_padding = onnx.helper.make_node(\n \"DeformConv\",\n inputs=[\"X\", \"W\", \"offset_without_padding\"],\n outputs=[\"Y_without_padding\"],\n kernel_shape=[2, 2],\n pads=[0, 0, 0, 0],\n)\nY_without_padding = np.array(\n [\n [\n [\n [9.5, 11.9], # (1, 1, 2, 2) output tensor\n [20.0, 24.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_without_padding,\n inputs=[X, W, offset_without_padding],\n outputs=[Y_without_padding],\n name=\"test_basic_deform_conv_without_padding\",\n)" }, { "summary": "deformconv_with_mask_bias", "code": "X = np.arange(9).astype(np.float32)\nX.shape = (1, 1, 3, 3)\nW = np.ones((1, 1, 2, 2), dtype=np.float32)\nB = np.ones((1,), dtype=np.float32)\n\noffset = np.zeros((1, 8, 2, 2), dtype=np.float32)\n# h-coord of [0, 0] element of kernel, at output position [0, 0]\noffset[0, 0, 0, 0] = 0.5\n# w-coord of [1, 0] element of kernel, at output position [0, 1]\noffset[0, 5, 0, 1] = -0.1\n\nmask = np.ones((1, 4, 2, 2), dtype=np.float32)\nmask[0, 2, 1, 1] = 0.2 # [1, 0] element of kernel at output position [1, 1]\n\nnode = onnx.helper.make_node(\n \"DeformConv\",\n inputs=[\"X\", \"W\", \"offset\", \"B\", \"mask\"],\n outputs=[\"Y\"],\n kernel_shape=[2, 2],\n pads=[0, 0, 0, 0],\n)\nY = np.array(\n [\n [\n [\n [10.5, 12.9], # (1, 1, 2, 2) output tensor\n [21.0, 19.4],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node,\n inputs=[X, W, offset, B, mask],\n outputs=[Y],\n name=\"test_deform_conv_with_mask_bias\",\n)" }, { "summary": "deformconv_with_multiple_offset_groups", "code": "X = np.zeros((1, 2, 3, 3), dtype=np.float32)\nX[0, 0] = np.reshape(np.arange(9).astype(np.float32), (3, 3))\nX[0, 1] = np.reshape(np.arange(8, -1, -1).astype(np.float32), (3, 3))\nX.shape = (1, 2, 3, 3)\nW = np.ones((1, 2, 2, 2), dtype=np.float32)\n\noffset = np.zeros((1, 16, 2, 2), dtype=np.float32)\n# h-coord of [0, 0] element of kernel in channel 0, at output position [0, 0]\noffset[0, 0, 0, 0] = 0.5\n# w-coord of [1, 0] element of kernel in channel 1, at output position [0, 1]\noffset[0, 13, 0, 1] = -0.1\n\nnode = onnx.helper.make_node(\n \"DeformConv\",\n inputs=[\"X\", \"W\", \"offset\"],\n outputs=[\"Y\"],\n kernel_shape=[2, 2],\n pads=[0, 0, 0, 0],\n offset_group=2,\n)\nY = np.array(\n [\n [\n [\n [33.5, 32.1], # (1, 1, 2, 2) output tensor\n [32.0, 32.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node,\n inputs=[X, W, offset],\n outputs=[Y],\n name=\"test_deform_conv_with_multiple_offset_groups\",\n)" } ] }, { "name": "DeformConv", "module": "ai.onnx", "version": 22, "description": "Performs deformable convolution as described in https://arxiv.org/abs/1703.06211 and https://arxiv.org/abs/1811.11168.\nThis operator specification supports the general N-D case. Note that most common use cases have 2D or 3D data.\n", "attributes": [ { "name": "dilations", "type": "int64[]", "required": false, "description": "Dilation value along each spatial axis of the kernel. Default is 1 along each axis." }, { "name": "group", "type": "int64", "required": false, "default": 1, "description": "Number of groups the input and output channels, C and oC, are divided into. C and oC must both be divisible by group. Default is 1." }, { "name": "kernel_shape", "type": "int64[]", "required": false, "description": "Shape of the convolution kernel. If not present, it is inferred from the shape of input W." }, { "name": "offset_group", "type": "int64", "required": false, "default": 1, "description": "Number of groups of offset. C must be divisible by offset_group. Default is 1." }, { "name": "pads", "type": "int64[]", "required": false, "description": "Padding for the beginning and end along each spatial axis. The values represent the number of pixels added to the beginning and end of the corresponding axis and can take any nonnegative value. The format should be as follows: [x1_begin, x2_begin, ..., x1_end, x2_end, ...], where xi_begin is the number of pixels added at the beginning of axis `i` and xi_end is the number of pixels added at the end of axis `i`. Default is 0 along each axis." }, { "name": "strides", "type": "int64[]", "required": false, "description": "Stride along each spatial axis. Default is 1 along each axis." } ], "inputs": [ { "name": "X", "type": "T", "description": "Input data tensor. For 2D image data, it has shape (N, C, H, W) where N is the batch size, C is the number of input channels, and H and W are the height and width. In general, the shape is (N, C, D1, D2, ... , Dn) for n-dimensional data, where D1 to Dn are the spatial dimension sizes. Most common use cases have n = 2 or 3." }, { "name": "W", "type": "T", "description": "Weight tensor that will be used in the convolutions. It has shape (oC, C/group, kH, kW), where oC is the number of output channels and kH and kW are the kernel height and width. For more than 2 dimensions, it has shape (oC, C/group, k1, k2, ... , kn)." }, { "name": "offset", "type": "T", "description": "Offset tensor denoting the offset for the sampling locations in the convolution kernel. It has shape (N, offset_group * kH * kW * 2, oH, oW) for 2D data or (N, offset_group * k1 * k2 * ... * kn * n, o1, o2, ... , on) for nD data. Use linear interpolationfor fractional offset values. Sampling locations outside of the padded input tensor gives zero." }, { "name": "B", "type": "T", "option": "optional", "description": "Optional 1D bias of length oC to be added to the convolution. Default is a tensor of zeros." }, { "name": "mask", "type": "T", "option": "optional", "description": "The mask tensor to be applied to each position in the convolution kernel. It has shape (N, offset_group * kH * kW, oH, oW) for 2D data or (N, offset_group * k1 * k2 * ... * kn * n, o1, o2, ... , on) for nD data. Default is a tensor of ones." } ], "min_input": 3, "max_input": 5, "outputs": [ { "name": "Y", "type": "T", "description": "Output data tensor that contains the result of convolution. It has shape (N, oC, oH, oW) for 2D data or (N, oC, o1, o2, ..., on) for nD data" } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - 5", "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": "deformconv", "code": "X = np.arange(9).astype(np.float32)\nX.shape = (1, 1, 3, 3)\nW = np.ones((1, 1, 2, 2), dtype=np.float32)\n\n# Convolution with padding\noffset_with_padding = np.zeros((1, 8, 4, 4), dtype=np.float32)\n# h-coord of [0, 0] element of kernel, at output position [0, 0]\noffset_with_padding[0, 0, 0, 0] = 0.5\n# w-coord of [1, 0] element of kernel, at output position [1, 2]\noffset_with_padding[0, 5, 1, 2] = -0.1\n\nnode_with_padding = onnx.helper.make_node(\n \"DeformConv\",\n inputs=[\"X\", \"W\", \"offset_with_padding\"],\n outputs=[\"Y_with_padding\"],\n kernel_shape=[2, 2],\n pads=[1, 1, 1, 1],\n)\nY_with_padding = np.array(\n [\n [\n [\n [0.0, 1.0, 3.0, 2.0], # (1, 1, 4, 4) output tensor\n [3.0, 8.0, 11.9, 7.0],\n [9.0, 20.0, 24.0, 13.0],\n [6.0, 13.0, 15.0, 8.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_with_padding,\n inputs=[X, W, offset_with_padding],\n outputs=[Y_with_padding],\n name=\"test_basic_deform_conv_with_padding\",\n)\n\n# Convolution without padding\noffset_without_padding = np.zeros((1, 8, 2, 2), dtype=np.float32)\n# h-coord of [0, 0] element of kernel, at output position [0, 0]\noffset_without_padding[0, 0, 0, 0] = 0.5\n# w-coord of [1, 0] element of kernel, at output position [0, 1]\noffset_without_padding[0, 5, 0, 1] = -0.1\n\nnode_without_padding = onnx.helper.make_node(\n \"DeformConv\",\n inputs=[\"X\", \"W\", \"offset_without_padding\"],\n outputs=[\"Y_without_padding\"],\n kernel_shape=[2, 2],\n pads=[0, 0, 0, 0],\n)\nY_without_padding = np.array(\n [\n [\n [\n [9.5, 11.9], # (1, 1, 2, 2) output tensor\n [20.0, 24.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node_without_padding,\n inputs=[X, W, offset_without_padding],\n outputs=[Y_without_padding],\n name=\"test_basic_deform_conv_without_padding\",\n)" }, { "summary": "deformconv_with_mask_bias", "code": "X = np.arange(9).astype(np.float32)\nX.shape = (1, 1, 3, 3)\nW = np.ones((1, 1, 2, 2), dtype=np.float32)\nB = np.ones((1,), dtype=np.float32)\n\noffset = np.zeros((1, 8, 2, 2), dtype=np.float32)\n# h-coord of [0, 0] element of kernel, at output position [0, 0]\noffset[0, 0, 0, 0] = 0.5\n# w-coord of [1, 0] element of kernel, at output position [0, 1]\noffset[0, 5, 0, 1] = -0.1\n\nmask = np.ones((1, 4, 2, 2), dtype=np.float32)\nmask[0, 2, 1, 1] = 0.2 # [1, 0] element of kernel at output position [1, 1]\n\nnode = onnx.helper.make_node(\n \"DeformConv\",\n inputs=[\"X\", \"W\", \"offset\", \"B\", \"mask\"],\n outputs=[\"Y\"],\n kernel_shape=[2, 2],\n pads=[0, 0, 0, 0],\n)\nY = np.array(\n [\n [\n [\n [10.5, 12.9], # (1, 1, 2, 2) output tensor\n [21.0, 19.4],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node,\n inputs=[X, W, offset, B, mask],\n outputs=[Y],\n name=\"test_deform_conv_with_mask_bias\",\n)" }, { "summary": "deformconv_with_multiple_offset_groups", "code": "X = np.zeros((1, 2, 3, 3), dtype=np.float32)\nX[0, 0] = np.reshape(np.arange(9).astype(np.float32), (3, 3))\nX[0, 1] = np.reshape(np.arange(8, -1, -1).astype(np.float32), (3, 3))\nX.shape = (1, 2, 3, 3)\nW = np.ones((1, 2, 2, 2), dtype=np.float32)\n\noffset = np.zeros((1, 16, 2, 2), dtype=np.float32)\n# h-coord of [0, 0] element of kernel in channel 0, at output position [0, 0]\noffset[0, 0, 0, 0] = 0.5\n# w-coord of [1, 0] element of kernel in channel 1, at output position [0, 1]\noffset[0, 13, 0, 1] = -0.1\n\nnode = onnx.helper.make_node(\n \"DeformConv\",\n inputs=[\"X\", \"W\", \"offset\"],\n outputs=[\"Y\"],\n kernel_shape=[2, 2],\n pads=[0, 0, 0, 0],\n offset_group=2,\n)\nY = np.array(\n [\n [\n [\n [33.5, 32.1], # (1, 1, 2, 2) output tensor\n [32.0, 32.0],\n ]\n ]\n ]\n).astype(np.float32)\nexpect(\n node,\n inputs=[X, W, offset],\n outputs=[Y],\n name=\"test_deform_conv_with_multiple_offset_groups\",\n)" } ] }, { "name": "DepthToSpace", "module": "ai.onnx", "version": 1, "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\", inputs=[\"x\"], outputs=[\"y\"], blocksize=2, mode=\"CRD\"\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0]],\n [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0]],\n [[27.0, 28.0, 29.0], [30.0, 31.0, 32.0]],\n [[36.0, 37.0, 38.0], [39.0, 40.0, 41.0]],\n [[45.0, 46.0, 47.0], [48.0, 49.0, 50.0]],\n [[54.0, 55.0, 56.0], [57.0, 58.0, 59.0]],\n [[63.0, 64.0, 65.0], [66.0, 67.0, 68.0]],\n ]\n ]\n).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array(\n [\n [\n [\n [0.0, 9.0, 1.0, 10.0, 2.0, 11.0],\n [18.0, 27.0, 19.0, 28.0, 20.0, 29.0],\n [3.0, 12.0, 4.0, 13.0, 5.0, 14.0],\n [21.0, 30.0, 22.0, 31.0, 23.0, 32.0],\n ],\n [\n [36.0, 45.0, 37.0, 46.0, 38.0, 47.0],\n [54.0, 63.0, 55.0, 64.0, 56.0, 65.0],\n [39.0, 48.0, 40.0, 49.0, 41.0, 50.0],\n [57.0, 66.0, 58.0, 67.0, 59.0, 68.0],\n ],\n ]\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_depthtospace_crd_mode_example\")" }, { "summary": "default_mode_example", "code": "node = onnx.helper.make_node(\n \"DepthToSpace\", inputs=[\"x\"], outputs=[\"y\"], blocksize=2, mode=\"DCR\"\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0]],\n [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0]],\n [[27.0, 28.0, 29.0], [30.0, 31.0, 32.0]],\n [[36.0, 37.0, 38.0], [39.0, 40.0, 41.0]],\n [[45.0, 46.0, 47.0], [48.0, 49.0, 50.0]],\n [[54.0, 55.0, 56.0], [57.0, 58.0, 59.0]],\n [[63.0, 64.0, 65.0], [66.0, 67.0, 68.0]],\n ]\n ]\n).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array(\n [\n [\n [\n [0.0, 18.0, 1.0, 19.0, 2.0, 20.0],\n [36.0, 54.0, 37.0, 55.0, 38.0, 56.0],\n [3.0, 21.0, 4.0, 22.0, 5.0, 23.0],\n [39.0, 57.0, 40.0, 58.0, 41.0, 59.0],\n ],\n [\n [9.0, 27.0, 10.0, 28.0, 11.0, 29.0],\n [45.0, 63.0, 46.0, 64.0, 47.0, 65.0],\n [12.0, 30.0, 13.0, 31.0, 14.0, 32.0],\n [48.0, 66.0, 49.0, 67.0, 50.0, 68.0],\n ],\n ]\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_depthtospace_example\")" } ] }, { "name": "DepthToSpace", "module": "ai.onnx", "version": 11, "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\", inputs=[\"x\"], outputs=[\"y\"], blocksize=2, mode=\"CRD\"\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0]],\n [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0]],\n [[27.0, 28.0, 29.0], [30.0, 31.0, 32.0]],\n [[36.0, 37.0, 38.0], [39.0, 40.0, 41.0]],\n [[45.0, 46.0, 47.0], [48.0, 49.0, 50.0]],\n [[54.0, 55.0, 56.0], [57.0, 58.0, 59.0]],\n [[63.0, 64.0, 65.0], [66.0, 67.0, 68.0]],\n ]\n ]\n).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array(\n [\n [\n [\n [0.0, 9.0, 1.0, 10.0, 2.0, 11.0],\n [18.0, 27.0, 19.0, 28.0, 20.0, 29.0],\n [3.0, 12.0, 4.0, 13.0, 5.0, 14.0],\n [21.0, 30.0, 22.0, 31.0, 23.0, 32.0],\n ],\n [\n [36.0, 45.0, 37.0, 46.0, 38.0, 47.0],\n [54.0, 63.0, 55.0, 64.0, 56.0, 65.0],\n [39.0, 48.0, 40.0, 49.0, 41.0, 50.0],\n [57.0, 66.0, 58.0, 67.0, 59.0, 68.0],\n ],\n ]\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_depthtospace_crd_mode_example\")" }, { "summary": "default_mode_example", "code": "node = onnx.helper.make_node(\n \"DepthToSpace\", inputs=[\"x\"], outputs=[\"y\"], blocksize=2, mode=\"DCR\"\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0]],\n [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0]],\n [[27.0, 28.0, 29.0], [30.0, 31.0, 32.0]],\n [[36.0, 37.0, 38.0], [39.0, 40.0, 41.0]],\n [[45.0, 46.0, 47.0], [48.0, 49.0, 50.0]],\n [[54.0, 55.0, 56.0], [57.0, 58.0, 59.0]],\n [[63.0, 64.0, 65.0], [66.0, 67.0, 68.0]],\n ]\n ]\n).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array(\n [\n [\n [\n [0.0, 18.0, 1.0, 19.0, 2.0, 20.0],\n [36.0, 54.0, 37.0, 55.0, 38.0, 56.0],\n [3.0, 21.0, 4.0, 22.0, 5.0, 23.0],\n [39.0, 57.0, 40.0, 58.0, 41.0, 59.0],\n ],\n [\n [9.0, 27.0, 10.0, 28.0, 11.0, 29.0],\n [45.0, 63.0, 46.0, 64.0, 47.0, 65.0],\n [12.0, 30.0, 13.0, 31.0, 14.0, 32.0],\n [48.0, 66.0, 49.0, 67.0, 50.0, 68.0],\n ],\n ]\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_depthtospace_example\")" } ] }, { "name": "DepthToSpace", "module": "ai.onnx", "version": 13, "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\n```\nb, c, h, w = x.shape\ntmp = np.reshape(x, [b, blocksize, blocksize, c // (blocksize**2), h, w])\ntmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2])\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\n```\nb, c, h, w = x.shape\ntmp = np.reshape(x, [b, c // (blocksize ** 2), blocksize, blocksize, h, w])\ntmp = np.transpose(tmp, [0, 1, 4, 2, 5, 3])\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\", inputs=[\"x\"], outputs=[\"y\"], blocksize=2, mode=\"CRD\"\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0]],\n [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0]],\n [[27.0, 28.0, 29.0], [30.0, 31.0, 32.0]],\n [[36.0, 37.0, 38.0], [39.0, 40.0, 41.0]],\n [[45.0, 46.0, 47.0], [48.0, 49.0, 50.0]],\n [[54.0, 55.0, 56.0], [57.0, 58.0, 59.0]],\n [[63.0, 64.0, 65.0], [66.0, 67.0, 68.0]],\n ]\n ]\n).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array(\n [\n [\n [\n [0.0, 9.0, 1.0, 10.0, 2.0, 11.0],\n [18.0, 27.0, 19.0, 28.0, 20.0, 29.0],\n [3.0, 12.0, 4.0, 13.0, 5.0, 14.0],\n [21.0, 30.0, 22.0, 31.0, 23.0, 32.0],\n ],\n [\n [36.0, 45.0, 37.0, 46.0, 38.0, 47.0],\n [54.0, 63.0, 55.0, 64.0, 56.0, 65.0],\n [39.0, 48.0, 40.0, 49.0, 41.0, 50.0],\n [57.0, 66.0, 58.0, 67.0, 59.0, 68.0],\n ],\n ]\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_depthtospace_crd_mode_example\")" }, { "summary": "default_mode_example", "code": "node = onnx.helper.make_node(\n \"DepthToSpace\", inputs=[\"x\"], outputs=[\"y\"], blocksize=2, mode=\"DCR\"\n)\n\n# (1, 8, 2, 3) input tensor\nx = np.array(\n [\n [\n [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]],\n [[9.0, 10.0, 11.0], [12.0, 13.0, 14.0]],\n [[18.0, 19.0, 20.0], [21.0, 22.0, 23.0]],\n [[27.0, 28.0, 29.0], [30.0, 31.0, 32.0]],\n [[36.0, 37.0, 38.0], [39.0, 40.0, 41.0]],\n [[45.0, 46.0, 47.0], [48.0, 49.0, 50.0]],\n [[54.0, 55.0, 56.0], [57.0, 58.0, 59.0]],\n [[63.0, 64.0, 65.0], [66.0, 67.0, 68.0]],\n ]\n ]\n).astype(np.float32)\n\n# (1, 2, 4, 6) output tensor\ny = np.array(\n [\n [\n [\n [0.0, 18.0, 1.0, 19.0, 2.0, 20.0],\n [36.0, 54.0, 37.0, 55.0, 38.0, 56.0],\n [3.0, 21.0, 4.0, 22.0, 5.0, 23.0],\n [39.0, 57.0, 40.0, 58.0, 41.0, 59.0],\n ],\n [\n [9.0, 27.0, 10.0, 28.0, 11.0, 29.0],\n [45.0, 63.0, 46.0, 64.0, 47.0, 65.0],\n [12.0, 30.0, 13.0, 31.0, 14.0, 32.0],\n [48.0, 66.0, 49.0, 67.0, 50.0, 68.0],\n ],\n ]\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_depthtospace_example\")" } ] }, { "name": "DepthToSpace", "module": "com.ms.internal.nhwc", "version": 1, "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)" ] } ] }, { "name": "DepthToSpace", "module": "com.ms.internal.nhwc", "version": 11, "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)" ] } ] }, { "name": "DepthToSpace", "module": "com.ms.internal.nhwc", "version": 13, "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\n```\nb, c, h, w = x.shape\ntmp = np.reshape(x, [b, blocksize, blocksize, c // (blocksize**2), h, w])\ntmp = np.transpose(tmp, [0, 3, 4, 1, 5, 2])\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\n```\nb, c, h, w = x.shape\ntmp = np.reshape(x, [b, c // (blocksize ** 2), blocksize, blocksize, h, w])\ntmp = np.transpose(tmp, [0, 1, 4, 2, 5, 3])\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)" ] } ] }, { "name": "DequantizeBFP", "module": "com.microsoft", "version": 1, "description": "The BFP dequantization operator.\nIt consumes the raw BFP data and some metadata such as the shape and strides of the original tensor and computes the dequantized tensor.\nMore documentation on the BFP format can be found in this paper: https://www.microsoft.com/en-us/research/publication/pushing-the-limits-of-narrow-precision-inferencing-at-cloud-scale-with-microsoft-floating-point/", "attributes": [ { "name": "bfp_type", "type": "int64", "required": true, "description": "The type of BFP - must match with the BFPType enum" }, { "name": "block_dim", "type": "int64", "required": false, "default": -1, "description": "Each bounding box spans this dimension.Typically, the block dimension corresponds to the reduction dimension of the matrix multipication that consumes the output of this operator.For example, for a 2D matrix multiplication A@W, QuantizeBFP(A) would use block_dim 1 and QuantizeBFP(W) would use block_dim 0.The default is the last dimension." }, { "name": "dtype", "type": "int64", "required": false, "default": 1, "description": "The datatype to dequantize to." } ], "inputs": [ { "name": "x", "type": "T1", "description": "1-D, contiguous, raw, BFP data to be de-quantized." }, { "name": "shape", "type": "T2", "description": "shape of the original tensor." }, { "name": "strides", "type": "T2", "description": "strides of the original tensor." } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "y", "type": "T3", "description": "de-quantized tensor." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain the input to uint8.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(uint8)" ] }, { "description": "Constrain shape and strides to uint64.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int64)" ] }, { "description": "Constrain y to float and bfloat16.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ] }, { "name": "DequantizeLinear", "module": "ai.onnx", "version": 10, "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(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\n# 1-D tensor zero point and scale of size equal to axis 1 of the input tensor\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\nx_scale = np.array([2, 4, 5], dtype=np.float32)\nx_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (\n x.astype(np.float32) - x_zero_point.reshape(1, 3, 1, 1).astype(np.float32)\n) * x_scale.reshape(1, 3, 1, 1)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_axis\",\n)" }, { "summary": "blocked", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[5, 12], [12, 33], [65, 42]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\n\nx_scale = np.array(\n [\n [\n [[3.0, 2.0], [4.0, 1.0], [2.0, 2.0]],\n [[5.0, 2.0], [4.0, 3.0], [5.0, 2.0]],\n ],\n ],\n dtype=np.float32,\n)\nx_zero_point = np.array(\n [\n [\n [[1, 0], [0, 1], [2, 20]],\n [[3, 2], [4, 3], [15, 2]],\n ],\n ],\n dtype=np.uint8,\n)\n\n# x.shape = (1, 4, 3, 2)\n# x_scale.shape = (1, 2, 3, 2)\nassert x_scale.shape == x_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // x_scale.shape[i] for i in range(len(x.shape))] = (1, 2, 1, 1)\nassert all(\n x.shape[i] == x_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % x_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // x_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\nx_scale_elementwise = np.repeat(x_scale, repeats=repeats, axis=block_axis)\nx_zero_point_elementwise = np.repeat(\n x_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = (\n x.astype(np.float32) - x_zero_point_elementwise.astype(np.float32)\n) * x_scale_elementwise\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_blocked\",\n)" }, { "summary": "dequantizelinear", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn\",\n)" }, { "summary": "e4m3fn_float16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float16(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float16)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_float16\",\n)" }, { "summary": "e4m3fn_zero_point", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nzero_point = make_tensor(\"zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_zero_point\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, -96])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 98304.0, -192.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT4E2M1, [5], [0, 1, -1, 1.5, -4])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.FLOAT4E2M1, (1,), [0])\ny = np.array([0, 2, -2, 3, -8], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-300, -30, -1025, 1270]).astype(np.int16)\nx_scale = np.float32(2)\nx_zero_point = np.int16(-1024)\ny = np.array([1448.0, 1988.0, -2.0, 4588.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT2, [4], [0, 1, -1, -2])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT2, (1,), [1])\ny = np.array([-2, 0, -4, -6], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT4, [5], [0, 1, 7, -4, -8])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT4, (1,), [1])\ny = np.array([-2, 0, 12, -10, -18], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int4\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([30000, 31000, 32768, 33000]).astype(np.uint16)\nx_scale = np.float32(2)\nx_zero_point = np.uint16(32767)\ny = np.array([-5534.0, -3534.0, 2.0, 466.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT2, [4], [0, 1, 2, 3])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT2, (1,), [1])\ny = np.array([-2, 0, 2, 4], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT4, [5], [0, 1, 7, 10, 15])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT4, (1,), [1])\ny = np.array([-2, 0, 12, 18, 28], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint4\",\n)" } ] }, { "name": "DequantizeLinear", "module": "ai.onnx", "version": 13, "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(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\n# 1-D tensor zero point and scale of size equal to axis 1 of the input tensor\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\nx_scale = np.array([2, 4, 5], dtype=np.float32)\nx_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (\n x.astype(np.float32) - x_zero_point.reshape(1, 3, 1, 1).astype(np.float32)\n) * x_scale.reshape(1, 3, 1, 1)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_axis\",\n)" }, { "summary": "blocked", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[5, 12], [12, 33], [65, 42]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\n\nx_scale = np.array(\n [\n [\n [[3.0, 2.0], [4.0, 1.0], [2.0, 2.0]],\n [[5.0, 2.0], [4.0, 3.0], [5.0, 2.0]],\n ],\n ],\n dtype=np.float32,\n)\nx_zero_point = np.array(\n [\n [\n [[1, 0], [0, 1], [2, 20]],\n [[3, 2], [4, 3], [15, 2]],\n ],\n ],\n dtype=np.uint8,\n)\n\n# x.shape = (1, 4, 3, 2)\n# x_scale.shape = (1, 2, 3, 2)\nassert x_scale.shape == x_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // x_scale.shape[i] for i in range(len(x.shape))] = (1, 2, 1, 1)\nassert all(\n x.shape[i] == x_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % x_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // x_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\nx_scale_elementwise = np.repeat(x_scale, repeats=repeats, axis=block_axis)\nx_zero_point_elementwise = np.repeat(\n x_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = (\n x.astype(np.float32) - x_zero_point_elementwise.astype(np.float32)\n) * x_scale_elementwise\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_blocked\",\n)" }, { "summary": "dequantizelinear", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn\",\n)" }, { "summary": "e4m3fn_float16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float16(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float16)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_float16\",\n)" }, { "summary": "e4m3fn_zero_point", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nzero_point = make_tensor(\"zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_zero_point\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, -96])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 98304.0, -192.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT4E2M1, [5], [0, 1, -1, 1.5, -4])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.FLOAT4E2M1, (1,), [0])\ny = np.array([0, 2, -2, 3, -8], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-300, -30, -1025, 1270]).astype(np.int16)\nx_scale = np.float32(2)\nx_zero_point = np.int16(-1024)\ny = np.array([1448.0, 1988.0, -2.0, 4588.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT2, [4], [0, 1, -1, -2])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT2, (1,), [1])\ny = np.array([-2, 0, -4, -6], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT4, [5], [0, 1, 7, -4, -8])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT4, (1,), [1])\ny = np.array([-2, 0, 12, -10, -18], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int4\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([30000, 31000, 32768, 33000]).astype(np.uint16)\nx_scale = np.float32(2)\nx_zero_point = np.uint16(32767)\ny = np.array([-5534.0, -3534.0, 2.0, 466.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT2, [4], [0, 1, 2, 3])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT2, (1,), [1])\ny = np.array([-2, 0, 2, 4], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT4, [5], [0, 1, 7, 10, 15])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT4, (1,), [1])\ny = np.array([-2, 0, 12, 18, 28], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint4\",\n)" } ] }, { "name": "DequantizeLinear", "module": "ai.onnx", "version": 19, "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`zero-point` is usually not used in the case of float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz quantization,\nbut the dequantization formula remains the same for consistency and 'x_scale' still determines the output type.\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Used only for per-axis quantization. Negative value means counting dimensions from the back. Accepted range is `[-r, r-1]` where `r = rank(input)`. When the rank of the input is 1, per-tensor quantization is applied, rendering the axis unnecessary in this scenario." } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D quantized input tensor to be de-quantized." }, { "name": "x_scale", "type": "T2", "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": "T1", "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": "T2", "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 integer or float, or /32-bit integer tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int32)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "description": "'x_scale' determines the output type.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\n# 1-D tensor zero point and scale of size equal to axis 1 of the input tensor\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\nx_scale = np.array([2, 4, 5], dtype=np.float32)\nx_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (\n x.astype(np.float32) - x_zero_point.reshape(1, 3, 1, 1).astype(np.float32)\n) * x_scale.reshape(1, 3, 1, 1)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_axis\",\n)" }, { "summary": "blocked", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[5, 12], [12, 33], [65, 42]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\n\nx_scale = np.array(\n [\n [\n [[3.0, 2.0], [4.0, 1.0], [2.0, 2.0]],\n [[5.0, 2.0], [4.0, 3.0], [5.0, 2.0]],\n ],\n ],\n dtype=np.float32,\n)\nx_zero_point = np.array(\n [\n [\n [[1, 0], [0, 1], [2, 20]],\n [[3, 2], [4, 3], [15, 2]],\n ],\n ],\n dtype=np.uint8,\n)\n\n# x.shape = (1, 4, 3, 2)\n# x_scale.shape = (1, 2, 3, 2)\nassert x_scale.shape == x_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // x_scale.shape[i] for i in range(len(x.shape))] = (1, 2, 1, 1)\nassert all(\n x.shape[i] == x_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % x_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // x_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\nx_scale_elementwise = np.repeat(x_scale, repeats=repeats, axis=block_axis)\nx_zero_point_elementwise = np.repeat(\n x_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = (\n x.astype(np.float32) - x_zero_point_elementwise.astype(np.float32)\n) * x_scale_elementwise\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_blocked\",\n)" }, { "summary": "dequantizelinear", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn\",\n)" }, { "summary": "e4m3fn_float16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float16(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float16)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_float16\",\n)" }, { "summary": "e4m3fn_zero_point", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nzero_point = make_tensor(\"zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_zero_point\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, -96])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 98304.0, -192.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT4E2M1, [5], [0, 1, -1, 1.5, -4])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.FLOAT4E2M1, (1,), [0])\ny = np.array([0, 2, -2, 3, -8], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-300, -30, -1025, 1270]).astype(np.int16)\nx_scale = np.float32(2)\nx_zero_point = np.int16(-1024)\ny = np.array([1448.0, 1988.0, -2.0, 4588.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT2, [4], [0, 1, -1, -2])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT2, (1,), [1])\ny = np.array([-2, 0, -4, -6], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT4, [5], [0, 1, 7, -4, -8])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT4, (1,), [1])\ny = np.array([-2, 0, 12, -10, -18], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int4\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([30000, 31000, 32768, 33000]).astype(np.uint16)\nx_scale = np.float32(2)\nx_zero_point = np.uint16(32767)\ny = np.array([-5534.0, -3534.0, 2.0, 466.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT2, [4], [0, 1, 2, 3])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT2, (1,), [1])\ny = np.array([-2, 0, 2, 4], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT4, [5], [0, 1, 7, 10, 15])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT4, (1,), [1])\ny = np.array([-2, 0, 12, 18, 28], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint4\",\n)" } ] }, { "name": "DequantizeLinear", "module": "ai.onnx", "version": 21, "description": "The linear dequantization operator. It consumes a quantized tensor, a scale, and a zero point to compute the\nfull-precision tensor. The dequantization formula is `y = (x - x_zero_point) * x_scale`. `x_scale` and `x_zero_point`\nmust have the same shape, determining the quantization's granularity: a scalar for per-tensor/per-layer quantization,\na 1-D tensor for per-axis quantization, or have a rank identical to the input for blocked quantization.\nSee QuantizeLinear for details on quantization granularity.\n`x_zero_point` and `x` must have the same type. `x` and `y` must have the same shape. In the case of dequantizing\n`int32`, there's no zero point (zero point is supposed to be 0).\n`zero-point` is usually not used in the case of float8 types quantization, but the dequantization formula remains the same\nfor consistency, and `x_scale` still determines the output type.\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Used for per-axis and blocked quantization. Negative value means counting dimensions from the back. Accepted range is `[-r, r-1]` where `r = rank(input)`." }, { "name": "block_size", "type": "int64", "required": false, "description": "(Optional) The size of the quantization block (number of times every scale is replicated). Used only for blocked quantization. The block size is a positive integer. Given `x` shape `(D0, ..., Di, ..., Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`" } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D quantized input tensor to be de-quantized." }, { "name": "x_scale", "type": "T2", "description": "Scale for input `x`. For per-tensor/layer dequantization the scale is a scalar, for per per-axis dequantization it is a 1-D Tensor and for blocked dequantization it has the same shape as the input, except for one dimension in which blocking is performed." }, { "name": "x_zero_point", "type": "T1", "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": "T2", "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": "The type of the inputs 'x_zero_point' and 'x'.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(int32)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] }, { "description": "'x_scale' determines the output type.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\n# 1-D tensor zero point and scale of size equal to axis 1 of the input tensor\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\nx_scale = np.array([2, 4, 5], dtype=np.float32)\nx_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (\n x.astype(np.float32) - x_zero_point.reshape(1, 3, 1, 1).astype(np.float32)\n) * x_scale.reshape(1, 3, 1, 1)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_axis\",\n)" }, { "summary": "blocked", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[5, 12], [12, 33], [65, 42]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\n\nx_scale = np.array(\n [\n [\n [[3.0, 2.0], [4.0, 1.0], [2.0, 2.0]],\n [[5.0, 2.0], [4.0, 3.0], [5.0, 2.0]],\n ],\n ],\n dtype=np.float32,\n)\nx_zero_point = np.array(\n [\n [\n [[1, 0], [0, 1], [2, 20]],\n [[3, 2], [4, 3], [15, 2]],\n ],\n ],\n dtype=np.uint8,\n)\n\n# x.shape = (1, 4, 3, 2)\n# x_scale.shape = (1, 2, 3, 2)\nassert x_scale.shape == x_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // x_scale.shape[i] for i in range(len(x.shape))] = (1, 2, 1, 1)\nassert all(\n x.shape[i] == x_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % x_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // x_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\nx_scale_elementwise = np.repeat(x_scale, repeats=repeats, axis=block_axis)\nx_zero_point_elementwise = np.repeat(\n x_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = (\n x.astype(np.float32) - x_zero_point_elementwise.astype(np.float32)\n) * x_scale_elementwise\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_blocked\",\n)" }, { "summary": "dequantizelinear", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn\",\n)" }, { "summary": "e4m3fn_float16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float16(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float16)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_float16\",\n)" }, { "summary": "e4m3fn_zero_point", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nzero_point = make_tensor(\"zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_zero_point\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, -96])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 98304.0, -192.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT4E2M1, [5], [0, 1, -1, 1.5, -4])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.FLOAT4E2M1, (1,), [0])\ny = np.array([0, 2, -2, 3, -8], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-300, -30, -1025, 1270]).astype(np.int16)\nx_scale = np.float32(2)\nx_zero_point = np.int16(-1024)\ny = np.array([1448.0, 1988.0, -2.0, 4588.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT2, [4], [0, 1, -1, -2])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT2, (1,), [1])\ny = np.array([-2, 0, -4, -6], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT4, [5], [0, 1, 7, -4, -8])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT4, (1,), [1])\ny = np.array([-2, 0, 12, -10, -18], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int4\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([30000, 31000, 32768, 33000]).astype(np.uint16)\nx_scale = np.float32(2)\nx_zero_point = np.uint16(32767)\ny = np.array([-5534.0, -3534.0, 2.0, 466.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT2, [4], [0, 1, 2, 3])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT2, (1,), [1])\ny = np.array([-2, 0, 2, 4], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT4, [5], [0, 1, 7, 10, 15])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT4, (1,), [1])\ny = np.array([-2, 0, 12, 18, 28], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint4\",\n)" } ] }, { "name": "DequantizeLinear", "module": "ai.onnx", "version": 23, "description": "The linear dequantization operator. It consumes a quantized tensor, a scale, and a zero point to compute the\nfull-precision tensor. The dequantization formula is `y = (x - x_zero_point) * x_scale`. `x_scale` and `x_zero_point`\nmust have the same shape, determining the quantization's granularity: a scalar for per-tensor/per-layer quantization,\na 1-D tensor for per-axis quantization, or have a rank identical to the input for blocked quantization.\nSee QuantizeLinear for details on quantization granularity.\n\n`x_zero_point` and `x` must have the same type. `x` and `y` must have the same shape. In the case of dequantizing\n`int32`, there's no zero point (zero point is supposed to be 0).\n`zero-point` is usually not used in the case of float8 and 4-bit types quantization, but the dequantization formula remains the same\nfor consistency. The output type is determined by the attribute `output_dtype`. If `output_dtype` is not supplied then the output type\nis the same as `x_scale`. The output type also determines the precision of the multiplication operation.\n\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Used for per-axis and blocked quantization. Negative value means counting dimensions from the back. Accepted range is `[-r, r-1]` where `r = rank(input)`." }, { "name": "block_size", "type": "int64", "required": false, "description": "(Optional) The size of the quantization block (number of times every scale is replicated). Used only for blocked quantization. The block size is a positive integer. Given `x` shape `(D0, ..., Di, ..., Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`" }, { "name": "output_dtype", "type": "int64", "required": false, "description": "(Optional) The output data type. If not supplied, the output data type is inferred from `x_scale` data type (`T2`)" } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D quantized input tensor to be de-quantized." }, { "name": "x_scale", "type": "T2", "description": "Scale for input `x`. For per-tensor/layer dequantization the scale is a scalar, for per per-axis dequantization it is a 1-D Tensor and for blocked dequantization it has the same shape as the input, except for one dimension in which blocking is performed." }, { "name": "x_zero_point", "type": "T1", "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": "T3", "description": "N-D full precision output tensor. It has the same shape as input `x`. The data type is specified by the `output_dtype` attribute or, in its absence, the type of `x_scale`." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 3", "type_constraints": [ { "description": "The type of the inputs 'x_zero_point' and 'x'.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(int32)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] }, { "description": "The type of the input 'x_scale'.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "The type of the output 'y'.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\n# 1-D tensor zero point and scale of size equal to axis 1 of the input tensor\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\nx_scale = np.array([2, 4, 5], dtype=np.float32)\nx_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (\n x.astype(np.float32) - x_zero_point.reshape(1, 3, 1, 1).astype(np.float32)\n) * x_scale.reshape(1, 3, 1, 1)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_axis\",\n)" }, { "summary": "blocked", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[5, 12], [12, 33], [65, 42]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\n\nx_scale = np.array(\n [\n [\n [[3.0, 2.0], [4.0, 1.0], [2.0, 2.0]],\n [[5.0, 2.0], [4.0, 3.0], [5.0, 2.0]],\n ],\n ],\n dtype=np.float32,\n)\nx_zero_point = np.array(\n [\n [\n [[1, 0], [0, 1], [2, 20]],\n [[3, 2], [4, 3], [15, 2]],\n ],\n ],\n dtype=np.uint8,\n)\n\n# x.shape = (1, 4, 3, 2)\n# x_scale.shape = (1, 2, 3, 2)\nassert x_scale.shape == x_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // x_scale.shape[i] for i in range(len(x.shape))] = (1, 2, 1, 1)\nassert all(\n x.shape[i] == x_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % x_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // x_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\nx_scale_elementwise = np.repeat(x_scale, repeats=repeats, axis=block_axis)\nx_zero_point_elementwise = np.repeat(\n x_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = (\n x.astype(np.float32) - x_zero_point_elementwise.astype(np.float32)\n) * x_scale_elementwise\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_blocked\",\n)" }, { "summary": "dequantizelinear", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn\",\n)" }, { "summary": "e4m3fn_float16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float16(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float16)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_float16\",\n)" }, { "summary": "e4m3fn_zero_point", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nzero_point = make_tensor(\"zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_zero_point\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, -96])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 98304.0, -192.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT4E2M1, [5], [0, 1, -1, 1.5, -4])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.FLOAT4E2M1, (1,), [0])\ny = np.array([0, 2, -2, 3, -8], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-300, -30, -1025, 1270]).astype(np.int16)\nx_scale = np.float32(2)\nx_zero_point = np.int16(-1024)\ny = np.array([1448.0, 1988.0, -2.0, 4588.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT2, [4], [0, 1, -1, -2])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT2, (1,), [1])\ny = np.array([-2, 0, -4, -6], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT4, [5], [0, 1, 7, -4, -8])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT4, (1,), [1])\ny = np.array([-2, 0, 12, -10, -18], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int4\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([30000, 31000, 32768, 33000]).astype(np.uint16)\nx_scale = np.float32(2)\nx_zero_point = np.uint16(32767)\ny = np.array([-5534.0, -3534.0, 2.0, 466.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT2, [4], [0, 1, 2, 3])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT2, (1,), [1])\ny = np.array([-2, 0, 2, 4], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT4, [5], [0, 1, 7, 10, 15])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT4, (1,), [1])\ny = np.array([-2, 0, 12, 18, 28], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint4\",\n)" } ] }, { "name": "DequantizeLinear", "module": "ai.onnx", "version": 24, "description": "The linear dequantization operator. It consumes a quantized tensor, a scale, and a zero point to compute the\nfull-precision tensor. The dequantization formula is `y = (x - x_zero_point) * x_scale`. `x_scale` and `x_zero_point`\nmust have the same shape, determining the quantization's granularity: a scalar for per-tensor/per-layer quantization,\na 1-D tensor for per-axis quantization, or have a rank identical to the input for blocked quantization.\nSee QuantizeLinear for details on quantization granularity.\n\n`x_zero_point` and `x` must have the same type. `x` and `y` must have the same shape. In the case of dequantizing\n`int32`, there's no zero point (zero point is supposed to be 0).\n`zero-point` is usually not used in the case of float8 and 4-bit types quantization, but the dequantization formula remains the same\nfor consistency. The output type is determined by the attribute `output_dtype`. If `output_dtype` is not supplied then the output type\nis the same as `x_scale`. The output type also determines the precision of the multiplication operation.\n\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Used for per-axis and blocked quantization. Negative value means counting dimensions from the back. Accepted range is `[-r, r-1]` where `r = rank(input)`." }, { "name": "block_size", "type": "int64", "required": false, "description": "(Optional) The size of the quantization block (number of times every scale is replicated). Used only for blocked quantization. The block size is a positive integer. Given `x` shape `(D0, ..., Di, ..., Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`" }, { "name": "output_dtype", "type": "int64", "required": false, "description": "(Optional) The output data type. If not supplied, the output data type is inferred from `x_scale` data type (`T2`)" } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D quantized input tensor to be de-quantized." }, { "name": "x_scale", "type": "T2", "description": "Scale for input `x`. For per-tensor/layer dequantization the scale is a scalar, for per per-axis dequantization it is a 1-D Tensor and for blocked dequantization it has the same shape as the input, except for one dimension in which blocking is performed." }, { "name": "x_zero_point", "type": "T1", "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": "T3", "description": "N-D full precision output tensor. It has the same shape as input `x`. The data type is specified by the `output_dtype` attribute or, in its absence, the type of `x_scale`." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 3", "type_constraints": [ { "description": "The type of the inputs 'x_zero_point' and 'x'.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(int32)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] }, { "description": "The type of the input 'x_scale'.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(float8e8m0)" ] }, { "description": "The type of the output 'y'.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\n# 1-D tensor zero point and scale of size equal to axis 1 of the input tensor\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\nx_scale = np.array([2, 4, 5], dtype=np.float32)\nx_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (\n x.astype(np.float32) - x_zero_point.reshape(1, 3, 1, 1).astype(np.float32)\n) * x_scale.reshape(1, 3, 1, 1)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_axis\",\n)" }, { "summary": "blocked", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[5, 12], [12, 33], [65, 42]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\n\nx_scale = np.array(\n [\n [\n [[3.0, 2.0], [4.0, 1.0], [2.0, 2.0]],\n [[5.0, 2.0], [4.0, 3.0], [5.0, 2.0]],\n ],\n ],\n dtype=np.float32,\n)\nx_zero_point = np.array(\n [\n [\n [[1, 0], [0, 1], [2, 20]],\n [[3, 2], [4, 3], [15, 2]],\n ],\n ],\n dtype=np.uint8,\n)\n\n# x.shape = (1, 4, 3, 2)\n# x_scale.shape = (1, 2, 3, 2)\nassert x_scale.shape == x_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // x_scale.shape[i] for i in range(len(x.shape))] = (1, 2, 1, 1)\nassert all(\n x.shape[i] == x_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % x_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // x_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\nx_scale_elementwise = np.repeat(x_scale, repeats=repeats, axis=block_axis)\nx_zero_point_elementwise = np.repeat(\n x_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = (\n x.astype(np.float32) - x_zero_point_elementwise.astype(np.float32)\n) * x_scale_elementwise\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_blocked\",\n)" }, { "summary": "dequantizelinear", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn\",\n)" }, { "summary": "e4m3fn_float16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float16(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float16)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_float16\",\n)" }, { "summary": "e4m3fn_zero_point", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nzero_point = make_tensor(\"zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_zero_point\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, -96])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 98304.0, -192.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT4E2M1, [5], [0, 1, -1, 1.5, -4])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.FLOAT4E2M1, (1,), [0])\ny = np.array([0, 2, -2, 3, -8], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-300, -30, -1025, 1270]).astype(np.int16)\nx_scale = np.float32(2)\nx_zero_point = np.int16(-1024)\ny = np.array([1448.0, 1988.0, -2.0, 4588.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT2, [4], [0, 1, -1, -2])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT2, (1,), [1])\ny = np.array([-2, 0, -4, -6], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT4, [5], [0, 1, 7, -4, -8])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT4, (1,), [1])\ny = np.array([-2, 0, 12, -10, -18], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int4\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([30000, 31000, 32768, 33000]).astype(np.uint16)\nx_scale = np.float32(2)\nx_zero_point = np.uint16(32767)\ny = np.array([-5534.0, -3534.0, 2.0, 466.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT2, [4], [0, 1, 2, 3])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT2, (1,), [1])\ny = np.array([-2, 0, 2, 4], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT4, [5], [0, 1, 7, 10, 15])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT4, (1,), [1])\ny = np.array([-2, 0, 12, 18, 28], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint4\",\n)" } ] }, { "name": "DequantizeLinear", "module": "ai.onnx", "version": 25, "description": "The linear dequantization operator. It consumes a quantized tensor, a scale, and a zero point to compute the\nfull-precision tensor. The dequantization formula is `y = (x - x_zero_point) * x_scale`. `x_scale` and `x_zero_point`\nmust have the same shape, determining the quantization's granularity: a scalar for per-tensor/per-layer quantization,\na 1-D tensor for per-axis quantization, or have a rank identical to the input for blocked quantization.\nSee QuantizeLinear for details on quantization granularity.\n\n`x_zero_point` and `x` must have the same type. `x` and `y` must have the same shape. In the case of dequantizing\n`int32`, there's no zero point (zero point is supposed to be 0).\n`zero-point` is usually not used in the case of float8 and 4-bit types quantization, but the dequantization formula remains the same\nfor consistency. The output type is determined by the attribute `output_dtype`. If `output_dtype` is not supplied then the output type\nis the same as `x_scale`. The output type also determines the precision of the multiplication operation.\n\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Used for per-axis and blocked quantization. Negative value means counting dimensions from the back. Accepted range is `[-r, r-1]` where `r = rank(input)`." }, { "name": "block_size", "type": "int64", "required": false, "description": "(Optional) The size of the quantization block (number of times every scale is replicated). Used only for blocked quantization. The block size is a positive integer. Given `x` shape `(D0, ..., Di, ..., Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`" }, { "name": "output_dtype", "type": "int64", "required": false, "description": "(Optional) The output data type. If not supplied, the output data type is inferred from `x_scale` data type (`T2`)" } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D quantized input tensor to be de-quantized." }, { "name": "x_scale", "type": "T2", "description": "Scale for input `x`. For per-tensor/layer dequantization the scale is a scalar, for per per-axis dequantization it is a 1-D Tensor and for blocked dequantization it has the same shape as the input, except for one dimension in which blocking is performed." }, { "name": "x_zero_point", "type": "T1", "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": "T3", "description": "N-D full precision output tensor. It has the same shape as input `x`. The data type is specified by the `output_dtype` attribute or, in its absence, the type of `x_scale`." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 3", "type_constraints": [ { "description": "The type of the inputs 'x_zero_point' and 'x'.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(int32)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(uint2)", "tensor(int2)" ] }, { "description": "The type of the input 'x_scale'.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(float8e8m0)" ] }, { "description": "The type of the output 'y'.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\n# 1-D tensor zero point and scale of size equal to axis 1 of the input tensor\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\nx_scale = np.array([2, 4, 5], dtype=np.float32)\nx_zero_point = np.array([84, 24, 196], dtype=np.uint8)\ny = (\n x.astype(np.float32) - x_zero_point.reshape(1, 3, 1, 1).astype(np.float32)\n) * x_scale.reshape(1, 3, 1, 1)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_axis\",\n)" }, { "summary": "blocked", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [\n [[3, 89], [34, 200], [74, 59]],\n [[5, 24], [24, 87], [32, 13]],\n [[5, 12], [12, 33], [65, 42]],\n [[245, 99], [4, 142], [121, 102]],\n ],\n ],\n dtype=np.uint8,\n)\n\nx_scale = np.array(\n [\n [\n [[3.0, 2.0], [4.0, 1.0], [2.0, 2.0]],\n [[5.0, 2.0], [4.0, 3.0], [5.0, 2.0]],\n ],\n ],\n dtype=np.float32,\n)\nx_zero_point = np.array(\n [\n [\n [[1, 0], [0, 1], [2, 20]],\n [[3, 2], [4, 3], [15, 2]],\n ],\n ],\n dtype=np.uint8,\n)\n\n# x.shape = (1, 4, 3, 2)\n# x_scale.shape = (1, 2, 3, 2)\nassert x_scale.shape == x_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // x_scale.shape[i] for i in range(len(x.shape))] = (1, 2, 1, 1)\nassert all(\n x.shape[i] == x_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % x_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // x_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\nx_scale_elementwise = np.repeat(x_scale, repeats=repeats, axis=block_axis)\nx_zero_point_elementwise = np.repeat(\n x_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = (\n x.astype(np.float32) - x_zero_point_elementwise.astype(np.float32)\n) * x_scale_elementwise\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_blocked\",\n)" }, { "summary": "dequantizelinear", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn\",\n)" }, { "summary": "e4m3fn_float16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nx_scale = np.float16(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float16)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_float16\",\n)" }, { "summary": "e4m3fn_zero_point", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, -104])\nzero_point = make_tensor(\"zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 896.0, -208.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_e4m3fn_zero_point\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, -96])\nx_scale = np.float32(2)\ny = np.array([0.0, 1.0, 2.0, 98304.0, -192.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale],\n outputs=[y],\n name=\"test_dequantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.FLOAT4E2M1, [5], [0, 1, -1, 1.5, -4])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.FLOAT4E2M1, (1,), [0])\ny = np.array([0, 2, -2, 3, -8], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-300, -30, -1025, 1270]).astype(np.int16)\nx_scale = np.float32(2)\nx_zero_point = np.int16(-1024)\ny = np.array([1448.0, 1988.0, -2.0, 4588.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT2, [4], [0, 1, -1, -2])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT2, (1,), [1])\ny = np.array([-2, 0, -4, -6], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.INT4, [5], [0, 1, 7, -4, -8])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.INT4, (1,), [1])\ny = np.array([-2, 0, 12, -10, -18], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_int4\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([30000, 31000, 32768, 33000]).astype(np.uint16)\nx_scale = np.float32(2)\nx_zero_point = np.uint16(32767)\ny = np.array([-5534.0, -3534.0, 2.0, 466.0], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT2, [4], [0, 1, 2, 3])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT2, (1,), [1])\ny = np.array([-2, 0, 2, 4], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"DequantizeLinear\",\n inputs=[\"x\", \"x_scale\", \"x_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\n# scalar zero point and scale\nx = make_tensor(\"x\", TensorProto.UINT4, [5], [0, 1, 7, 10, 15])\nx_scale = np.float32(2)\nx_zero_point = make_tensor(\"x_zero_point\", TensorProto.UINT4, (1,), [1])\ny = np.array([-2, 0, 12, 18, 28], dtype=np.float32)\n\nexpect(\n node,\n inputs=[x, x_scale, x_zero_point],\n outputs=[y],\n name=\"test_dequantizelinear_uint4\",\n)" } ] }, { "name": "DequantizeLinear", "module": "com.microsoft", "version": 1, "description": "The linear dequantization operator. It consumes a quantized data, a scale, a zero point and computes the full precision data.\nThe dequantization formula is y = (x - x_zero_point) * x_scale.\nScale and zero point must have same shape. They must be either scalar (per tensor) or 1-D tensor (per 'axis').", "attributes": [ { "name": "axis", "type": "int64", "required": false, "description": "The axis along which same quantization parameters are applied. It's optional.If it's not specified, it means per-tensor quantization and input 'x_scale' and 'x_zero_point' must be scalars.If it's specified, it means per 'axis' quantization and input 'x_scale' and 'x_zero_point' must be 1-D tensors." } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D quantized Input tensor to be de-quantized." }, { "name": "x_scale", "type": "T2", "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": "T1", "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": "T2", "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' and 'x_zero_point' to 8-bit integer tensors, 16-bit integer tensors, or 32-bit signed integer tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(int32)", "tensor(int4)", "tensor(uint4)" ] }, { "description": "Constrain 'y', 'x_scale' to float tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] } ] }, { "name": "DequantizeWithOrder", "module": "com.microsoft", "version": 1, "description": "Dequantize input matrix to specific layout used in cublaslt. attr to specify output type, float16 or float32", "attributes": [ { "name": "order_input", "type": "int64", "required": true, "description": "cublasLt order of input matrix. See the schema of QuantizeWithOrder for order definition." }, { "name": "order_output", "type": "int64", "required": true, "description": "cublasLt order of output matrix" }, { "name": "to", "type": "int64", "required": true, "description": "The output data type, only support TensorProto_DataType_FLOAT (1) and TensorProto_DataType_FLOAT16 (10)" } ], "inputs": [ { "name": "input", "type": "Q", "description": "TODO: input tensor of (ROWS, COLS). if less than 2d, will broadcast to (1, X). If 3d, it is treated as (B, ROWS, COS)" }, { "name": "scale_input", "type": "S", "description": "scale of the input" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "output", "type": "F", "description": "output tensor" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to int8 tensors.", "type_param_str": "Q", "allowed_type_strs": [ "tensor(int8)" ] }, { "description": "Constrain to float types", "type_param_str": "F", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] }, { "description": "Constrain Scale to float32 types", "type_param_str": "S", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "Det", "module": "ai.onnx", "version": 11, "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], 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(\n np.float32\n)\ny = np.linalg.det(x) # expect array([-2., -3., -8.])\nexpect(node, inputs=[x], outputs=[y], name=\"test_det_nd\")" } ] }, { "name": "Det", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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(\n np.float32\n)\ny = np.linalg.det(x) # expect array([-2., -3., -8.])\nexpect(node, inputs=[x], outputs=[y], name=\"test_det_nd\")" } ] }, { "name": "DictVectorizer", "module": "ai.onnx.ml", "version": 1, "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": "DisentangledAttention_TRT", "module": "ai.onnx", "version": 1, "description": "Disentangled Attention TensorRT Plugin.", "attributes": [ { "name": "factor", "type": "float32", "required": true, "description": "Scaling factor applied to attention values, 1/sqrt(3d). d is hidden size per head = H/N. H is hidden size, N is number of heads." }, { "name": "span", "type": "int64", "required": true, "description": "Maximum relative distance, k." } ], "inputs": [ { "name": "c2c_attention", "type": "T", "description": "content-to-content attention tensor, QcKc^T." }, { "name": "c2p_attention", "type": "T", "description": "content-to-position attention tensor, QcKr^T." }, { "name": "p2c_attention", "type": "T", "description": "position-to-content attention tensor, KcQr^T." } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "disentangled_attention", "type": "T", "description": "The disentangled attention 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(float)", "tensor(float16)" ] } ] }, { "name": "Div", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_div\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int16\")\n\nx = np.array([-3, 3, -3, 3], dtype=np.int32)\ny = np.array([2, 2, -2, -2], dtype=np.int32)\nz = np.array([-1, 1, 1, -1], dtype=np.int32)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int32_trunc\")\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], name=\"test_div_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint64\")" }, { "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], name=\"test_div_bcast\")" } ] }, { "name": "Div", "module": "ai.onnx", "version": 6, "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\n\nFor integer inputs, the result is computed using truncating division (rounding toward zero).\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], 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], name=\"test_div\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int16\")\n\nx = np.array([-3, 3, -3, 3], dtype=np.int32)\ny = np.array([2, 2, -2, -2], dtype=np.int32)\nz = np.array([-1, 1, 1, -1], dtype=np.int32)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int32_trunc\")\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], name=\"test_div_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint64\")" }, { "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], name=\"test_div_bcast\")" } ] }, { "name": "Div", "module": "ai.onnx", "version": 7, "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\nFor integer inputs, the result is computed using truncating division (rounding toward zero).\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], 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], name=\"test_div\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int16\")\n\nx = np.array([-3, 3, -3, 3], dtype=np.int32)\ny = np.array([2, 2, -2, -2], dtype=np.int32)\nz = np.array([-1, 1, 1, -1], dtype=np.int32)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int32_trunc\")\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], name=\"test_div_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint64\")" }, { "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], name=\"test_div_bcast\")" } ] }, { "name": "Div", "module": "ai.onnx", "version": 13, "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\nFor integer inputs, the result is computed using truncating division (rounding toward zero).\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], 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], name=\"test_div\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int16\")\n\nx = np.array([-3, 3, -3, 3], dtype=np.int32)\ny = np.array([2, 2, -2, -2], dtype=np.int32)\nz = np.array([-1, 1, 1, -1], dtype=np.int32)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int32_trunc\")\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], name=\"test_div_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint64\")" }, { "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], name=\"test_div_bcast\")" } ] }, { "name": "Div", "module": "ai.onnx", "version": 14, "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\nFor integer inputs, the result is computed using truncating division (rounding toward zero).\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], 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], name=\"test_div\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int16\")\n\nx = np.array([-3, 3, -3, 3], dtype=np.int32)\ny = np.array([2, 2, -2, -2], dtype=np.int32)\nz = np.array([-1, 1, 1, -1], dtype=np.int32)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_int32_trunc\")\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], name=\"test_div_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64) + 1\nz = x // y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_div_uint64\")" }, { "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], name=\"test_div_bcast\")" } ] }, { "name": "Dropout", "module": "ai.onnx", "version": 1, "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(\"Dropout\", inputs=[\"x\"], outputs=[\"y\"], seed=seed)\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\", inputs=[\"x\"], outputs=[\"y\", \"z\"], 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\", inputs=[\"x\", \"r\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r], outputs=[y, z], name=\"test_dropout_default_mask_ratio\"\n)" }, { "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(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_default_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "default_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\"], outputs=[\"y\"], 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=0.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_random_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "training", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_default\"\n)" }, { "summary": "training_default_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_default_mask\",\n)" }, { "summary": "training_default_zero_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_zero_ratio\"\n)" }, { "summary": "training_default_zero_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_zero_ratio_mask\",\n)" }, { "summary": "training_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r, t], outputs=[y, z], name=\"test_training_dropout_mask\"\n)" } ], "category": "Dropout" }, { "name": "Dropout", "module": "ai.onnx", "version": 6, "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(\"Dropout\", inputs=[\"x\"], outputs=[\"y\"], seed=seed)\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\", inputs=[\"x\"], outputs=[\"y\", \"z\"], 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\", inputs=[\"x\", \"r\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r], outputs=[y, z], name=\"test_dropout_default_mask_ratio\"\n)" }, { "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(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_default_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "default_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\"], outputs=[\"y\"], 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=0.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_random_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "training", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_default\"\n)" }, { "summary": "training_default_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_default_mask\",\n)" }, { "summary": "training_default_zero_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_zero_ratio\"\n)" }, { "summary": "training_default_zero_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_zero_ratio_mask\",\n)" }, { "summary": "training_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r, t], outputs=[y, z], name=\"test_training_dropout_mask\"\n)" } ], "category": "Dropout" }, { "name": "Dropout", "module": "ai.onnx", "version": 7, "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(\"Dropout\", inputs=[\"x\"], outputs=[\"y\"], seed=seed)\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\", inputs=[\"x\"], outputs=[\"y\", \"z\"], 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\", inputs=[\"x\", \"r\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r], outputs=[y, z], name=\"test_dropout_default_mask_ratio\"\n)" }, { "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(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_default_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "default_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\"], outputs=[\"y\"], 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=0.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_random_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "training", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_default\"\n)" }, { "summary": "training_default_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_default_mask\",\n)" }, { "summary": "training_default_zero_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_zero_ratio\"\n)" }, { "summary": "training_default_zero_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_zero_ratio_mask\",\n)" }, { "summary": "training_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r, t], outputs=[y, z], name=\"test_training_dropout_mask\"\n)" } ], "category": "Dropout" }, { "name": "Dropout", "module": "ai.onnx", "version": 10, "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(\"Dropout\", inputs=[\"x\"], outputs=[\"y\"], seed=seed)\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\", inputs=[\"x\"], outputs=[\"y\", \"z\"], 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\", inputs=[\"x\", \"r\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r], outputs=[y, z], name=\"test_dropout_default_mask_ratio\"\n)" }, { "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(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_default_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "default_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\"], outputs=[\"y\"], 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=0.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_random_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "training", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_default\"\n)" }, { "summary": "training_default_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_default_mask\",\n)" }, { "summary": "training_default_zero_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_zero_ratio\"\n)" }, { "summary": "training_default_zero_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_zero_ratio_mask\",\n)" }, { "summary": "training_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r, t], outputs=[y, z], name=\"test_training_dropout_mask\"\n)" } ], "category": "Dropout" }, { "name": "Dropout", "module": "ai.onnx", "version": 12, "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(\"Dropout\", inputs=[\"x\"], outputs=[\"y\"], seed=seed)\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\", inputs=[\"x\"], outputs=[\"y\", \"z\"], 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\", inputs=[\"x\", \"r\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r], outputs=[y, z], name=\"test_dropout_default_mask_ratio\"\n)" }, { "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(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_default_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "default_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\"], outputs=[\"y\"], 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=0.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_random_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "training", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_default\"\n)" }, { "summary": "training_default_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_default_mask\",\n)" }, { "summary": "training_default_zero_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_zero_ratio\"\n)" }, { "summary": "training_default_zero_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_zero_ratio_mask\",\n)" }, { "summary": "training_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r, t], outputs=[y, z], name=\"test_training_dropout_mask\"\n)" } ], "category": "Dropout" }, { "name": "Dropout", "module": "ai.onnx", "version": 13, "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(\"Dropout\", inputs=[\"x\"], outputs=[\"y\"], seed=seed)\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\", inputs=[\"x\"], outputs=[\"y\", \"z\"], 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\", inputs=[\"x\", \"r\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r], outputs=[y, z], name=\"test_dropout_default_mask_ratio\"\n)" }, { "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(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_default_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "default_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\"], outputs=[\"y\"], 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=0.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_random_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "training", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_default\"\n)" }, { "summary": "training_default_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_default_mask\",\n)" }, { "summary": "training_default_zero_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_zero_ratio\"\n)" }, { "summary": "training_default_zero_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_zero_ratio_mask\",\n)" }, { "summary": "training_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r, t], outputs=[y, z], name=\"test_training_dropout_mask\"\n)" } ], "category": "Dropout" }, { "name": "Dropout", "module": "ai.onnx", "version": 22, "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 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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "description": "Constrain input 'ratio' types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "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(\"Dropout\", inputs=[\"x\"], outputs=[\"y\"], seed=seed)\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\", inputs=[\"x\"], outputs=[\"y\", \"z\"], 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\", inputs=[\"x\", \"r\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r], outputs=[y, z], name=\"test_dropout_default_mask_ratio\"\n)" }, { "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(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_default_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "default_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\"], outputs=[\"y\"], 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=0.2,\n)\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = x\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_dropout_random_old\",\n opset_imports=[helper.make_opsetid(\"\", 11)],\n)" }, { "summary": "training", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_default\"\n)" }, { "summary": "training_default_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_default_mask\",\n)" }, { "summary": "training_default_zero_ratio", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\"], 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(\n node, inputs=[x, r, t], outputs=[y], name=\"test_training_dropout_zero_ratio\"\n)" }, { "summary": "training_default_zero_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node,\n inputs=[x, r, t],\n outputs=[y, z],\n name=\"test_training_dropout_zero_ratio_mask\",\n)" }, { "summary": "training_ratio_mask", "code": "seed = np.int64(0)\nnode = onnx.helper.make_node(\n \"Dropout\", inputs=[\"x\", \"r\", \"t\"], outputs=[\"y\", \"z\"], 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(\n node, inputs=[x, r, t], outputs=[y, z], name=\"test_training_dropout_mask\"\n)" } ], "category": "Dropout" }, { "name": "DynamicQuantizeLSTM", "module": "com.microsoft", "version": 1, "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": "T2", "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, input_size, 4*hidden_size]`." }, { "name": "R", "type": "T2", "description": "The recurrence weight tensor. Concatenation of `R[iofc]` and `RB[iofc]` (if bidirectional) along dimension 0. This tensor has shape `[num_directions, hidden_size, 4*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." }, { "name": "W_scale", "type": "T", "description": "W's scale. Its size is [num_directions] for per-tensor/layer quantization, or [num_directions, 4*hidden_size] for per-channel quantization on the axis input_size." }, { "name": "W_zero_point", "type": "T2", "description": "W's zero point. Its size is [num_directions] for per-tensor/layer quantization, or [num_directions, 4*hidden_size] for per-channel quantization on the axis input_size." }, { "name": "R_scale", "type": "T", "description": "R's scale. Its size is [num_directions] for per-tensor/layer quantization, or [num_directions, 4*hidden_size] for per-channel quantization on the axis input_size." }, { "name": "R_zero_point", "type": "T2", "description": "R's zero point. Its size is [num_directions] for per-tensor/layer quantization, or [num_directions, 4*hidden_size] for per-channel quantization on the axis input_size." } ], "min_input": 12, "max_input": 12, "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, "outputs_range": "0 - 3", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain seq_lens to integer tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int32)" ] }, { "description": "Constrain weights types to 8 bit tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "DynamicQuantizeLinear", "module": "ai.onnx", "version": 11, "description": "A Function to fuse calculation for Scale, Zero Point and FP32->8Bit conversion of FP32 Input data.\nOutputs Scale, ZeroPoint and Quantized Input for a given FP32 Input.\nScale is calculated as:\n```\ny_scale = (maximum(0, max(x)) - minimum(0, min(x))) / (qmax - qmin)\n```\n\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(intermediate_zero_point)))\n```\n\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```\n\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", "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(\n \"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(\n node,\n inputs=[X],\n outputs=[Y, Y_Scale, Y_ZeroPoint],\n name=\"test_dynamicquantizelinear\",\n)\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(\n node,\n inputs=[X],\n outputs=[Y, Y_Scale, Y_ZeroPoint],\n name=\"test_dynamicquantizelinear_max_adjusted\",\n)\n\nX = (\n np.array([1, 2.1, 1.3, 2.5, 3.34, 4.0, 1.5, 2.6, 3.9, 4.0, 3.0, 2.345])\n .astype(np.float32)\n .reshape((3, 4))\n)\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(\n node,\n inputs=[X],\n outputs=[Y, Y_Scale, Y_ZeroPoint],\n name=\"test_dynamicquantizelinear_min_adjusted\",\n)" } ] }, { "name": "DynamicQuantizeMatMul", "module": "com.microsoft", "version": 1, "inputs": [ { "name": "A", "type": "T1", "description": "N-dimensional matrix A" }, { "name": "B", "type": "T2", "description": "N-dimensional matrix B" }, { "name": "b_scale", "type": "T1", "description": "Scale of quantized input 'B'. It could be a scalar or a 1-D tensor, which means a per-tensor or per-column quantization. If it's a 1-D tensor, its number of elements should be equal to the number of columns of input 'B'." }, { "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 1-D tensor, which means a per-tensor or per-column quantization. If it's a 1-D tensor, its number of elements should be equal to the number of columns of input 'B'." }, { "name": "bias", "type": "T1", "option": "optional", "description": "1D input tensor, whose dimension is same as B's last dimension" } ], "min_input": 3, "max_input": 5, "outputs": [ { "name": "Y", "type": "T1", "description": "Matrix multiply results from A * B" } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - 5", "type_constraints": [ { "description": "Constrain input A, b_scale and output Y data type as float tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain input B data type to 8-bit integer tensor.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)" ] } ] }, { "name": "DynamicSlice", "module": "ai.onnx", "version": 1, "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` inputs 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", "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." } ], "min_input": 3, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Sliced data tensor." } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - 4", "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)" ] } ] }, { "name": "DynamicSlice", "module": "ai.onnx", "version": 10, "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` inputs 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", "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." } ], "min_input": 3, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Sliced data tensor." } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - 4", "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)" ] } ] }, { "name": "DynamicTimeWarping", "module": "com.microsoft", "version": 1, "description": "Input is cost matrix where each value in input[r][c] is the cost for pass the point (r, c). From current point(r, c), points (r+1, c), (r+1, c+1) or (r, c+1) could be arrived in next move. Given such cost matrix, return dynamic time warping of shape [2, x], where the path made by all points (output[0][t], output[1][t])have the lowest cost among all paths from (0, 0) to (M-1, N-1).", "inputs": [ { "name": "input", "type": "F", "description": "Input cost tensor, it must be 2D tensor of shape M x N, or 1 x M x N" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "I", "description": "Output tensor. shape is [2, x], where max(M, N) <= x < M + N" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain to float tensors.", "type_param_str": "F", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain to integer types.", "type_param_str": "I", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "EPContext", "module": "com.microsoft", "version": 1, "description": "Onnx node container for EP context.", "attributes": [ { "name": "embed_mode", "type": "int64", "required": false, "default": 1, "description": "1: indicate ep_cache_context is the context content. 0: indicate ep_cache_context is the file path to the context content.The path is relative to this Onnx file. Default is 1." }, { "name": "ep_cache_context", "type": "string", "required": false, "description": "payload of the execution provider context if embed_mode=1, or path to the context file if embed_mode=0." }, { "name": "ep_sdk_version", "type": "string", "required": false, "description": "(Optional) SDK version used to convert the model." }, { "name": "hardware_architecture", "type": "string", "required": false, "description": "(Optional) Hardware architecture." }, { "name": "main_context", "type": "int64", "required": false, "default": 1, "description": "Usually each single EPContext associate with a graph partition.But for some case like QNN, it has single EPContext contains all partitions.In that case, the node with ep_cache_context should set main_context=1. Other nodes set main_context=0 and skip ep_cache_context.The path is relative to this Onnx file. Default is 1." }, { "name": "max_size", "type": "int64", "required": false, "description": "max size in the context. Usage depend on the EP." }, { "name": "notes", "type": "string", "required": false, "description": "(Optional) Some notes for the model" }, { "name": "onnx_model_filename", "type": "string", "required": false, "description": "(Optional) Filename of the original ONNX model." }, { "name": "partition_name", "type": "string", "required": false, "description": "(Optional) partitioned graph name." }, { "name": "source", "type": "string", "required": false, "description": "(Optional) the source used to generate the engine/context cache file. Ort EP or native SDK tool chain" } ], "inputs": [ { "name": "inputs", "type": "T", "list": true, "description": "List of tensors for inputs" } ], "min_input": 1, "max_input": 2147483647, "outputs": [ { "name": "outputs", "type": "T", "list": true, "description": "One or more outputs, list of tensors for outputs" } ], "min_output": 1, "max_output": 2147483647, "inputs_range": "1 - ∞", "outputs_range": "1 - ∞", "type_constraints": [ { "description": "Constrain input and output types.", "type_param_str": "T", "allowed_type_strs": [ "tensor(bool)", "tensor(int8)", "tensor(int16)", "tensor(int32)", "tensor(int64)", "tensor(uint8)", "tensor(uint16)", "tensor(uint32)", "tensor(uint64)", "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] } ] }, { "name": "EfficientNMS_TRT", "module": "ai.onnx", "version": 1, "description": "Efficient NMS TensorRT Plugin.", "attributes": [ { "name": "background_class", "type": "int64", "required": true, "description": "Background class ID." }, { "name": "box_coding", "type": "int64", "required": true, "description": "Encoding type for the boxes or anchors inputs." }, { "name": "iou_threshold", "type": "float32", "required": true, "description": "Box IOU threshold value." }, { "name": "max_output_boxes", "type": "int64", "required": true, "description": "Max detections to output." }, { "name": "plugin_version", "type": "string", "required": true, "description": "Version number of the TRT plugin." }, { "name": "score_activation", "type": "int64", "required": true, "description": "Activation function to apply to the scores input." }, { "name": "score_threshold", "type": "float32", "required": true, "description": "Score threshold value." } ], "inputs": [ { "name": "boxes", "type": "T", "description": "The boxes input tensor." }, { "name": "scores", "type": "T", "description": "The scores input tensor." }, { "name": "anchors", "type": "T", "option": "optional", "description": "The anchors input tensor." } ], "min_input": 2, "max_input": 3, "outputs": [ { "name": "num_detections", "type": "tensor(int32)", "description": "The num_detections output tensor." }, { "name": "detection_boxes", "type": "T", "description": "The detection_boxes output tensor." }, { "name": "detection_scores", "type": "T", "description": "The detection_scores output tensor." }, { "name": "detection_classes", "type": "tensor(int32)", "description": "The detection_classes output tensor." } ], "min_output": 4, "max_output": 4, "inputs_range": "2 - 3", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] } ] }, { "name": "Einsum", "module": "ai.onnx", "version": 12, "description": "An einsum of the form `term1, term2 -> output-term` produces an output tensor using the following equation\n\n```\noutput[output-term] = reduce-sum( input1[term1] * input2[term2] )\n```\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\", inputs=[\"x\"], outputs=[\"y\"], 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\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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\", inputs=[\"x\", \"y\"], outputs=[\"z\"], 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_scalar", "code": "Eqn = \"->\"\nnode = onnx.helper.make_node(\n \"Einsum\", inputs=[\"x\"], outputs=[\"y\"], equation=Eqn\n)\n\nX = np.array(5.0) # scalar input\nZ = einsum_reference_implementation(Eqn, (X,))\n\nexpect(node, inputs=[X], outputs=[Z], name=\"test_einsum_scalar\")" }, { "summary": "einsum_sum", "code": "Eqn = \"ij->i\"\nnode = onnx.helper.make_node(\n \"Einsum\", inputs=[\"x\"], outputs=[\"y\"], 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\", inputs=[\"x\"], outputs=[\"y\"], 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, "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": "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": "elu", "code": "node = onnx.helper.make_node(\"Elu\", inputs=[\"x\"], outputs=[\"y\"], alpha=2.0)\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], 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], 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], name=\"test_elu_default\")" } ], "category": "Activation" }, { "name": "Elu", "module": "ai.onnx", "version": 6, "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": "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": "elu", "code": "node = onnx.helper.make_node(\"Elu\", inputs=[\"x\"], outputs=[\"y\"], alpha=2.0)\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], 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], 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], name=\"test_elu_default\")" } ], "category": "Activation" }, { "name": "Elu", "module": "ai.onnx", "version": 22, "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": "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": "elu", "code": "node = onnx.helper.make_node(\"Elu\", inputs=[\"x\"], outputs=[\"y\"], alpha=2.0)\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], 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], 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], name=\"test_elu_default\")" } ], "category": "Activation" }, { "name": "EmbedLayerNormalization", "module": "com.microsoft", "version": 1, "description": "EmbedLayerNormalization is the fusion of embedding layer in BERT model, with optional mask processing.\nThe embedding layer takes input_ids (word IDs) and segment_ids (sentence IDs) to look up word_embedding, position_embedding,\nand segment_emedding; the embeddings are added then applied layer normalization using gamma and beta tensors.\nThe last input mask is optional. If mask is provided, mask index (that is position of first 0 in mask, or number of words)\nwill be calculated.", "attributes": [ { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999960041972e-13, "description": "The epsilon value to use to avoid division by zero." }, { "name": "mask_index_type", "type": "int64", "required": false, "description": "The mask index tensor type for shape inference (0: None, 1: 1D mask_index)" } ], "inputs": [ { "name": "input_ids", "type": "T1", "description": "2D words IDs with shape (batch_size, sequence_length)" }, { "name": "segment_ids", "type": "T1", "option": "optional", "description": "2D segment IDs with shape (batch_size, sequence_length)" }, { "name": "word_embedding", "type": "T", "description": "2D with shape (,hidden_size)" }, { "name": "position_embedding", "type": "T", "description": "2D with shape (, hidden_size)" }, { "name": "segment_embedding", "type": "T", "option": "optional", "description": "2D with shape (, hidden_size)" }, { "name": "gamma", "type": "T", "description": "1D gamma tensor for layer normalization with shape (hidden_size)" }, { "name": "beta", "type": "T", "description": "1D beta tensor for layer normalization with shape (hidden_size)" }, { "name": "mask", "type": "T1", "option": "optional", "description": "2D attention mask with shape (batch_size, sequence_length)" }, { "name": "position_ids", "type": "T1", "option": "optional", "description": "2D position ids with shape (batch_size, sequence_length) or (1, sequence_length)" } ], "min_input": 7, "max_input": 9, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, hidden_size)" }, { "name": "mask_index", "type": "T1", "option": "optional", "description": "1D mask_index tensor with shape (batch_size)" }, { "name": "embedding_sum", "type": "T", "option": "optional", "description": "sum of word_embedding and position_embedding without layer normalization" } ], "min_output": 1, "max_output": 3, "inputs_range": "7 - 9", "outputs_range": "1 - 3", "type_constraints": [ { "description": "Constrain input and output integer tensors types", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int32)" ] }, { "description": "Constrain input and output float tensors types.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] } ] }, { "name": "Equal", "module": "ai.onnx", "version": 1, "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], name=\"test_equal\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int8\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int16\")\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)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint64\")" }, { "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], name=\"test_equal_bcast\")" }, { "summary": "equal_string", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\", \"string3\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string\")" }, { "summary": "equal_string_broadcast", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string_broadcast\")" } ] }, { "name": "Equal", "module": "ai.onnx", "version": 7, "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], name=\"test_equal\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int8\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int16\")\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)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint64\")" }, { "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], name=\"test_equal_bcast\")" }, { "summary": "equal_string", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\", \"string3\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string\")" }, { "summary": "equal_string_broadcast", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string_broadcast\")" } ] }, { "name": "Equal", "module": "ai.onnx", "version": 11, "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], name=\"test_equal\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int8\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int16\")\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)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint64\")" }, { "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], name=\"test_equal_bcast\")" }, { "summary": "equal_string", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\", \"string3\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string\")" }, { "summary": "equal_string_broadcast", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string_broadcast\")" } ] }, { "name": "Equal", "module": "ai.onnx", "version": 13, "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], name=\"test_equal\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int8\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int16\")\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)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint64\")" }, { "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], name=\"test_equal_bcast\")" }, { "summary": "equal_string", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\", \"string3\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string\")" }, { "summary": "equal_string_broadcast", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string_broadcast\")" } ] }, { "name": "Equal", "module": "ai.onnx", "version": 19, "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 (non-complex) 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)", "tensor(string)" ] }, { "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], name=\"test_equal\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int8)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int8\")\n\nx = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\ny = (np.random.randn(3, 4, 5) * 10).astype(np.int16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_int16\")\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)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_uint64\")" }, { "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], name=\"test_equal_bcast\")" }, { "summary": "equal_string", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\", \"string3\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string\")" }, { "summary": "equal_string_broadcast", "code": "node = onnx.helper.make_node(\n \"Equal\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n)\nx = np.array([\"string1\", \"string2\"], dtype=np.dtype(object))\ny = np.array([\"string1\"], dtype=np.dtype(object))\nz = np.equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_equal_string_broadcast\")" } ] }, { "name": "Erf", "module": "ai.onnx", "version": 9, "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], name=\"test_erf\")" } ] }, { "name": "Erf", "module": "ai.onnx", "version": 13, "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 float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(bfloat16)", "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], name=\"test_erf\")" } ] }, { "name": "Exp", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_exp\")" } ] }, { "name": "Exp", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_exp\")" } ] }, { "name": "Exp", "module": "ai.onnx", "version": 13, "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(bfloat16)", "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], 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], name=\"test_exp\")" } ] }, { "name": "Expand", "module": "ai.onnx", "version": 8, "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(\n node,\n inputs=[data, new_shape],\n outputs=[expanded],\n name=\"test_expand_dim_changed\",\n)" }, { "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(\n node,\n inputs=[data, new_shape],\n outputs=[expanded],\n name=\"test_expand_dim_unchanged\",\n)" } ] }, { "name": "Expand", "module": "ai.onnx", "version": 13, "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(\n node,\n inputs=[data, new_shape],\n outputs=[expanded],\n name=\"test_expand_dim_changed\",\n)" }, { "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(\n node,\n inputs=[data, new_shape],\n outputs=[expanded],\n name=\"test_expand_dim_unchanged\",\n)" } ] }, { "name": "ExpandDims", "module": "com.microsoft", "version": 1, "description": "ExpandDims echo operator.", "inputs": [ { "name": "X", "type": "T", "description": "input" }, { "name": "axis", "type": "tensor(int32)", "description": "Specified axis to insert a dimension" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "T", "description": "output" } ], "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": "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": "EyeLike", "module": "ai.onnx", "version": 9, "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(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_eyelike_populate_off_main_diagonal\",\n)" }, { "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": "EyeLike", "module": "ai.onnx", "version": 22, "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." }, { "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(uint8)", "tensor(uint16)", "tensor(uint32)", "tensor(uint64)", "tensor(int8)", "tensor(int16)", "tensor(int32)", "tensor(int64)", "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bool)" ] }, { "description": "Constrain output types. Strings and complex are not supported.", "type_param_str": "T2", "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(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(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_eyelike_populate_off_main_diagonal\",\n)" }, { "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": "FastGelu", "module": "com.microsoft", "version": 1, "description": "GELU (Gaussian Error Linear Unit) approximation: Y=0.5*X*(1+tanh(0.797885*X+0.035677*X*X*X)) with an optional input of bias that will be added to X before GELU.", "inputs": [ { "name": "X", "type": "T", "description": "input tensor" }, { "name": "bias", "type": "T", "option": "optional", "description": "bias tensor" } ], "min_input": 1, "max_input": 2, "outputs": [ { "name": "Y", "type": "T", "description": "output tensor" } ], "min_output": 1, "max_output": 1, "inputs_range": "1 - 2", "type_constraints": [ { "description": "Constrain input and output types to float or half tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(double)", "tensor(float16)", "tensor(bfloat16)" ] } ] }, { "name": "FeatureVectorizer", "module": "ai.onnx.ml", "version": 1, "description": "Concatenates input tensors into one continuous output.
\n All input shapes are 2-D and are concatenated along the second dimension. 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, "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], 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(\n node,\n inputs=[a],\n outputs=[b],\n name=\"test_flatten_negative_axis\" + str(abs(i)),\n )" }, { "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], name=\"test_flatten_default_axis\")" } ], "category": "Shape" }, { "name": "Flatten", "module": "ai.onnx", "version": 9, "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], 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(\n node,\n inputs=[a],\n outputs=[b],\n name=\"test_flatten_negative_axis\" + str(abs(i)),\n )" }, { "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], name=\"test_flatten_default_axis\")" } ], "category": "Shape" }, { "name": "Flatten", "module": "ai.onnx", "version": 11, "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], 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(\n node,\n inputs=[a],\n outputs=[b],\n name=\"test_flatten_negative_axis\" + str(abs(i)),\n )" }, { "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], name=\"test_flatten_default_axis\")" } ], "category": "Shape" }, { "name": "Flatten", "module": "ai.onnx", "version": 13, "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], 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(\n node,\n inputs=[a],\n outputs=[b],\n name=\"test_flatten_negative_axis\" + str(abs(i)),\n )" }, { "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], name=\"test_flatten_default_axis\")" } ], "category": "Shape" }, { "name": "Flatten", "module": "ai.onnx", "version": 21, "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 up to IRv10.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "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], 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(\n node,\n inputs=[a],\n outputs=[b],\n name=\"test_flatten_negative_axis\" + str(abs(i)),\n )" }, { "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], name=\"test_flatten_default_axis\")" } ], "category": "Shape" }, { "name": "Flatten", "module": "ai.onnx", "version": 23, "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 up to IRv10.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "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], 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(\n node,\n inputs=[a],\n outputs=[b],\n name=\"test_flatten_negative_axis\" + str(abs(i)),\n )" }, { "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], name=\"test_flatten_default_axis\")" } ], "category": "Shape" }, { "name": "Flatten", "module": "ai.onnx", "version": 24, "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 up to IRv12.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "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], 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(\n node,\n inputs=[a],\n outputs=[b],\n name=\"test_flatten_negative_axis\" + str(abs(i)),\n )" }, { "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], name=\"test_flatten_default_axis\")" } ], "category": "Shape" }, { "name": "Flatten", "module": "ai.onnx", "version": 25, "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 up to IRv13.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "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], 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(\n node,\n inputs=[a],\n outputs=[b],\n name=\"test_flatten_negative_axis\" + str(abs(i)),\n )" }, { "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], name=\"test_flatten_default_axis\")" } ], "category": "Shape" }, { "name": "FlexAttention", "module": "ai.onnx.preview", "version": 1, "status": "experimental", "description": "Computes scaled dot-product attention over rank-4 (batched, multi-head) inputs,\nwith optional user-provided customization subgraphs at two stages:\n\n1. score_mod: Modify the attention score tensor after Q\u00b7K^T\n2. prob_mod: Modify the probability tensor after Softmax\n\nThis operator mirrors the capabilities of PyTorch's flex_attention:\nhttps://docs.pytorch.org/docs/stable/nn.attention.flex_attention.html\n\nInput Shapes (MUST be rank-4 tensors):\n- Q: `(batch_size, q_num_heads, q_sequence_length, head_size)`\n- K: `(batch_size, kv_num_heads, kv_sequence_length, head_size)`\n- V: `(batch_size, kv_num_heads, kv_sequence_length, v_head_size)`\n\nOutput Shape:\n- Y: `(batch_size, q_num_heads, q_sequence_length, v_head_size)`\n\nFlexAttention Computation:\n```\nScores = (Q @ K^T) * scale\nScores = score_mod(Scores) # if 'score_mod' is provided\nProbs = Softmax(Scores, axis=-1)\nProbs = prob_mod(Probs) # if 'prob_mod' is provided\nY = Probs @ V\n```\n\nGrouped Query Attention (GQA):\nWhen `q_num_heads != kv_num_heads`, each K/V head is shared by a contiguous\ngroup of query heads in head-index order. Let\n`group_size = q_num_heads / kv_num_heads`; then query head `h` uses K/V head\n`floor(h / group_size)`. `q_num_heads` must be a multiple of\n`kv_num_heads`.\n\nModifier Subgraphs (score_mod, prob_mod):\nEach modifier subgraph takes exactly one rank-4 tensor input and must produce\nexactly one rank-4 tensor output of the same shape and element type.\n- score_mod input/output shape: `(batch_size, q_num_heads, q_sequence_length, kv_sequence_length)`\n- prob_mod input/output shape: `(batch_size, q_num_heads, q_sequence_length, kv_sequence_length)`\nThe element type is determined by softmax_precision (defaults to float32 for\nnon-double inputs, otherwise double).\n\nMasking can be expressed in score_mod by writing masked positions as -inf (or a\nlarge negative value appropriate for the target precision).\n", "attributes": [ { "name": "prob_mod", "type": "graph", "required": false, "description": "Optional probability modifier subgraph with 1 rank-4 tensor input and 1 rank-4 tensor output of the same shape and element type: (probs) -> probs_out. probs has softmax_precision element type and shape (B, Hq, L, S). The output must preserve the input shape." }, { "name": "scale", "type": "float32", "required": false, "description": "Scaling factor for Q*K^T. Defaults to 1/sqrt(head_size)." }, { "name": "score_mod", "type": "graph", "required": false, "description": "Optional score modifier subgraph with 1 rank-4 tensor input and 1 rank-4 tensor output of the same shape and element type: (scores) -> scores_out. scores has softmax_precision element type and shape (B, Hq, L, S). The output must preserve the input shape." }, { "name": "softmax_precision", "type": "int64", "required": false, "description": "Floating-point precision for softmax computation. Defaults to float32 for non-double inputs, otherwise uses double. Must be explicitly specified for non-float types." } ], "inputs": [ { "name": "Q", "type": "T1", "description": "Query tensor with shape `(batch_size, q_num_heads, q_seq_len, head_size)`." }, { "name": "K", "type": "T1", "description": "Key tensor with shape `(batch_size, kv_num_heads, kv_seq_len, head_size)`." }, { "name": "V", "type": "T1", "description": "Value tensor with shape `(batch_size, kv_num_heads, kv_seq_len, v_head_size)`." } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "Y", "type": "T1", "description": "Output tensor with shape `(batch_size, q_num_heads, q_seq_len, v_head_size)`." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain Q, K, V to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "flexattention", "code": "\"\"\"Basic FlexAttention test with default settings.\"\"\"\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\n\nB, Hq, L, E = 2, 4, 8, 16\nS, Ev = 6, 16\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float32)\nK = np.random.rand(B, Hq, S, E).astype(np.float32)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float32)\n\n(Y,) = _compute_flex_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_causal_mask", "code": "\"\"\"FlexAttention with causal masking score_mod (Qwen-3, Gemma-3, Llama-3 pattern).\"\"\"\nscore_mod_graph = _make_score_mod_causal_mask_graph(TensorProto.FLOAT)\n\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\nscore_mod_attr = helper.make_attribute(\"score_mod\", score_mod_graph)\nnode.attribute.append(score_mod_attr)\n\nB, Hq, L, E = 1, 2, 4, 8\nS, Ev = 4, 8\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float32)\nK = np.random.rand(B, Hq, S, E).astype(np.float32)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float32)\n\n# Manually compute expected output with causal masking\nscale = 1.0 / np.sqrt(E)\nscores = np.einsum(\"bhle,bhse->bhls\", Q, K) * scale\n# Apply causal mask: set future positions to -inf\nq_idx = np.arange(L).reshape(1, 1, L, 1)\nk_idx = np.arange(S).reshape(1, 1, 1, S)\nmask = q_idx >= k_idx\nscores = np.where(mask, scores, -np.inf)\nprobs = np.exp(scores - scores.max(axis=-1, keepdims=True))\nprobs = probs / probs.sum(axis=-1, keepdims=True)\nY = np.einsum(\"bhls,bhsv->bhlv\", probs, V).astype(np.float32)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_causal_mask\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_diff_head_sizes", "code": "\"\"\"FlexAttention with different head sizes for Q/K vs V.\"\"\"\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\n\nB, Hq, L, E = 2, 4, 8, 16\nS, Ev = 6, 32 # V has different head size\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float32)\nK = np.random.rand(B, Hq, S, E).astype(np.float32)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float32)\n\n(Y,) = _compute_flex_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_diff_head_sizes\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_double", "code": "\"\"\"FlexAttention with double precision inputs.\"\"\"\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\n\nB, Hq, L, E = 2, 4, 8, 16\nS, Ev = 6, 16\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float64)\nK = np.random.rand(B, Hq, S, E).astype(np.float64)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float64)\n\n(Y,) = _compute_flex_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_double\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_fp16", "code": "\"\"\"FlexAttention with float16 inputs.\"\"\"\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\n\nB, Hq, L, E = 2, 4, 8, 16\nS, Ev = 6, 16\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float16)\nK = np.random.rand(B, Hq, S, E).astype(np.float16)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float16)\n\n(Y,) = _compute_flex_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_fp16\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_gqa", "code": "\"\"\"FlexAttention with Grouped Query Attention (GQA).\"\"\"\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\n\nB, Hq, Hkv, L, S, E, Ev = 2, 8, 2, 4, 6, 16, 16\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float32)\nK = np.random.rand(B, Hkv, S, E).astype(np.float32)\nV = np.random.rand(B, Hkv, S, Ev).astype(np.float32)\n\n(Y,) = _compute_flex_attention(Q, K, V)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_gqa\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_prob_mod", "code": "\"\"\"FlexAttention with prob_mod subgraph (scales probabilities).\"\"\"\nscale_value = 0.5\nprob_mod_graph = _make_prob_mod_scale_graph(scale_value, TensorProto.FLOAT)\n\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\nprob_mod_attr = helper.make_attribute(\"prob_mod\", prob_mod_graph)\nnode.attribute.append(prob_mod_attr)\n\nB, Hq, L, E = 1, 2, 3, 4\nS, Ev = 3, 4\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float32)\nK = np.random.rand(B, Hq, S, E).astype(np.float32)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float32)\n\nscale = 1.0 / np.sqrt(E)\nscores = np.einsum(\"bhle,bhse->bhls\", Q, K) * scale\nprobs = np.exp(scores - scores.max(axis=-1, keepdims=True))\nprobs = probs / probs.sum(axis=-1, keepdims=True)\nprobs = probs * scale_value\nY = np.einsum(\"bhls,bhsv->bhlv\", probs, V).astype(np.float32)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_prob_mod\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_relative_positional", "code": "\"\"\"FlexAttention with relative positional bias score_mod.\"\"\"\nscore_mod_graph = _make_score_mod_relative_positional_graph(TensorProto.FLOAT)\n\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\nscore_mod_attr = helper.make_attribute(\"score_mod\", score_mod_graph)\nnode.attribute.append(score_mod_attr)\n\nB, Hq, L, E = 1, 2, 4, 8\nS, Ev = 4, 8\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float32)\nK = np.random.rand(B, Hq, S, E).astype(np.float32)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float32)\n\n# Manually compute expected output with relative positional bias\nscale = 1.0 / np.sqrt(E)\nscores = np.einsum(\"bhle,bhse->bhls\", Q, K) * scale\nq_idx = np.arange(L).reshape(-1, 1)\nk_idx = np.arange(S).reshape(1, -1)\nrel_pos = (q_idx - k_idx).astype(np.float32)\nscores = scores + rel_pos\nprobs = np.exp(scores - scores.max(axis=-1, keepdims=True))\nprobs = probs / probs.sum(axis=-1, keepdims=True)\nY = np.einsum(\"bhls,bhsv->bhlv\", probs, V).astype(np.float32)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_relative_positional\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_scaled", "code": "\"\"\"FlexAttention with explicit scale attribute.\"\"\"\nscale = 0.1\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n scale=scale,\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\n\nB, Hq, L, E = 2, 4, 8, 16\nS, Ev = 6, 16\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float32)\nK = np.random.rand(B, Hq, S, E).astype(np.float32)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float32)\n\n(Y,) = _compute_flex_attention(Q, K, V, scale=scale)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_scaled\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_score_mod", "code": "\"\"\"FlexAttention with score_mod subgraph (adds bias to scores).\"\"\"\nbias_value = 0.5\nscore_mod_graph = _make_score_mod_bias_graph(bias_value, TensorProto.FLOAT)\n\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\n# Add score_mod as a graph attribute\nscore_mod_attr = helper.make_attribute(\"score_mod\", score_mod_graph)\nnode.attribute.append(score_mod_attr)\n\nB, Hq, L, E = 1, 2, 3, 4\nS, Ev = 3, 4\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float32)\nK = np.random.rand(B, Hq, S, E).astype(np.float32)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float32)\n\nscale = 1.0 / np.sqrt(E)\nscores = np.einsum(\"bhle,bhse->bhls\", Q, K) * scale\nscores = scores + bias_value # score_mod: add bias\nprobs = np.exp(scores - scores.max(axis=-1, keepdims=True))\nprobs = probs / probs.sum(axis=-1, keepdims=True)\nY = np.einsum(\"bhls,bhsv->bhlv\", probs, V).astype(np.float32)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_score_mod\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" }, { "summary": "flexattention_soft_cap", "code": "\"\"\"FlexAttention with soft capping score_mod (Gemma-2 pattern).\"\"\"\ncap_value = 20.0\nscore_mod_graph = _make_score_mod_soft_cap_graph(cap_value, TensorProto.FLOAT)\n\nnode = helper.make_node(\n \"FlexAttention\",\n inputs=[\"Q\", \"K\", \"V\"],\n outputs=[\"Y\"],\n domain=AI_ONNX_PREVIEW_DOMAIN,\n)\nscore_mod_attr = helper.make_attribute(\"score_mod\", score_mod_graph)\nnode.attribute.append(score_mod_attr)\n\nB, Hq, L, E = 1, 2, 4, 8\nS, Ev = 4, 8\n\nQ = np.random.rand(B, Hq, L, E).astype(np.float32)\nK = np.random.rand(B, Hq, S, E).astype(np.float32)\nV = np.random.rand(B, Hq, S, Ev).astype(np.float32)\n\n# Manually compute expected output with soft capping\nscale = 1.0 / np.sqrt(E)\nscores = np.einsum(\"bhle,bhse->bhls\", Q, K) * scale\nscores = np.tanh(scores / cap_value) * cap_value\nprobs = np.exp(scores - scores.max(axis=-1, keepdims=True))\nprobs = probs / probs.sum(axis=-1, keepdims=True)\nY = np.einsum(\"bhls,bhsv->bhlv\", probs, V).astype(np.float32)\n\nexpect(\n node,\n inputs=[Q, K, V],\n outputs=[Y],\n name=\"test_flexattention_soft_cap\",\n opset_imports=[\n helper.make_opsetid(\"\", 26),\n helper.make_opsetid(AI_ONNX_PREVIEW_DOMAIN, 1),\n ],\n)" } ] }, { "name": "Floor", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_floor\")" } ] }, { "name": "Floor", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_floor\")" } ] }, { "name": "Floor", "module": "ai.onnx", "version": 13, "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. If x is integral, +0, -0, NaN, or infinite, x itself is returned.\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], 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], name=\"test_floor\")" } ] }, { "name": "FusedConv", "module": "com.microsoft", "version": 1, "description": "The fused convolution operator schema is the same as Conv besides it includes an attribute\nactivation.", "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "name": "dilations", "type": "int64[]", "required": false }, { "name": "group", "type": "int64", "required": false, "default": 1 }, { "name": "kernel_shape", "type": "int64[]", "required": false }, { "name": "pads", "type": "int64[]", "required": false }, { "name": "strides", "type": "int64[]", "required": false } ], "inputs": [ { "name": "X", "type": "T" }, { "name": "W", "type": "T" }, { "name": "B", "type": "T", "option": "optional" }, { "name": "Z", "type": "T", "option": "optional" } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "Y", "type": "T" } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "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": "Layer" }, { "name": "FusedGemm", "module": "com.microsoft", "version": 1, "description": "The FusedGemm operator schema is the same as Gemm besides it includes attributes\nactivation and leaky_relu_alpha.", "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_alpha", "type": "float32", "required": false }, { "name": "activation_beta", "type": "float32", "required": false }, { "name": "activation_gamma", "type": "float32", "required": false }, { "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": "Input tensor C. 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)" ] } ] }, { "name": "FusedMatMul", "module": "com.microsoft", "version": 1, "description": "Matrix product that behaves like numpy.matmul: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matmul.html\n", "attributes": [ { "name": "alpha", "type": "float32", "required": false, "default": 1.0, "description": "Scalar multiplier for the product of the input tensors." }, { "name": "transA", "type": "int64", "required": false, "description": "Whether A should be transposed on the last two dimensions before doing multiplication" }, { "name": "transB", "type": "int64", "required": false, "description": "Whether B should be transposed on the last two dimensions before doing multiplication" }, { "name": "transBatchA", "type": "int64", "required": false, "description": "Whether A should be transposed on the 1st dimension and batch dimensions (dim-1 to dim-rank-2) before doing multiplication" }, { "name": "transBatchB", "type": "int64", "required": false, "description": "Whether B should be transposed on the 1st dimension and batch dimensions (dim-1 to dim-rank-2) before doing multiplication" } ], "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" } ], "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)" ] } ] }, { "name": "FusedMatMulActivation", "module": "com.microsoft", "version": 1, "description": "Executes the same operation as FusedMatMul, but also has an activation function fused to its output.\n", "attributes": [ { "name": "activation", "type": "string", "required": true }, { "name": "activation_alpha", "type": "float32", "required": false }, { "name": "activation_axis", "type": "int64", "required": false }, { "name": "activation_beta", "type": "float32", "required": false }, { "name": "activation_gamma", "type": "float32", "required": false }, { "name": "alpha", "type": "float32", "required": false, "default": 1.0, "description": "Scalar multiplier for the product of the input tensors." }, { "name": "transA", "type": "int64", "required": false, "description": "Whether A should be transposed on the last two dimensions before doing multiplication" }, { "name": "transB", "type": "int64", "required": false, "description": "Whether B should be transposed on the last two dimensions before doing multiplication" }, { "name": "transBatchA", "type": "int64", "required": false, "description": "Whether A should be transposed on the 1st dimension and batch dimensions (dim-1 to dim-rank-2) before doing multiplication" }, { "name": "transBatchB", "type": "int64", "required": false, "description": "Whether B should be transposed on the 1st dimension and batch dimensions (dim-1 to dim-rank-2) before doing multiplication" } ], "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" } ], "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)" ] } ] }, { "name": "GRU", "module": "ai.onnx", "version": 1, "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_gru_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).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\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(\n np.float32\n)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_with_initial_bias\",\n)" }, { "summary": "seq_length", "code": "input = np.array(\n [\n [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],\n [[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]],\n ]\n).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(\n np.float32\n)\nR = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(\n np.float32\n)\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 = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_seq_length\",\n)" } ], "category": "Layer" }, { "name": "GRU", "module": "ai.onnx", "version": 3, "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_gru_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).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\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(\n np.float32\n)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_with_initial_bias\",\n)" }, { "summary": "seq_length", "code": "input = np.array(\n [\n [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],\n [[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]],\n ]\n).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(\n np.float32\n)\nR = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(\n np.float32\n)\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 = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_seq_length\",\n)" } ], "category": "Layer" }, { "name": "GRU", "module": "ai.onnx", "version": 7, "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_gru_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).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\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(\n np.float32\n)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_with_initial_bias\",\n)" }, { "summary": "seq_length", "code": "input = np.array(\n [\n [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],\n [[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]],\n ]\n).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(\n np.float32\n)\nR = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(\n np.float32\n)\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 = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_seq_length\",\n)" } ], "category": "Layer" }, { "name": "GRU", "module": "ai.onnx", "version": 14, "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* `z` - update gate\n* `r` - reset gate\n* `h` - hidden gate\n* `t` - time step (t-1 means previous time step)\n* `W[zrh]` - W parameter weight matrix for update, reset, and hidden gates\n* `R[zrh]` - R recurrence weight matrix for update, reset, and hidden gates\n* `Wb[zrh]` - W bias vectors for update, reset, and hidden gates\n* `Rb[zrh]` - R bias vectors for update, reset, and hidden gates\n* `WB[zrh]` - W parameter weight matrix for backward update, reset, and hidden gates\n* `RB[zrh]` - R recurrence weight matrix for backward update, reset, and hidden gates\n* `WBb[zrh]` - W bias vectors for backward update, reset, and hidden gates\n* `RBb[zrh]` - R bias vectors for backward update, reset, and hidden gates\n* `H` - Hidden state\n* `num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n* Relu(x) - max(0, x)\n* Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n* Sigmoid(x) - 1/(1 + e^{-x})\n\nNOTE:\n Below are optional\n\n* Affine(x) - alpha * x + beta\n* LeakyRelu(x) - x if x >= 0 else alpha * x\n* ThresholdedRelu(x) - x if x >= alpha else 0\n* ScaledTanh(x) - alpha * Tanh(beta * x)\n* HardSigmoid(x) - min(max(alpha * x + beta, 0), 1)\n* Elu(x) - x if x >= 0 else alpha * (e^x - 1)\n* Softsign(x) - x/(1 + |x|)\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* rt = f(Xt*(Wr^T) + Ht-1*(Rr^T) + Wbr + Rbr)\n* ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*(Rh^T) + Rbh + Wbh) # default, when linear_before_reset = 0\n* ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*(Rh^T) + Rbh)) + Wbh) # when linear_before_reset != 0\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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_gru_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).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\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(\n np.float32\n)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_with_initial_bias\",\n)" }, { "summary": "seq_length", "code": "input = np.array(\n [\n [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],\n [[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]],\n ]\n).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(\n np.float32\n)\nR = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(\n np.float32\n)\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 = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_seq_length\",\n)" } ], "category": "Layer" }, { "name": "GRU", "module": "ai.onnx", "version": 22, "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* `z` - update gate\n* `r` - reset gate\n* `h` - hidden gate\n* `t` - time step (t-1 means previous time step)\n* `W[zrh]` - W parameter weight matrix for update, reset, and hidden gates\n* `R[zrh]` - R recurrence weight matrix for update, reset, and hidden gates\n* `Wb[zrh]` - W bias vectors for update, reset, and hidden gates\n* `Rb[zrh]` - R bias vectors for update, reset, and hidden gates\n* `WB[zrh]` - W parameter weight matrix for backward update, reset, and hidden gates\n* `RB[zrh]` - R recurrence weight matrix for backward update, reset, and hidden gates\n* `WBb[zrh]` - W bias vectors for backward update, reset, and hidden gates\n* `RBb[zrh]` - R bias vectors for backward update, reset, and hidden gates\n* `H` - Hidden state\n* `num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n* Relu(x) - max(0, x)\n* Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n* Sigmoid(x) - 1/(1 + e^{-x})\n\nNOTE:\n Below are optional\n\n* Affine(x) - alpha * x + beta\n* LeakyRelu(x) - x if x >= 0 else alpha * x\n* ThresholdedRelu(x) - x if x >= alpha else 0\n* ScaledTanh(x) - alpha * Tanh(beta * x)\n* HardSigmoid(x) - min(max(alpha * x + beta, 0), 1)\n* Elu(x) - x if x >= 0 else alpha * (e^x - 1)\n* Softsign(x) - x/(1 + |x|)\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* rt = f(Xt*(Wr^T) + Ht-1*(Rr^T) + Wbr + Rbr)\n* ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*(Rh^T) + Rbh + Wbh) # default, when linear_before_reset = 0\n* ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*(Rh^T) + Rbh)) + Wbh) # when linear_before_reset != 0\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(bfloat16)", "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_gru_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).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\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\ngru = GRUHelper(X=input, W=W, R=R)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(\n np.float32\n)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), axis=1)\n\ngru = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_with_initial_bias\",\n)" }, { "summary": "seq_length", "code": "input = np.array(\n [\n [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],\n [[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]],\n ]\n).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(\n np.float32\n)\nR = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(\n np.float32\n)\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 = GRUHelper(X=input, W=W, R=R, B=B)\n_, Y_h = gru.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_gru_seq_length\",\n)" } ], "category": "Layer" }, { "name": "GRUUnit", "module": "ai.onnx", "version": 1, "description": "GRUUnit computes the activations of a standard GRU,\nin a sequence-length aware fashion.\nConcretely, given the (fused) inputs X (TxNxD), the previous hidden\nstate (NxD), and the sequence lengths (N), computes the GRU\nactivations, avoiding computation if the input is invalid (as in, the\nvalue at X[t][n] >= seqLengths[n].\n", "attributes": [ { "name": "drop_states", "type": "int64", "required": false, "description": "Bool to determine if hidden state is zeroes or passed along for timesteps past the given sequence_length." } ], "inputs": [ { "name": "hidden_prev", "type": "T", "description": "The previous GRU hidden state." }, { "name": "gates", "type": "T", "description": "Unactivated gate outputs from forget, update, and output gates, pre-activation." }, { "name": "seq_lengths", "type": "T", "description": "Array of sequence lengths. len(seq_lengths) should equal batch size N." }, { "name": "t", "type": "T", "description": "The timestep for this operation." } ], "min_input": 4, "max_input": 4, "outputs": [ { "name": "hidden", "type": "T", "description": "The new GRU hidden state calculated by this op." } ], "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)" ] } ] }, { "name": "GRUUnit", "module": "ai.onnx", "version": 10, "description": "GRUUnit computes the activations of a standard GRU,\nin a sequence-length aware fashion.\nConcretely, given the (fused) inputs X (TxNxD), the previous hidden\nstate (NxD), and the sequence lengths (N), computes the GRU\nactivations, avoiding computation if the input is invalid (as in, the\nvalue at X[t][n] >= seqLengths[n].\n", "attributes": [ { "name": "drop_states", "type": "int64", "required": false, "description": "Bool to determine if hidden state is zeroes or passed along for timesteps past the given sequence_length." } ], "inputs": [ { "name": "hidden_prev", "type": "T", "description": "The previous GRU hidden state." }, { "name": "gates", "type": "T", "description": "Unactivated gate outputs from forget, update, and output gates, pre-activation." }, { "name": "seq_lengths", "type": "T", "description": "Array of sequence lengths. len(seq_lengths) should equal batch size N." }, { "name": "t", "type": "T", "description": "The timestep for this operation." } ], "min_input": 4, "max_input": 4, "outputs": [ { "name": "hidden", "type": "T", "description": "The new GRU hidden state calculated by this op." } ], "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)" ] } ] }, { "name": "GatedRelativePositionBias", "module": "com.microsoft", "version": 1, "description": "query_layer = (query_layer + query_bias).reshape(batch_size, seq_len, num_heads, head_size).transpose(1, 2)\n gate_u, gate_r = torch.sigmoid(\n self.gate_ur_linear(query_layer).view(batch_size, num_head, seq_len, 2, D/2).sum(-1, keepdim=False)\n ).chunk(2, dim=-1)\n gate_u_1 = gate_u * (gate_r * self.eco_a - 1.0) + 2.0\n rel_pos_bias = gate_u_1 * rel_pos\n", "attributes": [ { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" } ], "inputs": [ { "name": "query_layer", "type": "T", "description": "tensor with shape (batch_size, seq_len, num_heads x head_size) or (token_count, num_heads x head_size)" }, { "name": "query_bias", "type": "T", "description": "1-d tensor with shape (num_heads x head_size)" }, { "name": "rel_pos", "type": "T", "description": "tensor with shape (1, num_head, seq_len, seq_len)" }, { "name": "weight", "type": "T", "description": "gemm weight for the gated_ur_linear, shape (head_size, D), D is divisible by 2" }, { "name": "bias", "type": "T", "description": "bias for the gated_ur_linear, shape (D)" }, { "name": "eco_a", "type": "T", "description": "tensor of shape (1, num_heads, 1, 1)" }, { "name": "token_offset", "type": "M", "option": "optional", "description": "offset of each token with shape (batch_size, seq_len)" } ], "min_input": 6, "max_input": 7, "outputs": [ { "name": "output", "type": "T", "description": "output tensor with shape (batch_size, num_heads, seq_len, seq_len)" } ], "min_output": 1, "max_output": 1, "inputs_range": "6 - 7", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain token_offset to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "Gather", "module": "ai.onnx", "version": 1, "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 [[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]" } ], "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_0\",\n)" }, { "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_1\",\n)" }, { "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_2d_indices\",\n)" }, { "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_negative_indices\",\n)" } ], "category": "Transform" }, { "name": "Gather", "module": "ai.onnx", "version": 11, "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(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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_0\",\n)" }, { "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_1\",\n)" }, { "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_2d_indices\",\n)" }, { "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_negative_indices\",\n)" } ], "category": "Transform" }, { "name": "Gather", "module": "ai.onnx", "version": 13, "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\nIt is an indexing operation that indexes into the input `data` along a single (specified) axis.\nEach entry in `indices` produces a `r-1` dimensional slice of the input tensor.\nThe entire operation produces, conceptually, a `q`-dimensional tensor of `r-1` dimensional slices,\nwhich is arranged into a `q + (r-1)`-dimensional tensor, with the `q` dimensions taking the\nplace of the original `axis` that is being indexed into.\n\nThe following few examples illustrate how `Gather` works for specific shapes of `data`,\n`indices`, and given value of `axis`:\n| data shape | indices shape | axis | output shape | output equation |\n| --- | --- | --- | --- | --- |\n| (P, Q) | ( ) (a scalar) | 0 | (Q) | output[q] = data[indices, q] |\n| (P, Q, R) | ( ) (a scalar) | 1 | (P, R) | output[p, r] = data[p, indices, r] |\n| (P, Q) | (R, S) | 0 | (R, S, Q) | output[r, s, q] = data[ [indices[r, s], q] |\n| (P, Q) | (R, S) | 1 | (P, R, S) | output[p, r, s] = data[ p, indices[r, s]] |\n\nMore generally, if `axis = 0`, let `k = indices[i_{0}, ..., i_{q-1}]`\nthen `output[i_{0}, ..., i_{q-1}, j_{0}, ..., j_{r-2}] = input[k , j_{0}, ..., j_{r-2}]`:\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\nindices = [\n [0, 1],\n [1, 2],\n]\noutput = [\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```\n\nIf `axis = 1`, let `k = indices[i_{0}, ..., i_{q-1}]`\nthen `output[j_{0}, i_{0}, ..., i_{q-1}, j_{1}, ..., j_{r-2}] = input[j_{0}, k, j_{1}, ..., j_{r-2}]`:\n\n```\ndata = [\n [1.0, 1.2, 1.9],\n [2.3, 3.4, 3.9],\n [4.5, 5.7, 5.9],\n]\nindices = [\n [0, 2],\n]\naxis = 1,\noutput = [\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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_0\",\n)" }, { "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_1\",\n)" }, { "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_2d_indices\",\n)" }, { "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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_negative_indices\",\n)" } ], "category": "Transform" }, { "name": "GatherBlockQuantized", "module": "com.microsoft", "version": 1, "description": "GatherBlockQuantized is a Gather with data quantized. It is similar to Gather (https://github.com/onnx/onnx/blob/main/docs/Operators.md#gather) with differences:\n 1. Input `data` is a constant. It is quantized block-wise along attribute `quantize_axis` with block size specified by attribute `block_size`.\n `block_size` must be a power of 2 and not smaller than 16, like 16, 32, 64, 128, ...\n 2. Input `data`'s scale and zero point are specified by input `scales` and `zero_points`. `scales` and `zero_points` are also constants.\n If `zero_points` is not provided, the default value is 0 for int4/uint4, or 2^(bits-1) for uint8.\n 3. During the op execution, `data` and `indices` are first used to generate the quantized output. Then, `scales` and `zero_points` are used\n to dequantize the output.\n 4. The `output` and `scales` have the same type. The `data` and `zero_points` have the same type.\n 5. For uint8 data, the `gather_axis` must be 0. The supported `bits` values for uint8 data are 2, 4, and 8;\n for `bits` < 8 the values are packed along the last dimension (low-order bits first).\n", "attributes": [ { "name": "bits", "type": "int64", "required": false, "default": 4, "description": "Number of bits used for weight quantization. Must be 2, 4 or 8. " }, { "name": "block_size", "type": "int64", "required": false, "default": 128, "description": "(Optional) block size used for weight quantization. It needs to be a power of 2 and not smaller than 16." }, { "name": "gather_axis", "type": "int64", "required": false, "description": "(Optional) Which axis to gather on. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." }, { "name": "quantize_axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) Which axis to block-wise quantize. Negative value means counting dimensions from the back. Accepted range is [-r, r-1] where r = rank(data)." } ], "inputs": [ { "name": "data", "type": "T1", "description": "Tensor of rank r >= 1. Block-wise quantized." }, { "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." }, { "name": "scales", "type": "T2", "description": "quantization scale" }, { "name": "zero_points", "type": "T1", "option": "optional", "description": "quantization zero points" } ], "min_input": 3, "max_input": 4, "outputs": [ { "name": "output", "type": "T2", "description": "Dequantized output tensor of rank q + (r - 1)." } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - 4", "type_constraints": [ { "description": "Constrain quantized types.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int4)", "tensor(uint4)", "tensor(uint8)" ] }, { "description": "Constrain dequantized types.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain indices to integer types.", "type_param_str": "Tind", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ] }, { "name": "GatherElements", "module": "ai.onnx", "version": 11, "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], [3, 4]], dtype=np.float32)\nindices = np.array([[0, 0], [1, 0]], dtype=np.int32)\n\ny = gather_elements(data, indices, axis)\n# print(y) produces\n# [[1, 1],\n# [4, 3]]\n\nexpect(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_elements_0\",\n)" }, { "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], [4, 5, 6], [7, 8, 9]], dtype=np.float32)\nindices = np.array([[1, 2, 0], [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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_elements_1\",\n)" }, { "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], [4, 5, 6], [7, 8, 9]], dtype=np.float32)\nindices = np.array([[-1, -2, 0], [-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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_elements_negative_indices\",\n)" } ] }, { "name": "GatherElements", "module": "ai.onnx", "version": 13, "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```\nout[i][j][k] = input[index[i][j][k]][j][k] if axis = 0,\nout[i][j][k] = input[i][index[i][j][k]][k] if axis = 1,\nout[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```\ndata = [\n [1, 2],\n [3, 4],\n]\nindices = [\n [0, 0],\n [1, 0],\n]\naxis = 1\noutput = [\n [1, 1],\n [4, 3],\n]\n```\nExample 2:\n```\ndata = [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9],\n]\nindices = [\n [1, 2, 0],\n [2, 0, 0],\n]\naxis = 0\noutput = [\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], [3, 4]], dtype=np.float32)\nindices = np.array([[0, 0], [1, 0]], dtype=np.int32)\n\ny = gather_elements(data, indices, axis)\n# print(y) produces\n# [[1, 1],\n# [4, 3]]\n\nexpect(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_elements_0\",\n)" }, { "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], [4, 5, 6], [7, 8, 9]], dtype=np.float32)\nindices = np.array([[1, 2, 0], [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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_elements_1\",\n)" }, { "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], [4, 5, 6], [7, 8, 9]], dtype=np.float32)\nindices = np.array([[-1, -2, 0], [-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(\n node,\n inputs=[data, indices.astype(np.int64)],\n outputs=[y],\n name=\"test_gather_elements_negative_indices\",\n)" } ] }, { "name": "GatherND", "module": "ai.onnx", "version": 11, "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(\n node,\n inputs=[data, indices],\n outputs=[output],\n name=\"test_gathernd_example_float32\",\n)" }, { "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(\n node,\n inputs=[data, indices],\n outputs=[output],\n name=\"test_gathernd_example_int32\",\n)" }, { "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(\n node,\n inputs=[data, indices],\n outputs=[output],\n name=\"test_gathernd_example_int32_batch_dim1\",\n)" } ] }, { "name": "GatherND", "module": "ai.onnx", "version": 12, "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(\n node,\n inputs=[data, indices],\n outputs=[output],\n name=\"test_gathernd_example_float32\",\n)" }, { "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(\n node,\n inputs=[data, indices],\n outputs=[output],\n name=\"test_gathernd_example_int32\",\n)" }, { "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(\n node,\n inputs=[data, indices],\n outputs=[output],\n name=\"test_gathernd_example_int32_batch_dim1\",\n)" } ] }, { "name": "GatherND", "module": "ai.onnx", "version": 13, "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```\nbatch_dims = 0\ndata = [[0,1],[2,3]] # data_shape = [2, 2]\nindices = [[0,0],[1,1]] # indices_shape = [2, 2]\noutput = [0,3] # output_shape = [2]\n```\n\n**Example 2**\n\n```\nbatch_dims = 0\ndata = [[0,1],[2,3]] # data_shape = [2, 2]\nindices = [[1],[0]] # indices_shape = [2, 1]\noutput = [[2,3],[0,1]] # output_shape = [2, 2]\n```\n\n**Example 3**\n\n```\nbatch_dims = 0\ndata = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\nindices = [[0,1],[1,0]] # indices_shape = [2, 2]\noutput = [[2,3],[4,5]] # output_shape = [2, 2]\n```\n\n**Example 4**\n\n```\nbatch_dims = 0\ndata = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\nindices = [[[0,1]],[[1,0]]] # indices_shape = [2, 1, 2]\noutput = [[[2,3]],[[4,5]]] # output_shape = [2, 1, 2]\n```\n\n**Example 5**\n\n```\nbatch_dims = 1\ndata = [[[0,1],[2,3]],[[4,5],[6,7]]] # data_shape = [2, 2, 2]\nindices = [[1],[0]] # indices_shape = [2, 1]\noutput = [[2,3],[4,5]] # output_shape = [2, 2]\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(\n node,\n inputs=[data, indices],\n outputs=[output],\n name=\"test_gathernd_example_float32\",\n)" }, { "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(\n node,\n inputs=[data, indices],\n outputs=[output],\n name=\"test_gathernd_example_int32\",\n)" }, { "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(\n node,\n inputs=[data, indices],\n outputs=[output],\n name=\"test_gathernd_example_int32_batch_dim1\",\n)" } ] }, { "name": "GatherND", "module": "com.microsoft", "version": 1, "description": "Given `data` tensor of rank r >= 1, and `indices` tensor of rank q >= 1, gather\nslices of `data` into an output tensor of rank q - 1 + r - indices[-1].\nExample 1:\n data = [[0,1],[2,3]]\n indices = [[0,0],[1,1]]\n output = [0,3]\nExample 2:\n data = [[0,1],[2,3]]\n indices = [[1],[0]]\n output = [[2,3],[0,1]]\nExample 3:\n data = [[[0,1],[2,3]],[[4,5],[6,7]]]\n indices = [[0,1],[1,0]]\n output = [[2,3],[4,5]]\nExample 4:\n data = [[[0,1],[2,3]],[[4,5],[6,7]]]\n indices = [[[0,1]],[[1,0]]]\n output = [[[2,3]],[[4,5]]]\n", "inputs": [ { "name": "data", "type": "T", "description": "Tensor of rank r >= 1." }, { "name": "indices", "type": "Tind", "description": "Tensor of rank q >= 1." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "output", "type": "T", "description": "Tensor of rank q-1+r-indices[-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 indice type to int32 or int64", "type_param_str": "Tind", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ] }, { "name": "Gelu", "module": "ai.onnx", "version": 20, "description": "Gelu takes one input data (Tensor) and produces one\noutput data (Tensor) where the gaussian error linear units function,\n$y = 0.5 * x * (1 + erf(x/sqrt(2)))$ is applied to the tensor elementwise.\nIf the attribute \"approximate\" is set to \"tanh\", the function estimation,\n$y = 0.5 * x * (1 + Tanh(sqrt(2/\\pi) * (x + 0.044715 * x^3)))$ is used and applied\nto the tensor elementwise.\n\n", "attributes": [ { "name": "approximate", "type": "string", "required": false, "default": "none", "description": "Gelu approximation algorithm: `\"tanh\"`, `\"none\"`(default).`\"none\"`: do not use approximation.`\"tanh\"`: use tanh approximation." } ], "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": "gelu_default", "code": "node = onnx.helper.make_node(\"Gelu\", inputs=[\"x\"], outputs=[\"y\"])\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-0.15865526, 0., 0.84134474]\ny = (0.5 * x * (1 + np.vectorize(math.erf)(x / np.sqrt(2)))).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_gelu_default_1\")\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\n# expected output [2.99595031, 3.99987331, 4.99999857]\ny = (0.5 * x * (1 + np.vectorize(math.erf)(x / np.sqrt(2)))).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_gelu_default_2\")" }, { "summary": "gelu_tanh", "code": "node = onnx.helper.make_node(\n \"Gelu\", inputs=[\"x\"], outputs=[\"y\"], approximate=\"tanh\"\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-0.158808, 0., 0.841192]\ny = (\n 0.5\n * x\n * (1 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * np.power(x, 3))))\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_gelu_tanh_1\")\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\n# expected output [2.9963627, 3.99993, 4.9999995]\ny = (\n 0.5\n * x\n * (1 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * np.power(x, 3))))\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_gelu_tanh_2\")" } ] }, { "name": "Gelu", "module": "com.microsoft", "version": 1, "description": "Gaussian Error Linear Unit.\nA high-performing neural network activation function.The GELU nonlinearity is\nthe expected transformation of a stochastic regularizer which randomly applies\nthe identity or zero map to a neuron's input. The GELU nonlinearity weights\ninputs by their magnitude, rather than gates inputs by their sign as in ReLUs.", "inputs": [ { "name": "X", "type": "T", "description": "The input data as Tensor." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "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)", "tensor(bfloat16)" ] } ] }, { "name": "Gemm", "module": "ai.onnx", "version": 1, "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(\n a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35\n)\nexpect(node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_all_attributes\")" }, { "summary": "alpha", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_alpha\")" }, { "summary": "beta", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_beta\")" }, { "summary": "default_matrix_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_matrix_bias\"\n)" }, { "summary": "default_no_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\"], outputs=[\"y\"])\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], name=\"test_gemm_default_no_bias\")" }, { "summary": "default_scalar_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_scalar_bias\"\n)" }, { "summary": "default_single_elem_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node,\n inputs=[a, b, c],\n outputs=[y],\n name=\"test_gemm_default_single_elem_vector_bias\",\n)" }, { "summary": "default_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_vector_bias\"\n)" }, { "summary": "default_zero_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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], name=\"test_gemm_default_zero_bias\")" }, { "summary": "transposeA", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeA\")" }, { "summary": "transposeB", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeB\")" } ], "category": "Layer" }, { "name": "Gemm", "module": "ai.onnx", "version": 6, "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(\n a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35\n)\nexpect(node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_all_attributes\")" }, { "summary": "alpha", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_alpha\")" }, { "summary": "beta", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_beta\")" }, { "summary": "default_matrix_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_matrix_bias\"\n)" }, { "summary": "default_no_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\"], outputs=[\"y\"])\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], name=\"test_gemm_default_no_bias\")" }, { "summary": "default_scalar_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_scalar_bias\"\n)" }, { "summary": "default_single_elem_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node,\n inputs=[a, b, c],\n outputs=[y],\n name=\"test_gemm_default_single_elem_vector_bias\",\n)" }, { "summary": "default_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_vector_bias\"\n)" }, { "summary": "default_zero_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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], name=\"test_gemm_default_zero_bias\")" }, { "summary": "transposeA", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeA\")" }, { "summary": "transposeB", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeB\")" } ], "category": "Layer" }, { "name": "Gemm", "module": "ai.onnx", "version": 7, "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(\n a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35\n)\nexpect(node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_all_attributes\")" }, { "summary": "alpha", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_alpha\")" }, { "summary": "beta", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_beta\")" }, { "summary": "default_matrix_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_matrix_bias\"\n)" }, { "summary": "default_no_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\"], outputs=[\"y\"])\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], name=\"test_gemm_default_no_bias\")" }, { "summary": "default_scalar_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_scalar_bias\"\n)" }, { "summary": "default_single_elem_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node,\n inputs=[a, b, c],\n outputs=[y],\n name=\"test_gemm_default_single_elem_vector_bias\",\n)" }, { "summary": "default_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_vector_bias\"\n)" }, { "summary": "default_zero_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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], name=\"test_gemm_default_zero_bias\")" }, { "summary": "transposeA", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeA\")" }, { "summary": "transposeB", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeB\")" } ], "category": "Layer" }, { "name": "Gemm", "module": "ai.onnx", "version": 9, "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(\n a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35\n)\nexpect(node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_all_attributes\")" }, { "summary": "alpha", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_alpha\")" }, { "summary": "beta", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_beta\")" }, { "summary": "default_matrix_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_matrix_bias\"\n)" }, { "summary": "default_no_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\"], outputs=[\"y\"])\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], name=\"test_gemm_default_no_bias\")" }, { "summary": "default_scalar_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_scalar_bias\"\n)" }, { "summary": "default_single_elem_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node,\n inputs=[a, b, c],\n outputs=[y],\n name=\"test_gemm_default_single_elem_vector_bias\",\n)" }, { "summary": "default_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_vector_bias\"\n)" }, { "summary": "default_zero_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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], name=\"test_gemm_default_zero_bias\")" }, { "summary": "transposeA", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeA\")" }, { "summary": "transposeB", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeB\")" } ], "category": "Layer" }, { "name": "Gemm", "module": "ai.onnx", "version": 11, "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(\n a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35\n)\nexpect(node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_all_attributes\")" }, { "summary": "alpha", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_alpha\")" }, { "summary": "beta", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_beta\")" }, { "summary": "default_matrix_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_matrix_bias\"\n)" }, { "summary": "default_no_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\"], outputs=[\"y\"])\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], name=\"test_gemm_default_no_bias\")" }, { "summary": "default_scalar_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_scalar_bias\"\n)" }, { "summary": "default_single_elem_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node,\n inputs=[a, b, c],\n outputs=[y],\n name=\"test_gemm_default_single_elem_vector_bias\",\n)" }, { "summary": "default_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_vector_bias\"\n)" }, { "summary": "default_zero_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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], name=\"test_gemm_default_zero_bias\")" }, { "summary": "transposeA", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeA\")" }, { "summary": "transposeB", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeB\")" } ], "category": "Layer" }, { "name": "Gemm", "module": "ai.onnx", "version": 13, "description": "General Matrix multiplication:\nhttps://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3\n\n* A' = transpose(A) if transA else A\n* B' = 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(\n a, b, c, transA=1, transB=1, alpha=0.25, beta=0.35\n)\nexpect(node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_all_attributes\")" }, { "summary": "alpha", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_alpha\")" }, { "summary": "beta", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_beta\")" }, { "summary": "default_matrix_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_matrix_bias\"\n)" }, { "summary": "default_no_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\"], outputs=[\"y\"])\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], name=\"test_gemm_default_no_bias\")" }, { "summary": "default_scalar_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_scalar_bias\"\n)" }, { "summary": "default_single_elem_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node,\n inputs=[a, b, c],\n outputs=[y],\n name=\"test_gemm_default_single_elem_vector_bias\",\n)" }, { "summary": "default_vector_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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(\n node, inputs=[a, b, c], outputs=[y], name=\"test_gemm_default_vector_bias\"\n)" }, { "summary": "default_zero_bias", "code": "node = onnx.helper.make_node(\"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"])\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], name=\"test_gemm_default_zero_bias\")" }, { "summary": "transposeA", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeA\")" }, { "summary": "transposeB", "code": "node = onnx.helper.make_node(\n \"Gemm\", inputs=[\"a\", \"b\", \"c\"], outputs=[\"y\"], 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], name=\"test_gemm_transposeB\")" } ], "category": "Layer" }, { "name": "GemmFastGelu", "module": "com.microsoft", "version": 1, "description": "It's a fusion of MatMul and FastGelu.", "inputs": [ { "name": "X", "type": "T", "description": "input tensor" }, { "name": "W", "type": "T", "description": "input tensor" }, { "name": "bias", "type": "T", "option": "optional", "description": "bias tensor" } ], "min_input": 2, "max_input": 3, "outputs": [ { "name": "Y", "type": "T", "description": "output tensor" } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 3", "type_constraints": [ { "description": "Constrain input and output types to float or half tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ] }, { "name": "GemmFloat8", "module": "com.microsoft", "version": 1, "description": "Generic Gemm for float and float 8.", "attributes": [ { "name": "activation", "type": "string", "required": false, "description": "Activation function, RELU or GELU or NONE (default)." }, { "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, "description": "Scalar multiplier for the product of input bias C." }, { "name": "dtype", "type": "int64", "required": false, "default": 1, "description": "Output Type. Same definition as attribute 'to' for operator Cast." }, { "name": "transA", "type": "int64", "required": false, "description": "Whether A should be transposed. Float 8 only supported transA=0." }, { "name": "transB", "type": "int64", "required": false, "description": "Whether B should be transposed. Float 8 only supported transB=1." } ], "inputs": [ { "name": "A", "type": "TA", "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": "TB", "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": "TC", "option": "optional", "description": "Input tensor C." }, { "name": "scaleA", "type": "TS", "option": "optional", "description": "Scale of tensor A if A is float 8 tensor" }, { "name": "scaleB", "type": "TS", "option": "optional", "description": "Scale of tensor B if B is float 8 tensor" }, { "name": "scaleY", "type": "TS", "option": "optional", "description": "Scale of the output tensor if A or B is float 8." } ], "min_input": 2, "max_input": 6, "outputs": [ { "name": "Y", "type": "TR", "description": "Output tensor of shape (M, N)." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 6", "type_constraints": [ { "description": "Constrain type to input A.", "type_param_str": "TA", "allowed_type_strs": [ "tensor(float8e4m3fn)", "tensor(float8e5m2)", "tensor(float16)", "tensor(bfloat16)", "tensor(float)" ] }, { "description": "Constrain type to input B.", "type_param_str": "TB", "allowed_type_strs": [ "tensor(float8e4m3fn)", "tensor(float8e5m2)", "tensor(float16)", "tensor(bfloat16)", "tensor(float)" ] }, { "description": "Constrain type to input C.", "type_param_str": "TC", "allowed_type_strs": [ "tensor(float16)", "tensor(bfloat16)", "tensor(float)" ] }, { "description": "Constrain type to result type.", "type_param_str": "TR", "allowed_type_strs": [ "tensor(float8e4m3fn)", "tensor(float8e5m2)", "tensor(float16)", "tensor(bfloat16)", "tensor(float)" ] }, { "description": "Constrain type for all input scales (scaleA, scaleB, scaleY).", "type_param_str": "TS", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "GemmaRotaryEmbedding", "module": "com.microsoft", "version": 1, "description": "GemmaRotaryEmbedding is the implementation of below part of rotary positional embeddings (RoPE). It implements below from modeling_gemma.py.\n\nHere's onnxscript that was tested\n\nfrom onnxscript import FLOAT, FLOAT16, script\nfrom onnxscript import opset18 as op\n\n@script()\ndef gemma_rotary_embedding(emb: FLOAT[\"bs\", \"seq_len\", \"dim\"], q: FLOAT16[\"bs\", \"num_heads\", \"seq_len\", \"dim\"], q_rot: FLOAT16[\"bs\", \"num_heads\", \"seq_len\", \"dim\"], k: FLOAT16[\"bs\", \"num_heads\", \"seq_len\", \"dim\"], k_rot: FLOAT16[\"bs\", \"num_heads\", \"seq_len\", \"dim\"]):\n sin_val = op.Sin(emb)\n casted_sin = op.Cast(sin_val, to=10) # for fp16 mix-precision training. Other types are not supported.\n cos_val = op.Cos(emb)\n casted_cos = op.Cast(cos_val, to=10)\n unsqueezed_sin = op.Unsqueeze(casted_sin, [1])\n unsqueezed_cos = op.Unsqueeze(casted_cos, [1])\n q_embed = (q * casted_cos) + (q_rot * casted_sin)\n k_embed = (k * casted_cos) + (k_rot * casted_sin)\n return q_embed, k_embed\n\nonnx_model = gemma_rotary_embedding.to_model_proto()\n\n\n", "inputs": [ { "name": "emb", "type": "U", "description": "embedding - 3D tensor with shape (batch_size, seq_len, dim)" }, { "name": "q", "type": "T", "description": "q state - 4D tensor with shape (batch_size, num_heads, seq_len, dim)" }, { "name": "q_rot", "type": "T", "description": "half rotated q state - 4D tensor with shape (batch_size, num_heads, seq_len, dim)" }, { "name": "k", "type": "T", "description": "k state - 4D tensor with shape (batch_size, num_heads, seq_len, dim)" }, { "name": "k_rot", "type": "T", "description": "k state - 4D tensor with shape (batch_size, num_heads, seq_len, dim)" } ], "min_input": 5, "max_input": 5, "outputs": [ { "name": "output1", "type": "T", "description": "4D tensor with shape (batch_size, num_heads, seq_len, dim)" }, { "name": "output2", "type": "T", "description": "4D tensor with shape (batch_size, num_heads, seq_len, dim)" } ], "min_output": 2, "max_output": 2, "type_constraints": [ { "description": "Constrain input and output types to float16 tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)" ] }, { "description": "Constrain input 0 type to float tensors", "type_param_str": "U", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "GivenTensorFill", "module": "ai.onnx", "version": 1, "attributes": [ { "name": "extra_shape", "type": "int64[]", "required": false }, { "name": "input_as_shape", "type": "int64", "required": false }, { "name": "shape", "type": "int64[]", "required": false }, { "name": "values", "type": "float32[]", "required": false } ], "inputs": [ { "name": "shape", "type": "T", "option": "optional", "description": "The shape of filled tensor" } ], "min_input": 0, "max_input": 1, "outputs": [ { "name": "X", "type": "T", "description": "The filled tensor" } ], "min_output": 1, "max_output": 1, "inputs_range": "0 - 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)" ] } ] }, { "name": "GivenTensorFill", "module": "ai.onnx", "version": 10, "attributes": [ { "name": "extra_shape", "type": "int64[]", "required": false }, { "name": "input_as_shape", "type": "int64", "required": false }, { "name": "shape", "type": "int64[]", "required": false }, { "name": "values", "type": "float32[]", "required": false } ], "inputs": [ { "name": "shape", "type": "T", "option": "optional", "description": "The shape of filled tensor" } ], "min_input": 0, "max_input": 1, "outputs": [ { "name": "X", "type": "T", "description": "The filled tensor" } ], "min_output": 1, "max_output": 1, "inputs_range": "0 - 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)" ] } ] }, { "name": "GlobalAveragePool", "module": "ai.onnx", "version": 1, "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": "node = onnx.helper.make_node(\n \"GlobalAveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\nx = np.array(\n [\n [\n [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[5]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_globalaveragepool_precomputed\")" } ], "category": "Pool" }, { "name": "GlobalAveragePool", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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": "node = onnx.helper.make_node(\n \"GlobalAveragePool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\nx = np.array(\n [\n [\n [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[5]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_globalaveragepool_precomputed\")" } ], "category": "Pool" }, { "name": "GlobalAveragePool", "module": "com.microsoft.nchwc", "version": 1, "inputs": [ { "name": "X", "type": "T" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T" } ], "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(float)" ] } ], "category": "Pool" }, { "name": "GlobalAveragePool", "module": "com.ms.internal.nhwc", "version": 1, "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)" ] } ], "category": "Pool" }, { "name": "GlobalLpPool", "module": "ai.onnx", "version": 1, "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, "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": "GlobalLpPool", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "category": "Pool" }, { "name": "GlobalLpPool", "module": "com.ms.internal.nhwc", "version": 2, "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, "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": "node = 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": "node = onnx.helper.make_node(\n \"GlobalMaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\nx = np.array(\n [\n [\n [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[9]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_globalmaxpool_precomputed\")" } ], "category": "Pool" }, { "name": "GlobalMaxPool", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "globalmaxpool", "code": "node = 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": "node = onnx.helper.make_node(\n \"GlobalMaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\nx = np.array(\n [\n [\n [\n [1, 2, 3],\n [4, 5, 6],\n [7, 8, 9],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[9]]]]).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_globalmaxpool_precomputed\")" } ], "category": "Pool" }, { "name": "GlobalMaxPool", "module": "com.microsoft.nchwc", "version": 1, "inputs": [ { "name": "X", "type": "T" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T" } ], "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(float)" ] } ], "category": "Pool" }, { "name": "GlobalMaxPool", "module": "com.ms.internal.nhwc", "version": 1, "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)" ] } ], "category": "Pool" }, { "name": "Gradient", "module": "ai.onnx.preview.training", "version": 1, "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\", [\"a\", \"b\"], [\"c\"], name=\"my_add\")\ngradient_node = onnx.helper.make_node(\n \"Gradient\",\n [\"a\", \"b\"],\n [\"dc_da\", \"dc_db\"],\n name=\"my_gradient\",\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN,\n xs=[\"a\", \"b\"],\n y=\"c\",\n)\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 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 onnx.helper.make_tensor_value_info(\"dc_da\", onnx.TensorProto.FLOAT, []),\n onnx.helper.make_tensor_value_info(\"dc_db\", onnx.TensorProto.FLOAT, []),\n ],\n)\nopsets = [\n onnx.helper.make_operatorsetid(ONNX_DOMAIN, 12),\n onnx.helper.make_operatorsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1),\n]\nmodel = onnx.helper.make_model_gen_version(\n graph, producer_name=\"backend-test\", opset_imports=opsets\n)\nexpect(\n model, inputs=[a, b], outputs=[c, dc_da, dc_db], name=\"test_gradient_of_add\"\n)" }, { "summary": "gradient_scalar_add_and_mul", "code": "add_node = onnx.helper.make_node(\"Add\", [\"a\", \"b\"], [\"c\"], name=\"my_add\")\nmul_node = onnx.helper.make_node(\"Mul\", [\"c\", \"a\"], [\"d\"], name=\"my_mul\")\ngradient_node = onnx.helper.make_node(\n \"Gradient\",\n [\"a\", \"b\"],\n [\"dd_da\", \"dd_db\"],\n name=\"my_gradient\",\n domain=AI_ONNX_PREVIEW_TRAINING_DOMAIN,\n xs=[\"a\", \"b\"],\n y=\"d\",\n)\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 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 onnx.helper.make_tensor_value_info(\"dd_da\", onnx.TensorProto.FLOAT, []),\n onnx.helper.make_tensor_value_info(\"dd_db\", onnx.TensorProto.FLOAT, []),\n ],\n)\n\nopsets = [\n onnx.helper.make_operatorsetid(ONNX_DOMAIN, 12),\n onnx.helper.make_operatorsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1),\n]\nmodel = onnx.helper.make_model_gen_version(\n graph, producer_name=\"backend-test\", opset_imports=opsets\n)\nexpect(\n model,\n inputs=[a, b],\n outputs=[d, dd_da, dd_db],\n name=\"test_gradient_of_add_and_mul\",\n)" } ] }, { "name": "Greater", "module": "ai.onnx", "version": 1, "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], name=\"test_greater\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_int16\")\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)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint64\")" }, { "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], name=\"test_greater_equal\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_int16\")\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)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint64\")" }, { "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], 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], name=\"test_greater_equal_bcast\")" } ] }, { "name": "Greater", "module": "ai.onnx", "version": 7, "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], name=\"test_greater\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_int16\")\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)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint64\")" }, { "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], name=\"test_greater_equal\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_int16\")\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)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint64\")" }, { "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], 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], name=\"test_greater_equal_bcast\")" } ] }, { "name": "Greater", "module": "ai.onnx", "version": 9, "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], name=\"test_greater\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_int16\")\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)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint64\")" }, { "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], name=\"test_greater_equal\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_int16\")\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)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint64\")" }, { "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], 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], name=\"test_greater_equal_bcast\")" } ] }, { "name": "Greater", "module": "ai.onnx", "version": 13, "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], name=\"test_greater\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_int16\")\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)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.greater(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_uint64\")" }, { "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], name=\"test_greater_equal\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_int16\")\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)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.greater_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_greater_equal_uint64\")" }, { "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], 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], name=\"test_greater_equal_bcast\")" } ] }, { "name": "GreaterOrEqual", "module": "ai.onnx", "version": 12, "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, "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": "GreedySearch", "module": "com.microsoft", "version": 1, "description": "Greedy Search for text generation.", "attributes": [ { "name": "decoder", "type": "graph", "required": true, "description": "Decoder subgraph to execute in a loop." }, { "name": "decoder_start_token_id", "type": "int64", "required": false, "default": -1, "description": "The id of the token that indicates decoding starts." }, { "name": "encoder", "type": "graph", "required": false, "description": "The subgraph for initialization of encoder and decoder. It will be called once before `decoder` subgraph." }, { "name": "eos_token_id", "type": "int64", "required": true, "description": "The id of the end-of-sequence token" }, { "name": "init_decoder", "type": "graph", "required": false, "description": "The subgraph for the first decoding run. It will be called once before `decoder` subgraph. This is relevant only for the GPT2 model. If this attribute is missing, the `decoder` subgraph will be used for all decoding runs" }, { "name": "model_type", "type": "int64", "required": false, "description": "model type: 0 for decoder only like GPT-2; 1 for encoder decoder like Bart" }, { "name": "no_repeat_ngram_size", "type": "int64", "required": false, "description": "no repeat ngrams size" }, { "name": "pad_token_id", "type": "int64", "required": true, "description": "The id of the padding token" }, { "name": "vocab_size", "type": "int64", "required": false, "default": -1, "description": "Size of the vocabulary. If not provided, it will be inferred from the decoder subgraph's output shape" } ], "inputs": [ { "name": "input_ids", "type": "I", "description": "The sequence used as a prompt for the generation. Shape is (batch_size, sequence_length)" }, { "name": "max_length", "type": "I", "description": "The maximum length of the sequence to be generated. Shape is (1)" }, { "name": "min_length", "type": "I", "option": "optional", "description": "The minimum length below which the score of eos_token_id is set to -Inf. Shape is (1)" }, { "name": "repetition_penalty", "type": "T", "option": "optional", "description": "The parameter for repetition penalty. Default value 1.0 means no penalty. Accepts value > 0.0. Shape is (1)" }, { "name": "vocab_mask", "type": "I", "option": "optional", "description": "Mask of vocabulary. Words that masked with 0 are not allowed to be generated, and 1 is allowed. Shape is (vocab_size)" }, { "name": "prefix_vocab_mask", "type": "I", "option": "optional", "description": "Mask of vocabulary for first step. Words that masked with 0 are not allowed to be generated, and 1 is allowed. Shape is (batch_size, vocab_size)" }, { "name": "attention_mask", "type": "I", "option": "optional", "description": "Custom attention mask. Shape is (batch_size, sequence_length)" } ], "min_input": 2, "max_input": 7, "outputs": [ { "name": "sequences", "type": "I", "description": "Word IDs of generated sequences. Shape is (batch_size, max_sequence_length)" } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 7", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain to integer types", "type_param_str": "I", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "GridSample", "module": "ai.onnx", "version": 16, "description": "Given an input `X` and a flow-field `grid`, computes the output `Y` using `X` values and pixel locations from `grid`.\nCurrently, only spatial (4-D) inputs are supported. For input `X` with shape (N, C, H, W) and `grid` with shape (N, H_out, W_out, 2),\nthe output `Y` will have shape (N, C, H_out, W_out).\n\nThe tensor `X` contains values at centers of square pixels in a H by W 2-dimensional image.\nThe tensor `grid` describes normalized positions where the output `Y` is to be computed\nusing a specified interpolation method (the mode) and a padding mode (for grid positions falling outside the 2-dimensional image).\n\nElements in `grid[N, H_out, W_out]` are size-2 vectors specifying positions in the 2-dimensional space of `X`.\nThey are used to interpolate output values of `Y[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": "T2", "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": "T1", "description": "4-D tensor of shape (N, C, H_out, W_out) of sampled values. For integer input types, intermediate values are computed as floating point and cast to integer at the end." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input `X` and output `Y` 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 grid 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=\"linear\",\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.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 ]\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], name=\"test_gridsample\")" }, { "summary": "gridsample_mode_aligncorners", "code": "# X shape, [N, C, H, W] - [1, 1, 3, 2]\nX = np.array(\n [[[[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]]],\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 [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=\"linear\",\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [[[[0.0000, 0.5000, 1.7000, 2.5000], [2.5000, 1.7000, 4.5000, 1.2500]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_bilinear\",\n)\n\n# setting mode = 'bilinear', align_corners = 1\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_align_corners = np.array(\n [[[[0.0000, 1.2500, 2.0000, 2.5000], [2.5000, 2.0000, 3.7500, 5.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_align_corners],\n name=\"test_gridsample_aligncorners_true\",\n)\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 [[[[0.0, 0.0, 2.0, 2.0], [2.0, 2.0, 5.0, 0.0]]]],\n dtype=np.float32,\n)\n\nexpect(\n node, inputs=[X, Grid], outputs=[Y_nearest], name=\"test_gridsample_nearest\"\n)\n\n# setting mode = 'bicubic'\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [[[[-0.1406, 0.3828, 1.7556, 2.9688], [2.9688, 1.7556, 5.1445, 1.3906]]]],\n dtype=np.float32,\n)\n\nexpect(\n node, inputs=[X, Grid], outputs=[Y_bicubic], name=\"test_gridsample_bicubic\"\n)\n\n# ============================================================================\n# Additional tests\n# The reference output tensors were generated using PyTorch 2.0.\nGrid = np.array(\n [\n [\n [[-1.0, -0.8], [-0.6, -0.5], [-0.1, -0.2], [0.7, 0.0]],\n [[0.0, 0.4], [0.2, -0.2], [-0.3, 0.5], [-1.0, 1.0]],\n ]\n ],\n dtype=np.float32,\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [[[[0.0, 0.0, 2.0, 3.0], [4.0, 3.0, 4.0, 4.0]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_nearest_align_corners_0_additional_1\",\n)\n\n# setting mode = 'nearest'\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [[[[0.0, 0.0, 2.0, 3.0], [2.0, 3.0, 4.0, 4.0]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_nearest_align_corners_1_additional_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [[[[0.0000, 0.4500, 1.8000, 2.4000], [3.7000, 2.1000, 3.7000, 1.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_bilinear_align_corners_0_additional_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [[[[0.4000, 1.2000, 2.0500, 2.8500], [3.3000, 2.2000, 3.3500, 4.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_bilinear_align_corners_1_additional_1\",\n)\n\n# These two new bicubic tests produces slightly higher error ~5e-5\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [\n [\n [\n [-0.173250, 0.284265, 1.923106, 2.568000],\n [5.170375, 2.284414, 4.744844, 1.046875],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bicubic],\n name=\"test_gridsample_bicubic_align_corners_0_additional_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [\n [\n [\n [0.304001, 1.128750, 2.266270, 3.144844],\n [4.531500, 2.455360, 4.599819, 4.000000],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bicubic],\n name=\"test_gridsample_bicubic_align_corners_1_additional_1\",\n)" }, { "summary": "gridsample_paddingmode", "code": "# X shape, [N, C, H, W] - [1, 1, 3, 2]\nX = np.array(\n [[[[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]]],\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 [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 [[[[0.0000, 0.0000, 1.7000, 0.0000], [0.0000, 1.7000, 0.0000, 0.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_zeros],\n name=\"test_gridsample_zeros_padding\",\n)\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 [[[[0.0000, 0.0000, 1.7000, 5.0000], [5.0000, 1.7000, 5.0000, 5.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_border],\n name=\"test_gridsample_border_padding\",\n)\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 [[[[2.5000, 0.0000, 1.7000, 2.5000], [2.5000, 1.7000, 5.0000, 2.5000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_reflection],\n name=\"test_gridsample_reflection_padding\",\n)" }, { "summary": "volumeetric_gridsample_mode_aligncorners", "code": "X = np.array(\n [\n [\n [\n [[1.0, 2.0], [3.0, 4.0]],\n [[5.0, 6.0], [7.0, 8.0]],\n [[9.0, 10.0], [11.0, 12.0]],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nGrid = np.array(\n [\n [\n [\n [[-1.0, -1.0, -1.0], [-1.0, -0.5, 0.3]],\n [[-0.5, -0.5, -0.5], [1.0, -0.6, -1.0]],\n [[-0.2, -0.2, -0.2], [0.4, 0.2, 0.6]],\n [[0.0, 0.0, 0.0], [-1.0, 0.0, 0.0]],\n ],\n [\n [[0.0, 0.0, 0.0], [-1.0, 1.0, 0.0]],\n [[-0.2, -0.2, -0.2], [1.0, 0.4, -0.2]],\n [[0.5, 0.5, 0.5], [-1.0, -0.8, 0.8]],\n [[1.0, 1.0, 1.0], [0.4, 0.6, -0.3]],\n ],\n ]\n ],\n dtype=np.float32,\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [\n [\n [\n [[1.0, 5.0], [1.0, 0.0], [5.0, 12.0], [5.0, 5.0]],\n [[5.0, 0.0], [5.0, 0.0], [12.0, 9.0], [0.0, 8.0]],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_volumetric_nearest_align_corners_0\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [\n [\n [\n [[1.0, 5.0], [1.0, 2.0], [5.0, 12.0], [5.0, 5.0]],\n [[5.0, 7.0], [5.0, 8.0], [12.0, 9.0], [12.0, 8.0]],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_volumetric_nearest_align_corners_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [\n [\n [\n [\n [0.1250, 3.4000],\n [2.0000, 0.4500],\n [4.7000, 10.9000],\n [6.5000, 3.0000],\n ],\n [\n [6.5000, 1.7500],\n [4.7000, 3.3000],\n [11.0000, 2.5200],\n [1.5000, 5.4900],\n ],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_volumetric_bilinear_align_corners_0\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [\n [\n [\n [\n [1.0000, 6.7000],\n [3.7500, 2.4000],\n [5.4000, 9.3000],\n [6.5000, 6.0000],\n ],\n [\n [6.5000, 7.0000],\n [5.4000, 6.6000],\n [9.2500, 8.4000],\n [12.0000, 6.1000],\n ],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_volumetric_bilinear_align_corners_1\",\n)" } ] }, { "name": "GridSample", "module": "ai.onnx", "version": 20, "description": "Given an input `X` and a flow-field `grid`, computes the output `Y` using `X` values and pixel locations from the `grid`.\nFor spatial input `X` with shape (N, C, H, W), the `grid` will have shape (N, H_out, W_out, 2),\nthe output `Y` will have shape (N, C, H_out, W_out). For volumetric input `X` with shape (N, C, D, H, W),\nthe `grid` will have shape (N, D_out, H_out, W_out, 3), the output `Y` will have shape (N, C, D_out, H_out, W_out).\nMore generally, for an input `X` of rank r+2 with shape (N, C, d1, d2, ..., dr),\nthe `grid` will have shape (N, D1_out, D2_out, ..., Dr_out, r), the output `Y` will have shape (N, C, D1_out, D2_out, ..., Dr_out).\n\nThe tensor `X` contains values at centers of square pixels (voxels, etc) locations such as (n, c, d1_in, d2_in, ..., dr_in).\nThe (n, d1_out, d2_out, ..., dr_out, :) values from the tensor `grid` are the normalized positions for interpolating the values\nat the (n, c, d1_out, d2_out, ..., dr_out) locations from the output tensor `Y` using a specified interpolation method (the mode)\nand a padding mode (for `grid` positions falling outside the 2-dimensional image).\n\nFor example, the values in `grid[n, h_out, w_out, :]` are size-2 vectors specifying normalized positions in the 2-dimensional space of `X`.\nThey are used to interpolate output values of `Y[n, c, h_out, w_out]`.\n\nThe GridSample operator is often used in doing grid generator and sampler in the\n[Spatial Transformer Networks](https://arxiv.org/abs/1506.02025).\nSee also in [torch.nn.functional.grid_sample](https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html).\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 (voxels, etc.). If align_corners=0, they are instead considered as referring to the corner points of the input's corner pixels (voxels, etc.), making the sampling more resolution agnostic." }, { "name": "mode", "type": "string", "required": false, "default": "linear", "description": "Three interpolation modes: linear (default), nearest and cubic. The \"linear\" mode includes linear and N-linear interpolation modes depending on the number of spatial dimensions of the input tensor (i.e. linear for 1 spatial dimension, bilinear for 2 spatial dimensions, etc.). The \"cubic\" mode also includes N-cubic interpolation modes following the same rules. The \"nearest\" mode rounds to the nearest even index when the sampling point falls halfway between two indices." }, { "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": "Input tensor of rank r+2 that has shape (N, C, D1, D2, ..., Dr), where N is the batch size, C is the number of channels, D1, D2, ..., Dr are the spatial dimensions." }, { "name": "grid", "type": "T2", "description": "Input offset of shape (N, D1_out, D2_out, ..., Dr_out, r), where D1_out, D2_out, ..., Dr_out are the spatial dimensions of the grid and output, and r is the number of spatial dimensions. Grid specifies the sampling locations normalized by the input spatial dimensions. Therefore, it should have most values in the range of [-1, 1]. If the grid has values outside the range of [-1, 1], the corresponding outputs will be handled as defined by padding_mode. Following computer vision convention, the coordinates in the length-r location vector are listed from the innermost tensor dimension to the outermost, the opposite of regular tensor indexing." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "T1", "description": "Output tensor of rank r+2 that has shape (N, C, D1_out, D2_out, ..., Dr_out) of the sampled values. For integer input types, intermediate values are computed as floating point and cast to integer at the end." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input `X` and output `Y` 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 grid 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=\"linear\",\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.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 ]\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], name=\"test_gridsample\")" }, { "summary": "gridsample_mode_aligncorners", "code": "# X shape, [N, C, H, W] - [1, 1, 3, 2]\nX = np.array(\n [[[[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]]],\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 [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=\"linear\",\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [[[[0.0000, 0.5000, 1.7000, 2.5000], [2.5000, 1.7000, 4.5000, 1.2500]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_bilinear\",\n)\n\n# setting mode = 'bilinear', align_corners = 1\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_align_corners = np.array(\n [[[[0.0000, 1.2500, 2.0000, 2.5000], [2.5000, 2.0000, 3.7500, 5.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_align_corners],\n name=\"test_gridsample_aligncorners_true\",\n)\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 [[[[0.0, 0.0, 2.0, 2.0], [2.0, 2.0, 5.0, 0.0]]]],\n dtype=np.float32,\n)\n\nexpect(\n node, inputs=[X, Grid], outputs=[Y_nearest], name=\"test_gridsample_nearest\"\n)\n\n# setting mode = 'bicubic'\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [[[[-0.1406, 0.3828, 1.7556, 2.9688], [2.9688, 1.7556, 5.1445, 1.3906]]]],\n dtype=np.float32,\n)\n\nexpect(\n node, inputs=[X, Grid], outputs=[Y_bicubic], name=\"test_gridsample_bicubic\"\n)\n\n# ============================================================================\n# Additional tests\n# The reference output tensors were generated using PyTorch 2.0.\nGrid = np.array(\n [\n [\n [[-1.0, -0.8], [-0.6, -0.5], [-0.1, -0.2], [0.7, 0.0]],\n [[0.0, 0.4], [0.2, -0.2], [-0.3, 0.5], [-1.0, 1.0]],\n ]\n ],\n dtype=np.float32,\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [[[[0.0, 0.0, 2.0, 3.0], [4.0, 3.0, 4.0, 4.0]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_nearest_align_corners_0_additional_1\",\n)\n\n# setting mode = 'nearest'\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [[[[0.0, 0.0, 2.0, 3.0], [2.0, 3.0, 4.0, 4.0]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_nearest_align_corners_1_additional_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [[[[0.0000, 0.4500, 1.8000, 2.4000], [3.7000, 2.1000, 3.7000, 1.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_bilinear_align_corners_0_additional_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [[[[0.4000, 1.2000, 2.0500, 2.8500], [3.3000, 2.2000, 3.3500, 4.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_bilinear_align_corners_1_additional_1\",\n)\n\n# These two new bicubic tests produces slightly higher error ~5e-5\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [\n [\n [\n [-0.173250, 0.284265, 1.923106, 2.568000],\n [5.170375, 2.284414, 4.744844, 1.046875],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bicubic],\n name=\"test_gridsample_bicubic_align_corners_0_additional_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [\n [\n [\n [0.304001, 1.128750, 2.266270, 3.144844],\n [4.531500, 2.455360, 4.599819, 4.000000],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bicubic],\n name=\"test_gridsample_bicubic_align_corners_1_additional_1\",\n)" }, { "summary": "gridsample_paddingmode", "code": "# X shape, [N, C, H, W] - [1, 1, 3, 2]\nX = np.array(\n [[[[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]]],\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 [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 [[[[0.0000, 0.0000, 1.7000, 0.0000], [0.0000, 1.7000, 0.0000, 0.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_zeros],\n name=\"test_gridsample_zeros_padding\",\n)\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 [[[[0.0000, 0.0000, 1.7000, 5.0000], [5.0000, 1.7000, 5.0000, 5.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_border],\n name=\"test_gridsample_border_padding\",\n)\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 [[[[2.5000, 0.0000, 1.7000, 2.5000], [2.5000, 1.7000, 5.0000, 2.5000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_reflection],\n name=\"test_gridsample_reflection_padding\",\n)" }, { "summary": "volumeetric_gridsample_mode_aligncorners", "code": "X = np.array(\n [\n [\n [\n [[1.0, 2.0], [3.0, 4.0]],\n [[5.0, 6.0], [7.0, 8.0]],\n [[9.0, 10.0], [11.0, 12.0]],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nGrid = np.array(\n [\n [\n [\n [[-1.0, -1.0, -1.0], [-1.0, -0.5, 0.3]],\n [[-0.5, -0.5, -0.5], [1.0, -0.6, -1.0]],\n [[-0.2, -0.2, -0.2], [0.4, 0.2, 0.6]],\n [[0.0, 0.0, 0.0], [-1.0, 0.0, 0.0]],\n ],\n [\n [[0.0, 0.0, 0.0], [-1.0, 1.0, 0.0]],\n [[-0.2, -0.2, -0.2], [1.0, 0.4, -0.2]],\n [[0.5, 0.5, 0.5], [-1.0, -0.8, 0.8]],\n [[1.0, 1.0, 1.0], [0.4, 0.6, -0.3]],\n ],\n ]\n ],\n dtype=np.float32,\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [\n [\n [\n [[1.0, 5.0], [1.0, 0.0], [5.0, 12.0], [5.0, 5.0]],\n [[5.0, 0.0], [5.0, 0.0], [12.0, 9.0], [0.0, 8.0]],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_volumetric_nearest_align_corners_0\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [\n [\n [\n [[1.0, 5.0], [1.0, 2.0], [5.0, 12.0], [5.0, 5.0]],\n [[5.0, 7.0], [5.0, 8.0], [12.0, 9.0], [12.0, 8.0]],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_volumetric_nearest_align_corners_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [\n [\n [\n [\n [0.1250, 3.4000],\n [2.0000, 0.4500],\n [4.7000, 10.9000],\n [6.5000, 3.0000],\n ],\n [\n [6.5000, 1.7500],\n [4.7000, 3.3000],\n [11.0000, 2.5200],\n [1.5000, 5.4900],\n ],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_volumetric_bilinear_align_corners_0\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [\n [\n [\n [\n [1.0000, 6.7000],\n [3.7500, 2.4000],\n [5.4000, 9.3000],\n [6.5000, 6.0000],\n ],\n [\n [6.5000, 7.0000],\n [5.4000, 6.6000],\n [9.2500, 8.4000],\n [12.0000, 6.1000],\n ],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_volumetric_bilinear_align_corners_1\",\n)" } ] }, { "name": "GridSample", "module": "ai.onnx", "version": 22, "description": "Given an input `X` and a flow-field `grid`, computes the output `Y` using `X` values and pixel locations from the `grid`.\nFor spatial input `X` with shape (N, C, H, W), the `grid` will have shape (N, H_out, W_out, 2),\nthe output `Y` will have shape (N, C, H_out, W_out). For volumetric input `X` with shape (N, C, D, H, W),\nthe `grid` will have shape (N, D_out, H_out, W_out, 3), the output `Y` will have shape (N, C, D_out, H_out, W_out).\nMore generally, for an input `X` of rank r+2 with shape (N, C, d1, d2, ..., dr),\nthe `grid` will have shape (N, D1_out, D2_out, ..., Dr_out, r), the output `Y` will have shape (N, C, D1_out, D2_out, ..., Dr_out).\n\nThe tensor `X` contains values at centers of square pixels (voxels, etc) locations such as (n, c, d1_in, d2_in, ..., dr_in).\nThe (n, d1_out, d2_out, ..., dr_out, :) values from the tensor `grid` are the normalized positions for interpolating the values\nat the (n, c, d1_out, d2_out, ..., dr_out) locations from the output tensor `Y` using a specified interpolation method (the mode)\nand a padding mode (for `grid` positions falling outside the 2-dimensional image).\n\nFor example, the values in `grid[n, h_out, w_out, :]` are size-2 vectors specifying normalized positions in the 2-dimensional space of `X`.\nThey are used to interpolate output values of `Y[n, c, h_out, w_out]`.\n\nThe GridSample operator is often used in doing grid generator and sampler in the\n[Spatial Transformer Networks](https://arxiv.org/abs/1506.02025).\nSee also in [torch.nn.functional.grid_sample](https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html).\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 (voxels, etc.). If align_corners=0, they are instead considered as referring to the corner points of the input's corner pixels (voxels, etc.), making the sampling more resolution agnostic." }, { "name": "mode", "type": "string", "required": false, "default": "linear", "description": "Three interpolation modes: linear (default), nearest and cubic. The \"linear\" mode includes linear and N-linear interpolation modes depending on the number of spatial dimensions of the input tensor (i.e. linear for 1 spatial dimension, bilinear for 2 spatial dimensions, etc.). The \"cubic\" mode also includes N-cubic interpolation modes following the same rules. The \"nearest\" mode rounds to the nearest even index when the sampling point falls halfway between two indices." }, { "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": "Input tensor of rank r+2 that has shape (N, C, D1, D2, ..., Dr), where N is the batch size, C is the number of channels, D1, D2, ..., Dr are the spatial dimensions." }, { "name": "grid", "type": "T2", "description": "Input offset of shape (N, D1_out, D2_out, ..., Dr_out, r), where D1_out, D2_out, ..., Dr_out are the spatial dimensions of the grid and output, and r is the number of spatial dimensions. Grid specifies the sampling locations normalized by the input spatial dimensions. Therefore, it should have most values in the range of [-1, 1]. If the grid has values outside the range of [-1, 1], the corresponding outputs will be handled as defined by padding_mode. Following computer vision convention, the coordinates in the length-r location vector are listed from the innermost tensor dimension to the outermost, the opposite of regular tensor indexing." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "T1", "description": "Output tensor of rank r+2 that has shape (N, C, D1_out, D2_out, ..., Dr_out) of the sampled values. For integer input types, intermediate values are computed as floating point and cast to integer at the end." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input `X` and output `Y` 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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(string)", "tensor(bool)", "tensor(complex64)", "tensor(complex128)" ] }, { "description": "Constrain grid types to float tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(bfloat16)", "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=\"linear\",\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.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 ]\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], name=\"test_gridsample\")" }, { "summary": "gridsample_mode_aligncorners", "code": "# X shape, [N, C, H, W] - [1, 1, 3, 2]\nX = np.array(\n [[[[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]]],\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 [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=\"linear\",\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [[[[0.0000, 0.5000, 1.7000, 2.5000], [2.5000, 1.7000, 4.5000, 1.2500]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_bilinear\",\n)\n\n# setting mode = 'bilinear', align_corners = 1\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_align_corners = np.array(\n [[[[0.0000, 1.2500, 2.0000, 2.5000], [2.5000, 2.0000, 3.7500, 5.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_align_corners],\n name=\"test_gridsample_aligncorners_true\",\n)\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 [[[[0.0, 0.0, 2.0, 2.0], [2.0, 2.0, 5.0, 0.0]]]],\n dtype=np.float32,\n)\n\nexpect(\n node, inputs=[X, Grid], outputs=[Y_nearest], name=\"test_gridsample_nearest\"\n)\n\n# setting mode = 'bicubic'\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [[[[-0.1406, 0.3828, 1.7556, 2.9688], [2.9688, 1.7556, 5.1445, 1.3906]]]],\n dtype=np.float32,\n)\n\nexpect(\n node, inputs=[X, Grid], outputs=[Y_bicubic], name=\"test_gridsample_bicubic\"\n)\n\n# ============================================================================\n# Additional tests\n# The reference output tensors were generated using PyTorch 2.0.\nGrid = np.array(\n [\n [\n [[-1.0, -0.8], [-0.6, -0.5], [-0.1, -0.2], [0.7, 0.0]],\n [[0.0, 0.4], [0.2, -0.2], [-0.3, 0.5], [-1.0, 1.0]],\n ]\n ],\n dtype=np.float32,\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [[[[0.0, 0.0, 2.0, 3.0], [4.0, 3.0, 4.0, 4.0]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_nearest_align_corners_0_additional_1\",\n)\n\n# setting mode = 'nearest'\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [[[[0.0, 0.0, 2.0, 3.0], [2.0, 3.0, 4.0, 4.0]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_nearest_align_corners_1_additional_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [[[[0.0000, 0.4500, 1.8000, 2.4000], [3.7000, 2.1000, 3.7000, 1.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_bilinear_align_corners_0_additional_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [[[[0.4000, 1.2000, 2.0500, 2.8500], [3.3000, 2.2000, 3.3500, 4.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_bilinear_align_corners_1_additional_1\",\n)\n\n# These two new bicubic tests produces slightly higher error ~5e-5\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [\n [\n [\n [-0.173250, 0.284265, 1.923106, 2.568000],\n [5.170375, 2.284414, 4.744844, 1.046875],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bicubic],\n name=\"test_gridsample_bicubic_align_corners_0_additional_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bicubic = np.array(\n [\n [\n [\n [0.304001, 1.128750, 2.266270, 3.144844],\n [4.531500, 2.455360, 4.599819, 4.000000],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bicubic],\n name=\"test_gridsample_bicubic_align_corners_1_additional_1\",\n)" }, { "summary": "gridsample_paddingmode", "code": "# X shape, [N, C, H, W] - [1, 1, 3, 2]\nX = np.array(\n [[[[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]]],\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 [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 [[[[0.0000, 0.0000, 1.7000, 0.0000], [0.0000, 1.7000, 0.0000, 0.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_zeros],\n name=\"test_gridsample_zeros_padding\",\n)\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 [[[[0.0000, 0.0000, 1.7000, 5.0000], [5.0000, 1.7000, 5.0000, 5.0000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_border],\n name=\"test_gridsample_border_padding\",\n)\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 [[[[2.5000, 0.0000, 1.7000, 2.5000], [2.5000, 1.7000, 5.0000, 2.5000]]]],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_reflection],\n name=\"test_gridsample_reflection_padding\",\n)" }, { "summary": "volumeetric_gridsample_mode_aligncorners", "code": "X = np.array(\n [\n [\n [\n [[1.0, 2.0], [3.0, 4.0]],\n [[5.0, 6.0], [7.0, 8.0]],\n [[9.0, 10.0], [11.0, 12.0]],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nGrid = np.array(\n [\n [\n [\n [[-1.0, -1.0, -1.0], [-1.0, -0.5, 0.3]],\n [[-0.5, -0.5, -0.5], [1.0, -0.6, -1.0]],\n [[-0.2, -0.2, -0.2], [0.4, 0.2, 0.6]],\n [[0.0, 0.0, 0.0], [-1.0, 0.0, 0.0]],\n ],\n [\n [[0.0, 0.0, 0.0], [-1.0, 1.0, 0.0]],\n [[-0.2, -0.2, -0.2], [1.0, 0.4, -0.2]],\n [[0.5, 0.5, 0.5], [-1.0, -0.8, 0.8]],\n [[1.0, 1.0, 1.0], [0.4, 0.6, -0.3]],\n ],\n ]\n ],\n dtype=np.float32,\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [\n [\n [\n [[1.0, 5.0], [1.0, 0.0], [5.0, 12.0], [5.0, 5.0]],\n [[5.0, 0.0], [5.0, 0.0], [12.0, 9.0], [0.0, 8.0]],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_volumetric_nearest_align_corners_0\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_nearest = np.array(\n [\n [\n [\n [[1.0, 5.0], [1.0, 2.0], [5.0, 12.0], [5.0, 5.0]],\n [[5.0, 7.0], [5.0, 8.0], [12.0, 9.0], [12.0, 8.0]],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_nearest],\n name=\"test_gridsample_volumetric_nearest_align_corners_1\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=0,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [\n [\n [\n [\n [0.1250, 3.4000],\n [2.0000, 0.4500],\n [4.7000, 10.9000],\n [6.5000, 3.0000],\n ],\n [\n [6.5000, 1.7500],\n [4.7000, 3.3000],\n [11.0000, 2.5200],\n [1.5000, 5.4900],\n ],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_volumetric_bilinear_align_corners_0\",\n)\n\nnode = onnx.helper.make_node(\n \"GridSample\",\n inputs=[\"X\", \"Grid\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n align_corners=1,\n)\n# Y shape, [N, C, H_out, W_out] - [1, 1, 2, 4]\nY_bilinear = np.array(\n [\n [\n [\n [\n [1.0000, 6.7000],\n [3.7500, 2.4000],\n [5.4000, 9.3000],\n [6.5000, 6.0000],\n ],\n [\n [6.5000, 7.0000],\n [5.4000, 6.6000],\n [9.2500, 8.4000],\n [12.0000, 6.1000],\n ],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[X, Grid],\n outputs=[Y_bilinear],\n name=\"test_gridsample_volumetric_bilinear_align_corners_1\",\n)" } ] }, { "name": "GridSample", "module": "com.microsoft", "version": 1, "description": "Given an `input` and a flow-field `grid`, computes the `output` using `input` values and pixel locations from `grid`.\n Currently, 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),\n the `output` will have shape (N, C, H_out, W_out).\n For each output location `output[n, :, h, w]`, the size-2 vector `grid[n, h, w]` specifies `input` pixel locations `x` and `y`,\n which are used to interpolate the output value `output[n, :, h, w]`.\n The GridSample operator is often used in doing grid generator and sampler in the [Spatial Transformer Networks](https://arxiv.org/abs/1506.02025).\n See 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." } ], "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)" ] } ] }, { "name": "GridSample", "module": "com.ms.internal.nhwc", "version": 16, "description": "Given an input `X` and a flow-field `grid`, computes the output `Y` using `X` values and pixel locations from `grid`.\nCurrently, only spatial (4-D) inputs are supported. For input `X` with shape (N, C, H, W) and `grid` with shape (N, H_out, W_out, 2),\nthe output `Y` will have shape (N, C, H_out, W_out).\n\nThe tensor `X` contains values at centers of square pixels in a H by W 2-dimensional image.\nThe tensor `grid` describes normalized positions where the output `Y` is to be computed\nusing a specified interpolation method (the mode) and a padding mode (for grid positions falling outside the 2-dimensional image).\n\nElements in `grid[N, H_out, W_out]` are size-2 vectors specifying positions in the 2-dimensional space of `X`.\nThey are used to interpolate output values of `Y[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": "T2", "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": "T1", "description": "4-D tensor of shape (N, C, H_out, W_out) of sampled values. For integer input types, intermediate values are computed as floating point and cast to integer at the end." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input `X` and output `Y` 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 grid types to float tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)" ] } ] }, { "name": "GridSample", "module": "com.ms.internal.nhwc", "version": 20, "description": "Given an input `X` and a flow-field `grid`, computes the output `Y` using `X` values and pixel locations from the `grid`.\nFor spatial input `X` with shape (N, C, H, W), the `grid` will have shape (N, H_out, W_out, 2),\nthe output `Y` will have shape (N, C, H_out, W_out). For volumetric input `X` with shape (N, C, D, H, W),\nthe `grid` will have shape (N, D_out, H_out, W_out, 3), the output `Y` will have shape (N, C, D_out, H_out, W_out).\nMore generally, for an input `X` of rank r+2 with shape (N, C, d1, d2, ..., dr),\nthe `grid` will have shape (N, D1_out, D2_out, ..., Dr_out, r), the output `Y` will have shape (N, C, D1_out, D2_out, ..., Dr_out).\n\nThe tensor `X` contains values at centers of square pixels (voxels, etc) locations such as (n, c, d1_in, d2_in, ..., dr_in).\nThe (n, d1_out, d2_out, ..., dr_out, :) values from the tensor `grid` are the normalized positions for interpolating the values\nat the (n, c, d1_out, d2_out, ..., dr_out) locations from the output tensor `Y` using a specified interpolation method (the mode)\nand a padding mode (for `grid` positions falling outside the 2-dimensional image).\n\nFor example, the values in `grid[n, h_out, w_out, :]` are size-2 vectors specifying normalized positions in the 2-dimensional space of `X`.\nThey are used to interpolate output values of `Y[n, c, h_out, w_out]`.\n\nThe GridSample operator is often used in doing grid generator and sampler in the\n[Spatial Transformer Networks](https://arxiv.org/abs/1506.02025).\nSee also in [torch.nn.functional.grid_sample](https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html).\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 (voxels, etc.). If align_corners=0, they are instead considered as referring to the corner points of the input's corner pixels (voxels, etc.), making the sampling more resolution agnostic." }, { "name": "mode", "type": "string", "required": false, "default": "linear", "description": "Three interpolation modes: linear (default), nearest and cubic. The \"linear\" mode includes linear and N-linear interpolation modes depending on the number of spatial dimensions of the input tensor (i.e. linear for 1 spatial dimension, bilinear for 2 spatial dimensions, etc.). The \"cubic\" mode also includes N-cubic interpolation modes following the same rules. The \"nearest\" mode rounds to the nearest even index when the sampling point falls halfway between two indices." }, { "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": "Input tensor of rank r+2 that has shape (N, C, D1, D2, ..., Dr), where N is the batch size, C is the number of channels, D1, D2, ..., Dr are the spatial dimensions." }, { "name": "grid", "type": "T2", "description": "Input offset of shape (N, D1_out, D2_out, ..., Dr_out, r), where D1_out, D2_out, ..., Dr_out are the spatial dimensions of the grid and output, and r is the number of spatial dimensions. Grid specifies the sampling locations normalized by the input spatial dimensions. Therefore, it should have most values in the range of [-1, 1]. If the grid has values outside the range of [-1, 1], the corresponding outputs will be handled as defined by padding_mode. Following computer vision convention, the coordinates in the length-r location vector are listed from the innermost tensor dimension to the outermost, the opposite of regular tensor indexing." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "T1", "description": "Output tensor of rank r+2 that has shape (N, C, D1_out, D2_out, ..., Dr_out) of the sampled values. For integer input types, intermediate values are computed as floating point and cast to integer at the end." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input `X` and output `Y` 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 grid types to float tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)" ] } ] }, { "name": "GroupNorm", "module": "com.microsoft", "version": 1, "description": "Applies Group Normalization over a mini-batch of inputs as described in the paper Group Normalization (https://arxiv.org/abs/1803.08494).\n\nThis operator transforms input according to\n y = gamma * (x - mean) / sqrt(variance + epsilon) + beta\n\nThe input channels are separated into num_groups groups, each containing num_channels / num_groups channels. num_channels must be divisible by num_groups. The mean and standard-deviation are calculated separately over the each group.\nThe weight and bias are per-channel affine transform parameter vectors of size num_channels.\n\nThe activation attribute can be used to enable activation after group normalization.\n", "attributes": [ { "name": "activation", "type": "int64", "required": true, "description": "Activation after group normalization: 0 for None, 1 for SiLU" }, { "name": "channels_last", "type": "int64", "required": false, "default": 1, "description": "1 if the input and output are in the NHWC layout, 0 if it is in the NCHW layout. Defaults to 1." }, { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "The epsilon value to use to avoid division by zero" }, { "name": "groups", "type": "int64", "required": true, "description": "The number of groups of channels. It should be a divisor of the number of channels C" } ], "inputs": [ { "name": "X", "type": "T", "description": "Input data tensor. Dimensions are (N x H x W x C) when channels_last is 1 or (N x C x H x W) otherwise, where N is the batch size, C is the number of channels, and H and W are the height and width of the data" }, { "name": "gamma", "type": "M", "description": "1D gamma tensor for normalization with shape (C), where C is number of channels" }, { "name": "beta", "type": "M", "description": "1D beta tensor for normalization with shape (C), where C is number of channels" } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "Y", "type": "T", "description": "The output tensor of the same shape as X" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input X and output Y types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] }, { "description": "Constrain gamma and beta to float tensors.", "type_param_str": "M", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] } ] }, { "name": "GroupNormalization", "module": "ai.onnx", "version": 18, "description": "A GroupNormalization function. Carries out group normalization as described in\nthe paper https://arxiv.org/abs/1803.08494\n\nThis operator transforms input according to\n```\ny = scale * (x - mean) / sqrt(variance + epsilon) + bias,\n```\nwhere the mean and variance are computed per instance per group of channels, and\n`scale` and `bias` should be specified for each group of channels. The number of\ngroups `num_groups` should be divisible by the number of channels so that there are\nan equal number of channels per group.\n\nWhen the number of groups is the same as the number of channels, this operator is\nequivalent to InstanceNormalization. When there is only one group, this operator\nis equivalent to LayerNormalization.\n", "attributes": [ { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "The epsilon value to use to avoid division by zero." }, { "name": "num_groups", "type": "int64", "required": true, "description": "The number of groups of channels. It should be a divisor of the number of channels `C`." } ], "inputs": [ { "name": "X", "type": "T", "description": "Input data tensor. Dimensions for image cases 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 width of the data. Statistics are computed for every group of channels over `C`, `H`, and `W`. For non-image cases, the dimensions are in the form of `(N x C x D1 x D2 ... Dn)`." }, { "name": "scale", "type": "T", "description": "Scale tensor of shape `(num_groups)`." }, { "name": "bias", "type": "T", "description": "Bias tensor of shape `(num_groups)`." } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "Y", "type": "T", "description": "The output tensor of the same shape 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)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "epsilon", "code": "c = 4\nnum_groups = 2\nx = np.random.randn(3, c, 2, 2).astype(np.float32)\nscale = np.random.randn(c).astype(np.float32)\nbias = np.random.randn(c).astype(np.float32)\nepsilon = 1e-2\ny = _group_normalization(x, num_groups, scale, bias, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"GroupNormalization\",\n inputs=[\"x\", \"scale\", \"bias\"],\n outputs=[\"y\"],\n epsilon=epsilon,\n num_groups=num_groups,\n)\n\nexpect(\n node,\n inputs=[x, scale, bias],\n outputs=[y],\n name=\"test_group_normalization_epsilon\",\n)" }, { "summary": "groupnormalization", "code": "c = 4\nnum_groups = 2\nx = np.random.randn(3, c, 2, 2).astype(np.float32)\nscale = np.random.randn(c).astype(np.float32)\nbias = np.random.randn(c).astype(np.float32)\ny = _group_normalization(x, num_groups, scale, bias).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"GroupNormalization\",\n inputs=[\"x\", \"scale\", \"bias\"],\n outputs=[\"y\"],\n num_groups=num_groups,\n)\n\nexpect(\n node,\n inputs=[x, scale, bias],\n outputs=[y],\n name=\"test_group_normalization_example\",\n)" } ] }, { "name": "GroupNormalization", "module": "ai.onnx", "version": 21, "description": "A GroupNormalization function. Carries out group normalization as described in\nthe paper https://arxiv.org/abs/1803.08494\n\nThis operator transforms input according to\n```\ny = scale * (x - mean) / sqrt(variance + epsilon) + bias,\n```\nwhere the mean and variance are computed per instance per group of channels, and\n`scale` and `bias` should be specified for each channel. The number of\ngroups `num_groups` should be divisible by the number of channels so that there are\nan equal number of channels per group.\n\nThe overall computation has two stages: the first stage normalizes the elements to\nhave zero mean and unit variance for each instance in each group, and the second\nstage scales and shifts the results of the first stage. The floating-point precision\nused in the first stage is determined by the `stash_type` attribute. For example,\nif `stash_type` is 1, the operator casts all input variables to 32-bit float,\nperforms the computation, and finally casts the normalized results back to the\noriginal type of `X`. The second stage does not depend on `stash_type`.\n\nWhen the number of groups is the same as the number of channels, this operator is\nequivalent to InstanceNormalization. When there is only one group, this operator\nis equivalent to LayerNormalization.\n", "attributes": [ { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "The epsilon value to use to avoid division by zero." }, { "name": "num_groups", "type": "int64", "required": true, "description": "The number of groups of channels. It should be a divisor of the number of channels `C`." }, { "name": "stash_type", "type": "int64", "required": false, "default": 1, "description": "The floating-point precision used in stage one of the computation." } ], "inputs": [ { "name": "X", "type": "T", "description": "Input data tensor. Dimensions for image cases 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 width of the data. Statistics are computed for every group of channels over `C`, `H`, and `W`. For non-image cases, the dimensions are in the form of `(N x C x D1 x D2 ... Dn)`." }, { "name": "scale", "type": "T", "description": "Scale tensor of shape `(C)`." }, { "name": "bias", "type": "T", "description": "Bias tensor of shape `(C)`." } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "Y", "type": "T", "description": "The output tensor of the same shape 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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "epsilon", "code": "c = 4\nnum_groups = 2\nx = np.random.randn(3, c, 2, 2).astype(np.float32)\nscale = np.random.randn(c).astype(np.float32)\nbias = np.random.randn(c).astype(np.float32)\nepsilon = 1e-2\ny = _group_normalization(x, num_groups, scale, bias, epsilon).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"GroupNormalization\",\n inputs=[\"x\", \"scale\", \"bias\"],\n outputs=[\"y\"],\n epsilon=epsilon,\n num_groups=num_groups,\n)\n\nexpect(\n node,\n inputs=[x, scale, bias],\n outputs=[y],\n name=\"test_group_normalization_epsilon\",\n)" }, { "summary": "groupnormalization", "code": "c = 4\nnum_groups = 2\nx = np.random.randn(3, c, 2, 2).astype(np.float32)\nscale = np.random.randn(c).astype(np.float32)\nbias = np.random.randn(c).astype(np.float32)\ny = _group_normalization(x, num_groups, scale, bias).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"GroupNormalization\",\n inputs=[\"x\", \"scale\", \"bias\"],\n outputs=[\"y\"],\n num_groups=num_groups,\n)\n\nexpect(\n node,\n inputs=[x, scale, bias],\n outputs=[y],\n name=\"test_group_normalization_example\",\n)" } ] }, { "name": "GroupQueryAttention", "module": "com.microsoft", "version": 1, "description": "Group Query Self/Cross Attention with KV Cache Quantization Support.\n\nThis operator implements causal grouped-query attention with past state (KV cache) support.\nIt also supports optional float8, int8 or int4 quantization for the KV cache to reduce memory footprint.\n\n**Cache Format:**\nThe past and present KV cache tensors are expected in a BNSH format: `(batch_size, num_heads, cache_sequence_length, head_size)`, where `cache_sequence_length` is the length of the cached key/value sequences, or the maximum sequence length when past and present buffer sharing is used.\n\n**Quantization:**\nWhen quantization is enabled, `past_key` and `past_value` inputs can be of type `float8e4m3fn`, `uint8` or `int8`. The corresponding `k_scale` and `v_scale` tensors must be provided.\nThe operator will output `present_key` and `present_value` in same format as the `past_key` and `past_value`.\n\nFor 4-bit quantization, the data type is uint8 where each byte contains two 4-bit values. The bit width of quantized KV cache can be set using `kv_cache_bit_width` attribute.\n\nThe shapes of the k_scale, v_scale tensors shall be broadcastable to present_key shape.\n\n**Quantization Modes (`k_quant_type`, `v_quant_type` attributes):**\n- **\"NONE\"**: No quantization.\n- **\"PER_TENSOR\"**: A single scale for the entire tensor. Scale example shape: `[1]`.\n- **\"PER_CHANNEL\"**: A scale for each channel. Scale example shape: `[1, num_heads_k, 1, head_size]`.\n", "attributes": [ { "name": "do_rotary", "type": "int64", "required": false, "description": "Whether to use rotary position embedding. Default value is 0." }, { "name": "k_quant_type", "type": "string", "required": false, "default": "NONE", "description": "Quantization type for K cache. One of 'NONE', 'PER_TENSOR', 'PER_CHANNEL'." }, { "name": "kv_cache_bit_width", "type": "int64", "required": false, "description": "Bit width of quantized KV cache. Supported values are 8 and 4." }, { "name": "kv_num_heads", "type": "int64", "required": true, "description": "Number of attention heads for k and v" }, { "name": "local_window_size", "type": "int64", "required": false, "default": -1, "description": "left_window_size for local attention (like Mistral). Default value is -1 meaning unused." }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads for q" }, { "name": "qk_norm_epsilon", "type": "float32", "required": false, "default": 9.999999974752427e-07, "description": "Epsilon used by the per-head RMS norm applied to Q and K when q_norm_weight and k_norm_weight inputs are provided. Default value is 1e-6." }, { "name": "qk_output", "type": "int64", "required": false, "description": "Output values of QK matrix multiplication before (1) or after (2) softmax normalization. Default value is 0 (don't output)." }, { "name": "rotary_interleaved", "type": "int64", "required": false, "description": "Rotate using interleaved pattern. Default value is 0 (False)." }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1/sqrt(head_size)" }, { "name": "smooth_softmax", "type": "int64", "required": false, "default": -1, "description": "Use a smooth factor in softmax." }, { "name": "softcap", "type": "float32", "required": false, "description": "Softcap value for attention weights. Default value is 0." }, { "name": "v_quant_type", "type": "string", "required": false, "default": "NONE", "description": "Quantization type for V cache. One of 'NONE', 'PER_TENSOR', 'PER_CHANNEL'." } ], "inputs": [ { "name": "query", "type": "T", "description": "Query with shape (batch_size, sequence_length, hidden_size), or packed QKV with shape(batch_size, sequence_length, d) where d is (num_heads * head_size + 2 * kv_num_heads * head_size)." }, { "name": "key", "type": "T", "option": "optional", "description": "Key with shape (batch_size, kv_sequence_length, kv_hidden_size) " }, { "name": "value", "type": "T", "option": "optional", "description": "Value with shape (batch_size, kv_sequence_length, kv_hidden_size)" }, { "name": "past_key", "type": "T_CACHE", "option": "optional", "description": "past state key with support for format BNSH. When past_key uses same tensor as present_key(k-v cache), it is of length max_sequence_length... otherwise of length past_sequence_length." }, { "name": "past_value", "type": "T_CACHE", "option": "optional", "description": "past state value with support for format BNSH. When past_value uses same tensor as present_value(k-v cache), it is of length max_sequence_length... otherwise of length past_sequence_length." }, { "name": "seqlens_k", "type": "M", "description": "1D Tensor of shape (batch_size). Equivalent to (total_sequence_lengths - 1)." }, { "name": "total_sequence_length", "type": "M", "description": "Scalar tensor equivalent to the maximum total sequence length (past + new) of the batch. Used for checking inputs and determining prompt vs token generation case." }, { "name": "cos_cache", "type": "T", "option": "optional", "description": "2D tensor with shape (max_sequence_length, head_size / 2)." }, { "name": "sin_cache", "type": "T", "option": "optional", "description": "2D tensor with shape (max_sequence_length, head_size / 2)." }, { "name": "position_ids", "type": "tensor(int64)", "option": "optional", "description": "2D tensor with shape (batch_size, sequence_length). When processing the first prompt the kernel uses only the first element" }, { "name": "attention_bias", "type": "T", "option": "optional", "description": "additional add to QxK' with shape (batch_size or 1, num_heads or 1, sequence_length, total_sequence_length)" }, { "name": "head_sink", "type": "T", "option": "optional", "description": "1D tensor with shape (num_heads). Each head has a smooth factor adding to the denominator of softmax." }, { "name": "k_scale", "type": "T_KV_SCALE", "option": "optional", "description": "Scale tensor for past_key." }, { "name": "v_scale", "type": "T_KV_SCALE", "option": "optional", "description": "Scale tensor for past_value." }, { "name": "q_norm_weight", "type": "T", "option": "optional", "description": "Optional 1D tensor of shape (head_size). When provided together with k_norm_weight, the kernel applies a per-head RMS normalization to Q (and K) before any rotary embedding. Used by Qwen3-style models that wrap their Q/K projections in a Reshape -> SimplifiedLayerNormalization -> Reshape stack; downstream graph fusion folds that pattern into this input. Currently honored by the CUDA and native WebGPU execution providers; JSEP WebGPU/JS and other EPs must reject the node when this input is set." }, { "name": "k_norm_weight", "type": "T", "option": "optional", "description": "Optional 1D tensor of shape (head_size). See q_norm_weight. Must be provided together with q_norm_weight." } ], "min_input": 7, "max_input": 16, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, hidden_size)" }, { "name": "present_key", "type": "T_CACHE", "option": "optional", "description": "present state key with support for format BNSH. When past_key uses same tensor as present_key(k-v buffer), it is of length max_sequence_length... otherwise of length past_sequence_length +kv_sequence_length." }, { "name": "present_value", "type": "T_CACHE", "option": "optional", "description": "present state value with support for format BNSH. When past_value uses same tensor as present_value(k-v buffer), it is of length max_sequence_length... otherwise of length past_sequence_length +kv_sequence_length." }, { "name": "output_qk", "type": "T", "option": "optional", "description": "Values of QK matrix multiplication, either before or after softmax normalization" } ], "min_output": 1, "max_output": 4, "inputs_range": "7 - 16", "outputs_range": "1 - 4", "type_constraints": [ { "description": "Constrain input and output to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(bfloat16)", "tensor(float)" ] }, { "description": "Constrain KV cache types.", "type_param_str": "T_CACHE", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(uint8)", "tensor(int8)", "tensor(float8e4m3fn)" ] }, { "description": "Constrain KV cache scale types.", "type_param_str": "T_KV_SCALE", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain mask to int tensor.", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "HammingWindow", "module": "ai.onnx", "version": 17, "description": "Generates a Hamming window as described in the paper https://ieeexplore.ieee.org/document/1455106.\n", "attributes": [ { "name": "output_datatype", "type": "int64", "required": false, "default": 1, "description": "The data type of the output tensor. Strictly must be one of the values from DataType enum in TensorProto whose values correspond to T2. The default value is 1 = FLOAT. " }, { "name": "periodic", "type": "int64", "required": false, "default": 1, "description": "If 1, returns a window to be used as periodic function. If 0, return a symmetric window. When 'periodic' is specified, hann computes a window of length size + 1 and returns the first size points. The default value is 1. " } ], "inputs": [ { "name": "size", "type": "T1", "description": "A scalar value indicating the length of the window." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T2", "description": "A Hamming window with length: size. The output has the shape: [size]." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain the input size to int32_t or int64_t.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] }, { "description": "Constrain output types to numeric tensors.", "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)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "hammingwindow", "code": "# Test periodic window\nnode = onnx.helper.make_node(\n \"HammingWindow\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\nsize = np.int32(10)\na0 = 25 / 46\na1 = 1 - a0\ny = a0 - a1 * np.cos(2 * np.pi * np.arange(0, size, 1, dtype=np.float32) / size)\nexpect(\n node,\n inputs=[size],\n outputs=[y.astype(np.float32)],\n name=\"test_hammingwindow\",\n)\n\n# Test symmetric window\nnode = onnx.helper.make_node(\n \"HammingWindow\", inputs=[\"x\"], outputs=[\"y\"], periodic=0\n)\nsize = np.int32(10)\na0 = 25 / 46\na1 = 1 - a0\ny = a0 - a1 * np.cos(\n 2 * np.pi * np.arange(0, size, 1, dtype=np.float32) / (size - 1)\n)\nexpect(\n node,\n inputs=[size],\n outputs=[y.astype(np.float32)],\n name=\"test_hammingwindow_symmetric\",\n)" } ] }, { "name": "HannWindow", "module": "ai.onnx", "version": 17, "description": "Generates a Hann window as described in the paper https://ieeexplore.ieee.org/document/1455106.\n", "attributes": [ { "name": "output_datatype", "type": "int64", "required": false, "default": 1, "description": "The data type of the output tensor. Strictly must be one of the values from DataType enum in TensorProto whose values correspond to T2. The default value is 1 = FLOAT. " }, { "name": "periodic", "type": "int64", "required": false, "default": 1, "description": "If 1, returns a window to be used as periodic function. If 0, return a symmetric window. When 'periodic' is specified, hann computes a window of length size + 1 and returns the first size points. The default value is 1. " } ], "inputs": [ { "name": "size", "type": "T1", "description": "A scalar value indicating the length of the window." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T2", "description": "A Hann window with length: size. The output has the shape: [size]." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain the input size to int32_t or int64_t.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] }, { "description": "Constrain output types to numeric tensors.", "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)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "hannwindow", "code": "# Test periodic window\nnode = onnx.helper.make_node(\n \"HannWindow\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\nsize = np.int32(10)\na0 = 0.5\na1 = 0.5\ny = a0 - a1 * np.cos(2 * np.pi * np.arange(0, size, 1, dtype=np.float32) / size)\nexpect(\n node, inputs=[size], outputs=[y.astype(np.float32)], name=\"test_hannwindow\"\n)\n\n# Test symmetric window\nnode = onnx.helper.make_node(\n \"HannWindow\", inputs=[\"x\"], outputs=[\"y\"], periodic=0\n)\nsize = np.int32(10)\na0 = 0.5\na1 = 0.5\ny = a0 - a1 * np.cos(\n 2 * np.pi * np.arange(0, size, 1, dtype=np.float32) / (size - 1)\n)\nexpect(\n node,\n inputs=[size],\n outputs=[y.astype(np.float32)],\n name=\"test_hannwindow_symmetric\",\n)" } ] }, { "name": "HardSigmoid", "module": "ai.onnx", "version": 1, "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\", inputs=[\"x\"], outputs=[\"y\"], alpha=0.5, 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], 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], 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], name=\"test_hardsigmoid_default\")" } ], "category": "Activation" }, { "name": "HardSigmoid", "module": "ai.onnx", "version": 6, "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\", inputs=[\"x\"], outputs=[\"y\"], alpha=0.5, 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], 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], 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], name=\"test_hardsigmoid_default\")" } ], "category": "Activation" }, { "name": "HardSigmoid", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "hardsigmoid", "code": "node = onnx.helper.make_node(\n \"HardSigmoid\", inputs=[\"x\"], outputs=[\"y\"], alpha=0.5, 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], 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], 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], name=\"test_hardsigmoid_default\")" } ], "category": "Activation" }, { "name": "HardSwish", "module": "ai.onnx", "version": 14, "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], name=\"test_hardswish\")" } ], "category": "Activation" }, { "name": "HardSwish", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], name=\"test_hardswish\")" } ], "category": "Activation" }, { "name": "Hardmax", "module": "ai.onnx", "version": 1, "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], [0, 1, 2, 3]]).astype(\n np.float32\n)\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], 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], 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], 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], 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], 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], 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], name=\"test_hardmax_default_axis\")" } ] }, { "name": "Hardmax", "module": "ai.onnx", "version": 11, "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], [0, 1, 2, 3]]).astype(\n np.float32\n)\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], 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], 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], 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], 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], 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], 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], name=\"test_hardmax_default_axis\")" } ] }, { "name": "Hardmax", "module": "ai.onnx", "version": 13, "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], [0, 1, 2, 3]]).astype(\n np.float32\n)\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], 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], 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], 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], 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], 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], 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], name=\"test_hardmax_default_axis\")" } ] }, { "name": "Identity", "module": "ai.onnx", "version": 1, "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity\")" }, { "summary": "identity_opt", "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"opt_in\"], outputs=[\"opt_out\"]\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(\n identity_node,\n inputs=[x],\n outputs=[x],\n 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],\n)" }, { "summary": "sequence", "code": "node = onnx.helper.make_node(\n \"Identity\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\ndata = [\n np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n np.array(\n [\n [\n [\n [2, 3],\n [1, 5],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n]\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity_sequence\")" } ] }, { "name": "Identity", "module": "ai.onnx", "version": 13, "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity\")" }, { "summary": "identity_opt", "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"opt_in\"], outputs=[\"opt_out\"]\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(\n identity_node,\n inputs=[x],\n outputs=[x],\n 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],\n)" }, { "summary": "sequence", "code": "node = onnx.helper.make_node(\n \"Identity\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\ndata = [\n np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n np.array(\n [\n [\n [\n [2, 3],\n [1, 5],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n]\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity_sequence\")" } ] }, { "name": "Identity", "module": "ai.onnx", "version": 14, "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity\")" }, { "summary": "identity_opt", "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"opt_in\"], outputs=[\"opt_out\"]\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(\n identity_node,\n inputs=[x],\n outputs=[x],\n 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],\n)" }, { "summary": "sequence", "code": "node = onnx.helper.make_node(\n \"Identity\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\ndata = [\n np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n np.array(\n [\n [\n [\n [2, 3],\n [1, 5],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n]\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity_sequence\")" } ] }, { "name": "Identity", "module": "ai.onnx", "version": 16, "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity\")" }, { "summary": "identity_opt", "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"opt_in\"], outputs=[\"opt_out\"]\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(\n identity_node,\n inputs=[x],\n outputs=[x],\n 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],\n)" }, { "summary": "sequence", "code": "node = onnx.helper.make_node(\n \"Identity\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\ndata = [\n np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n np.array(\n [\n [\n [\n [2, 3],\n [1, 5],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n]\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity_sequence\")" } ] }, { "name": "Identity", "module": "ai.onnx", "version": 19, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity\")" }, { "summary": "identity_opt", "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"opt_in\"], outputs=[\"opt_out\"]\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(\n identity_node,\n inputs=[x],\n outputs=[x],\n 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],\n)" }, { "summary": "sequence", "code": "node = onnx.helper.make_node(\n \"Identity\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\ndata = [\n np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n np.array(\n [\n [\n [\n [2, 3],\n [1, 5],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n]\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity_sequence\")" } ] }, { "name": "Identity", "module": "ai.onnx", "version": 21, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity\")" }, { "summary": "identity_opt", "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"opt_in\"], outputs=[\"opt_out\"]\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(\n identity_node,\n inputs=[x],\n outputs=[x],\n 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],\n)" }, { "summary": "sequence", "code": "node = onnx.helper.make_node(\n \"Identity\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\ndata = [\n np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n np.array(\n [\n [\n [\n [2, 3],\n [1, 5],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n]\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity_sequence\")" } ] }, { "name": "Identity", "module": "ai.onnx", "version": 23, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity\")" }, { "summary": "identity_opt", "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"opt_in\"], outputs=[\"opt_out\"]\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(\n identity_node,\n inputs=[x],\n outputs=[x],\n 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],\n)" }, { "summary": "sequence", "code": "node = onnx.helper.make_node(\n \"Identity\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\ndata = [\n np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n np.array(\n [\n [\n [\n [2, 3],\n [1, 5],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n]\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity_sequence\")" } ] }, { "name": "Identity", "module": "ai.onnx", "version": 24, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity\")" }, { "summary": "identity_opt", "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"opt_in\"], outputs=[\"opt_out\"]\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(\n identity_node,\n inputs=[x],\n outputs=[x],\n 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],\n)" }, { "summary": "sequence", "code": "node = onnx.helper.make_node(\n \"Identity\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\ndata = [\n np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n np.array(\n [\n [\n [\n [2, 3],\n [1, 5],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n]\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity_sequence\")" } ] }, { "name": "Identity", "module": "ai.onnx", "version": 25, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)", "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity\")" }, { "summary": "identity_opt", "code": "ten_in_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"opt_in\"], outputs=[\"opt_out\"]\n)\n\nx = [np.array([1, 2, 3, 4, 5]).astype(np.float32)]\n\nexpect(\n identity_node,\n inputs=[x],\n outputs=[x],\n 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],\n)" }, { "summary": "sequence", "code": "node = onnx.helper.make_node(\n \"Identity\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\ndata = [\n np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n np.array(\n [\n [\n [\n [2, 3],\n [1, 5],\n ]\n ]\n ],\n dtype=np.float32,\n ),\n]\n\nexpect(node, inputs=[data], outputs=[data], name=\"test_identity_sequence\")" } ] }, { "name": "If", "module": "ai.onnx", "version": 1, "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. The tensor must contain a single element." } ], "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(\n \"then_out\", onnx.TensorProto.FLOAT, [5]\n)\nelse_out = onnx.helper.make_tensor_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, [5]\n)\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], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[], outputs=[\"optional_empty\"], type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph([opt_empty_in], \"then_body\", [], [then_out])\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\", inputs=[\"x\"], outputs=[\"else_seq\"]\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n \"Optional\", inputs=[\"else_seq\"], 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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_opt\",\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n)" }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\nelse_out = onnx.helper.make_tensor_sequence_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"x\"], 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\", inputs=[\"y\"], outputs=[\"else_out\"]\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" } ] }, { "name": "If", "module": "ai.onnx", "version": 11, "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. The tensor must contain a single element." } ], "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 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(\n \"then_out\", onnx.TensorProto.FLOAT, [5]\n)\nelse_out = onnx.helper.make_tensor_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, [5]\n)\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], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[], outputs=[\"optional_empty\"], type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph([opt_empty_in], \"then_body\", [], [then_out])\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\", inputs=[\"x\"], outputs=[\"else_seq\"]\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n \"Optional\", inputs=[\"else_seq\"], 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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_opt\",\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n)" }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\nelse_out = onnx.helper.make_tensor_sequence_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"x\"], 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\", inputs=[\"y\"], outputs=[\"else_out\"]\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" } ] }, { "name": "If", "module": "ai.onnx", "version": 13, "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. The tensor must contain a single element." } ], "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 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(\n \"then_out\", onnx.TensorProto.FLOAT, [5]\n)\nelse_out = onnx.helper.make_tensor_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, [5]\n)\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], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[], outputs=[\"optional_empty\"], type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph([opt_empty_in], \"then_body\", [], [then_out])\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\", inputs=[\"x\"], outputs=[\"else_seq\"]\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n \"Optional\", inputs=[\"else_seq\"], 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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_opt\",\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n)" }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\nelse_out = onnx.helper.make_tensor_sequence_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"x\"], 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\", inputs=[\"y\"], outputs=[\"else_out\"]\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" } ] }, { "name": "If", "module": "ai.onnx", "version": 16, "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. The tensor must contain a single element." } ], "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 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 up to IRv4.", "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(\n \"then_out\", onnx.TensorProto.FLOAT, [5]\n)\nelse_out = onnx.helper.make_tensor_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, [5]\n)\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], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[], outputs=[\"optional_empty\"], type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph([opt_empty_in], \"then_body\", [], [then_out])\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\", inputs=[\"x\"], outputs=[\"else_seq\"]\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n \"Optional\", inputs=[\"else_seq\"], 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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_opt\",\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n)" }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\nelse_out = onnx.helper.make_tensor_sequence_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"x\"], 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\", inputs=[\"y\"], outputs=[\"else_out\"]\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" } ] }, { "name": "If", "module": "ai.onnx", "version": 19, "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. The tensor must contain a single element." } ], "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 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 up to IRv9.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))" ] }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, [5]\n)\nelse_out = onnx.helper.make_tensor_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, [5]\n)\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], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[], outputs=[\"optional_empty\"], type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph([opt_empty_in], \"then_body\", [], [then_out])\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\", inputs=[\"x\"], outputs=[\"else_seq\"]\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n \"Optional\", inputs=[\"else_seq\"], 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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_opt\",\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n)" }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\nelse_out = onnx.helper.make_tensor_sequence_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"x\"], 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\", inputs=[\"y\"], outputs=[\"else_out\"]\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" } ] }, { "name": "If", "module": "ai.onnx", "version": 21, "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. The tensor must contain a single element." } ], "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 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 up to IRv10.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "seq(tensor(uint4))", "seq(tensor(int4))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))", "optional(tensor(uint4))", "optional(tensor(int4))" ] }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, [5]\n)\nelse_out = onnx.helper.make_tensor_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, [5]\n)\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], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[], outputs=[\"optional_empty\"], type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph([opt_empty_in], \"then_body\", [], [then_out])\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\", inputs=[\"x\"], outputs=[\"else_seq\"]\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n \"Optional\", inputs=[\"else_seq\"], 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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_opt\",\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n)" }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\nelse_out = onnx.helper.make_tensor_sequence_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"x\"], 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\", inputs=[\"y\"], outputs=[\"else_out\"]\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" } ] }, { "name": "If", "module": "ai.onnx", "version": 23, "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. The tensor must contain a single element." } ], "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 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 up to IRv11.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "seq(tensor(uint4))", "seq(tensor(int4))", "seq(tensor(float4e2m1))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))", "optional(tensor(uint4))", "optional(tensor(int4))", "optional(tensor(float4e2m1))" ] }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, [5]\n)\nelse_out = onnx.helper.make_tensor_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, [5]\n)\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], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[], outputs=[\"optional_empty\"], type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph([opt_empty_in], \"then_body\", [], [then_out])\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\", inputs=[\"x\"], outputs=[\"else_seq\"]\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n \"Optional\", inputs=[\"else_seq\"], 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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_opt\",\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n)" }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\nelse_out = onnx.helper.make_tensor_sequence_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"x\"], 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\", inputs=[\"y\"], outputs=[\"else_out\"]\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" } ] }, { "name": "If", "module": "ai.onnx", "version": 24, "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. The tensor must contain a single element." } ], "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 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 up to IRv11.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "seq(tensor(uint4))", "seq(tensor(int4))", "seq(tensor(float4e2m1))", "seq(tensor(float8e8m0))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))", "optional(tensor(uint4))", "optional(tensor(int4))", "optional(tensor(float4e2m1))", "optional(tensor(float8e8m0))" ] }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, [5]\n)\nelse_out = onnx.helper.make_tensor_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, [5]\n)\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], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[], outputs=[\"optional_empty\"], type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph([opt_empty_in], \"then_body\", [], [then_out])\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\", inputs=[\"x\"], outputs=[\"else_seq\"]\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n \"Optional\", inputs=[\"else_seq\"], 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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_opt\",\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n)" }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\nelse_out = onnx.helper.make_tensor_sequence_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"x\"], 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\", inputs=[\"y\"], outputs=[\"else_out\"]\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" } ] }, { "name": "If", "module": "ai.onnx", "version": 25, "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. The tensor must contain a single element." } ], "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 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 up to IRv13.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "seq(tensor(uint4))", "seq(tensor(int4))", "seq(tensor(float4e2m1))", "seq(tensor(float8e8m0))", "seq(tensor(uint2))", "seq(tensor(int2))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))", "optional(tensor(uint4))", "optional(tensor(int4))", "optional(tensor(float4e2m1))", "optional(tensor(float8e8m0))", "optional(tensor(uint2))", "optional(tensor(int2))" ] }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, [5]\n)\nelse_out = onnx.helper.make_tensor_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, [5]\n)\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], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\nseq_in_tp = onnx.helper.make_sequence_type_proto(ten_in_tp)\n\nthen_out_tensor_tp = onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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(\n onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[], outputs=[\"optional_empty\"], type=seq_in_tp\n)\n\nthen_body = onnx.helper.make_graph([opt_empty_in], \"then_body\", [], [then_out])\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\", inputs=[\"x\"], outputs=[\"else_seq\"]\n)\n\nelse_optional_seq_node = onnx.helper.make_node(\n \"Optional\", inputs=[\"else_seq\"], 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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_opt\",\n output_type_protos=[else_out_opt_tp],\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n)" }, { "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(\n \"then_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\nelse_out = onnx.helper.make_tensor_sequence_value_info(\n \"else_out\", onnx.TensorProto.FLOAT, shape=[5]\n)\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\", inputs=[\"x\"], 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\", inputs=[\"y\"], outputs=[\"else_out\"]\n)\n\nthen_body = onnx.helper.make_graph(\n [then_const_node, then_seq_node], \"then_body\", [], [then_out]\n)\n\nelse_body = onnx.helper.make_graph(\n [else_const_node, else_seq_node], \"else_body\", [], [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(\n if_node,\n inputs=[cond],\n outputs=[res],\n name=\"test_if_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" } ] }, { "name": "ImageDecoder", "module": "ai.onnx", "version": 20, "description": "Loads and decodes and image from a file. If it can't decode for any reason (e.g. corrupted encoded\nstream, invalid format, it will return an empty matrix).\nThe following image formats are supported:\n* BMP\n* JPEG (note: Lossless JPEG support is optional)\n* JPEG2000\n* TIFF\n* PNG\n* WebP\n* Portable image format (PBM, PGM, PPM, PXM, PNM)\nDecoded images follow a channel-last layout: (Height, Width, Channels).\n**JPEG chroma upsampling method:**\nWhen upsampling the chroma components by a factor of 2, the pixels are linearly interpolated so that the\ncenters of the output pixels are 1/4 and 3/4 of the way between input pixel centers.\nWhen rounding, 0.5 is rounded down and up at alternative pixels locations to prevent bias towards\nlarger values (ordered dither pattern).\nConsidering adjacent input pixels A, B, and C, B is upsampled to pixels B0 and B1 so that\n```\nB0 = round_half_down((1/4) * A + (3/4) * B)\nB1 = round_half_up((3/4) * B + (1/4) * C)\n```\nThis method, is the default chroma upsampling method in the well-established libjpeg-turbo library,\nalso referred as \"smooth\" or \"fancy\" upsampling.\n", "attributes": [ { "name": "pixel_format", "type": "string", "required": false, "default": "RGB", "description": "Pixel format. Can be one of \"RGB\", \"BGR\", or \"Grayscale\"." } ], "inputs": [ { "name": "encoded_stream", "type": "T1", "description": "Encoded stream" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "image", "type": "T2", "description": "Decoded image" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input types to 8-bit unsigned integer tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(uint8)" ] }, { "description": "Constrain output types to 8-bit unsigned integer tensor.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(uint8)" ] } ], "examples": [ { "summary": "image_decoder_decode_bmp_rgb", "code": "node = onnx.helper.make_node(\n \"ImageDecoder\",\n inputs=[\"data\"],\n outputs=[\"output\"],\n pixel_format=\"RGB\",\n)\n\ndata, output = _generate_test_data(\n \"bmp\", _image_decoder_data.image_decoder_decode_bmp_rgb, \"RGB\"\n)\nexpect(\n node,\n inputs=[data],\n outputs=[output],\n name=\"test_image_decoder_decode_bmp_rgb\",\n)" }, { "summary": "image_decoder_decode_jpeg2k_rgb", "code": "node = onnx.helper.make_node(\n \"ImageDecoder\",\n inputs=[\"data\"],\n outputs=[\"output\"],\n pixel_format=\"RGB\",\n)\n\ndata, output = _generate_test_data(\n \"jpeg2000\", _image_decoder_data.image_decoder_decode_jpeg2k_rgb, \"RGB\"\n)\nexpect(\n node,\n inputs=[data],\n outputs=[output],\n name=\"test_image_decoder_decode_jpeg2k_rgb\",\n)" }, { "summary": "image_decoder_decode_jpeg_bgr", "code": "node = onnx.helper.make_node(\n \"ImageDecoder\",\n inputs=[\"data\"],\n outputs=[\"output\"],\n pixel_format=\"BGR\",\n)\n\ndata, output = _generate_test_data(\n \"jpeg\", _image_decoder_data.image_decoder_decode_jpeg_bgr, \"BGR\"\n)\nexpect(\n node,\n inputs=[data],\n outputs=[output],\n name=\"test_image_decoder_decode_jpeg_bgr\",\n)" }, { "summary": "image_decoder_decode_jpeg_grayscale", "code": "node = onnx.helper.make_node(\n \"ImageDecoder\",\n inputs=[\"data\"],\n outputs=[\"output\"],\n pixel_format=\"Grayscale\",\n)\n\ndata, output = _generate_test_data(\n \"jpeg\", _image_decoder_data.image_decoder_decode_jpeg_grayscale, \"Grayscale\"\n)\nexpect(\n node,\n inputs=[data],\n outputs=[output],\n name=\"test_image_decoder_decode_jpeg_grayscale\",\n)" }, { "summary": "image_decoder_decode_jpeg_rgb", "code": "node = onnx.helper.make_node(\n \"ImageDecoder\",\n inputs=[\"data\"],\n outputs=[\"output\"],\n pixel_format=\"RGB\",\n)\n\ndata, output = _generate_test_data(\n \"jpeg\", _image_decoder_data.image_decoder_decode_jpeg_rgb, \"RGB\"\n)\nexpect(\n node,\n inputs=[data],\n outputs=[output],\n name=\"test_image_decoder_decode_jpeg_rgb\",\n)" }, { "summary": "image_decoder_decode_png_rgb", "code": "node = onnx.helper.make_node(\n \"ImageDecoder\",\n inputs=[\"data\"],\n outputs=[\"output\"],\n pixel_format=\"RGB\",\n)\n\ndata, output = _generate_test_data(\n \"png\", _image_decoder_data.image_decoder_decode_png_rgb, \"RGB\"\n)\nexpect(\n node,\n inputs=[data],\n outputs=[output],\n name=\"test_image_decoder_decode_png_rgb\",\n)" }, { "summary": "image_decoder_decode_pnm_rgb", "code": "node = onnx.helper.make_node(\n \"ImageDecoder\",\n inputs=[\"data\"],\n outputs=[\"output\"],\n pixel_format=\"RGB\",\n)\n\ndata, output = _generate_test_data(\n \"ppm\", _image_decoder_data.image_decoder_decode_pnm_rgb, \"RGB\"\n)\nexpect(\n node,\n inputs=[data],\n outputs=[output],\n name=\"test_image_decoder_decode_pnm_rgb\",\n)" }, { "summary": "image_decoder_decode_tiff_rgb", "code": "node = onnx.helper.make_node(\n \"ImageDecoder\",\n inputs=[\"data\"],\n outputs=[\"output\"],\n pixel_format=\"RGB\",\n)\n\ndata, output = _generate_test_data(\n \"tiff\", _image_decoder_data.image_decoder_decode_tiff_rgb, \"RGB\"\n)\nexpect(\n node,\n inputs=[data],\n outputs=[output],\n name=\"test_image_decoder_decode_tiff_rgb\",\n)" }, { "summary": "image_decoder_decode_webp_rgb", "code": "node = onnx.helper.make_node(\n \"ImageDecoder\",\n inputs=[\"data\"],\n outputs=[\"output\"],\n pixel_format=\"RGB\",\n)\n\ndata, output = _generate_test_data(\n \"webp\", _image_decoder_data.image_decoder_decode_webp_rgb, \"RGB\"\n)\nexpect(\n node,\n inputs=[data],\n outputs=[output],\n name=\"test_image_decoder_decode_webp_rgb\",\n)" } ] }, { "name": "ImageScaler", "module": "ai.onnx", "version": 1, "description": "Scale and bias the input image. Bias values are stored in\nthe same ordering as the image pixel format.", "attributes": [ { "name": "bias", "type": "float32[]", "required": false, "description": "Bias applied to each channel, same size as C." }, { "name": "scale", "type": "float32", "required": false, "default": 1.0, "description": "The scale to apply." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor of shape [N,C,H,W]" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "Result, has same shape and type 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)" ] } ], "category": "Data" }, { "name": "ImageScaler", "module": "ai.onnx", "version": 10, "description": "Scale and bias the input image. Bias values are stored in\nthe same ordering as the image pixel format.", "attributes": [ { "name": "bias", "type": "float32[]", "required": false, "description": "Bias applied to each channel, same size as C." }, { "name": "scale", "type": "float32", "required": false, "default": 1.0, "description": "The scale to apply." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor of shape [N,C,H,W]" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "Result, has same shape and type 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)" ] } ], "category": "Data" }, { "name": "Imputer", "module": "ai.onnx.ml", "version": 1, "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, "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(\n x: np.ndarray, s: np.ndarray, bias: np.ndarray, epsilon: float = 1e-5\n) -> np.ndarray:\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], 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], name=\"test_instancenorm_epsilon\")" } ], "category": "Normalization" }, { "name": "InstanceNormalization", "module": "ai.onnx", "version": 6, "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(\n x: np.ndarray, s: np.ndarray, bias: np.ndarray, epsilon: float = 1e-5\n) -> np.ndarray:\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], 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], name=\"test_instancenorm_epsilon\")" } ], "category": "Normalization" }, { "name": "InstanceNormalization", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "instancenormalization", "code": "def _instancenorm_test_mode(\n x: np.ndarray, s: np.ndarray, bias: np.ndarray, epsilon: float = 1e-5\n) -> np.ndarray:\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], 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], name=\"test_instancenorm_epsilon\")" } ], "category": "Normalization" }, { "name": "InstanceNormalization", "module": "com.ms.internal.nhwc", "version": 6, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Normalization" }, { "name": "Inverse", "module": "com.microsoft", "version": 1, "inputs": [ { "name": "X", "type": "T", "description": "Input tensor. Every matrix in the batch must be invertible." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "Output tensor of the same type and 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)" ] } ] }, { "name": "Irfft", "module": "com.microsoft", "version": 1, "description": "This function computes the inverse of the one-dimensional n-point RFFT computed in 'com.microsoft.rfft'.", "attributes": [ { "name": "normalized", "type": "int64", "required": false, "description": "must be 0, normalization currently not supported" }, { "name": "onesided", "type": "int64", "required": false, "default": 1, "description": "must be 1, only one sided FFTs supported" }, { "name": "signal_ndim", "type": "int64", "required": true, "description": "number of dimensions comprising the signal" } ], "inputs": [ { "name": "X", "type": "T", "description": "input tensor with size (n//2 + 1) in the signal dim and 2 in the last dimension for the real and complex parts" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "output tensor with size n in the signal dim" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to float or half tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(double)", "tensor(float16)" ] } ] }, { "name": "IsAllFinite", "module": "com.microsoft", "version": 1, "status": "experimental", "description": "IsAllFinite", "attributes": [ { "name": "isinf_only", "type": "int64", "required": false, "description": "If true, check only for Inf, -Inf." }, { "name": "isnan_only", "type": "int64", "required": false, "description": "If true, check only for NaN." } ], "inputs": [ { "name": "input", "type": "V", "list": true, "description": "Input tensors to check." } ], "min_input": 1, "max_input": 2147483647, "outputs": [ { "name": "output", "type": "T", "description": "The output scalar. Its value is true if all input tensors are finite. Otherwise, the output value would be false." } ], "min_output": 1, "max_output": 1, "inputs_range": "1 - ∞", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "V", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] }, { "description": "Constrain the output to a boolean tensor.", "type_param_str": "T", "allowed_type_strs": [ "tensor(bool)" ] } ] }, { "name": "IsInf", "module": "ai.onnx", "version": 10, "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(\n \"IsInf\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float32)\ny = np.isinf(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isinf\")" }, { "summary": "infinity_float16", "code": "node = onnx.helper.make_node(\n \"IsInf\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float16)\ny = np.isinf(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isinf_float16\")" }, { "summary": "negative_infinity_only", "code": "node = onnx.helper.make_node(\n \"IsInf\", inputs=[\"x\"], outputs=[\"y\"], detect_positive=0\n)\n\nx = np.array([-1.7, np.nan, np.inf, -3.6, -np.inf, np.inf], 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(\n \"IsInf\", inputs=[\"x\"], outputs=[\"y\"], detect_negative=0\n)\n\nx = np.array([-1.7, np.nan, np.inf, 3.6, -np.inf, np.inf], dtype=np.float32)\ny = np.isposinf(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isinf_positive\")" } ] }, { "name": "IsInf", "module": "ai.onnx", "version": 20, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "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(\n \"IsInf\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float32)\ny = np.isinf(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isinf\")" }, { "summary": "infinity_float16", "code": "node = onnx.helper.make_node(\n \"IsInf\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float16)\ny = np.isinf(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isinf_float16\")" }, { "summary": "negative_infinity_only", "code": "node = onnx.helper.make_node(\n \"IsInf\", inputs=[\"x\"], outputs=[\"y\"], detect_positive=0\n)\n\nx = np.array([-1.7, np.nan, np.inf, -3.6, -np.inf, np.inf], 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(\n \"IsInf\", inputs=[\"x\"], outputs=[\"y\"], detect_negative=0\n)\n\nx = np.array([-1.7, np.nan, np.inf, 3.6, -np.inf, np.inf], dtype=np.float32)\ny = np.isposinf(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isinf_positive\")" } ] }, { "name": "IsNaN", "module": "ai.onnx", "version": 9, "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": "float16", "code": "node = onnx.helper.make_node(\n \"IsNaN\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float16)\ny = np.isnan(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isnan_float16\")" }, { "summary": "isnan", "code": "node = onnx.helper.make_node(\n \"IsNaN\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float32)\ny = np.isnan(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isnan\")" } ] }, { "name": "IsNaN", "module": "ai.onnx", "version": 13, "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": "float16", "code": "node = onnx.helper.make_node(\n \"IsNaN\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float16)\ny = np.isnan(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isnan_float16\")" }, { "summary": "isnan", "code": "node = onnx.helper.make_node(\n \"IsNaN\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float32)\ny = np.isnan(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isnan\")" } ] }, { "name": "IsNaN", "module": "ai.onnx", "version": 20, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "description": "Constrain output types to boolean tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(bool)" ] } ], "examples": [ { "summary": "float16", "code": "node = onnx.helper.make_node(\n \"IsNaN\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float16)\ny = np.isnan(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isnan_float16\")" }, { "summary": "isnan", "code": "node = onnx.helper.make_node(\n \"IsNaN\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n)\n\nx = np.array([-1.2, np.nan, np.inf, 2.8, -np.inf, np.inf], dtype=np.float32)\ny = np.isnan(x)\nexpect(node, inputs=[x], outputs=[y], name=\"test_isnan\")" } ] }, { "name": "LRN", "module": "ai.onnx", "version": 1, "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 }, { "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(\"LRN\", inputs=[\"x\"], outputs=[\"y\"], size=3)\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(\n x[\n n,\n max(0, c - math.floor((nsize - 1) / 2)) : min(\n 5, c + math.ceil((nsize - 1) / 2) + 1\n ),\n h,\n w,\n ]\n ** 2\n )\ny = x / ((bias + (alpha / nsize) * square_sum) ** beta)\nexpect(node, inputs=[x], outputs=[y], 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(\n x[\n n,\n max(0, c - math.floor((nsize - 1) / 2)) : min(\n 5, c + math.ceil((nsize - 1) / 2) + 1\n ),\n h,\n w,\n ]\n ** 2\n )\ny = x / ((bias + (alpha / nsize) * square_sum) ** beta)\nexpect(node, inputs=[x], outputs=[y], name=\"test_lrn\")" } ], "category": "Normalization" }, { "name": "LRN", "module": "ai.onnx", "version": 13, "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\n`square_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\n`Y[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 }, { "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(\"LRN\", inputs=[\"x\"], outputs=[\"y\"], size=3)\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(\n x[\n n,\n max(0, c - math.floor((nsize - 1) / 2)) : min(\n 5, c + math.ceil((nsize - 1) / 2) + 1\n ),\n h,\n w,\n ]\n ** 2\n )\ny = x / ((bias + (alpha / nsize) * square_sum) ** beta)\nexpect(node, inputs=[x], outputs=[y], 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(\n x[\n n,\n max(0, c - math.floor((nsize - 1) / 2)) : min(\n 5, c + math.ceil((nsize - 1) / 2) + 1\n ),\n h,\n w,\n ]\n ** 2\n )\ny = x / ((bias + (alpha / nsize) * square_sum) ** beta)\nexpect(node, inputs=[x], outputs=[y], name=\"test_lrn\")" } ], "category": "Normalization" }, { "name": "LRN", "module": "com.ms.internal.nhwc", "version": 1, "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 }, { "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)" ] } ], "category": "Normalization" }, { "name": "LRN", "module": "com.ms.internal.nhwc", "version": 13, "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\n`square_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\n`Y[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 }, { "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)" ] } ], "category": "Normalization" }, { "name": "LSTM", "module": "ai.onnx", "version": 1, "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\nlstm = LSTMHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_lstm_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).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\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\nlstm = LSTMHelper(X=input, W=W, R=R)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(\n np.float32\n)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), 1)\n\nlstm = LSTMHelper(X=input, W=W, R=R, B=B)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_with_initial_bias\",\n)" }, { "summary": "peepholes", "code": "input = np.array([[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).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(\n np.float32\n)\n\nlstm = LSTMHelper(\n X=input, W=W, R=R, B=B, P=P, initial_c=init_c, initial_h=init_h\n)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R, B, seq_lens, init_h, init_c, P],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_with_peepholes\",\n)" } ], "category": "Layer" }, { "name": "LSTM", "module": "ai.onnx", "version": 7, "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\nlstm = LSTMHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_lstm_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).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\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\nlstm = LSTMHelper(X=input, W=W, R=R)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(\n np.float32\n)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), 1)\n\nlstm = LSTMHelper(X=input, W=W, R=R, B=B)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_with_initial_bias\",\n)" }, { "summary": "peepholes", "code": "input = np.array([[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).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(\n np.float32\n)\n\nlstm = LSTMHelper(\n X=input, W=W, R=R, B=B, P=P, initial_c=init_c, initial_h=init_h\n)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R, B, seq_lens, init_h, init_c, P],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_with_peepholes\",\n)" } ], "category": "Layer" }, { "name": "LSTM", "module": "ai.onnx", "version": 14, "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* `i` - input gate\n* `o` - output gate\n* `f` - forget gate\n* `c` - cell gate\n* `t` - time step (t-1 means previous time step)\n* `W[iofc]` - W parameter weight matrix for input, output, forget, and cell gates\n* `R[iofc]` - R recurrence weight matrix for input, output, forget, and cell gates\n* `Wb[iofc]` - W bias vectors for input, output, forget, and cell gates\n* `Rb[iofc]` - R bias vectors for input, output, forget, and cell gates\n* `P[iof]` - P peephole weight vector for input, output, and forget gates\n* `WB[iofc]` - W parameter weight matrix for backward input, output, forget, and cell gates\n* `RB[iofc]` - R recurrence weight matrix for backward input, output, forget, and cell gates\n* `WBb[iofc]` - W bias vectors for backward input, output, forget, and cell gates\n* `RBb[iofc]` - R bias vectors for backward input, output, forget, and cell gates\n* `PB[iof]` - P peephole weight vector for backward input, output, and forget gates\n* `H` - Hidden state\n* `num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n* Relu(x) - max(0, x)\n* Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n* Sigmoid(x) - 1/(1 + e^{-x})\n\nNOTE: Below are optional\n\n* Affine(x) - alpha*x + beta\n* LeakyRelu(x) - x if x >= 0 else alpha * x\n* ThresholdedRelu(x) - x if x >= alpha else 0\n* ScaledTanh(x) - alpha*Tanh(beta*x)\n* HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n* Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n* Softsign(x) - x/(1 + |x|)\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* ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Pf (.) Ct-1 + Wbf + Rbf)\n* ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)\n* Ct = ft (.) Ct-1 + it (.) ct\n* ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Po (.) Ct + Wbo + Rbo)\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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\nlstm = LSTMHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_lstm_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).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\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\nlstm = LSTMHelper(X=input, W=W, R=R)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(\n np.float32\n)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), 1)\n\nlstm = LSTMHelper(X=input, W=W, R=R, B=B)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_with_initial_bias\",\n)" }, { "summary": "peepholes", "code": "input = np.array([[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).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(\n np.float32\n)\n\nlstm = LSTMHelper(\n X=input, W=W, R=R, B=B, P=P, initial_c=init_c, initial_h=init_h\n)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R, B, seq_lens, init_h, init_c, P],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_with_peepholes\",\n)" } ], "category": "Layer" }, { "name": "LSTM", "module": "ai.onnx", "version": 22, "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* `i` - input gate\n* `o` - output gate\n* `f` - forget gate\n* `c` - cell gate\n* `t` - time step (t-1 means previous time step)\n* `W[iofc]` - W parameter weight matrix for input, output, forget, and cell gates\n* `R[iofc]` - R recurrence weight matrix for input, output, forget, and cell gates\n* `Wb[iofc]` - W bias vectors for input, output, forget, and cell gates\n* `Rb[iofc]` - R bias vectors for input, output, forget, and cell gates\n* `P[iof]` - P peephole weight vector for input, output, and forget gates\n* `WB[iofc]` - W parameter weight matrix for backward input, output, forget, and cell gates\n* `RB[iofc]` - R recurrence weight matrix for backward input, output, forget, and cell gates\n* `WBb[iofc]` - W bias vectors for backward input, output, forget, and cell gates\n* `RBb[iofc]` - R bias vectors for backward input, output, forget, and cell gates\n* `PB[iof]` - P peephole weight vector for backward input, output, and forget gates\n* `H` - Hidden state\n* `num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n* Relu(x) - max(0, x)\n* Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n* Sigmoid(x) - 1/(1 + e^{-x})\n\nNOTE: Below are optional\n\n* Affine(x) - alpha*x + beta\n* LeakyRelu(x) - x if x >= 0 else alpha * x\n* ThresholdedRelu(x) - x if x >= alpha else 0\n* ScaledTanh(x) - alpha*Tanh(beta*x)\n* HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n* Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n* Softsign(x) - x/(1 + |x|)\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* ft = f(Xt*(Wf^T) + Ht-1*(Rf^T) + Pf (.) Ct-1 + Wbf + Rbf)\n* ct = g(Xt*(Wc^T) + Ht-1*(Rc^T) + Wbc + Rbc)\n* Ct = ft (.) Ct-1 + it (.) ct\n* ot = f(Xt*(Wo^T) + Ht-1*(Ro^T) + Po (.) Ct + Wbo + Rbo)\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(bfloat16)", "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\nlstm = LSTMHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_lstm_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).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\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], hidden_size=hidden_size\n)\n\nW = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\nlstm = LSTMHelper(X=input, W=W, R=R)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).astype(np.float32)\n\n# Adding custom bias\nW_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(\n np.float32\n)\nR_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)\nB = np.concatenate((W_B, R_B), 1)\n\nlstm = LSTMHelper(X=input, W=W, R=R, B=B)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_with_initial_bias\",\n)" }, { "summary": "peepholes", "code": "input = np.array([[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]]).astype(\n np.float32\n)\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(\n (1, number_of_gates * hidden_size, input_size)\n).astype(np.float32)\nR = weight_scale * np.ones(\n (1, number_of_gates * hidden_size, hidden_size)\n).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(\n np.float32\n)\n\nlstm = LSTMHelper(\n X=input, W=W, R=R, B=B, P=P, initial_c=init_c, initial_h=init_h\n)\n_, Y_h = lstm.step()\nexpect(\n node,\n inputs=[input, W, R, B, seq_lens, init_h, init_c, P],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_lstm_with_peepholes\",\n)" } ], "category": "Layer" }, { "name": "LabelEncoder", "module": "ai.onnx.ml", "version": 1, "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)" ] } ], "examples": [ { "summary": "string_int_label_encoder", "code": "node = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_strings=[\"a\", \"b\", \"c\"],\n values_int64s=[0, 1, 2],\n default_int64=42,\n)\nx = np.array([\"a\", \"b\", \"d\", \"c\", \"g\"]).astype(object)\ny = np.array([0, 1, 42, 2, 42]).astype(np.int64)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_string_int\",\n)\n\nnode = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_strings=[\"a\", \"b\", \"c\"],\n values_int64s=[0, 1, 2],\n)\nx = np.array([\"a\", \"b\", \"d\", \"c\", \"g\"]).astype(object)\ny = np.array([0, 1, -1, 2, -1]).astype(np.int64)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_string_int_no_default\",\n)" }, { "summary": "tensor_based_label_encoder", "code": "tensor_keys = make_tensor(\n \"keys_tensor\", onnx.TensorProto.STRING, (3,), [\"a\", \"b\", \"c\"]\n)\nrepeated_string_keys = [\"a\", \"b\", \"c\"]\nx = np.array([\"a\", \"b\", \"d\", \"c\", \"g\"]).astype(object)\ny = np.array([0, 1, 42, 2, 42]).astype(np.int16)\n\nnode = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_tensor=tensor_keys,\n values_tensor=make_tensor(\n \"values_tensor\", onnx.TensorProto.INT16, (3,), [0, 1, 2]\n ),\n default_tensor=make_tensor(\n \"default_tensor\", onnx.TensorProto.INT16, (1,), [42]\n ),\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_tensor_mapping\",\n)\n\nnode = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_strings=repeated_string_keys,\n values_tensor=make_tensor(\n \"values_tensor\", onnx.TensorProto.INT16, (3,), [0, 1, 2]\n ),\n default_tensor=make_tensor(\n \"default_tensor\", onnx.TensorProto.INT16, (1,), [42]\n ),\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_tensor_value_only_mapping\",\n)" } ] }, { "name": "LabelEncoder", "module": "ai.onnx.ml", "version": 2, "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)" ] } ], "examples": [ { "summary": "string_int_label_encoder", "code": "node = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_strings=[\"a\", \"b\", \"c\"],\n values_int64s=[0, 1, 2],\n default_int64=42,\n)\nx = np.array([\"a\", \"b\", \"d\", \"c\", \"g\"]).astype(object)\ny = np.array([0, 1, 42, 2, 42]).astype(np.int64)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_string_int\",\n)\n\nnode = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_strings=[\"a\", \"b\", \"c\"],\n values_int64s=[0, 1, 2],\n)\nx = np.array([\"a\", \"b\", \"d\", \"c\", \"g\"]).astype(object)\ny = np.array([0, 1, -1, 2, -1]).astype(np.int64)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_string_int_no_default\",\n)" }, { "summary": "tensor_based_label_encoder", "code": "tensor_keys = make_tensor(\n \"keys_tensor\", onnx.TensorProto.STRING, (3,), [\"a\", \"b\", \"c\"]\n)\nrepeated_string_keys = [\"a\", \"b\", \"c\"]\nx = np.array([\"a\", \"b\", \"d\", \"c\", \"g\"]).astype(object)\ny = np.array([0, 1, 42, 2, 42]).astype(np.int16)\n\nnode = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_tensor=tensor_keys,\n values_tensor=make_tensor(\n \"values_tensor\", onnx.TensorProto.INT16, (3,), [0, 1, 2]\n ),\n default_tensor=make_tensor(\n \"default_tensor\", onnx.TensorProto.INT16, (1,), [42]\n ),\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_tensor_mapping\",\n)\n\nnode = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_strings=repeated_string_keys,\n values_tensor=make_tensor(\n \"values_tensor\", onnx.TensorProto.INT16, (3,), [0, 1, 2]\n ),\n default_tensor=make_tensor(\n \"default_tensor\", onnx.TensorProto.INT16, (1,), [42]\n ),\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_tensor_value_only_mapping\",\n)" } ] }, { "name": "LabelEncoder", "module": "ai.onnx.ml", "version": 4, "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. Note that the 'keys_*' and 'values_*' attributes\n must have the same length. 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. The type of the 'default_*'\n attribute must match the 'values_*' attribute chosen.
\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 Float keys with value 'NaN' match any input 'NaN' value regardless of bit\n value. If a key is repeated, the last key takes precedence.\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": "default_tensor", "type": "tensor", "required": false, "description": "A default tensor. {\"_Unused\"} if values_* has string type, {-1} if values_* has integral type, and {-0.f} if values_* has float type." }, { "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." }, { "name": "keys_tensor", "type": "tensor", "required": false, "description": "Keys encoded as a 1D tensor. 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." }, { "name": "values_tensor", "type": "tensor", "required": false, "description": "Values encoded as a 1D tensor. One and only one of 'values_*'s should be set." } ], "inputs": [ { "name": "X", "type": "T1", "description": "Input data. It must have the same element type as the keys_* attribute set." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T2", "description": "Output data. This tensor's element type is based on the values_* attribute set." } ], "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)", "tensor(int32)", "tensor(int16)", "tensor(double)" ] }, { "description": "Output type is determined by the specified 'values_*' attribute.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(string)", "tensor(int64)", "tensor(float)", "tensor(int32)", "tensor(int16)", "tensor(double)" ] } ], "examples": [ { "summary": "string_int_label_encoder", "code": "node = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_strings=[\"a\", \"b\", \"c\"],\n values_int64s=[0, 1, 2],\n default_int64=42,\n)\nx = np.array([\"a\", \"b\", \"d\", \"c\", \"g\"]).astype(object)\ny = np.array([0, 1, 42, 2, 42]).astype(np.int64)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_string_int\",\n)\n\nnode = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_strings=[\"a\", \"b\", \"c\"],\n values_int64s=[0, 1, 2],\n)\nx = np.array([\"a\", \"b\", \"d\", \"c\", \"g\"]).astype(object)\ny = np.array([0, 1, -1, 2, -1]).astype(np.int64)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_string_int_no_default\",\n)" }, { "summary": "tensor_based_label_encoder", "code": "tensor_keys = make_tensor(\n \"keys_tensor\", onnx.TensorProto.STRING, (3,), [\"a\", \"b\", \"c\"]\n)\nrepeated_string_keys = [\"a\", \"b\", \"c\"]\nx = np.array([\"a\", \"b\", \"d\", \"c\", \"g\"]).astype(object)\ny = np.array([0, 1, 42, 2, 42]).astype(np.int16)\n\nnode = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_tensor=tensor_keys,\n values_tensor=make_tensor(\n \"values_tensor\", onnx.TensorProto.INT16, (3,), [0, 1, 2]\n ),\n default_tensor=make_tensor(\n \"default_tensor\", onnx.TensorProto.INT16, (1,), [42]\n ),\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_tensor_mapping\",\n)\n\nnode = onnx.helper.make_node(\n \"LabelEncoder\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n domain=\"ai.onnx.ml\",\n keys_strings=repeated_string_keys,\n values_tensor=make_tensor(\n \"values_tensor\", onnx.TensorProto.INT16, (3,), [0, 1, 2]\n ),\n default_tensor=make_tensor(\n \"default_tensor\", onnx.TensorProto.INT16, (1,), [42]\n ),\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_label_encoder_tensor_value_only_mapping\",\n)" } ] }, { "name": "LayerNormalization", "module": "ai.onnx", "version": 1, "status": "experimental", "description": "LayerNormalization", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": -1, "description": "The first normalization dimension: normalization will be performed along dimensions axis : rank(inputs)." }, { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "The epsilon value to use to avoid division by zero." }, { "name": "stash_type", "type": "int64", "required": false, "default": 1, "description": "type used for stash mean/inv_std_var" } ], "inputs": [ { "name": "X", "type": "T", "description": "Input data tensor from the previous layer." }, { "name": "Scale", "type": "V", "description": "Scale tensor." }, { "name": "B", "type": "V", "option": "optional", "description": "Bias tensor." } ], "min_input": 2, "max_input": 3, "outputs": [ { "name": "Y", "type": "V", "description": "Output data tensor." }, { "name": "Mean", "type": "U", "option": "optional", "description": "Saved mean used during training to speed up gradient computation" }, { "name": "InvStdDev", "type": "U", "option": "optional", "description": "Saved inverse standard deviation used during training to speed up gradient computation." } ], "min_output": 1, "max_output": 3, "inputs_range": "2 - 3", "outputs_range": "1 - 3", "type_constraints": [ { "description": "Constrain input X type to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] }, { "description": "Type of Mean and InvStdDev tensors.", "type_param_str": "U", "allowed_type_strs": [ "tensor(float)", "tensor(double)" ] }, { "description": "Constrain output Y, scale and bias type to float tensors.", "type_param_str": "V", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] } ], "category": "Normalization" }, { "name": "LayerNormalization", "module": "ai.onnx", "version": 17, "description": "This is layer normalization defined in ONNX as function.\n The overall computation can be split into two stages.\n The first stage is standardization, which makes the\n normalized elements have zero mean and unit variances.\n The computation required by standardization can be\n described by the following equations.\n ```\n Mean = ReduceMean(X)\n D = Sub(X, Mean)\n DD = Mul(D, D)\n Var = ReduceMean(DD)\n VarEps = Add(Var, epsilon)\n StdDev = Sqrt(VarEps)\n InvStdDev = Reciprocal(StdDev)\n Normalized = Mul(D, InvStdDev)\n ```\n where `normalized_axes` is `[axis, ..., rank of X - 1]`.\n The variables `Var` and `StdDev` stand for variance and\n standard deviation, respectively. The second output is\n `Mean` and the last one is `InvStdDev`.\n Depending on `stash_type` attribute, the actual computation\n must happen in different floating-point precision.\n For example, if `stash_type` is 1, this operator casts\n all input variables to 32-bit float, perform the computation, and\n finally cast `Normalized` back to the original type of `X`.\n The second stage then scales and shifts the outcome of the\n first stage using\n ```\n NormalizedScaled = Mul(Normalized, Scale)\n Y = Add(NormalizedScaled, B)\n ```\n The second stage doesn't depends on `stash_type`.\n All equations are in [this syntax](https://github.com/onnx/onnx/blob/main/docs/Syntax.md).\n The same variable (i.e., input, output, and attribute) uses\n the same name in the equations above and this operator's definition.\n Let `d[i]` indicate the i-th dimension of `X`.\n If `X`'s shape is `[d[0], ..., d[axis-1], d[axis], ..., d[rank-1]]`,\n the shape of `Mean` and `InvStdDev` is `[d[0], ..., d[axis-1], 1, ..., 1]`.\n `Y` and `X` have the same shape. This operator supports unidirectional broadcasting\n (tensors `Scale` and `B` should be unidirectional broadcastable to tensor `X`);\n for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": -1, "description": "The first normalization dimension. If rank(X) is r, axis' allowed range is [-r, r). Negative value means counting dimensions from the back." }, { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "The epsilon value to use to avoid division by zero." }, { "name": "stash_type", "type": "int64", "required": false, "default": 1, "description": "Type of Mean and InvStdDev. This also specifies stage one's computation precision." } ], "inputs": [ { "name": "X", "type": "T", "description": "Tensor to be normalized." }, { "name": "Scale", "type": "T", "description": "Scale tensor." }, { "name": "B", "type": "T", "option": "optional", "description": "Bias tensor." } ], "min_input": 2, "max_input": 3, "outputs": [ { "name": "Y", "type": "T", "description": "Normalized tensor." }, { "name": "Mean", "type": "U", "option": "optional", "description": "Saved mean used during training to speed up gradient computation" }, { "name": "InvStdDev", "type": "U", "option": "optional", "description": "Saved inverse standard deviation used during training to speed up gradient computation." } ], "min_output": 1, "max_output": 3, "inputs_range": "2 - 3", "outputs_range": "1 - 3", "type_constraints": [ { "description": "Constrain input types and output Y type to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] }, { "description": "Type of Mean and InvStdDev tensors.", "type_param_str": "U", "allowed_type_strs": [ "tensor(float)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "d", "code": "X = np.random.randn(3, 4).astype(np.float32)\n\ndef case(axis: int) -> None:\n normalized_shape = calculate_normalized_shape(X.shape, axis)\n W = np.random.randn(*normalized_shape).astype(np.float32)\n B = np.random.randn(*normalized_shape).astype(np.float32)\n Y, mean, inv_std_dev = _layer_normalization(X, W, B, axis=axis)\n\n node = onnx.helper.make_node(\n \"LayerNormalization\",\n inputs=[\"X\", \"W\", \"B\"],\n outputs=[\"Y\", \"Mean\", \"InvStdDev\"],\n axis=axis,\n )\n\n if axis < 0:\n name = f\"test_layer_normalization_2d_axis_negative_{-axis}\"\n else:\n name = f\"test_layer_normalization_2d_axis{axis}\"\n\n expect(node, inputs=[X, W, B], outputs=[Y, mean, inv_std_dev], name=name)\n\nfor i in range(len(X.shape)):\n case(i)\n case(i - len(X.shape))" }, { "summary": "d_epsilon", "code": "epsilon = 1e-1\nX = np.random.randn(2, 3, 5).astype(np.float32)\n\ndef case(axis: int) -> None:\n normalized_shape = calculate_normalized_shape(X.shape, axis)\n W = np.random.randn(*normalized_shape).astype(np.float32)\n B = np.random.randn(*normalized_shape).astype(np.float32)\n Y, mean, inv_std_dev = _layer_normalization(X, W, B, axis, epsilon)\n node = onnx.helper.make_node(\n \"LayerNormalization\",\n inputs=[\"X\", \"W\", \"B\"],\n outputs=[\"Y\", \"Mean\", \"InvStdDev\"],\n axis=axis,\n epsilon=epsilon,\n )\n\n if axis < 0:\n name = f\"test_layer_normalization_3d_axis_negative_{-axis}_epsilon\"\n else:\n name = f\"test_layer_normalization_3d_axis{axis}_epsilon\"\n\n expect(node, inputs=[X, W, B], outputs=[Y, mean, inv_std_dev], name=name)\n\nfor i in range(len(X.shape)):\n case(i)\n case(i - len(X.shape))" }, { "summary": "default_axis", "code": "X = np.random.randn(2, 3, 4, 5).astype(np.float32)\n\n# Default axis in LayerNormalization is -1.\nnormalized_shape = calculate_normalized_shape(X.shape, -1)\nW = np.random.randn(*normalized_shape).astype(np.float32)\nB = np.random.randn(*normalized_shape).astype(np.float32)\n# Axis is default to -1 in the reference implementation.\nY, mean, inv_std_dev = _layer_normalization(X, W, B)\n\n# Not specifying axis attribute means -1.\nnode = onnx.helper.make_node(\n \"LayerNormalization\",\n inputs=[\"X\", \"W\", \"B\"],\n outputs=[\"Y\", \"Mean\", \"InvStdDev\"],\n)\n\nexpect(\n node,\n inputs=[X, W, B],\n outputs=[Y, mean, inv_std_dev],\n name=\"test_layer_normalization_default_axis\",\n)" }, { "summary": "layernormalization", "code": "X = np.random.randn(2, 3, 4, 5).astype(np.float32)\n\ndef case(axis: int) -> None:\n normalized_shape = calculate_normalized_shape(X.shape, axis)\n W = np.random.randn(*normalized_shape).astype(np.float32)\n B = np.random.randn(*normalized_shape).astype(np.float32)\n Y, mean, inv_std_dev = _layer_normalization(X, W, B, axis)\n\n node = onnx.helper.make_node(\n \"LayerNormalization\",\n inputs=[\"X\", \"W\", \"B\"],\n outputs=[\"Y\", \"Mean\", \"InvStdDev\"],\n axis=axis,\n )\n\n if axis < 0:\n name = f\"test_layer_normalization_4d_axis_negative_{-axis}\"\n else:\n name = f\"test_layer_normalization_4d_axis{axis}\"\n\n expect(node, inputs=[X, W, B], outputs=[Y, mean, inv_std_dev], name=name)\n\nfor i in range(len(X.shape)):\n case(i)\n case(i - len(X.shape))" } ], "category": "Normalization" }, { "name": "LeakyRelu", "module": "ai.onnx", "version": 1, "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\", inputs=[\"x\"], outputs=[\"y\"], 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], 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], 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], name=\"test_leakyrelu_default\")" } ], "category": "Activation" }, { "name": "LeakyRelu", "module": "ai.onnx", "version": 6, "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\", inputs=[\"x\"], outputs=[\"y\"], 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], 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], 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], name=\"test_leakyrelu_default\")" } ], "category": "Activation" }, { "name": "LeakyRelu", "module": "ai.onnx", "version": 16, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "leakyrelu", "code": "node = onnx.helper.make_node(\n \"LeakyRelu\", inputs=[\"x\"], outputs=[\"y\"], 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], 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], 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], name=\"test_leakyrelu_default\")" } ], "category": "Activation" }, { "name": "Less", "module": "ai.onnx", "version": 1, "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], name=\"test_less\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_int16\")\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)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint64\")" }, { "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], name=\"test_less_equal\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_int16\")\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)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint64\")" }, { "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], 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], name=\"test_less_equal_bcast\")" } ] }, { "name": "Less", "module": "ai.onnx", "version": 7, "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], name=\"test_less\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_int16\")\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)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint64\")" }, { "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], name=\"test_less_equal\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_int16\")\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)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint64\")" }, { "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], 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], name=\"test_less_equal_bcast\")" } ] }, { "name": "Less", "module": "ai.onnx", "version": 9, "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], name=\"test_less\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_int16\")\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)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint64\")" }, { "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], name=\"test_less_equal\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_int16\")\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)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint64\")" }, { "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], 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], name=\"test_less_equal_bcast\")" } ] }, { "name": "Less", "module": "ai.onnx", "version": 13, "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], name=\"test_less\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_int16\")\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)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.less(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_uint64\")" }, { "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], name=\"test_less_equal\")\n\nx = np.random.randn(3, 4, 5).astype(np.int8)\ny = np.random.randn(3, 4, 5).astype(np.int8)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_int8\")\n\nx = np.random.randn(3, 4, 5).astype(np.int16)\ny = np.random.randn(3, 4, 5).astype(np.int16)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_int16\")\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)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint8\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint16\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint32\")\n\nx = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = np.less_equal(x, y)\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_less_equal_uint64\")" }, { "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], 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], name=\"test_less_equal_bcast\")" } ] }, { "name": "LessOrEqual", "module": "ai.onnx", "version": 12, "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, "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": "LinearAttention", "module": "ai.onnx", "version": 27, "description": "Unified linear attention operator for autoregressive decoding (T=1) and prefill (T>1).\n\nThe query, key, value, and (where applicable) decay/beta inputs use 3D packed format\n[B, T, H*D], where heads are flattened into the last dimension; q_num_heads and\nkv_num_heads are always required and are used to unpack to 4D internally for computation.\nThe optional past_state and present_state are 4D with shape (B, H_kv, d_k, d_v).\n\nGroup-query attention (GQA) is supported: q_num_heads must be a positive multiple of\nkv_num_heads. When q_num_heads == kv_num_heads this reduces to multi-headed linear\nattention; when q_num_heads > kv_num_heads each KV head (and its recurrent state) is\nshared by `q_num_heads / kv_num_heads` query heads (multi-query attention is the\nspecial case kv_num_heads == 1).\n\nThe update_rule attribute selects the recurrence type:\n- \"linear\": S_t = S_{t-1} + k_t \u2297 v_t; o_t = scale * q_t^T S_t\n- \"gated\": S_t = exp(g_t) * S_{t-1} + k_t \u2297 v_t; o_t = scale * q_t^T S_t\n- \"delta\": S_t = S_{t-1} + \u03b2_t * k_t \u2297 (v_t - S_{t-1}^T k_t); o_t = scale * q_t^T S_t\n- \"gated_delta\": S_t = exp(g_t) * S_{t-1} + \u03b2_t * k_t \u2297 (v_t - exp(g_t) * S_{t-1}^T k_t); o_t = scale * q_t^T S_t\n\nwhere g_t is the decay (in log-space), \u03b2_t is the update rate, and \u2297 denotes outer product.\n\nSemantics: Equivalent to running the recurrent update sequentially for each token,\nbut may be implemented using chunk-parallel algorithms for GPU efficiency.\n\n", "attributes": [ { "name": "chunk_size", "type": "int64", "required": false, "default": 64, "description": "Chunk size for the chunk-parallel WY decomposition during prefill (T>1). Tuning hint; does not affect output correctness." }, { "name": "kv_num_heads", "type": "int64", "required": true, "description": "Number of key/value heads. Always required." }, { "name": "q_num_heads", "type": "int64", "required": true, "description": "Number of query heads. Always required." }, { "name": "scale", "type": "float32", "required": false, "description": "Output scaling factor. When 0.0 (default), derives d_k = query.shape[-1] / q_num_heads and uses 1/sqrt(d_k). Set explicitly to override." }, { "name": "update_rule", "type": "string", "required": false, "default": "gated_delta", "description": "The update rule for the linear attention recurrence. One of: 'linear', 'gated', 'delta', 'gated_delta'. Default is 'gated_delta'." } ], "inputs": [ { "name": "query", "type": "T", "description": "Query vectors with 3D packed shape (B, T, H_q * d_k). Heads are packed into the last dimension." }, { "name": "key", "type": "T", "description": "Key vectors with 3D packed shape (B, T, H_kv * d_k). Should be L2-normalized for delta/gated_delta modes." }, { "name": "value", "type": "T", "description": "Value vectors with 3D packed shape (B, T, H_kv * d_v)." }, { "name": "past_state", "type": "S", "option": "optional", "description": "Recurrent state from previous step with shape (B, H_kv, d_k, d_v). Always 4D. If not provided, defaults to zeros." }, { "name": "decay", "type": "T", "option": "optional", "description": "Exponential decay gate in log-space. 3D packed shape: (B, T, H_kv * d_k) for per-key-dimension decay (GLA/RWKV-6), or (B, T, H_kv) for per-head scalar decay (DeltaNet/RetNet). Required for 'gated' and 'gated_delta' modes." }, { "name": "beta", "type": "T", "option": "optional", "description": "Update rate (sigmoid output). 3D packed shape: (B, T, H_kv) or (B, T, 1). Required for 'delta' and 'gated_delta' modes." } ], "min_input": 3, "max_input": 6, "outputs": [ { "name": "output", "type": "T", "description": "Attention output with 3D packed shape (B, T, H_q * d_v)." }, { "name": "present_state", "type": "S", "description": "Updated recurrent state with shape (B, H_kv, d_k, d_v). Always 4D." } ], "min_output": 2, "max_output": 2, "inputs_range": "3 - 6", "type_constraints": [ { "description": "Constrain activation input and output types to float16, bfloat16, or float32 tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(bfloat16)", "tensor(float)" ] }, { "description": "Constrain state types to float16, bfloat16, or float32 tensors. Should be float32 or the same as T for numerical stability on long sequences.", "type_param_str": "S", "allowed_type_strs": [ "tensor(float16)", "tensor(bfloat16)", "tensor(float)" ] } ], "examples": [ { "summary": "decode_step", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"past_state\", \"decay\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 1, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float32), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\npast_state = np.random.randn(b, h_kv, d_k, d_v).astype(np.float32) * 0.1\ndecay = -np.abs(np.random.randn(b, t, h_kv * d_k)).astype(np.float32) * 0.1\nbeta = np.random.rand(b, t, h_kv).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n past_state=past_state,\n decay=decay,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n)\nexpect(\n node,\n inputs=[query, key, value, past_state, decay, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_decode_step\",\n opset_imports=_OPSET,\n)" }, { "summary": "delta", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"\", \"\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n update_rule=\"delta\",\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float32), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\nbeta = np.random.rand(b, t, h_kv).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n update_rule=\"delta\",\n)\nexpect(\n node,\n inputs=[query, key, value, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_delta\",\n opset_imports=_OPSET,\n)" }, { "summary": "explicit_scale", "code": "scale = 0.25\nnode = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"\", \"decay\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n q_num_heads=4,\n kv_num_heads=4,\n scale=scale,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float32), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\ndecay = -np.abs(np.random.randn(b, t, h_kv * d_k)).astype(np.float32) * 0.1\nbeta = np.random.rand(b, t, h_kv).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n decay=decay,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n scale=scale,\n)\nexpect(\n node,\n inputs=[query, key, value, decay, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_explicit_scale\",\n opset_imports=_OPSET,\n)" }, { "summary": "fp16", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"\", \"decay\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n q_num_heads=8,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 8, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float16)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float16), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float16)\ndecay = (-np.abs(np.random.randn(b, t, h_kv * d_k)) * 0.1).astype(np.float16)\nbeta = np.random.rand(b, t, h_kv).astype(np.float16)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n decay=decay,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n)\nexpect(\n node,\n inputs=[query, key, value, decay, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_fp16\",\n opset_imports=_OPSET,\n)" }, { "summary": "gated", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"\", \"decay\"],\n outputs=[\"output\", \"present_state\"],\n update_rule=\"gated\",\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = np.random.randn(b, t, h_kv * d_k).astype(np.float32)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\n# Per-key-dim decay in log-space (negative -> decay).\ndecay = -np.abs(np.random.randn(b, t, h_kv * d_k)).astype(np.float32) * 0.1\n\noutput, present_state = _compute(\n query,\n key,\n value,\n decay=decay,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n update_rule=\"gated\",\n)\nexpect(\n node,\n inputs=[query, key, value, decay],\n outputs=[output, present_state],\n name=\"test_linear_attention_gated\",\n opset_imports=_OPSET,\n)" }, { "summary": "gated_delta", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"\", \"decay\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float32), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\ndecay = -np.abs(np.random.randn(b, t, h_kv * d_k)).astype(np.float32) * 0.1\nbeta = np.random.rand(b, t, h_kv).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n decay=decay,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n)\nexpect(\n node,\n inputs=[query, key, value, decay, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_gated_delta\",\n opset_imports=_OPSET,\n)" }, { "summary": "gated_delta_beta_scalar", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"\", \"decay\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float32), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\ndecay = -np.abs(np.random.randn(b, t, h_kv * d_k)).astype(np.float32) * 0.1\nbeta = np.random.rand(b, t, 1).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n decay=decay,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n)\nexpect(\n node,\n inputs=[query, key, value, decay, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_gated_delta_beta_scalar\",\n opset_imports=_OPSET,\n)" }, { "summary": "gated_delta_gqa", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"\", \"decay\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n q_num_heads=8,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 8, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float32), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\ndecay = -np.abs(np.random.randn(b, t, h_kv * d_k)).astype(np.float32) * 0.1\nbeta = np.random.rand(b, t, h_kv).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n decay=decay,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n)\nexpect(\n node,\n inputs=[query, key, value, decay, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_gated_delta_gqa\",\n opset_imports=_OPSET,\n)" }, { "summary": "gated_delta_mqa", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"\", \"decay\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n q_num_heads=8,\n kv_num_heads=1,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 8, 1, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float32), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\ndecay = -np.abs(np.random.randn(b, t, h_kv * d_k)).astype(np.float32) * 0.1\nbeta = np.random.rand(b, t, h_kv).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n decay=decay,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n)\nexpect(\n node,\n inputs=[query, key, value, decay, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_gated_delta_mqa\",\n opset_imports=_OPSET,\n)" }, { "summary": "gated_per_head_decay", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"\", \"decay\"],\n outputs=[\"output\", \"present_state\"],\n update_rule=\"gated\",\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = np.random.randn(b, t, h_kv * d_k).astype(np.float32)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\n# Per-head scalar decay.\ndecay = -np.abs(np.random.randn(b, t, h_kv)).astype(np.float32) * 0.1\n\noutput, present_state = _compute(\n query,\n key,\n value,\n decay=decay,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n update_rule=\"gated\",\n)\nexpect(\n node,\n inputs=[query, key, value, decay],\n outputs=[output, present_state],\n name=\"test_linear_attention_gated_per_head_decay\",\n opset_imports=_OPSET,\n)" }, { "summary": "linear", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\"],\n outputs=[\"output\", \"present_state\"],\n update_rule=\"linear\",\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = np.random.randn(b, t, h_kv * d_k).astype(np.float32)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n update_rule=\"linear\",\n)\nexpect(\n node,\n inputs=[query, key, value],\n outputs=[output, present_state],\n name=\"test_linear_attention_linear\",\n opset_imports=_OPSET,\n)" }, { "summary": "linear_t1_no_past", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\"],\n outputs=[\"output\", \"present_state\"],\n update_rule=\"linear\",\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 1, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = np.random.randn(b, t, h_kv * d_k).astype(np.float32)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n update_rule=\"linear\",\n)\nexpect(\n node,\n inputs=[query, key, value],\n outputs=[output, present_state],\n name=\"test_linear_attention_linear_t1_no_past\",\n opset_imports=_OPSET,\n)" }, { "summary": "no_past_explicit_zeros", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"past_state\", \"decay\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float32), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\npast_state = np.zeros((b, h_kv, d_k, d_v), dtype=np.float32)\ndecay = -np.abs(np.random.randn(b, t, h_kv * d_k)).astype(np.float32) * 0.1\nbeta = np.random.rand(b, t, h_kv).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n past_state=past_state,\n decay=decay,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n)\nexpect(\n node,\n inputs=[query, key, value, past_state, decay, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_no_past_explicit_zeros\",\n opset_imports=_OPSET,\n)" }, { "summary": "prefill_with_past", "code": "node = onnx.helper.make_node(\n \"LinearAttention\",\n inputs=[\"query\", \"key\", \"value\", \"past_state\", \"decay\", \"beta\"],\n outputs=[\"output\", \"present_state\"],\n q_num_heads=4,\n kv_num_heads=4,\n)\nb, t, h_q, h_kv, d_k, d_v = 2, 4, 4, 4, 8, 8\nquery = np.random.randn(b, t, h_q * d_k).astype(np.float32)\nkey = _l2_normalize(np.random.randn(b, t, h_kv * d_k).astype(np.float32), h_kv)\nvalue = np.random.randn(b, t, h_kv * d_v).astype(np.float32)\npast_state = np.random.randn(b, h_kv, d_k, d_v).astype(np.float32) * 0.1\ndecay = -np.abs(np.random.randn(b, t, h_kv * d_k)).astype(np.float32) * 0.1\nbeta = np.random.rand(b, t, h_kv).astype(np.float32)\n\noutput, present_state = _compute(\n query,\n key,\n value,\n past_state=past_state,\n decay=decay,\n beta=beta,\n q_num_heads=h_q,\n kv_num_heads=h_kv,\n)\nexpect(\n node,\n inputs=[query, key, value, past_state, decay, beta],\n outputs=[output, present_state],\n name=\"test_linear_attention_prefill_with_past\",\n opset_imports=_OPSET,\n)" } ] }, { "name": "LinearAttention", "module": "com.microsoft", "version": 1, "description": "Unified linear attention operator for autoregressive decoding (T=1) and prefill (T>1).\n\nAll inputs use 3D packed format [B, T, H*D]; q_num_heads and kv_num_heads are always\nrequired. The op internally unpacks to 4D for computation.\n\nThe update_rule attribute selects the recurrence type:\n- \"linear\": S_t = S_{t-1} + k_t \u2297 v_t; o_t = scale * q_t^T S_t\n- \"gated\": S_t = exp(g_t) * S_{t-1} + k_t \u2297 v_t; o_t = scale * q_t^T S_t\n- \"delta\": S_t = S_{t-1} + \u03b2_t * k_t \u2297 (v_t - S_{t-1}^T k_t); o_t = scale * q_t^T S_t\n- \"gated_delta\": S_t = exp(g_t) * S_{t-1} + \u03b2_t * k_t \u2297 (v_t - exp(g_t) * S_{t-1}^T k_t); o_t = scale * q_t^T S_t\n\nwhere g_t is the decay (in log-space), \u03b2_t is the update rate, and \u2297 denotes outer product.\n\nSemantics: Equivalent to running the recurrent update sequentially for each token,\nbut may be implemented using chunk-parallel algorithms for GPU efficiency.\n", "attributes": [ { "name": "chunk_size", "type": "int64", "required": false, "default": 64, "description": "Chunk size for the chunk-parallel WY decomposition during prefill (T>1). Tuning hint; does not affect output correctness." }, { "name": "kv_num_heads", "type": "int64", "required": true, "description": "Number of key/value heads. Always required." }, { "name": "q_num_heads", "type": "int64", "required": true, "description": "Number of query heads. Always required." }, { "name": "scale", "type": "float32", "required": false, "description": "Output scaling factor. When 0.0 (default), derives d_k = query.shape[-1] / q_num_heads and uses 1/sqrt(d_k). Set explicitly to override." }, { "name": "update_rule", "type": "string", "required": false, "default": "gated_delta", "description": "The update rule for the linear attention recurrence. One of: 'linear', 'gated', 'delta', 'gated_delta'. Default is 'gated_delta'." } ], "inputs": [ { "name": "query", "type": "T", "description": "Query vectors with 3D packed shape (B, T, H_q * d_k). Heads are packed into the last dimension." }, { "name": "key", "type": "T", "description": "Key vectors with 3D packed shape (B, T, H_kv * d_k). Should be L2-normalized for delta/gated_delta modes." }, { "name": "value", "type": "T", "description": "Value vectors with 3D packed shape (B, T, H_kv * d_v)." }, { "name": "past_state", "type": "S", "option": "optional", "description": "Recurrent state from previous step with shape (B, H_kv, d_k, d_v). Always 4D. If not provided, defaults to zeros." }, { "name": "decay", "type": "T", "option": "optional", "description": "Exponential decay gate in log-space. 3D packed shape: (B, T, H_kv * d_k) for per-key-dimension decay (GLA/RWKV-6), or (B, T, H_kv) for per-head scalar decay (DeltaNet/RetNet). Required for 'gated' and 'gated_delta' modes." }, { "name": "beta", "type": "T", "option": "optional", "description": "Update rate (sigmoid output). 3D packed shape: (B, T, H_kv) or (B, T, 1). Required for 'delta' and 'gated_delta' modes." } ], "min_input": 3, "max_input": 6, "outputs": [ { "name": "output", "type": "T", "description": "Attention output with 3D packed shape (B, T, H_q * d_v)." }, { "name": "present_state", "type": "S", "description": "Updated recurrent state with shape (B, H_kv, d_k, d_v). Always 4D." } ], "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(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain state types to float tensors.", "type_param_str": "S", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ] }, { "name": "LinearClassifier", "module": "ai.onnx.ml", "version": 1, "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 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, "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, "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], 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], name=\"test_log\")" } ] }, { "name": "Log", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_log\")" } ] }, { "name": "Log", "module": "ai.onnx", "version": 13, "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], 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], name=\"test_log\")" } ] }, { "name": "LogSoftmax", "module": "ai.onnx", "version": 1, "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], name=\"test_logsoftmax_example_1\")" }, { "summary": "logsoftmax_axis", "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]).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], 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], 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], 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], 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], 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], name=\"test_logsoftmax_default_axis\")" } ], "category": "Activation" }, { "name": "LogSoftmax", "module": "ai.onnx", "version": 11, "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], name=\"test_logsoftmax_example_1\")" }, { "summary": "logsoftmax_axis", "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]).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], 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], 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], 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], 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], 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], name=\"test_logsoftmax_default_axis\")" } ], "category": "Activation" }, { "name": "LogSoftmax", "module": "ai.onnx", "version": 13, "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], name=\"test_logsoftmax_example_1\")" }, { "summary": "logsoftmax_axis", "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]).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], 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], 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], 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], 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], 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], name=\"test_logsoftmax_default_axis\")" } ], "category": "Activation" }, { "name": "LongformerAttention", "module": "com.microsoft", "version": 1, "description": "Longformer Self Attention with a local context and a global context. Tokens attend locally: Each token\nattends to its W previous tokens and W succeeding tokens with W being the window length. A selected few tokens\nattend globally to all other tokens.\n\nThe attention mask is of shape (batch_size, sequence_length), where sequence_length is a multiple of 2W after padding.\nMask value < 0 (like -10000.0) means the token is masked, 0 otherwise.\n\nGlobal attention flags have value 1 for the tokens attend globally and 0 otherwise.\n", "attributes": [ { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "window", "type": "int64", "required": true, "description": "One sided attention windows length W, or half of total window length" } ], "inputs": [ { "name": "input", "type": "T", "description": "3D input tensor with shape (batch_size, sequence_length, hidden_size), hidden_size = num_heads * head_size" }, { "name": "weight", "type": "T", "description": "2D input tensor with shape (hidden_size, 3 * hidden_size)" }, { "name": "bias", "type": "T", "description": "1D input tensor with shape (3 * hidden_size)" }, { "name": "mask", "type": "T", "description": "Attention mask with shape (batch_size, sequence_length)" }, { "name": "global_weight", "type": "T", "description": "2D input tensor with shape (hidden_size, 3 * hidden_size)" }, { "name": "global_bias", "type": "T", "description": "1D input tensor with shape (3 * hidden_size)" }, { "name": "global", "type": "G", "description": "Global attention flags with shape (batch_size, sequence_length)" } ], "min_input": 7, "max_input": 7, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, hidden_size)" } ], "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(float)", "tensor(float16)" ] }, { "description": "Constrain to integer types", "type_param_str": "G", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "Loop", "module": "ai.onnx", "version": 1, "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\nOperator 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 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(\n \"scan_out\", onnx.TensorProto.FLOAT, [1]\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"iter_count\"], outputs=[\"slice_start\"], axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\"], outputs=[\"slice_end\"], axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ny_add_node = onnx.helper.make_node(\n \"Add\", inputs=[\"y_in\", \"slice_out\"], outputs=[\"y_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nscan_identity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"y_out\"], outputs=[\"scan_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n i_add_node,\n start_unsqueeze_node,\n end_unsqueeze_node,\n slice_node,\n y_add_node,\n scan_identity_node,\n ],\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(\n node,\n inputs=[trip_count, cond, y],\n outputs=[res_y, res_scan],\n name=\"test_loop11\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n \"seq_in\", onnx.TensorProto.FLOAT, None\n)\nseq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_out\", onnx.TensorProto.FLOAT, None\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"seq_in\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, seq_empty],\n outputs=[seq_res],\n name=\"test_loop13_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\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, [])\n ),\n ],\n)" }, { "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(\n \"seq_out\", onnx.TensorProto.FLOAT, []\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"opt_seq_in\"], outputs=[\"optional_has_elem\"]\n)\n\noptional_is_none = onnx.helper.make_node(\n \"Not\", inputs=[\"optional_has_elem\"], outputs=[\"optional_is_none\"]\n)\n\noptional_get_elem = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"opt_seq_in\"], 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\", data_type=onnx.TensorProto.FLOAT, dims=(), vals=[0]\n ),\n)\n\nseq_const_in = onnx.helper.make_node(\n \"SequenceConstruct\", inputs=[\"constant_in\"], outputs=[\"init_seq_in\"]\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"init_seq_in\", onnx.TensorProto.FLOAT, []\n)\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in], \"then_body\", [], [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_in\", onnx.TensorProto.FLOAT, []\n)\nelse_body = onnx.helper.make_graph(\n [optional_get_elem], \"else_body\", [], [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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"sequence\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n optional_has_elem_node,\n optional_is_none,\n if_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, opt_seq_in],\n outputs=[seq_res],\n name=\"test_loop16_seq_none\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp,\n ],\n)" } ] }, { "name": "Loop", "module": "ai.onnx", "version": 11, "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\nOperator 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 modeled 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(\n \"scan_out\", onnx.TensorProto.FLOAT, [1]\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"iter_count\"], outputs=[\"slice_start\"], axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\"], outputs=[\"slice_end\"], axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ny_add_node = onnx.helper.make_node(\n \"Add\", inputs=[\"y_in\", \"slice_out\"], outputs=[\"y_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nscan_identity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"y_out\"], outputs=[\"scan_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n i_add_node,\n start_unsqueeze_node,\n end_unsqueeze_node,\n slice_node,\n y_add_node,\n scan_identity_node,\n ],\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(\n node,\n inputs=[trip_count, cond, y],\n outputs=[res_y, res_scan],\n name=\"test_loop11\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n \"seq_in\", onnx.TensorProto.FLOAT, None\n)\nseq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_out\", onnx.TensorProto.FLOAT, None\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"seq_in\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, seq_empty],\n outputs=[seq_res],\n name=\"test_loop13_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\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, [])\n ),\n ],\n)" }, { "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(\n \"seq_out\", onnx.TensorProto.FLOAT, []\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"opt_seq_in\"], outputs=[\"optional_has_elem\"]\n)\n\noptional_is_none = onnx.helper.make_node(\n \"Not\", inputs=[\"optional_has_elem\"], outputs=[\"optional_is_none\"]\n)\n\noptional_get_elem = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"opt_seq_in\"], 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\", data_type=onnx.TensorProto.FLOAT, dims=(), vals=[0]\n ),\n)\n\nseq_const_in = onnx.helper.make_node(\n \"SequenceConstruct\", inputs=[\"constant_in\"], outputs=[\"init_seq_in\"]\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"init_seq_in\", onnx.TensorProto.FLOAT, []\n)\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in], \"then_body\", [], [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_in\", onnx.TensorProto.FLOAT, []\n)\nelse_body = onnx.helper.make_graph(\n [optional_get_elem], \"else_body\", [], [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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"sequence\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n optional_has_elem_node,\n optional_is_none,\n if_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, opt_seq_in],\n outputs=[seq_res],\n name=\"test_loop16_seq_none\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp,\n ],\n)" } ] }, { "name": "Loop", "module": "ai.onnx", "version": 13, "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\nOperator 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 modeled 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(\n \"scan_out\", onnx.TensorProto.FLOAT, [1]\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"iter_count\"], outputs=[\"slice_start\"], axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\"], outputs=[\"slice_end\"], axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ny_add_node = onnx.helper.make_node(\n \"Add\", inputs=[\"y_in\", \"slice_out\"], outputs=[\"y_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nscan_identity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"y_out\"], outputs=[\"scan_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n i_add_node,\n start_unsqueeze_node,\n end_unsqueeze_node,\n slice_node,\n y_add_node,\n scan_identity_node,\n ],\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(\n node,\n inputs=[trip_count, cond, y],\n outputs=[res_y, res_scan],\n name=\"test_loop11\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n \"seq_in\", onnx.TensorProto.FLOAT, None\n)\nseq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_out\", onnx.TensorProto.FLOAT, None\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"seq_in\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, seq_empty],\n outputs=[seq_res],\n name=\"test_loop13_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\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, [])\n ),\n ],\n)" }, { "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(\n \"seq_out\", onnx.TensorProto.FLOAT, []\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"opt_seq_in\"], outputs=[\"optional_has_elem\"]\n)\n\noptional_is_none = onnx.helper.make_node(\n \"Not\", inputs=[\"optional_has_elem\"], outputs=[\"optional_is_none\"]\n)\n\noptional_get_elem = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"opt_seq_in\"], 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\", data_type=onnx.TensorProto.FLOAT, dims=(), vals=[0]\n ),\n)\n\nseq_const_in = onnx.helper.make_node(\n \"SequenceConstruct\", inputs=[\"constant_in\"], outputs=[\"init_seq_in\"]\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"init_seq_in\", onnx.TensorProto.FLOAT, []\n)\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in], \"then_body\", [], [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_in\", onnx.TensorProto.FLOAT, []\n)\nelse_body = onnx.helper.make_graph(\n [optional_get_elem], \"else_body\", [], [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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"sequence\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n optional_has_elem_node,\n optional_is_none,\n if_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, opt_seq_in],\n outputs=[seq_res],\n name=\"test_loop16_seq_none\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp,\n ],\n)" } ] }, { "name": "Loop", "module": "ai.onnx", "version": 16, "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\nOperator 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 modeled 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 up to IRv4.", "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(\n \"scan_out\", onnx.TensorProto.FLOAT, [1]\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"iter_count\"], outputs=[\"slice_start\"], axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\"], outputs=[\"slice_end\"], axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ny_add_node = onnx.helper.make_node(\n \"Add\", inputs=[\"y_in\", \"slice_out\"], outputs=[\"y_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nscan_identity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"y_out\"], outputs=[\"scan_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n i_add_node,\n start_unsqueeze_node,\n end_unsqueeze_node,\n slice_node,\n y_add_node,\n scan_identity_node,\n ],\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(\n node,\n inputs=[trip_count, cond, y],\n outputs=[res_y, res_scan],\n name=\"test_loop11\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n \"seq_in\", onnx.TensorProto.FLOAT, None\n)\nseq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_out\", onnx.TensorProto.FLOAT, None\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"seq_in\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, seq_empty],\n outputs=[seq_res],\n name=\"test_loop13_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\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, [])\n ),\n ],\n)" }, { "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(\n \"seq_out\", onnx.TensorProto.FLOAT, []\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"opt_seq_in\"], outputs=[\"optional_has_elem\"]\n)\n\noptional_is_none = onnx.helper.make_node(\n \"Not\", inputs=[\"optional_has_elem\"], outputs=[\"optional_is_none\"]\n)\n\noptional_get_elem = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"opt_seq_in\"], 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\", data_type=onnx.TensorProto.FLOAT, dims=(), vals=[0]\n ),\n)\n\nseq_const_in = onnx.helper.make_node(\n \"SequenceConstruct\", inputs=[\"constant_in\"], outputs=[\"init_seq_in\"]\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"init_seq_in\", onnx.TensorProto.FLOAT, []\n)\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in], \"then_body\", [], [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_in\", onnx.TensorProto.FLOAT, []\n)\nelse_body = onnx.helper.make_graph(\n [optional_get_elem], \"else_body\", [], [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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"sequence\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n optional_has_elem_node,\n optional_is_none,\n if_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, opt_seq_in],\n outputs=[seq_res],\n name=\"test_loop16_seq_none\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp,\n ],\n)" } ] }, { "name": "Loop", "module": "ai.onnx", "version": 19, "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\nOperator 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 modeled 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 up to IRv9.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))" ] }, { "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(\n \"scan_out\", onnx.TensorProto.FLOAT, [1]\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"iter_count\"], outputs=[\"slice_start\"], axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\"], outputs=[\"slice_end\"], axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ny_add_node = onnx.helper.make_node(\n \"Add\", inputs=[\"y_in\", \"slice_out\"], outputs=[\"y_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nscan_identity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"y_out\"], outputs=[\"scan_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n i_add_node,\n start_unsqueeze_node,\n end_unsqueeze_node,\n slice_node,\n y_add_node,\n scan_identity_node,\n ],\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(\n node,\n inputs=[trip_count, cond, y],\n outputs=[res_y, res_scan],\n name=\"test_loop11\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n \"seq_in\", onnx.TensorProto.FLOAT, None\n)\nseq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_out\", onnx.TensorProto.FLOAT, None\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"seq_in\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, seq_empty],\n outputs=[seq_res],\n name=\"test_loop13_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\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, [])\n ),\n ],\n)" }, { "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(\n \"seq_out\", onnx.TensorProto.FLOAT, []\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"opt_seq_in\"], outputs=[\"optional_has_elem\"]\n)\n\noptional_is_none = onnx.helper.make_node(\n \"Not\", inputs=[\"optional_has_elem\"], outputs=[\"optional_is_none\"]\n)\n\noptional_get_elem = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"opt_seq_in\"], 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\", data_type=onnx.TensorProto.FLOAT, dims=(), vals=[0]\n ),\n)\n\nseq_const_in = onnx.helper.make_node(\n \"SequenceConstruct\", inputs=[\"constant_in\"], outputs=[\"init_seq_in\"]\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"init_seq_in\", onnx.TensorProto.FLOAT, []\n)\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in], \"then_body\", [], [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_in\", onnx.TensorProto.FLOAT, []\n)\nelse_body = onnx.helper.make_graph(\n [optional_get_elem], \"else_body\", [], [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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"sequence\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n optional_has_elem_node,\n optional_is_none,\n if_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, opt_seq_in],\n outputs=[seq_res],\n name=\"test_loop16_seq_none\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp,\n ],\n)" } ] }, { "name": "Loop", "module": "ai.onnx", "version": 21, "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\nOperator 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 modeled 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 up to IRv10.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "seq(tensor(uint4))", "seq(tensor(int4))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))", "optional(tensor(uint4))", "optional(tensor(int4))" ] }, { "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(\n \"scan_out\", onnx.TensorProto.FLOAT, [1]\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"iter_count\"], outputs=[\"slice_start\"], axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\"], outputs=[\"slice_end\"], axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ny_add_node = onnx.helper.make_node(\n \"Add\", inputs=[\"y_in\", \"slice_out\"], outputs=[\"y_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nscan_identity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"y_out\"], outputs=[\"scan_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n i_add_node,\n start_unsqueeze_node,\n end_unsqueeze_node,\n slice_node,\n y_add_node,\n scan_identity_node,\n ],\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(\n node,\n inputs=[trip_count, cond, y],\n outputs=[res_y, res_scan],\n name=\"test_loop11\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n \"seq_in\", onnx.TensorProto.FLOAT, None\n)\nseq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_out\", onnx.TensorProto.FLOAT, None\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"seq_in\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, seq_empty],\n outputs=[seq_res],\n name=\"test_loop13_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\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, [])\n ),\n ],\n)" }, { "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(\n \"seq_out\", onnx.TensorProto.FLOAT, []\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"opt_seq_in\"], outputs=[\"optional_has_elem\"]\n)\n\noptional_is_none = onnx.helper.make_node(\n \"Not\", inputs=[\"optional_has_elem\"], outputs=[\"optional_is_none\"]\n)\n\noptional_get_elem = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"opt_seq_in\"], 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\", data_type=onnx.TensorProto.FLOAT, dims=(), vals=[0]\n ),\n)\n\nseq_const_in = onnx.helper.make_node(\n \"SequenceConstruct\", inputs=[\"constant_in\"], outputs=[\"init_seq_in\"]\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"init_seq_in\", onnx.TensorProto.FLOAT, []\n)\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in], \"then_body\", [], [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_in\", onnx.TensorProto.FLOAT, []\n)\nelse_body = onnx.helper.make_graph(\n [optional_get_elem], \"else_body\", [], [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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"sequence\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n optional_has_elem_node,\n optional_is_none,\n if_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, opt_seq_in],\n outputs=[seq_res],\n name=\"test_loop16_seq_none\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp,\n ],\n)" } ] }, { "name": "Loop", "module": "ai.onnx", "version": 23, "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\nOperator 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 modeled 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 up to IRv11.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "seq(tensor(uint4))", "seq(tensor(int4))", "seq(tensor(float4e2m1))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))", "optional(tensor(uint4))", "optional(tensor(int4))", "optional(tensor(float4e2m1))" ] }, { "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(\n \"scan_out\", onnx.TensorProto.FLOAT, [1]\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"iter_count\"], outputs=[\"slice_start\"], axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\"], outputs=[\"slice_end\"], axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ny_add_node = onnx.helper.make_node(\n \"Add\", inputs=[\"y_in\", \"slice_out\"], outputs=[\"y_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nscan_identity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"y_out\"], outputs=[\"scan_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n i_add_node,\n start_unsqueeze_node,\n end_unsqueeze_node,\n slice_node,\n y_add_node,\n scan_identity_node,\n ],\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(\n node,\n inputs=[trip_count, cond, y],\n outputs=[res_y, res_scan],\n name=\"test_loop11\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n \"seq_in\", onnx.TensorProto.FLOAT, None\n)\nseq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_out\", onnx.TensorProto.FLOAT, None\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"seq_in\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, seq_empty],\n outputs=[seq_res],\n name=\"test_loop13_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\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, [])\n ),\n ],\n)" }, { "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(\n \"seq_out\", onnx.TensorProto.FLOAT, []\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"opt_seq_in\"], outputs=[\"optional_has_elem\"]\n)\n\noptional_is_none = onnx.helper.make_node(\n \"Not\", inputs=[\"optional_has_elem\"], outputs=[\"optional_is_none\"]\n)\n\noptional_get_elem = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"opt_seq_in\"], 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\", data_type=onnx.TensorProto.FLOAT, dims=(), vals=[0]\n ),\n)\n\nseq_const_in = onnx.helper.make_node(\n \"SequenceConstruct\", inputs=[\"constant_in\"], outputs=[\"init_seq_in\"]\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"init_seq_in\", onnx.TensorProto.FLOAT, []\n)\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in], \"then_body\", [], [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_in\", onnx.TensorProto.FLOAT, []\n)\nelse_body = onnx.helper.make_graph(\n [optional_get_elem], \"else_body\", [], [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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"sequence\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n optional_has_elem_node,\n optional_is_none,\n if_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, opt_seq_in],\n outputs=[seq_res],\n name=\"test_loop16_seq_none\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp,\n ],\n)" } ] }, { "name": "Loop", "module": "ai.onnx", "version": 24, "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\nOperator 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 modeled 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 up to IRv11.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "seq(tensor(uint4))", "seq(tensor(int4))", "seq(tensor(float4e2m1))", "seq(tensor(float8e8m0))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))", "optional(tensor(uint4))", "optional(tensor(int4))", "optional(tensor(float4e2m1))", "optional(tensor(float8e8m0))" ] }, { "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(\n \"scan_out\", onnx.TensorProto.FLOAT, [1]\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"iter_count\"], outputs=[\"slice_start\"], axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\"], outputs=[\"slice_end\"], axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ny_add_node = onnx.helper.make_node(\n \"Add\", inputs=[\"y_in\", \"slice_out\"], outputs=[\"y_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nscan_identity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"y_out\"], outputs=[\"scan_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n i_add_node,\n start_unsqueeze_node,\n end_unsqueeze_node,\n slice_node,\n y_add_node,\n scan_identity_node,\n ],\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(\n node,\n inputs=[trip_count, cond, y],\n outputs=[res_y, res_scan],\n name=\"test_loop11\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n \"seq_in\", onnx.TensorProto.FLOAT, None\n)\nseq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_out\", onnx.TensorProto.FLOAT, None\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"seq_in\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, seq_empty],\n outputs=[seq_res],\n name=\"test_loop13_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\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, [])\n ),\n ],\n)" }, { "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(\n \"seq_out\", onnx.TensorProto.FLOAT, []\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"opt_seq_in\"], outputs=[\"optional_has_elem\"]\n)\n\noptional_is_none = onnx.helper.make_node(\n \"Not\", inputs=[\"optional_has_elem\"], outputs=[\"optional_is_none\"]\n)\n\noptional_get_elem = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"opt_seq_in\"], 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\", data_type=onnx.TensorProto.FLOAT, dims=(), vals=[0]\n ),\n)\n\nseq_const_in = onnx.helper.make_node(\n \"SequenceConstruct\", inputs=[\"constant_in\"], outputs=[\"init_seq_in\"]\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"init_seq_in\", onnx.TensorProto.FLOAT, []\n)\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in], \"then_body\", [], [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_in\", onnx.TensorProto.FLOAT, []\n)\nelse_body = onnx.helper.make_graph(\n [optional_get_elem], \"else_body\", [], [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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"sequence\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n optional_has_elem_node,\n optional_is_none,\n if_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, opt_seq_in],\n outputs=[seq_res],\n name=\"test_loop16_seq_none\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp,\n ],\n)" } ] }, { "name": "Loop", "module": "ai.onnx", "version": 25, "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\nOperator 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 modeled 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 up to IRv13.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)", "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))", "seq(tensor(float8e4m3fn))", "seq(tensor(float8e4m3fnuz))", "seq(tensor(float8e5m2))", "seq(tensor(float8e5m2fnuz))", "seq(tensor(uint4))", "seq(tensor(int4))", "seq(tensor(float4e2m1))", "seq(tensor(float8e8m0))", "seq(tensor(uint2))", "seq(tensor(int2))", "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))", "optional(tensor(float8e4m3fn))", "optional(tensor(float8e4m3fnuz))", "optional(tensor(float8e5m2))", "optional(tensor(float8e5m2fnuz))", "optional(tensor(uint4))", "optional(tensor(int4))", "optional(tensor(float4e2m1))", "optional(tensor(float8e8m0))", "optional(tensor(uint2))", "optional(tensor(int2))" ] }, { "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(\n \"scan_out\", onnx.TensorProto.FLOAT, [1]\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nstart_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"iter_count\"], outputs=[\"slice_start\"], axes=[0]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\"], outputs=[\"slice_end\"], axes=[0]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ny_add_node = onnx.helper.make_node(\n \"Add\", inputs=[\"y_in\", \"slice_out\"], outputs=[\"y_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nscan_identity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"y_out\"], outputs=[\"scan_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n i_add_node,\n start_unsqueeze_node,\n end_unsqueeze_node,\n slice_node,\n y_add_node,\n scan_identity_node,\n ],\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(\n node,\n inputs=[trip_count, cond, y],\n outputs=[res_y, res_scan],\n name=\"test_loop11\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 11)],\n)" }, { "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(\n \"seq_in\", onnx.TensorProto.FLOAT, None\n)\nseq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_out\", onnx.TensorProto.FLOAT, None\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"seq_in\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, seq_empty],\n outputs=[seq_res],\n name=\"test_loop13_seq\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\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, [])\n ),\n ],\n)" }, { "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(\n \"seq_out\", onnx.TensorProto.FLOAT, []\n)\ncond_in = onnx.helper.make_tensor_value_info(\n \"cond_in\", onnx.TensorProto.BOOL, []\n)\ncond_out = onnx.helper.make_tensor_value_info(\n \"cond_out\", onnx.TensorProto.BOOL, []\n)\niter_count = onnx.helper.make_tensor_value_info(\n \"iter_count\", onnx.TensorProto.INT64, []\n)\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\", inputs=[\"opt_seq_in\"], outputs=[\"optional_has_elem\"]\n)\n\noptional_is_none = onnx.helper.make_node(\n \"Not\", inputs=[\"optional_has_elem\"], outputs=[\"optional_is_none\"]\n)\n\noptional_get_elem = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"opt_seq_in\"], 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\", data_type=onnx.TensorProto.FLOAT, dims=(), vals=[0]\n ),\n)\n\nseq_const_in = onnx.helper.make_node(\n \"SequenceConstruct\", inputs=[\"constant_in\"], outputs=[\"init_seq_in\"]\n)\n\nthen_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"init_seq_in\", onnx.TensorProto.FLOAT, []\n)\nthen_body = onnx.helper.make_graph(\n [constant_in, seq_const_in], \"then_body\", [], [then_seq_out]\n)\n\nelse_seq_out = onnx.helper.make_tensor_sequence_value_info(\n \"seq_in\", onnx.TensorProto.FLOAT, []\n)\nelse_body = onnx.helper.make_graph(\n [optional_get_elem], \"else_body\", [], [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\", inputs=[\"iter_count\", \"one\"], outputs=[\"end\"]\n)\n\nend_unsqueeze_node = onnx.helper.make_node(\n \"Unsqueeze\", inputs=[\"end\", \"axes\"], outputs=[\"slice_end\"]\n)\n\nslice_node = onnx.helper.make_node(\n \"Slice\", inputs=[\"x\", \"slice_start\", \"slice_end\"], outputs=[\"slice_out\"]\n)\n\ninsert_node = onnx.helper.make_node(\n \"SequenceInsert\", inputs=[\"sequence\", \"slice_out\"], outputs=[\"seq_out\"]\n)\n\nidentity_node = onnx.helper.make_node(\n \"Identity\", inputs=[\"cond_in\"], outputs=[\"cond_out\"]\n)\n\nloop_body = onnx.helper.make_graph(\n [\n identity_node,\n optional_has_elem_node,\n optional_is_none,\n if_node,\n x_const_node,\n one_const_node,\n zero_const_node,\n add_node,\n axes_node,\n end_unsqueeze_node,\n slice_node,\n insert_node,\n ],\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(\n node,\n inputs=[trip_count, cond, opt_seq_in],\n outputs=[seq_res],\n name=\"test_loop16_seq_none\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 16)],\n input_type_protos=[\n onnx.helper.make_tensor_type_proto(\n onnx.TensorProto.INT64, trip_count.shape\n ),\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.BOOL, cond.shape),\n opt_in_tp,\n ],\n)" } ] }, { "name": "LpNormalization", "module": "ai.onnx", "version": 1, "description": "Given a matrix, apply Lp-normalization along the provided axis.\nThe output is computed as: `output = input / Lp_norm(input, axis)`.\nWhen the Lp norm is zero (i.e., all elements along the axis are zero),\nthe output is defined to be zero to avoid division by zero.\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)" ] } ], "examples": [ { "summary": "default", "code": "node = onnx.helper.make_node(\"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"])\nx = np.array(\n [[[1.0, 2.0, 2.0], [3.0, 4.0, 0.0]], [[0.0, 5.0, 5.0], [6.0, 8.0, 0.0]]],\n dtype=np.float32,\n)\nlp_norm_default = np.sqrt(np.sum(x**2, axis=-1, keepdims=True))\ny = x / lp_norm_default\nexpect(node, inputs=[x], outputs=[y], name=\"test_lpnormalization_default\")" }, { "summary": "l1normalization_axis_0", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=0, p=1\n)\nx = np.array([3.0, 4.0], dtype=np.float32)\nl1_norm_axis_0 = np.sum(abs(x), axis=0, keepdims=True)\ny = x / l1_norm_axis_0\nexpect(node, inputs=[x], outputs=[y], name=\"test_l1normalization_axis_0\")" }, { "summary": "l1normalization_axis_1", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=1, p=1\n)\nx = np.array([[3.0, 4.0], [6.0, 8.0]], dtype=np.float32)\nl1_norm_axis_1 = np.sum(abs(x), axis=1, keepdims=True)\ny = x / l1_norm_axis_1\nexpect(node, inputs=[x], outputs=[y], name=\"test_l1normalization_axis_1\")" }, { "summary": "l1normalization_axis_last", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=-1, p=1\n)\nx = np.array(\n [[[1.0, 2.0, 2.0], [3.0, 4.0, 0.0]], [[0.0, 5.0, 5.0], [6.0, 8.0, 0.0]]],\n dtype=np.float32,\n)\nl1_norm_axis_last = np.sum(abs(x), axis=-1, keepdims=True)\ny = x / l1_norm_axis_last\nexpect(node, inputs=[x], outputs=[y], name=\"test_l1normalization_axis_last\")" }, { "summary": "l2normalization_axis_0", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=0, p=2\n)\nx = np.array(\n [[[1.0, 2.0, 2.0], [3.0, 4.0, 0.0]], [[0.0, 5.0, 5.0], [6.0, 8.0, 0.0]]],\n dtype=np.float32,\n)\nl2_norm_axis_0 = np.sqrt(np.sum(x**2, axis=0, keepdims=True))\n# When norm is 0, output is 0 (0/0 = 0)\ny = np.where(l2_norm_axis_0 == 0, 0, x / l2_norm_axis_0)\nexpect(node, inputs=[x], outputs=[y], name=\"test_l2normalization_axis_0\")" }, { "summary": "l2normalization_axis_1", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=1, p=2\n)\nx = np.array([[3.0, 4.0], [6.0, 8.0]], dtype=np.float32)\nl2_norm_axis_1 = np.sqrt(np.sum(x**2, axis=1, keepdims=True))\ny = x / l2_norm_axis_1\nexpect(node, inputs=[x], outputs=[y], name=\"test_l2normalization_axis_1\")" } ], "category": "Normalization" }, { "name": "LpNormalization", "module": "ai.onnx", "version": 22, "description": "Given a matrix, apply Lp-normalization along the provided axis.\nThe output is computed as: `output = input / Lp_norm(input, axis)`.\nWhen the Lp norm is zero (i.e., all elements along the axis are zero),\nthe output is defined to be zero to avoid division by zero.\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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "default", "code": "node = onnx.helper.make_node(\"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"])\nx = np.array(\n [[[1.0, 2.0, 2.0], [3.0, 4.0, 0.0]], [[0.0, 5.0, 5.0], [6.0, 8.0, 0.0]]],\n dtype=np.float32,\n)\nlp_norm_default = np.sqrt(np.sum(x**2, axis=-1, keepdims=True))\ny = x / lp_norm_default\nexpect(node, inputs=[x], outputs=[y], name=\"test_lpnormalization_default\")" }, { "summary": "l1normalization_axis_0", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=0, p=1\n)\nx = np.array([3.0, 4.0], dtype=np.float32)\nl1_norm_axis_0 = np.sum(abs(x), axis=0, keepdims=True)\ny = x / l1_norm_axis_0\nexpect(node, inputs=[x], outputs=[y], name=\"test_l1normalization_axis_0\")" }, { "summary": "l1normalization_axis_1", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=1, p=1\n)\nx = np.array([[3.0, 4.0], [6.0, 8.0]], dtype=np.float32)\nl1_norm_axis_1 = np.sum(abs(x), axis=1, keepdims=True)\ny = x / l1_norm_axis_1\nexpect(node, inputs=[x], outputs=[y], name=\"test_l1normalization_axis_1\")" }, { "summary": "l1normalization_axis_last", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=-1, p=1\n)\nx = np.array(\n [[[1.0, 2.0, 2.0], [3.0, 4.0, 0.0]], [[0.0, 5.0, 5.0], [6.0, 8.0, 0.0]]],\n dtype=np.float32,\n)\nl1_norm_axis_last = np.sum(abs(x), axis=-1, keepdims=True)\ny = x / l1_norm_axis_last\nexpect(node, inputs=[x], outputs=[y], name=\"test_l1normalization_axis_last\")" }, { "summary": "l2normalization_axis_0", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=0, p=2\n)\nx = np.array(\n [[[1.0, 2.0, 2.0], [3.0, 4.0, 0.0]], [[0.0, 5.0, 5.0], [6.0, 8.0, 0.0]]],\n dtype=np.float32,\n)\nl2_norm_axis_0 = np.sqrt(np.sum(x**2, axis=0, keepdims=True))\n# When norm is 0, output is 0 (0/0 = 0)\ny = np.where(l2_norm_axis_0 == 0, 0, x / l2_norm_axis_0)\nexpect(node, inputs=[x], outputs=[y], name=\"test_l2normalization_axis_0\")" }, { "summary": "l2normalization_axis_1", "code": "node = onnx.helper.make_node(\n \"LpNormalization\", inputs=[\"x\"], outputs=[\"y\"], axis=1, p=2\n)\nx = np.array([[3.0, 4.0], [6.0, 8.0]], dtype=np.float32)\nl2_norm_axis_1 = np.sqrt(np.sum(x**2, axis=1, keepdims=True))\ny = x / l2_norm_axis_1\nexpect(node, inputs=[x], outputs=[y], name=\"test_l2normalization_axis_1\")" } ], "category": "Normalization" }, { "name": "LpPool", "module": "ai.onnx", "version": 1, "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)" ] } ], "examples": [ { "summary": "lppool_1d_default", "code": "\"\"\"input_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\np = 3\nkernel_shape = [2]\nstrides = [1]\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n p=p,\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_1d_default\")" }, { "summary": "lppool_2d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_default\")" }, { "summary": "lppool_2d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n p=p,\n)\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array(\n [\n [\n [\n [14.560219778561036, 16.24807680927192],\n [21.633307652783937, 23.49468024894146],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_dilations\")" }, { "summary": "lppool_2d_pads", "code": "\"\"\"input_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2],\n p=p,\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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"LPPOOL\",\n pads_required=extra_pads,\n pads=pads,\n p=p,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_pads\")" }, { "summary": "lppool_2d_same_lower", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_LOWER\",\n p=p,\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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_lower\")" }, { "summary": "lppool_2d_same_upper", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_UPPER\",\n p=p,\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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_upper\")" }, { "summary": "lppool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[5, 5],\n strides=[3, 3],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_strides\")" }, { "summary": "lppool_3d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_3d_default\")" } ], "category": "Pool" }, { "name": "LpPool", "module": "ai.onnx", "version": 2, "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)" ] } ], "examples": [ { "summary": "lppool_1d_default", "code": "\"\"\"input_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\np = 3\nkernel_shape = [2]\nstrides = [1]\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n p=p,\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_1d_default\")" }, { "summary": "lppool_2d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_default\")" }, { "summary": "lppool_2d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n p=p,\n)\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array(\n [\n [\n [\n [14.560219778561036, 16.24807680927192],\n [21.633307652783937, 23.49468024894146],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_dilations\")" }, { "summary": "lppool_2d_pads", "code": "\"\"\"input_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2],\n p=p,\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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"LPPOOL\",\n pads_required=extra_pads,\n pads=pads,\n p=p,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_pads\")" }, { "summary": "lppool_2d_same_lower", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_LOWER\",\n p=p,\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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_lower\")" }, { "summary": "lppool_2d_same_upper", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_UPPER\",\n p=p,\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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_upper\")" }, { "summary": "lppool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[5, 5],\n strides=[3, 3],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_strides\")" }, { "summary": "lppool_3d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_3d_default\")" } ], "category": "Pool" }, { "name": "LpPool", "module": "ai.onnx", "version": 11, "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)" ] } ], "examples": [ { "summary": "lppool_1d_default", "code": "\"\"\"input_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\np = 3\nkernel_shape = [2]\nstrides = [1]\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n p=p,\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_1d_default\")" }, { "summary": "lppool_2d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_default\")" }, { "summary": "lppool_2d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n p=p,\n)\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array(\n [\n [\n [\n [14.560219778561036, 16.24807680927192],\n [21.633307652783937, 23.49468024894146],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_dilations\")" }, { "summary": "lppool_2d_pads", "code": "\"\"\"input_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2],\n p=p,\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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"LPPOOL\",\n pads_required=extra_pads,\n pads=pads,\n p=p,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_pads\")" }, { "summary": "lppool_2d_same_lower", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_LOWER\",\n p=p,\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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_lower\")" }, { "summary": "lppool_2d_same_upper", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_UPPER\",\n p=p,\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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_upper\")" }, { "summary": "lppool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[5, 5],\n strides=[3, 3],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_strides\")" }, { "summary": "lppool_3d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_3d_default\")" } ], "category": "Pool" }, { "name": "LpPool", "module": "ai.onnx", "version": 18, "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. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - {kernelSpatialShape}) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - {kernelSpatialShape}) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled `pad_shape[i]` is the sum of pads along axis `i`.\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] - {kernelSpatialShape} + 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] + {kernelSpatialShape} - input_spatial_shape[i]\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 the filter. If not present, the dilation defaults is 1 along each spatial axis." }, { "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)" ] } ], "examples": [ { "summary": "lppool_1d_default", "code": "\"\"\"input_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\np = 3\nkernel_shape = [2]\nstrides = [1]\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n p=p,\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_1d_default\")" }, { "summary": "lppool_2d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_default\")" }, { "summary": "lppool_2d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n p=p,\n)\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array(\n [\n [\n [\n [14.560219778561036, 16.24807680927192],\n [21.633307652783937, 23.49468024894146],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_dilations\")" }, { "summary": "lppool_2d_pads", "code": "\"\"\"input_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2],\n p=p,\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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"LPPOOL\",\n pads_required=extra_pads,\n pads=pads,\n p=p,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_pads\")" }, { "summary": "lppool_2d_same_lower", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_LOWER\",\n p=p,\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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_lower\")" }, { "summary": "lppool_2d_same_upper", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_UPPER\",\n p=p,\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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_upper\")" }, { "summary": "lppool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[5, 5],\n strides=[3, 3],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_strides\")" }, { "summary": "lppool_3d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_3d_default\")" } ], "category": "Pool" }, { "name": "LpPool", "module": "ai.onnx", "version": 22, "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. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - {kernelSpatialShape}) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - {kernelSpatialShape}) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled `pad_shape[i]` is the sum of pads along axis `i`.\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] - {kernelSpatialShape} + 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] + {kernelSpatialShape} - input_spatial_shape[i]\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 the filter. If not present, the dilation defaults is 1 along each spatial axis." }, { "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "lppool_1d_default", "code": "\"\"\"input_shape: [1, 3, 32]\noutput_shape: [1, 3, 31]\n\"\"\"\np = 3\nkernel_shape = [2]\nstrides = [1]\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n p=p,\n)\nx = np.random.randn(1, 3, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_1d_default\")" }, { "summary": "lppool_2d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 31, 31]\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_default\")" }, { "summary": "lppool_2d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n strides=[1, 1],\n dilations=[2, 2],\n p=p,\n)\nx = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\n\ny = np.array(\n [\n [\n [\n [14.560219778561036, 16.24807680927192],\n [21.633307652783937, 23.49468024894146],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_dilations\")" }, { "summary": "lppool_2d_pads", "code": "\"\"\"input_shape: [1, 3, 28, 28]\noutput_shape: [1, 3, 30, 30]\npad_shape: [4, 4] -> [2, 2, 2, 2] by axis\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[3, 3],\n pads=[2, 2, 2, 2],\n p=p,\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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (extra_pads[0], extra_pads[2]),\n (extra_pads[1], extra_pads[3]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"LPPOOL\",\n pads_required=extra_pads,\n pads=pads,\n p=p,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_pads\")" }, { "summary": "lppool_2d_same_lower", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [1, 0, 1, 0] by axis\n\"\"\"\np = 4\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_LOWER\",\n p=p,\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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_lower\")" }, { "summary": "lppool_2d_same_upper", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 32, 32]\npad_shape: [1, 1] -> [0, 1, 0, 1] by axis\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2],\n auto_pad=\"SAME_UPPER\",\n p=p,\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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=0,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(\n padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", pads, pads, p=p\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_same_upper\")" }, { "summary": "lppool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\np = 2\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[5, 5],\n strides=[3, 3],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_2d_strides\")" }, { "summary": "lppool_3d_default", "code": "\"\"\"input_shape: [1, 3, 32, 32, 32]\noutput_shape: [1, 3, 31, 31, 31]\n\"\"\"\np = 3\nnode = onnx.helper.make_node(\n \"LpPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n p=p,\n)\nx = np.random.randn(1, 3, 32, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"LPPOOL\", p=p)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_lppool_3d_default\")" } ], "category": "Pool" }, { "name": "LpPool", "module": "com.ms.internal.nhwc", "version": 11, "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": "LpPool", "module": "com.ms.internal.nhwc", "version": 18, "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. The output spatial shape will be following:\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - {kernelSpatialShape}) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - {kernelSpatialShape}) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled `pad_shape[i]` is the sum of pads along axis `i`.\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] - {kernelSpatialShape} + 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] + {kernelSpatialShape} - input_spatial_shape[i]\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 the filter. If not present, the dilation defaults is 1 along each spatial axis." }, { "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, "description": "Matrix product that behaves like [numpy.matmul](https://numpy.org/doc/stable/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], 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], 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], name=\"test_matmul_4d\")\n\n# broadcasting\na = np.random.randn(3, 1, 3, 4).astype(np.float32)\nb = np.random.randn(1, 2, 4, 2).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_bcast\")\n\n# 1d + 3d\na = np.random.randn(4).astype(np.float32)\nb = np.random.randn(2, 4, 1).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_1d_3d\")\n\n# 3d + 1d\na = np.random.randn(1, 2, 4, 3).astype(np.float32)\nb = np.random.randn(3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_4d_1d\")\n\n# 1d + 1d\na = np.random.randn(3).astype(np.float32)\nb = np.random.randn(3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_1d_1d\")" } ] }, { "name": "MatMul", "module": "ai.onnx", "version": 9, "description": "Matrix product that behaves like [numpy.matmul](https://numpy.org/doc/stable/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], 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], 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], name=\"test_matmul_4d\")\n\n# broadcasting\na = np.random.randn(3, 1, 3, 4).astype(np.float32)\nb = np.random.randn(1, 2, 4, 2).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_bcast\")\n\n# 1d + 3d\na = np.random.randn(4).astype(np.float32)\nb = np.random.randn(2, 4, 1).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_1d_3d\")\n\n# 3d + 1d\na = np.random.randn(1, 2, 4, 3).astype(np.float32)\nb = np.random.randn(3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_4d_1d\")\n\n# 1d + 1d\na = np.random.randn(3).astype(np.float32)\nb = np.random.randn(3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_1d_1d\")" } ] }, { "name": "MatMul", "module": "ai.onnx", "version": 13, "description": "Matrix product that behaves like [numpy.matmul](https://numpy.org/doc/stable/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], 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], 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], name=\"test_matmul_4d\")\n\n# broadcasting\na = np.random.randn(3, 1, 3, 4).astype(np.float32)\nb = np.random.randn(1, 2, 4, 2).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_bcast\")\n\n# 1d + 3d\na = np.random.randn(4).astype(np.float32)\nb = np.random.randn(2, 4, 1).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_1d_3d\")\n\n# 3d + 1d\na = np.random.randn(1, 2, 4, 3).astype(np.float32)\nb = np.random.randn(3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_4d_1d\")\n\n# 1d + 1d\na = np.random.randn(3).astype(np.float32)\nb = np.random.randn(3).astype(np.float32)\nc = np.matmul(a, b)\nexpect(node, inputs=[a, b], outputs=[c], name=\"test_matmul_1d_1d\")" } ] }, { "name": "MatMulBnb4", "module": "com.microsoft", "version": 1, "description": "MatMulBnb4 is a MatMul with weight quantized with 4 bits using either FP4 or NF4 data type (https://arxiv.org/pdf/2305.14314.pdf). It does Matrix Multiplication like MatMul (https://github.com/onnx/onnx/blob/main/docs/Operators.md#matmul) with differences:\n 1. Input B is a 2D constant Matrix. Its input feature count and output feature count are specified by attribute 'K' and 'N'.\n 2. Input B is quantized with 4 bits with quantization data type specified by attribute 'quant_type'. It is transposed, flattened and quantized blockwisely with block size specified by attribute 'block_size'.\n And block_size is not an arbitrary number and must be a power of 2 and not smaller than 16, like 16, 32, 64, 128,..\n 3. Input B's quantization constants or scales are specified by input 'absmax'.\n\n Input B is stored as uint8_t with shape: [(N * K + 1) / 2].\n Input absmax is stored in same type as original type of B(float32, float16) with shape like: [(N * K + block_size - 1) / block_size].\n\n\n 1. (Default value) transB=True (Majorly used for forward pass)\n Shape of A: [D0, D1, ..., Dn, K]\n Shape of Dequanted B: [N, K], this is aligned with how PyTorch defined the linear weight, .e.g [out_features, in_features].\n\n The computation math:\n dequant_B = dequant(B, absmax, quant_type, block_size)\n transposed_dequant_B = dequant_B^T\n output = A @ transposed_dequant_B\n\n Shape of output: [D0, D1, ..., Dn, N]\n\n 2. transB=False (Majorly used for backward pass)\n Shape of A: [D0, D1, ..., Dn, N]\n Shape of Dequanted B: [N, K], this is aligned with how PyTorch defined the linear weight, .e.g [out_features, in_features].\n\n The computation math:\n dequant_B = dequant(B, absmax, quant_type, block_size)\n output = A @ dequant_B\n\n Shape of output: [D0, D1, ..., Dn, K]\n\n", "attributes": [ { "name": "K", "type": "int64", "required": true, "description": "size of each input feature" }, { "name": "N", "type": "int64", "required": true, "description": "size of each output feature" }, { "name": "block_size", "type": "int64", "required": true, "description": "number of groupsize used for weight quantization. It needs to be a power of 2 and not smaller than 16." }, { "name": "quant_type", "type": "int64", "required": true, "description": "quantization data type. 0 for FP4, 1 for NF4." }, { "name": "training_mode", "type": "int64", "required": false, "description": "Indicate if the ops run in training_mode, by default, False." }, { "name": "transB", "type": "int64", "required": false, "default": 1, "description": "Whether B should be transposed on the last two dimensions before doing multiplication. Default to be 1." } ], "inputs": [ { "name": "A", "type": "T1", "description": "The input tensor, not quantized" }, { "name": "B", "type": "T2", "description": "1-dimensional quantized data for weight" }, { "name": "absmax", "type": "T1", "description": "quantization constants" } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "Y", "type": "T1", "description": "tensor. The output tensor has the same rank as the input. " } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to float/half_float/brain_float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain quantized weight types to uint8.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(uint8)" ] } ] }, { "name": "MatMulFpQ4", "module": "com.microsoft", "version": 1, "description": "Matrix product with right hand matrix being pre-packed and quantized int4 data blob.\nDuring quantization, the matrix is divided into blocks, where each block is a\ncontiguous subset inside each column. Each block is quantized into a\nsequence of 4b integers with a scaling factor and an optional offset.\nCurrently 3 quantization types are supported:\n(0): block size 32, no offset, (1): block size 32, with offset, (2): block size 64,\nno offset\n", "attributes": [ { "name": "blk_quant_type", "type": "int64", "required": false, "default": 1, "description": "Quantization type" } ], "inputs": [ { "name": "A", "type": "T1", "description": "N-dimensional matrix A" }, { "name": "B", "type": "T2", "description": "1-dimensional data blob" }, { "name": "B_shape", "type": "T3", "description": "Shape information of B" } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "Y", "type": "T1", "description": "Matrix multiply results from A * B" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input matrix data types as single precision float tensor", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain input B data types as data blob", "type_param_str": "T2", "allowed_type_strs": [ "tensor(uint8)" ] }, { "description": "Constrain shape of B must be int64 tensor.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(int64)" ] } ] }, { "name": "MatMulInteger16", "module": "com.microsoft", "version": 1, "description": "Matrix product that behaves like numpy.matmul: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matmul.html.\n The production MUST never overflow. The accumulation may overflow if and only if in 32 bits.", "inputs": [ { "name": "A", "type": "T1", "description": "N-dimensional matrix A" }, { "name": "B", "type": "T2", "description": "N-dimensional matrix B" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "T3", "description": "Matrix multiply results from A * B" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input A data types as 16-bit integer tensor", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int16)", "tensor(uint16)" ] }, { "description": "Constrain input B data types as 16-bit integer tensor", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int16)", "tensor(uint16)" ] }, { "description": "Constrain output Y data types as 32-bit integer tensor.T3 must be tensor(uint32) when both T1 and T2 are tensor(uint16),or must be tensor(int32) when either T1 or T2 is tensor(int16).", "type_param_str": "T3", "allowed_type_strs": [ "tensor(int32)", "tensor(uint32)" ] } ] }, { "name": "MatMulInteger", "module": "ai.onnx", "version": 10, "description": "Matrix product that behaves like [numpy.matmul](https://numpy.org/doc/stable/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(\n \"MatMulInteger\",\n inputs=[\"A\", \"B\", \"a_zero_point\", \"b_zero_point\"],\n outputs=[\"Y\"],\n)\n\nA = np.array(\n [\n [11, 7, 3],\n [10, 6, 2],\n [9, 5, 1],\n [8, 4, 0],\n ],\n dtype=np.uint8,\n)\n\na_zero_point = np.array([12], dtype=np.uint8)\n\nB = np.array(\n [\n [1, 4],\n [2, 5],\n [3, 6],\n ],\n dtype=np.uint8,\n)\n\nb_zero_point = np.array([0], dtype=np.uint8)\n\noutput = np.array(\n [\n [-38, -83],\n [-44, -98],\n [-50, -113],\n [-56, -128],\n ],\n dtype=np.int32,\n)\n\nexpect(\n node,\n inputs=[A, B, a_zero_point, b_zero_point],\n outputs=[output],\n name=\"test_matmulinteger\",\n)" } ] }, { "name": "MatMulIntegerToFloat", "module": "com.microsoft", "version": 1, "inputs": [ { "name": "A", "type": "T1", "description": "N-dimensional matrix A" }, { "name": "B", "type": "T2", "description": "N-dimensional matrix B" }, { "name": "a_scale", "type": "T3", "description": "Scale of quantized input 'A'. It could be a scalar or a 1-D tensor, which means a per-tensor or per-column quantization. If it's a 1-D tensor, its number of elements should be equal to the number of columns of input 'A'." }, { "name": "b_scale", "type": "T3", "description": "Scale of quantized input 'B'. It could be a scalar or a 1-D tensor, which means a per-tensor or per-column quantization. If it's a 1-D tensor, its number of elements should be equal to the number of columns of input '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 a 1-D tensor, which means a per-tensor or per-column quantization. If it's a 1-D tensor, its number of elements should be equal to the number of columns of input 'A'." }, { "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 1-D tensor, which means a per-tensor or per-column quantization. If it's a 1-D tensor, its number of elements should be equal to the number of columns of input 'B'." }, { "name": "bias", "type": "T3", "option": "optional", "description": "1D input tensor, whose dimension is same as B's last dimension" } ], "min_input": 4, "max_input": 7, "outputs": [ { "name": "Y", "type": "T3", "description": "Matrix multiply results from A * B" } ], "min_output": 1, "max_output": 1, "inputs_range": "4 - 7", "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 input a_scale, b_scale and output Y data type as float tensor.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] } ] }, { "name": "MatMulNBits", "module": "com.microsoft", "version": 1, "description": "MatMulNBits performs a matrix multiplication where the right-hand-side matrix (weights) is quantized to N bits.\n\nIt is a fusion of two operations:\n1. Linear dequantization of the quantized weights using scale and (optionally) zero-point with formula:\n dequantized_weight = (quantized_weight - zero_point) * scale\n2. Matrix multiplication between the input matrix A and the dequantized weight matrix.\n\nThe weight matrix is a 2D constant matrix with the input feature count and output feature count specified by attributes 'K' and 'N'.\nIt is quantized block-wise along the K dimension with a block size specified by the 'block_size' attribute.\nThe block size must be a power of 2 and not smaller than 16 (e.g., 16, 32, 64, 128). Each block has its own scale and zero-point.\nThe quantization is performed using a bit-width specified by the 'bits' attribute, which can take values from 2 to 8.\n\nThe quantized weights are stored in a bit-packed format along the K dimension, with each block being represented by a blob of uint8.\nFor example, for 4 bits, the first 4 bits are stored in the lower 4 bits of a byte, and the second 4 bits are stored in the higher 4 bits of a byte.\n", "attributes": [ { "name": "K", "type": "int64", "required": true, "description": "Input feature dimension of the weight matrix." }, { "name": "N", "type": "int64", "required": true, "description": "Output feature dimension of the weight matrix." }, { "name": "accuracy_level", "type": "int64", "required": false, "description": "The minimum accuracy level of input A, can be: 0(unset), 1(fp32), 2(fp16), 3(bf16), or 4(int8) (default unset). It is used to control how input A is quantized or downcast internally while doing computation, for example: 0 means input A will not be quantized or downcast while doing computation. 4 means input A can be quantized with the same block_size to int8 internally from type T1." }, { "name": "bits", "type": "int64", "required": false, "default": 4, "description": "Bit-width used to quantize the weights (supported values: 2, 4, 8)" }, { "name": "block_size", "type": "int64", "required": true, "description": "Size of each quantization block along the K (input feature) dimension. Must be a power of two and \u2265 16 (e.g., 16, 32, 64, 128)." }, { "name": "weight_prepacked", "type": "int64", "required": false, "description": "If set, input B is already prepacked into an EP-specific layout and the EP skips runtime weight prepacking. 0 (default): not prepacked. 1: prepacked in the CUDA SM80 fpA_intB layout. 2: prepacked in the CUDA SM90 (Hopper) fpA_intB layout, consumed by the native SM90 kernel (requires a compute capability 9.0 device and block_size in {64, 128})." } ], "inputs": [ { "name": "A", "type": "T1", "description": "The input tensor, not quantized." }, { "name": "B", "type": "T2", "description": "Packed uint8 tensor of shape (N, k_blocks, blob_size), where k_blocks = ceil(K / block_size) and blob_size = (block_size * bits / 8). The quantized weights are stored in a bit-packed format along the K dimension, packed within each block_size." }, { "name": "scales", "type": "T1", "description": "Per-block scaling factors for dequantization with shape (N, k_blocks) and same data type as input A." }, { "name": "zero_points", "type": "T3", "option": "optional", "description": "Per-block zero point for dequantization. It can be either packed or unpacked: Packed (uint8) format has shape (N, ceil(k_blocks * bits / 8)), and it uses same bit-packing method as Input B. Unpacked (same type as A) format has shape (N, k_blocks). If not provided, a default zero point is used: 2^(bits - 1) (e.g., 8 for 4-bit quantization, 128 for 8-bit). " }, { "name": "g_idx", "type": "T4", "option": "optional", "description": "group_idx. This input is deprecated" }, { "name": "bias", "type": "T1", "option": "optional", "description": "Bias to add to result. It should have shape [N]." } ], "min_input": 3, "max_input": 6, "outputs": [ { "name": "Y", "type": "T1", "description": "tensor. The output tensor has the same rank as the input. " } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - 6", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain quantized weight types to uint8.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(uint8)" ] }, { "description": "Constrain quantized zero point types to uint8 or float tensors.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(uint8)", "tensor(float16)", "tensor(float)", "tensor(bfloat16)" ] }, { "description": "the index tensor.", "type_param_str": "T4", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "MatMulNBitsMlp", "module": "com.microsoft", "version": 1, "description": "MatMulNBitsMlp fuses two MatMulNBits projections that share the same input and computes\n\n gate = MatMulNBits(A, gate_weight) + gate_bias\n up = MatMulNBits(A, up_weight) + up_bias\n Y = activation(gate) * up\n\nIt can also optionally fuse SimplifiedLayerNormalization or SkipSimplifiedLayerNormalization before the\ntwo projections:\n\n A_norm = SimplifiedLayerNormalization(A, norm_scale, epsilon)\n gate = MatMulNBits(A_norm, gate_weight) + gate_bias\n up = MatMulNBits(A_norm, up_weight) + up_bias\n Y = activation(gate) * up\n\n A_norm = SkipSimplifiedLayerNormalization(A, skip, norm_scale, epsilon)\n gate = MatMulNBits(A_norm, gate_weight) + gate_bias\n up = MatMulNBits(A_norm, up_weight) + up_bias\n Y = activation(gate) * up\n\nThis operator is intended for decoder MLP patterns such as Qwen-style gate and up projections, but it remains\nsemantically valid for both prefill and decode because the output shape is the standard MatMul result shape\nderived from the runtime shape of A and the shared attributes K and N.\n\nThe operator contract includes a string attribute describing the fused gate activation.\n\nWhen fused from SkipSimplifiedLayerNormalization, the optional residual-sum output may also be materialized:\n\n A_norm, input_skip_bias_sum = SkipSimplifiedLayerNormalization(A, skip, norm_scale, epsilon)\n gate = MatMulNBits(A_norm, gate_weight) + gate_bias\n up = MatMulNBits(A_norm, up_weight) + up_bias\n Y = activation(gate) * up\n", "attributes": [ { "name": "K", "type": "int64", "required": true, "description": "Input feature dimension shared by both quantized weight matrices." }, { "name": "N", "type": "int64", "required": true, "description": "Output feature dimension shared by both quantized weight matrices." }, { "name": "accuracy_level", "type": "int64", "required": false, "description": "The minimum accuracy level of input A. It follows the same semantics as MatMulNBits." }, { "name": "activation", "type": "string", "required": true, "description": "Activation applied to the gate projection." }, { "name": "bits", "type": "int64", "required": false, "default": 4, "description": "Bit-width used to quantize both weight matrices. Currently only bits=4 is supported by the WebGPU kernel." }, { "name": "block_size", "type": "int64", "required": true, "description": "Size of each quantization block along the K dimension. Currently only block_size=32 is supported by the WebGPU kernel." }, { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "Epsilon used by the optional fused (Skip)SimplifiedLayerNormalization. Defaults to 1e-5." } ], "inputs": [ { "name": "A", "type": "T1", "description": "The shared input tensor." }, { "name": "skip", "type": "T1", "option": "optional", "description": "Optional skip input used by SkipSimplifiedLayerNormalization." }, { "name": "norm_scale", "type": "T1", "option": "optional", "description": "Optional RMSNorm scale with shape [K] used by SimplifiedLayerNormalization or SkipSimplifiedLayerNormalization." }, { "name": "gate_B", "type": "T2", "description": "Packed uint8 tensor for the gate projection weights." }, { "name": "gate_scales", "type": "T1", "description": "Per-block scaling factors for the gate projection." }, { "name": "gate_bias", "type": "T1", "option": "optional", "description": "Optional bias for the gate projection with shape [N]." }, { "name": "up_B", "type": "T2", "description": "Packed uint8 tensor for the up projection weights." }, { "name": "up_scales", "type": "T1", "description": "Per-block scaling factors for the up projection." }, { "name": "up_bias", "type": "T1", "option": "optional", "description": "Optional bias for the up projection with shape [N]." } ], "min_input": 8, "max_input": 9, "outputs": [ { "name": "Y", "type": "T1", "description": "The fused gated MLP output tensor." }, { "name": "input_skip_bias_sum", "type": "T1", "option": "optional", "description": "Optional residual-sum output for SkipSimplifiedLayerNormalization." } ], "min_output": 1, "max_output": 2, "inputs_range": "8 - 9", "outputs_range": "1 - 2", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain quantized weight types to uint8.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(uint8)" ] } ] }, { "name": "MatMulNBitsQkv", "module": "com.microsoft", "version": 1, "description": "MatMulNBitsQkv fuses either SimplifiedLayerNormalization (RMSNorm)\nor SkipSimplifiedLayerNormalization with three MatMulNBits projections that share the\nsame normalized activation.\n\n A_norm = SimplifiedLayerNormalization(A, norm_scale, epsilon)\n Q = MatMulNBits(A_norm, q_weight) + q_bias\n K = MatMulNBits(A_norm, k_weight) + k_bias\n V = MatMulNBits(A_norm, v_weight) + v_bias\n\nIf skip is provided, the operator computes the SkipSimplifiedLayerNormalization variant\nand may also return the input+skip residual sum as output 3.\n\nThis operator is intended as a decode-oriented QKV fusion primitive.\n", "attributes": [ { "name": "K", "type": "int64", "required": true, "description": "Input feature dimension shared by the normalized input and all projection weights." }, { "name": "Nkv", "type": "int64", "required": true, "description": "Output feature dimension shared by the K and V projections." }, { "name": "Nq", "type": "int64", "required": true, "description": "Output feature dimension of the Q projection." }, { "name": "accuracy_level", "type": "int64", "required": false, "description": "The minimum accuracy level of input A. It follows the same semantics as MatMulNBits." }, { "name": "bits", "type": "int64", "required": false, "default": 4, "description": "Bit-width used to quantize all weight matrices. Currently only bits=4 is supported by the WebGPU kernel." }, { "name": "block_size", "type": "int64", "required": true, "description": "Size of each quantization block along the K dimension. Currently only block_size=32 is supported by the WebGPU kernel." }, { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999974752427e-07, "description": "Epsilon used by the simplified layer norm reduction." } ], "inputs": [ { "name": "A", "type": "T1", "description": "The shared input tensor." }, { "name": "skip", "type": "T1", "option": "optional", "description": "Optional residual input for SkipSimplifiedLayerNormalization." }, { "name": "norm_scale", "type": "T1", "description": "Scale input for the simplified layer norm with shape [K]." }, { "name": "q_B", "type": "T2", "description": "Packed uint8 tensor for the Q projection weights." }, { "name": "q_scales", "type": "T1", "description": "Per-block scaling factors for the Q projection." }, { "name": "q_bias", "type": "T1", "option": "optional", "description": "Optional bias for the Q projection with shape [Nq]." }, { "name": "k_B", "type": "T2", "description": "Packed uint8 tensor for the K projection weights." }, { "name": "k_scales", "type": "T1", "description": "Per-block scaling factors for the K projection." }, { "name": "k_bias", "type": "T1", "option": "optional", "description": "Optional bias for the K projection with shape [Nkv]." }, { "name": "v_B", "type": "T2", "description": "Packed uint8 tensor for the V projection weights." }, { "name": "v_scales", "type": "T1", "description": "Per-block scaling factors for the V projection." }, { "name": "v_bias", "type": "T1", "option": "optional", "description": "Optional bias for the V projection with shape [Nkv]." } ], "min_input": 11, "max_input": 12, "outputs": [ { "name": "Q", "type": "T1", "description": "The Q projection output tensor." }, { "name": "K", "type": "T1", "description": "The K projection output tensor." }, { "name": "V", "type": "T1", "description": "The V projection output tensor." }, { "name": "input_skip_bias_sum", "type": "T1", "option": "optional", "description": "Optional residual-sum output for SkipSimplifiedLayerNormalization." } ], "min_output": 3, "max_output": 4, "inputs_range": "11 - 12", "outputs_range": "3 - 4", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain quantized weight types to uint8.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(uint8)" ] } ] }, { "name": "Max", "module": "ai.onnx", "version": 1, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_max_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Max\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_max_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_max_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "Max", "module": "ai.onnx", "version": 6, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_max_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Max\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_max_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_max_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "Max", "module": "ai.onnx", "version": 8, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_max_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Max\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_max_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_max_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "Max", "module": "ai.onnx", "version": 12, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_max_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Max\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_max_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_max_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "Max", "module": "ai.onnx", "version": 13, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_max_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Max\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_max_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_max_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "MaxPool", "module": "ai.onnx", "version": 1, "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_1d_default\")" }, { "summary": "maxpool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_ceil\")" }, { "summary": "maxpool_2d_ceil_output_size_reduce_by_one", "code": "\"\"\"input_shape: [1, 1, 2, 2]\noutput_shape: [1, 1, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[1, 1],\n strides=[2, 2],\n ceil_mode=True,\n)\nx = np.array([[[[1, 2], [3, 4]]]]).astype(np.float32)\ny = np.array([[[[1]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_2d_ceil_output_size_reduce_by_one\",\n)" }, { "summary": "maxpool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_default\")" }, { "summary": "maxpool_2d_dilations", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_dilations\")" }, { "summary": "maxpool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\n\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_pads\")" }, { "summary": "maxpool_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_pads\")" }, { "summary": "maxpool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9, 10], [17, 19, 20], [22, 24, 25]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_same_upper\"\n)" }, { "summary": "maxpool_2d_precomputed_strides", "code": "\"\"\"input_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[2, 2], strides=[2, 2]\n)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_strides\"\n)" }, { "summary": "maxpool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_lower\")" }, { "summary": "maxpool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_upper\")" }, { "summary": "maxpool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[5, 5], strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_strides\")" }, { "summary": "maxpool_2d_uint8", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.uint8)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_uint8\")" }, { "summary": "maxpool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_default\")" }, { "summary": "maxpool_3d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[[11, 12], [15, 16]], [[11, 12], [15, 16]]]]]).astype(\n np.float32\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations\")" }, { "summary": "maxpool_3d_dilations_use_ref_impl", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\ndilations = [2, 2, 2]\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nceil_mode = False\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=dilations,\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\nx_shape = x.shape[2:]\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = x\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations_use_ref_impl\"\n)" }, { "summary": "maxpool_3d_dilations_use_ref_impl_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\nceil_mode = True\n\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n)\n\nx = np.random.randn(1, 1, *x_shape).astype(np.float32)\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (pads[0], pads[3]),\n (pads[1], pads[4]),\n (pads[2], pads[5]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_3d_dilations_use_ref_impl_large\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\nz = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_pads\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16], [8, 18]]]]).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_strides\",\n)" } ], "category": "Pool" }, { "name": "MaxPool", "module": "ai.onnx", "version": 8, "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_1d_default\")" }, { "summary": "maxpool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_ceil\")" }, { "summary": "maxpool_2d_ceil_output_size_reduce_by_one", "code": "\"\"\"input_shape: [1, 1, 2, 2]\noutput_shape: [1, 1, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[1, 1],\n strides=[2, 2],\n ceil_mode=True,\n)\nx = np.array([[[[1, 2], [3, 4]]]]).astype(np.float32)\ny = np.array([[[[1]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_2d_ceil_output_size_reduce_by_one\",\n)" }, { "summary": "maxpool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_default\")" }, { "summary": "maxpool_2d_dilations", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_dilations\")" }, { "summary": "maxpool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\n\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_pads\")" }, { "summary": "maxpool_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_pads\")" }, { "summary": "maxpool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9, 10], [17, 19, 20], [22, 24, 25]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_same_upper\"\n)" }, { "summary": "maxpool_2d_precomputed_strides", "code": "\"\"\"input_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[2, 2], strides=[2, 2]\n)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_strides\"\n)" }, { "summary": "maxpool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_lower\")" }, { "summary": "maxpool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_upper\")" }, { "summary": "maxpool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[5, 5], strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_strides\")" }, { "summary": "maxpool_2d_uint8", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.uint8)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_uint8\")" }, { "summary": "maxpool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_default\")" }, { "summary": "maxpool_3d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[[11, 12], [15, 16]], [[11, 12], [15, 16]]]]]).astype(\n np.float32\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations\")" }, { "summary": "maxpool_3d_dilations_use_ref_impl", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\ndilations = [2, 2, 2]\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nceil_mode = False\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=dilations,\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\nx_shape = x.shape[2:]\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = x\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations_use_ref_impl\"\n)" }, { "summary": "maxpool_3d_dilations_use_ref_impl_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\nceil_mode = True\n\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n)\n\nx = np.random.randn(1, 1, *x_shape).astype(np.float32)\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (pads[0], pads[3]),\n (pads[1], pads[4]),\n (pads[2], pads[5]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_3d_dilations_use_ref_impl_large\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\nz = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_pads\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16], [8, 18]]]]).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_strides\",\n)" } ], "category": "Pool" }, { "name": "MaxPool", "module": "ai.onnx", "version": 10, "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_1d_default\")" }, { "summary": "maxpool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_ceil\")" }, { "summary": "maxpool_2d_ceil_output_size_reduce_by_one", "code": "\"\"\"input_shape: [1, 1, 2, 2]\noutput_shape: [1, 1, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[1, 1],\n strides=[2, 2],\n ceil_mode=True,\n)\nx = np.array([[[[1, 2], [3, 4]]]]).astype(np.float32)\ny = np.array([[[[1]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_2d_ceil_output_size_reduce_by_one\",\n)" }, { "summary": "maxpool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_default\")" }, { "summary": "maxpool_2d_dilations", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_dilations\")" }, { "summary": "maxpool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\n\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_pads\")" }, { "summary": "maxpool_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_pads\")" }, { "summary": "maxpool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9, 10], [17, 19, 20], [22, 24, 25]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_same_upper\"\n)" }, { "summary": "maxpool_2d_precomputed_strides", "code": "\"\"\"input_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[2, 2], strides=[2, 2]\n)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_strides\"\n)" }, { "summary": "maxpool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_lower\")" }, { "summary": "maxpool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_upper\")" }, { "summary": "maxpool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[5, 5], strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_strides\")" }, { "summary": "maxpool_2d_uint8", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.uint8)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_uint8\")" }, { "summary": "maxpool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_default\")" }, { "summary": "maxpool_3d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[[11, 12], [15, 16]], [[11, 12], [15, 16]]]]]).astype(\n np.float32\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations\")" }, { "summary": "maxpool_3d_dilations_use_ref_impl", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\ndilations = [2, 2, 2]\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nceil_mode = False\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=dilations,\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\nx_shape = x.shape[2:]\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = x\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations_use_ref_impl\"\n)" }, { "summary": "maxpool_3d_dilations_use_ref_impl_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\nceil_mode = True\n\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n)\n\nx = np.random.randn(1, 1, *x_shape).astype(np.float32)\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (pads[0], pads[3]),\n (pads[1], pads[4]),\n (pads[2], pads[5]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_3d_dilations_use_ref_impl_large\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\nz = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_pads\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16], [8, 18]]]]).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_strides\",\n)" } ], "category": "Pool" }, { "name": "MaxPool", "module": "ai.onnx", "version": 11, "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_1d_default\")" }, { "summary": "maxpool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_ceil\")" }, { "summary": "maxpool_2d_ceil_output_size_reduce_by_one", "code": "\"\"\"input_shape: [1, 1, 2, 2]\noutput_shape: [1, 1, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[1, 1],\n strides=[2, 2],\n ceil_mode=True,\n)\nx = np.array([[[[1, 2], [3, 4]]]]).astype(np.float32)\ny = np.array([[[[1]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_2d_ceil_output_size_reduce_by_one\",\n)" }, { "summary": "maxpool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_default\")" }, { "summary": "maxpool_2d_dilations", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_dilations\")" }, { "summary": "maxpool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\n\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_pads\")" }, { "summary": "maxpool_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_pads\")" }, { "summary": "maxpool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9, 10], [17, 19, 20], [22, 24, 25]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_same_upper\"\n)" }, { "summary": "maxpool_2d_precomputed_strides", "code": "\"\"\"input_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[2, 2], strides=[2, 2]\n)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_strides\"\n)" }, { "summary": "maxpool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_lower\")" }, { "summary": "maxpool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_upper\")" }, { "summary": "maxpool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[5, 5], strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_strides\")" }, { "summary": "maxpool_2d_uint8", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.uint8)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_uint8\")" }, { "summary": "maxpool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_default\")" }, { "summary": "maxpool_3d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[[11, 12], [15, 16]], [[11, 12], [15, 16]]]]]).astype(\n np.float32\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations\")" }, { "summary": "maxpool_3d_dilations_use_ref_impl", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\ndilations = [2, 2, 2]\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nceil_mode = False\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=dilations,\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\nx_shape = x.shape[2:]\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = x\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations_use_ref_impl\"\n)" }, { "summary": "maxpool_3d_dilations_use_ref_impl_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\nceil_mode = True\n\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n)\n\nx = np.random.randn(1, 1, *x_shape).astype(np.float32)\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (pads[0], pads[3]),\n (pads[1], pads[4]),\n (pads[2], pads[5]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_3d_dilations_use_ref_impl_large\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\nz = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_pads\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16], [8, 18]]]]).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_strides\",\n)" } ], "category": "Pool" }, { "name": "MaxPool", "module": "ai.onnx", "version": 12, "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 is calculated differently\n depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized.\n With explicit padding (https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html?highlight=maxpool#torch.nn.MaxPool2d):\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled. `pad_shape[i]` is the sum of pads along axis `i`.\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following when ceil_mode is enabled:\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 or when ceil_mode is disabled (https://www.tensorflow.org/api_docs/python/tf/keras/layers/AveragePooling2D):\n ```\n VALID: output_spatial_shape[i] = floor((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i]) + 1\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = floor((input_spatial_shape[i] - 1) / strides_spatial_shape[i]) + 1\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. This attribute is used only to convert an n-tuple index value into a single integer value for producing the second output. " }, { "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_1d_default\")" }, { "summary": "maxpool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_ceil\")" }, { "summary": "maxpool_2d_ceil_output_size_reduce_by_one", "code": "\"\"\"input_shape: [1, 1, 2, 2]\noutput_shape: [1, 1, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[1, 1],\n strides=[2, 2],\n ceil_mode=True,\n)\nx = np.array([[[[1, 2], [3, 4]]]]).astype(np.float32)\ny = np.array([[[[1]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_2d_ceil_output_size_reduce_by_one\",\n)" }, { "summary": "maxpool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_default\")" }, { "summary": "maxpool_2d_dilations", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_dilations\")" }, { "summary": "maxpool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\n\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_pads\")" }, { "summary": "maxpool_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_pads\")" }, { "summary": "maxpool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9, 10], [17, 19, 20], [22, 24, 25]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_same_upper\"\n)" }, { "summary": "maxpool_2d_precomputed_strides", "code": "\"\"\"input_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[2, 2], strides=[2, 2]\n)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_strides\"\n)" }, { "summary": "maxpool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_lower\")" }, { "summary": "maxpool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_upper\")" }, { "summary": "maxpool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[5, 5], strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_strides\")" }, { "summary": "maxpool_2d_uint8", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.uint8)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_uint8\")" }, { "summary": "maxpool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_default\")" }, { "summary": "maxpool_3d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[[11, 12], [15, 16]], [[11, 12], [15, 16]]]]]).astype(\n np.float32\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations\")" }, { "summary": "maxpool_3d_dilations_use_ref_impl", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\ndilations = [2, 2, 2]\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nceil_mode = False\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=dilations,\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\nx_shape = x.shape[2:]\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = x\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations_use_ref_impl\"\n)" }, { "summary": "maxpool_3d_dilations_use_ref_impl_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\nceil_mode = True\n\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n)\n\nx = np.random.randn(1, 1, *x_shape).astype(np.float32)\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (pads[0], pads[3]),\n (pads[1], pads[4]),\n (pads[2], pads[5]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_3d_dilations_use_ref_impl_large\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\nz = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_pads\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16], [8, 18]]]]).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_strides\",\n)" } ], "category": "Pool" }, { "name": "MaxPool", "module": "ai.onnx", "version": 22, "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 is calculated differently\n depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized.\n With explicit padding (https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html?highlight=maxpool#torch.nn.MaxPool2d):\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled. `pad_shape[i]` is the sum of pads along axis `i`. Sliding windows that would start in the right padded region are ignored.\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following when ceil_mode is enabled:\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 or when ceil_mode is disabled (https://www.tensorflow.org/api_docs/python/tf/keras/layers/AveragePooling2D):\n ```\n VALID: output_spatial_shape[i] = floor((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i]) + 1\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = floor((input_spatial_shape[i] - 1) / strides_spatial_shape[i]) + 1\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. This attribute is used only to convert an n-tuple index value into a single integer value for producing the second output. " }, { "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(bfloat16)", "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": "\"\"\"input_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)\npads = None\nkernel_shape = [2]\nstrides = [1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_1d_default\")" }, { "summary": "maxpool_2d_ceil", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_ceil\")" }, { "summary": "maxpool_2d_ceil_output_size_reduce_by_one", "code": "\"\"\"input_shape: [1, 1, 2, 2]\noutput_shape: [1, 1, 1, 1]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[1, 1],\n strides=[2, 2],\n ceil_mode=True,\n)\nx = np.array([[[[1, 2], [3, 4]]]]).astype(np.float32)\ny = np.array([[[[1]]]]).astype(np.float32)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_2d_ceil_output_size_reduce_by_one\",\n)" }, { "summary": "maxpool_2d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = (2, 2)\nstrides = (1, 1)\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_default\")" }, { "summary": "maxpool_2d_dilations", "code": "\"\"\"input_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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[11, 12], [15, 16]]]]).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_dilations\")" }, { "summary": "maxpool_2d_pads", "code": "\"\"\"input_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\npads = [pad_top, pad_left, pad_bottom, pad_right]\nout_shape, extra_pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = np.pad(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\n\ny = pool(\n padded,\n x_shape,\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=extra_pads,\n pads=pads,\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_pads\")" }, { "summary": "maxpool_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_pads\")" }, { "summary": "maxpool_2d_precomputed_same_upper", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9, 10], [17, 19, 20], [22, 24, 25]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_same_upper\"\n)" }, { "summary": "maxpool_2d_precomputed_strides", "code": "\"\"\"input_shape: [1, 1, 5, 5]\noutput_shape: [1, 1, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[2, 2], strides=[2, 2]\n)\nx = np.array(\n [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_precomputed_strides\"\n)" }, { "summary": "maxpool_2d_same_lower", "code": "\"\"\"input_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_auto_pad(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_LOWER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_lower\")" }, { "summary": "maxpool_2d_same_upper", "code": "\"\"\"input_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_auto_pad(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides\n)\npad_shape = get_pad_shape(\n \"SAME_UPPER\", x_shape[2:], kernel_shape, strides, out_shape\n)\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(\n x,\n ((0, 0), (0, 0), (pad_top, pad_bottom), (pad_left, pad_right)),\n mode=\"constant\",\n constant_values=np.nan,\n)\npads = [pad_top, pad_left, pad_bottom, pad_right]\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\", pads, pads)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_same_upper\")" }, { "summary": "maxpool_2d_strides", "code": "\"\"\"input_shape: [1, 3, 32, 32]\noutput_shape: [1, 3, 10, 10]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\", inputs=[\"x\"], outputs=[\"y\"], kernel_shape=[5, 5], strides=[3, 3]\n)\nx = np.random.randn(1, 3, 32, 32).astype(np.float32)\nx_shape = np.shape(x)\npads = None\nkernel_shape = (5, 5)\nstrides = (3, 3)\nout_shape, pads = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_strides\")" }, { "summary": "maxpool_2d_uint8", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.uint8)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.uint8)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_2d_uint8\")" }, { "summary": "maxpool_3d_default", "code": "\"\"\"input_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)\npads = None\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nout_shape, _ = get_output_shape_explicit_padding(\n pads, x_shape[2:], kernel_shape, strides\n)\npadded = x\ny = pool(padded, x_shape, kernel_shape, strides, out_shape, \"MAX\")\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_default\")" }, { "summary": "maxpool_3d_dilations", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=[2, 2, 2],\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[[11, 12], [15, 16]], [[11, 12], [15, 16]]]]]).astype(\n np.float32\n)\n\nexpect(node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations\")" }, { "summary": "maxpool_3d_dilations_use_ref_impl", "code": "\"\"\"input_shape: [1, 1, 4, 4, 4]\noutput_shape: [1, 1, 2, 2, 2]\n\"\"\"\ndilations = [2, 2, 2]\nkernel_shape = [2, 2, 2]\nstrides = [1, 1, 1]\nceil_mode = False\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=[2, 2, 2],\n strides=[1, 1, 1],\n dilations=dilations,\n)\nx = np.array(\n [\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ],\n ]\n ]\n ]\n).astype(np.float32)\n\nx_shape = x.shape[2:]\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = x\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node, inputs=[x], outputs=[y], name=\"test_maxpool_3d_dilations_use_ref_impl\"\n)" }, { "summary": "maxpool_3d_dilations_use_ref_impl_large", "code": "x_shape = (32, 32, 32)\ndilations = (2, 2, 2)\nkernel_shape = (5, 5, 5)\nstrides = (3, 3, 3)\nceil_mode = True\n\nnode = onnx.helper.make_node(\n \"MaxPool\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n kernel_shape=kernel_shape,\n strides=strides,\n dilations=dilations,\n ceil_mode=ceil_mode,\n)\n\nx = np.random.randn(1, 1, *x_shape).astype(np.float32)\nout_shape, pads = get_output_shape_explicit_padding(\n None, x_shape, kernel_shape, strides, dilations, ceil_mode=ceil_mode\n)\npadded = np.pad(\n x,\n (\n (0, 0),\n (0, 0),\n (pads[0], pads[3]),\n (pads[1], pads[4]),\n (pads[2], pads[5]),\n ),\n mode=\"constant\",\n constant_values=0,\n)\ny = pool(\n padded,\n (1, 1, *x_shape),\n kernel_shape,\n strides,\n out_shape,\n \"MAX\",\n pads_required=pads,\n pads=None,\n dilations=dilations,\n)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_maxpool_3d_dilations_use_ref_impl_large\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_pads", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.float32)\nz = np.array(\n [\n [\n [\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],\n ]\n ]\n ]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_pads\",\n)" }, { "summary": "maxpool_with_argmax_2d_precomputed_strides", "code": "\"\"\"input_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 [\n [\n [\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 ]\n ]\n ]\n).astype(np.float32)\ny = np.array([[[[7, 9], [17, 19]]]]).astype(np.float32)\nz = np.array([[[[6, 16], [8, 18]]]]).astype(np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y, z],\n name=\"test_maxpool_with_argmax_2d_precomputed_strides\",\n)" } ], "category": "Pool" }, { "name": "MaxPool", "module": "com.microsoft.nchwc", "version": 1, "attributes": [ { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "name": "ceil_mode", "type": "int64", "required": false }, { "name": "dilations", "type": "int64[]", "required": false }, { "name": "kernel_shape", "type": "int64[]", "required": true }, { "name": "pads", "type": "int64[]", "required": false }, { "name": "storage_order", "type": "int64", "required": false }, { "name": "strides", "type": "int64[]", "required": false } ], "inputs": [ { "name": "X", "type": "T" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T" } ], "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(float)" ] } ], "category": "Pool" }, { "name": "MaxPool", "module": "com.ms.internal.nhwc", "version": 1, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Pool" }, { "name": "MaxPool", "module": "com.ms.internal.nhwc", "version": 8, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Pool" }, { "name": "MaxPool", "module": "com.ms.internal.nhwc", "version": 10, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Pool" }, { "name": "MaxPool", "module": "com.ms.internal.nhwc", "version": 11, "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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ], "category": "Pool" }, { "name": "MaxPool", "module": "com.ms.internal.nhwc", "version": 12, "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 is calculated differently\n depending on whether explicit padding is used, where pads is employed, or auto padding is used, where auto_pad is utilized.\n With explicit padding (https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html?highlight=maxpool#torch.nn.MaxPool2d):\n ```\n output_spatial_shape[i] = floor((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n or\n ```\n output_spatial_shape[i] = ceil((input_spatial_shape[i] + pad_shape[i] - dilation[i] * (kernel_shape[i] - 1) - 1) / strides_spatial_shape[i] + 1)\n ```\n if ceil_mode is enabled. `pad_shape[i]` is the sum of pads along axis `i`.\n\n `auto_pad` is a DEPRECATED attribute. If you are using them currently, the output spatial shape will be following when ceil_mode is enabled:\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 or when ceil_mode is disabled (https://www.tensorflow.org/api_docs/python/tf/keras/layers/AveragePooling2D):\n ```\n VALID: output_spatial_shape[i] = floor((input_spatial_shape[i] - ((kernel_spatial_shape[i] - 1) * dilations[i] + 1)) / strides_spatial_shape[i]) + 1\n SAME_UPPER or SAME_LOWER: output_spatial_shape[i] = floor((input_spatial_shape[i] - 1) / strides_spatial_shape[i]) + 1\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": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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. This attribute is used only to convert an n-tuple index value into a single integer value for producing the second output. " }, { "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)" ] } ], "category": "Pool" }, { "name": "MaxRoiPool", "module": "ai.onnx", "version": 1, "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": "MaxRoiPool", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "category": "Pool" }, { "name": "MaxUnpool", "module": "ai.onnx", "version": 9, "description": "MaxUnpool essentially computes the partial inverse of the MaxPool op.\n The input information to this op is typically 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 corresponding\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 corresponding\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], [7, 8]]]], dtype=np.float32)\nxI = np.array([[[[5, 7], [13, 15]]]], dtype=np.int64)\noutput_shape = np.array((1, 1, 5, 5), dtype=np.int64)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[xT, xI, output_shape],\n outputs=[y],\n name=\"test_maxunpool_export_with_output_shape\",\n)" }, { "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], [3, 4]]]], dtype=np.float32)\nxI = np.array([[[[5, 7], [13, 15]]]], dtype=np.int64)\ny = np.array(\n [[[[0, 0, 0, 0], [0, 1, 0, 2], [0, 0, 0, 0], [0, 3, 0, 4]]]],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[xT, xI],\n outputs=[y],\n name=\"test_maxunpool_export_without_output_shape\",\n)" } ] }, { "name": "MaxUnpool", "module": "ai.onnx", "version": 11, "description": "MaxUnpool essentially computes the partial inverse of the MaxPool op.\n The input information to this op is typically 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 corresponding\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 corresponding\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], [7, 8]]]], dtype=np.float32)\nxI = np.array([[[[5, 7], [13, 15]]]], dtype=np.int64)\noutput_shape = np.array((1, 1, 5, 5), dtype=np.int64)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[xT, xI, output_shape],\n outputs=[y],\n name=\"test_maxunpool_export_with_output_shape\",\n)" }, { "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], [3, 4]]]], dtype=np.float32)\nxI = np.array([[[[5, 7], [13, 15]]]], dtype=np.int64)\ny = np.array(\n [[[[0, 0, 0, 0], [0, 1, 0, 2], [0, 0, 0, 0], [0, 3, 0, 4]]]],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[xT, xI],\n outputs=[y],\n name=\"test_maxunpool_export_without_output_shape\",\n)" } ] }, { "name": "MaxUnpool", "module": "ai.onnx", "version": 22, "description": "MaxUnpool essentially computes the partial inverse of the MaxPool op.\n The input information to this op is typically 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 corresponding\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 corresponding\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(bfloat16)", "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], [7, 8]]]], dtype=np.float32)\nxI = np.array([[[[5, 7], [13, 15]]]], dtype=np.int64)\noutput_shape = np.array((1, 1, 5, 5), dtype=np.int64)\ny = np.array(\n [\n [\n [\n [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],\n ]\n ]\n ],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[xT, xI, output_shape],\n outputs=[y],\n name=\"test_maxunpool_export_with_output_shape\",\n)" }, { "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], [3, 4]]]], dtype=np.float32)\nxI = np.array([[[[5, 7], [13, 15]]]], dtype=np.int64)\ny = np.array(\n [[[[0, 0, 0, 0], [0, 1, 0, 2], [0, 0, 0, 0], [0, 3, 0, 4]]]],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[xT, xI],\n outputs=[y],\n name=\"test_maxunpool_export_without_output_shape\",\n)" } ] }, { "name": "MaxUnpool", "module": "com.ms.internal.nhwc", "version": 9, "description": "MaxUnpool essentially computes the partial inverse of the MaxPool op.\n The input information to this op is typically 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 corresponding\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 corresponding\n pooling op that the unpooling op is trying to invert.\n", "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ] }, { "name": "MaxUnpool", "module": "com.ms.internal.nhwc", "version": 11, "description": "MaxUnpool essentially computes the partial inverse of the MaxPool op.\n The input information to this op is typically 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 corresponding\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 corresponding\n pooling op that the unpooling op is trying to invert.\n", "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "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)" ] } ] }, { "name": "MaxpoolWithMask", "module": "com.microsoft", "version": 1, "description": "For internal use.", "attributes": [ { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "name": "kernel_shape", "type": "int64[]", "required": false }, { "name": "pads", "type": "int64[]", "required": false }, { "name": "storage_order", "type": "int64", "required": false }, { "name": "strides", "type": "int64[]", "required": false } ], "inputs": [ { "name": "X", "type": "T" }, { "name": "M", "type": "tensor(int32)", "description": "mask" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "T" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input0 and output types to float tensors", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "Mean", "module": "ai.onnx", "version": 1, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_mean_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Mean\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], name=\"test_mean_one_input\")\n\nresult = np.divide(np.add(data_0, data_1), 2.0)\nnode = onnx.helper.make_node(\n \"Mean\",\n inputs=[\"data_0\", \"data_1\"],\n outputs=[\"result\"],\n)\nexpect(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_mean_two_inputs\"\n)" } ] }, { "name": "Mean", "module": "ai.onnx", "version": 6, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_mean_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Mean\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], name=\"test_mean_one_input\")\n\nresult = np.divide(np.add(data_0, data_1), 2.0)\nnode = onnx.helper.make_node(\n \"Mean\",\n inputs=[\"data_0\", \"data_1\"],\n outputs=[\"result\"],\n)\nexpect(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_mean_two_inputs\"\n)" } ] }, { "name": "Mean", "module": "ai.onnx", "version": 8, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_mean_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Mean\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], name=\"test_mean_one_input\")\n\nresult = np.divide(np.add(data_0, data_1), 2.0)\nnode = onnx.helper.make_node(\n \"Mean\",\n inputs=[\"data_0\", \"data_1\"],\n outputs=[\"result\"],\n)\nexpect(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_mean_two_inputs\"\n)" } ] }, { "name": "Mean", "module": "ai.onnx", "version": 13, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_mean_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Mean\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], name=\"test_mean_one_input\")\n\nresult = np.divide(np.add(data_0, data_1), 2.0)\nnode = onnx.helper.make_node(\n \"Mean\",\n inputs=[\"data_0\", \"data_1\"],\n outputs=[\"result\"],\n)\nexpect(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_mean_two_inputs\"\n)" } ] }, { "name": "MeanVarianceNormalization", "module": "ai.onnx", "version": 1, "description": "Perform mean variance normalization.", "attributes": [ { "name": "across_channels", "type": "int64", "required": false, "description": "If 1, mean and variance are computed across channels. Default is 0." }, { "name": "normalize_variance", "type": "int64", "required": false, "default": 1, "description": "If 0, normalize the mean only. Default is 1." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor of shape [N,C,H,W]" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "Result, has same shape and type 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)" ] } ] }, { "name": "MeanVarianceNormalization", "module": "ai.onnx", "version": 9, "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 calculate 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\", inputs=[\"X\"], outputs=[\"Y\"]\n)\n\ninput_data = np.array(\n [\n [\n [[0.8439683], [0.5665144], [0.05836735]],\n [[0.02916367], [0.12964272], [0.5060197]],\n [[0.79538304], [0.9411346], [0.9546573]],\n ],\n [\n [[0.17730942], [0.46192095], [0.26480448]],\n [[0.6746842], [0.01665257], [0.62473077]],\n [[0.9240844], [0.9722341], [0.11965699]],\n ],\n [\n [[0.41356155], [0.9129373], [0.59330076]],\n [[0.81929934], [0.7862604], [0.11799799]],\n [[0.69248444], [0.54119414], [0.07513223]],\n ],\n ],\n dtype=np.float32,\n)\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], name=\"test_mvn\")" } ] }, { "name": "MeanVarianceNormalization", "module": "ai.onnx", "version": 13, "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 calculate 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\", inputs=[\"X\"], outputs=[\"Y\"]\n)\n\ninput_data = np.array(\n [\n [\n [[0.8439683], [0.5665144], [0.05836735]],\n [[0.02916367], [0.12964272], [0.5060197]],\n [[0.79538304], [0.9411346], [0.9546573]],\n ],\n [\n [[0.17730942], [0.46192095], [0.26480448]],\n [[0.6746842], [0.01665257], [0.62473077]],\n [[0.9240844], [0.9722341], [0.11965699]],\n ],\n [\n [[0.41356155], [0.9129373], [0.59330076]],\n [[0.81929934], [0.7862604], [0.11799799]],\n [[0.69248444], [0.54119414], [0.07513223]],\n ],\n ],\n dtype=np.float32,\n)\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], name=\"test_mvn\")" } ] }, { "name": "MelWeightMatrix", "module": "ai.onnx", "version": 17, "description": "Generate a MelWeightMatrix that can be used to re-weight a Tensor containing a linearly sampled frequency spectra (from DFT or STFT) into num_mel_bins frequency information based on the [lower_edge_hertz, upper_edge_hertz] range on the mel scale.\nThis function defines the mel scale in terms of a frequency in hertz according to the following formula:\n\n mel(f) = 2595 * log10(1 + f/700)\n\nIn the returned matrix, all the triangles (filterbanks) have a peak value of 1.0.\n\nThe returned MelWeightMatrix can be used to right-multiply a spectrogram S of shape [frames, num_spectrogram_bins] of linear scale spectrum values (e.g. STFT magnitudes) to generate a \"mel spectrogram\" M of shape [frames, num_mel_bins].\n", "attributes": [ { "name": "output_datatype", "type": "int64", "required": false, "default": 1, "description": "The data type of the output tensor. Strictly must be one of the values from DataType enum in TensorProto whose values correspond to T3. The default value is 1 = FLOAT. " } ], "inputs": [ { "name": "num_mel_bins", "type": "T1", "description": "The number of bands in the mel spectrum." }, { "name": "dft_length", "type": "T1", "description": "The size of the original DFT. The size of the original DFT is used to infer the size of the onesided DFT, which is understood to be floor(dft_length/2) + 1, i.e. the spectrogram only contains the nonredundant DFT bins." }, { "name": "sample_rate", "type": "T1", "description": "Samples per second of the input signal used to create the spectrogram. Used to figure out the frequencies corresponding to each spectrogram bin, which dictates how they are mapped into the mel scale." }, { "name": "lower_edge_hertz", "type": "T2", "description": "Lower bound on the frequencies to be included in the mel spectrum. This corresponds to the lower edge of the lowest triangular band." }, { "name": "upper_edge_hertz", "type": "T2", "description": "The desired top edge of the highest frequency band." } ], "min_input": 5, "max_input": 5, "outputs": [ { "name": "output", "type": "T3", "description": "The Mel Weight Matrix. The output has the shape: [floor(dft_length/2) + 1][num_mel_bins]." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain to integer tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] }, { "description": "Constrain to float tensors", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(double)", "tensor(bfloat16)" ] }, { "description": "Constrain to any numerical types.", "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(bfloat16)" ] } ], "examples": [ { "summary": "melweightmatrix", "code": "node = onnx.helper.make_node(\n \"MelWeightMatrix\",\n inputs=[\n \"num_mel_bins\",\n \"dft_length\",\n \"sample_rate\",\n \"lower_edge_hertz\",\n \"upper_edge_hertz\",\n ],\n outputs=[\"output\"],\n)\n\nnum_mel_bins = np.int32(8)\ndft_length = np.int32(16)\nsample_rate = np.int32(8192)\nlower_edge_hertz = np.float32(0)\nupper_edge_hertz = np.float32(8192 / 2)\n\nnum_spectrogram_bins = dft_length // 2 + 1\nfrequency_bins = np.arange(0, num_mel_bins + 2)\n\nlow_frequency_mel = 2595 * np.log10(1 + lower_edge_hertz / 700)\nhigh_frequency_mel = 2595 * np.log10(1 + upper_edge_hertz / 700)\nmel_step = (high_frequency_mel - low_frequency_mel) / frequency_bins.shape[0]\n\nfrequency_bins = frequency_bins * mel_step + low_frequency_mel\nfrequency_bins = 700 * (np.power(10, (frequency_bins / 2595)) - 1)\nfrequency_bins = ((dft_length + 1) * frequency_bins) // sample_rate\nfrequency_bins = frequency_bins.astype(int)\n\noutput = np.zeros((num_spectrogram_bins, num_mel_bins))\noutput.flags.writeable = True\n\nfor i in range(num_mel_bins):\n lower_frequency_value = frequency_bins[i] # left\n center_frequency_point = frequency_bins[i + 1] # center\n higher_frequency_point = frequency_bins[i + 2] # right\n low_to_center = center_frequency_point - lower_frequency_value\n if low_to_center == 0:\n output[center_frequency_point, i] = 1\n else:\n for j in range(lower_frequency_value, center_frequency_point + 1):\n output[j, i] = float(j - lower_frequency_value) / float(\n low_to_center\n )\n center_to_high = higher_frequency_point - center_frequency_point\n if center_to_high > 0:\n for j in range(center_frequency_point, higher_frequency_point):\n output[j, i] = float(higher_frequency_point - j) / float(\n center_to_high\n )\n\n# Expected output\n# 1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,\n# 0.000000, 0.000000, 1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000,\n# 0.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000,\n# 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000,\n# 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000,\n# 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 1.000000,\n# 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,\n# 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,\n# 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,\noutput = output.astype(np.float32)\nexpect(\n node,\n inputs=[\n num_mel_bins,\n dft_length,\n sample_rate,\n lower_edge_hertz,\n upper_edge_hertz,\n ],\n outputs=[output],\n name=\"test_melweightmatrix\",\n)" } ] }, { "name": "MemcpyFromHost", "module": "ai.onnx", "version": 1, "description": "Internal copy node\n", "inputs": [ { "name": "X", "type": "T", "description": "input" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "output" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain to all fixed size tensor and sequence types. If the dtype attribute is not provided this must be a valid output 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(bool)", "tensor(complex64)", "tensor(complex128)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "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(bool))", "seq(tensor(complex64))", "seq(tensor(complex128))", "seq(tensor(bfloat16))" ] } ] }, { "name": "MemcpyToHost", "module": "ai.onnx", "version": 1, "description": "Internal copy node\n", "inputs": [ { "name": "X", "type": "T", "description": "input" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "output" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain to all fixed size tensor and sequence types. If the dtype attribute is not provided this must be a valid output 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(bool)", "tensor(complex64)", "tensor(complex128)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "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(bool))", "seq(tensor(complex64))", "seq(tensor(complex128))", "seq(tensor(bfloat16))" ] } ] }, { "name": "Min", "module": "ai.onnx", "version": 1, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_min_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Min\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_min_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_min_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "Min", "module": "ai.onnx", "version": 6, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_min_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Min\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_min_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_min_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "Min", "module": "ai.onnx", "version": 8, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_min_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Min\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_min_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_min_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "Min", "module": "ai.onnx", "version": 12, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_min_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Min\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_min_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_min_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "Min", "module": "ai.onnx", "version": 13, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_min_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Min\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_min_two_inputs\"\n)" }, { "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(\n node,\n inputs=[data_0, data_1],\n outputs=[result],\n name=f\"test_min_{np.dtype(op_dtype).name}\",\n )" } ] }, { "name": "Mish", "module": "ai.onnx", "version": 18, "description": "Mish: A Self Regularized Non-Monotonic Neural Activation Function.\n\nPerform the linear unit element-wise on the input tensor X using formula:\n\n```\nmish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + e^{x}))\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 X and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "mish", "code": "node = onnx.helper.make_node(\"Mish\", inputs=[\"X\"], outputs=[\"Y\"])\n\ninput_data = np.linspace(-10, 10, 10000, dtype=np.float32)\n\n# Calculate expected output data\nexpected_output = input_data * np.tanh(np.log1p(np.exp(input_data)))\n\nexpect(node, inputs=[input_data], outputs=[expected_output], name=\"test_mish\")" } ] }, { "name": "Mish", "module": "ai.onnx", "version": 22, "description": "Mish: A Self Regularized Non-Monotonic Neural Activation Function.\n\nPerform the linear unit element-wise on the input tensor X using formula:\n\n```\nmish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + e^{x}))\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 X and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "mish", "code": "node = onnx.helper.make_node(\"Mish\", inputs=[\"X\"], outputs=[\"Y\"])\n\ninput_data = np.linspace(-10, 10, 10000, dtype=np.float32)\n\n# Calculate expected output data\nexpected_output = input_data * np.tanh(np.log1p(np.exp(input_data)))\n\nexpect(node, inputs=[input_data], outputs=[expected_output], name=\"test_mish\")" } ] }, { "name": "MoE", "module": "com.microsoft", "version": 1, "description": "Mixture of experts. Examples: Switch transformer(https://arxiv.org/pdf/2101.03961.pdf) use top 1,\n GLaM(https://arxiv.org/abs/2112.06905) activates top 2 FFN, Vision MOE(https://arxiv.org/pdf/2106.05974.pdf)\n usually uses top 32 experts and Mixtral(https://huggingface.co/blog/mixtral).\n\n The SwiGLU (Swish-Gated Linear Unit) activation function is like:\n g = xW + b\n l = xV + c\n G = clamp(g, max=limit)\n L = clamp(l, min=-limit, max=limit)\n swiglu = G * sigmoid(alpha * G) * (L + beta)\n where x is the input, W and V are weight matrices, b and c are bias vectors, and alpha, beta and limit are constant float parameters.\n When swiglu_fusion=0, two GEMMs are not fused, and they are FC1 and FC3 in the inputs.\n When swiglu_fusion=1, two GEMMs are fused so that g and l are computed in a single GEMM (FC1), and g and l are interleaved on each row of size 2 * inter_size.\n When swiglu_fusion=2, two GEMMs are fused, and g and l are concatenated on each row.\n ", "attributes": [ { "name": "activation_alpha", "type": "float32", "required": false, "default": 1.0, "description": "Alpha parameter used in activation function." }, { "name": "activation_beta", "type": "float32", "required": false, "description": "Beta parameter used in activation function." }, { "name": "activation_type", "type": "string", "required": false, "default": "relu", "description": "Activation function to use. Choose from relu, gelu, silu, swiglu and identity. Default is relu" }, { "name": "k", "type": "int64", "required": false, "default": 1, "description": "Number of top experts to select from expert pool" }, { "name": "normalize_routing_weights", "type": "int64", "required": false, "description": "Whether to normalize routing weights" }, { "name": "swiglu_fusion", "type": "int64", "required": false, "description": "0: not fused, 1: fused and interleaved. 2: fused and not interleaved." }, { "name": "swiglu_limit", "type": "float32", "required": false, "description": "The limit used to clamp in SwiGLU. No clamp when limit is not provided." }, { "name": "use_sparse_mixer", "type": "int64", "required": false, "description": "Whether to use sparse mixer" } ], "inputs": [ { "name": "input", "type": "T", "description": "2D input tensor with shape (num_tokens, hidden_size) or 3D input tensor with shape (batch_size, sequence_length, hidden_size)" }, { "name": "router_probs", "type": "T", "description": "2D input tensor with shape (num_tokens, num_experts)" }, { "name": "fc1_experts_weights", "type": "T", "description": "3D input tensor with shape (num_experts, fusion_size * inter_size, hidden_size), where fusion_size is 2 for fused swiglu, and 1 otherwise" }, { "name": "fc1_experts_bias", "type": "T", "option": "optional", "description": "2D optional input tensor with shape (num_experts, fusion_size * inter_size)" }, { "name": "fc2_experts_weights", "type": "T", "description": "3D input tensor with shape (num_experts, hidden_size, inter_size)" }, { "name": "fc2_experts_bias", "type": "T", "option": "optional", "description": "2D optional input tensor with shape (num_experts, hidden_size)" }, { "name": "fc3_experts_weights", "type": "T", "option": "optional", "description": "3D optional input tensor with shape (num_experts, inter_size, hidden_size)" }, { "name": "fc3_experts_bias", "type": "T", "option": "optional", "description": "2D optional input tensor with shape (num_experts, inter_size)" } ], "min_input": 5, "max_input": 8, "outputs": [ { "name": "output", "type": "T", "description": "2D input tensor with shape (num_tokens, hidden_size) or 3D input tensor with shape (batch_size, sequence_length, hidden_size)" } ], "min_output": 1, "max_output": 1, "inputs_range": "5 - 8", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] } ] }, { "name": "Mod", "module": "ai.onnx", "version": 10, "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], name=\"test_mod_broadcast\")" }, { "summary": "mod_int64_fmod", "code": "node = onnx.helper.make_node(\"Mod\", inputs=[\"x\", \"y\"], outputs=[\"z\"], fmod=1)\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], name=\"test_mod_int64_fmod\")" }, { "summary": "mod_mixed_sign_float16", "code": "node = onnx.helper.make_node(\"Mod\", inputs=[\"x\", \"y\"], outputs=[\"z\"], fmod=1)\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(\n x, y\n) # expected output [-0.10156, 0.3984 , 5. , 0.10156, -0.3984 , 3.]\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mod_mixed_sign_float16\")" }, { "summary": "mod_mixed_sign_float32", "code": "node = onnx.helper.make_node(\"Mod\", inputs=[\"x\", \"y\"], outputs=[\"z\"], fmod=1)\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(\n x, y\n) # expected output [-0.10000038, 0.39999962, 5. , 0.10000038, -0.39999962, 3.]\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mod_mixed_sign_float32\")" }, { "summary": "mod_mixed_sign_float64", "code": "node = onnx.helper.make_node(\"Mod\", inputs=[\"x\", \"y\"], outputs=[\"z\"], fmod=1)\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], 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], 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], 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], 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], 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], 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], 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], 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], name=\"test_mod_uint8\")" } ] }, { "name": "Mod", "module": "ai.onnx", "version": 13, "description": "Performs an element-wise binary modulo operation.\nThe semantics and supported data types depend on the value of the `fmod` attribute which must be `0` (default), or `1`.\n\nIf the `fmod` attribute is set to `0`, `T` is constrained to integer data types and the semantics follow that of the Python `%`-operator.\nThe sign of the result is that of the divisor.\n\nIf `fmod` is set to `1`, the behavior of this operator follows that of the `fmod` function in C and `T` is constrained to floating point data types.\nThe result of this operator is the remainder of the division operation `x / y` where `x` and `y` are respective elements of `A` and `B`. The result is exactly the value `x - n * y`, where `n` is `x / y` with its fractional part truncated.\nThe returned value has the same sign as `x` (except if `x` is `-0`) and is less or equal to `|y|` in magnitude.\nThe following special cases apply when `fmod` is set to `1`:\n- If `x` is `-0` and `y` is greater than zero, either `+0` or `-0` may be returned.\n- If `x` is `\u00b1\u221e` and `y` is not `NaN`, `NaN` is returned.\n- If `y` is `\u00b10` and `x` is not `NaN`, `NaN` should be returned.\n- If `y` is `\u00b1\u221e` and `x` is finite, `x` is returned.\n- If either argument is `NaN`, `NaN` is returned.\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", "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], name=\"test_mod_broadcast\")" }, { "summary": "mod_int64_fmod", "code": "node = onnx.helper.make_node(\"Mod\", inputs=[\"x\", \"y\"], outputs=[\"z\"], fmod=1)\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], name=\"test_mod_int64_fmod\")" }, { "summary": "mod_mixed_sign_float16", "code": "node = onnx.helper.make_node(\"Mod\", inputs=[\"x\", \"y\"], outputs=[\"z\"], fmod=1)\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(\n x, y\n) # expected output [-0.10156, 0.3984 , 5. , 0.10156, -0.3984 , 3.]\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mod_mixed_sign_float16\")" }, { "summary": "mod_mixed_sign_float32", "code": "node = onnx.helper.make_node(\"Mod\", inputs=[\"x\", \"y\"], outputs=[\"z\"], fmod=1)\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(\n x, y\n) # expected output [-0.10000038, 0.39999962, 5. , 0.10000038, -0.39999962, 3.]\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mod_mixed_sign_float32\")" }, { "summary": "mod_mixed_sign_float64", "code": "node = onnx.helper.make_node(\"Mod\", inputs=[\"x\", \"y\"], outputs=[\"z\"], fmod=1)\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], 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], 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], 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], 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], 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], 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], 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], 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], name=\"test_mod_uint8\")" } ] }, { "name": "Momentum", "module": "ai.onnx.preview.training", "version": 1, "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(\n \"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, norm_coefficient, alpha, beta)\n\n# Check results.\nexpect(\n node,\n inputs=[r, t, x, g, v],\n outputs=[x_new, v_new],\n name=\"test_momentum\",\n opset_imports=[\n onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)\n ],\n)" }, { "summary": "momentum_multiple", "code": "# Define operator attributes.\nnorm_coefficient = 0.001\nalpha = 0.95\nbeta = 0.85\n\nnode = onnx.helper.make_node(\n \"Momentum\",\n inputs=[\"R\", \"T\", \"X1\", \"X2\", \"G1\", \"G2\", \"H1\", \"H2\"],\n outputs=[\"X1_new\", \"X2_new\", \"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, norm_coefficient, alpha, beta)\nx2_new, v2_new = apply_momentum(r, t, x2, g2, v2, norm_coefficient, alpha, beta)\n\n# Check results.\nexpect(\n node,\n inputs=[r, t, x1, x2, g1, g2, v1, v2],\n outputs=[x1_new, x2_new, v1_new, v2_new],\n name=\"test_momentum_multiple\",\n opset_imports=[\n onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)\n ],\n)" }, { "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(\n \"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, norm_coefficient, alpha, beta)\n\n# Check results.\nexpect(\n node,\n inputs=[r, t, x, g, v],\n outputs=[x_new, v_new],\n name=\"test_nesterov_momentum\",\n opset_imports=[\n onnx.helper.make_opsetid(AI_ONNX_PREVIEW_TRAINING_DOMAIN, 1)\n ],\n)" } ] }, { "name": "Mul", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_mul\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int16\")\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], name=\"test_mul_uint8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint16\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint32\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint64\")" }, { "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], name=\"test_mul_bcast\")" } ] }, { "name": "Mul", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_mul\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int16\")\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], name=\"test_mul_uint8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint16\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint32\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint64\")" }, { "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], name=\"test_mul_bcast\")" } ] }, { "name": "Mul", "module": "ai.onnx", "version": 7, "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], 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], name=\"test_mul\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int16\")\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], name=\"test_mul_uint8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint16\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint32\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint64\")" }, { "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], name=\"test_mul_bcast\")" } ] }, { "name": "Mul", "module": "ai.onnx", "version": 13, "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], 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], name=\"test_mul\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int16\")\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], name=\"test_mul_uint8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint16\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint32\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint64\")" }, { "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], name=\"test_mul_bcast\")" } ] }, { "name": "Mul", "module": "ai.onnx", "version": 14, "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], 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], name=\"test_mul\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int8)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.int16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_int16\")\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], name=\"test_mul_uint8\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint16)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint16\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint32)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint32\")\n\nx = np.random.randint(4, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(24, size=(3, 4, 5), dtype=np.uint64)\nz = x * y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_mul_uint64\")" }, { "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], name=\"test_mul_bcast\")" } ] }, { "name": "MulInteger", "module": "com.microsoft", "version": 1, "description": "Performs element-wise binary quantized multiplication (with Numpy-style broadcasting support).\n\"This operator supports **multidirectional (i.e., Numpy-style) broadcasting**\"\nThe output of this op is the int32 accumulated result of the mul operation\n\n```\nC (int32) = (A - A_zero_point) * (B - B_zero_point)\n```\n\n", "inputs": [ { "name": "A", "type": "T", "description": "First operand." }, { "name": "A_zero_point", "type": "T", "option": "optional", "description": "Input A zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "B", "type": "T", "description": "Second operand." }, { "name": "B_zero_point", "type": "T", "option": "optional", "description": "Input B zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." } ], "min_input": 3, "max_input": 4, "outputs": [ { "name": "C", "type": "T1", "description": "Constrain output to 32 bit tensor" } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - 4", "type_constraints": [ { "description": "Constrain input types to 8 bit signed and unsigned tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] }, { "description": "Constrain output types to 32 bit tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "MultiHeadAttention", "module": "com.microsoft", "version": 1, "description": "Multi-Head Self/Cross Attention. Bias from input projection is included.\n\nThe key padding mask is optional. When its shape is (batch_size, kv_sequence_length), value 0\nmeans padding or 1 otherwise. When key has right-side padding, its shape could be (batch_size): it is actual length of\neach key sequence excluding paddings.\n", "attributes": [ { "name": "mask_filter_value", "type": "float32", "required": false, "description": "The value to be filled in the attention mask. Default value is -10000.0f" }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1/sqrt(head_size)" }, { "name": "unidirectional", "type": "int64", "required": false, "description": "Whether every token can only attend to previous tokens. Default value is 0." } ], "inputs": [ { "name": "query", "type": "T", "description": "Query with shape (batch_size, sequence_length, hidden_size), or packed QKV with shape (batch_size, kv_sequence_length, num_heads, 3, head_size)" }, { "name": "key", "type": "T", "option": "optional", "description": "Key with shape (batch_size, kv_sequence_length, hidden_size), or packed KV with shape (batch_size, kv_sequence_length, num_heads, 2, head_size), or past_key with shape (batch_size, num_heads, kv_sequence_length, head_size)" }, { "name": "value", "type": "T", "option": "optional", "description": "Value with shape (batch_size, kv_sequence_length, v_hidden_size), or past_value with shape (batch_size, num_heads, kv_sequence_length, head_size)" }, { "name": "bias", "type": "T", "option": "optional", "description": "Bias tensor with shape (hidden_size + hidden_size + v_hidden_size) from input projection" }, { "name": "key_padding_mask", "type": "M", "option": "optional", "description": "Key padding mask with shape (batch_size), (3 * batch_size + 2), (batch_size, kv_sequence_length), (batch_size, total_sequence_length), or (batch_size, sequence_length, total_sequence_length)" }, { "name": "attention_bias", "type": "T", "option": "optional", "description": "bias added to QxK' with shape (batch_size or 1, num_heads or 1, sequence_length, total_sequence_length)" }, { "name": "past_key", "type": "T", "option": "optional", "description": "past state for key with shape (batch_size, num_heads, past_sequence_length, head_size) or (batch_size, num_heads, max_sequence_length, head_size) when buffer sharing is used" }, { "name": "past_value", "type": "T", "option": "optional", "description": "past state for value with shape (batch_size, num_heads, past_sequence_length, head_size) or (batch_size, num_heads, max_sequence_length, head_size) when buffer sharing is used" }, { "name": "past_sequence_length", "type": "M", "option": "optional", "description": "The past_sequence_length buffer sharing is used with" }, { "name": "cache_indirection", "type": "M", "option": "optional", "description": "A buffer of shape [batch_size, beam_width, max_sequence_length] where an [i, j, k] entry specifieswhich beam the 'k' th token came from for the 'j' th beam for batch 'i' in the current iteration" } ], "min_input": 1, "max_input": 10, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, v_hidden_size)" }, { "name": "present_key", "type": "T", "option": "optional", "description": "present state for key with shape (batch_size, num_heads, total_sequence_length, head_size) or (batch_size, num_heads, max_sequence_length, head_size) when buffer sharing is used" }, { "name": "present_value", "type": "T", "option": "optional", "description": "present state for value with shape (batch_size, num_heads, total_sequence_length, head_size) or (batch_size, num_heads, max_sequence_length, head_size) when buffer sharing is used" }, { "name": "qk", "type": "QK", "option": "optional", "description": "normalized Q * K, of shape (batch_size, num_heads, sequence_length, total_sequence_length). " } ], "min_output": 1, "max_output": 4, "inputs_range": "1 - 10", "outputs_range": "1 - 4", "type_constraints": [ { "description": "Constrain input and output to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain QK output to float32 or float16 tensors, independent of input type or output type.", "type_param_str": "QK", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain mask to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "MultilevelCropAndResize_TRT", "module": "ai.onnx", "version": 1, "description": "Multilevel Crop and Resize TensorRT Plugin.", "attributes": [ { "name": "image_size", "type": "int64[]", "required": true, "description": "Image size." }, { "name": "plugin_version", "type": "string", "required": true, "description": "Version number of the TRT plugin." }, { "name": "pooled_size", "type": "int64", "required": true, "description": "Pooled size." } ], "inputs": [ { "name": "boxes", "type": "T", "description": "The boxes input tensor." }, { "name": "feature_map_0", "type": "T", "description": "The first feature map input tensor." }, { "name": "feature_map_1", "type": "T", "description": "The second feature map input tensor." }, { "name": "feature_map_2", "type": "T", "description": "The third feature map input tensor." }, { "name": "feature_map_3", "type": "T", "description": "The fourth feature map input tensor." } ], "min_input": 5, "max_input": 5, "outputs": [ { "name": "patches", "type": "T", "description": "The cropped patches 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(float)" ] } ] }, { "name": "Multinomial", "module": "ai.onnx", "version": 7, "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": "Multinomial", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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": "MurmurHash3", "module": "com.microsoft", "version": 1, "description": "The underlying implementation is MurmurHash3_x86_32 generating low latency 32bits hash suitable for implementing lookup tables, Bloom filters, count min sketch or feature hashing.", "attributes": [ { "name": "positive", "type": "int64", "required": false, "default": 1, "description": "If value is 1, output type is uint32_t, else int32_t. Default value is 1." }, { "name": "seed", "type": "int64", "required": false, "description": "Seed for the hashing algorithm, unsigned 32-bit integer, default to 0." } ], "inputs": [ { "name": "X", "type": "T1", "description": "An input tensor to hash." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T2", "description": "32-bit hash value." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input type to unsigned or signed 32-bit integer tensor, or string tensor. It should be utf-8 encoded if using unicode.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(uint32)", "tensor(int32)", "tensor(uint64)", "tensor(int64)", "tensor(float)", "tensor(double)", "tensor(string)" ] }, { "description": "Constrain output type to unsigned and signed 32-bit integer tensor.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(uint32)", "tensor(int32)" ] } ] }, { "name": "NGramRepeatBlock", "module": "com.microsoft", "version": 1, "description": "Enforce no repetition of n-grams. Scores are set to `-inf` for tokens that form a repeated n-gram if added to the back of the input_ids.\n", "attributes": [ { "name": "ngram_size", "type": "int64", "required": true, "description": "The NGram size." } ], "inputs": [ { "name": "input_ids", "type": "Tid", "description": "2D input tensor with shape (batch_size, sequence_length)" }, { "name": "scores", "type": "T", "description": "2D input tensor with shape (batch_size, vocab_size)" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "scores_out", "type": "T", "description": "2D output tensor with shape (batch_size, vocab_size)" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain indices to integer types", "type_param_str": "Tid", "allowed_type_strs": [ "tensor(int64)" ] }, { "description": "Constrain scores input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "Neg", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_neg\")" } ] }, { "name": "Neg", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_neg\")" } ] }, { "name": "Neg", "module": "ai.onnx", "version": 13, "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], 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], name=\"test_neg\")" } ] }, { "name": "NegativeLogLikelihoodLoss", "module": "ai.onnx", "version": 12, "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NC\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_ii\",\n)" }, { "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)\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(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_mean_weight_negative_ii\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_weight\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_weight_ii\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2\",\n)" }, { "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(\n input, target, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_no_weight_reduction_mean_ii\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_reduction_mean\",\n)" }, { "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)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_reduction_sum\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight_reduction_mean\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight_reduction_sum\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight_reduction_sum_ii\",\n)" }, { "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)\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(\n np.int64\n)\ntarget[0][0][0][0] = -5\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3_none_no_weight_negative_ii\",\n)" }, { "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)\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)\ntarget[0] = 10\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3_sum_weight_high_ii\",\n)" }, { "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)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3d4d5_mean_weight\",\n)" }, { "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)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3d4d5_none_no_weight\",\n)" } ] }, { "name": "NegativeLogLikelihoodLoss", "module": "ai.onnx", "version": 13, "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```\nloss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k].\n```\n\nWhen an optional \"weight\" is provided, the sample loss is calculated as:\n\n```\nloss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k] * weight[c].\n```\n\nloss is zero for the case when target-value equals ignore_index.\n\n```\nloss[n][d_1][d_2]...[d_k] = 0, when target[n][d_1][d_2]...[d_k] = ignore_index\n```\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```\nmean(loss), if \"weight\" is not provided,\n```\n\nor if weight is provided,\n\n```\nsum(loss) / sum(weight[target[n][d_1][d_2]...[d_k]]]), for all samples.\n```\n\nIf \"reduction\" attribute is set to \"sum\", the output is a scalar: `sum(loss)`.\n\nSee also https://pytorch.org/docs/stable/nn.html#torch.nn.NLLLoss.\n\nExample 1:\n\n```\n// negative log likelihood loss, \"none\" reduction\nN, C, d1 = 2, 3, 2\ninput = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\ntarget = [[2, 1], [0, 2]]\n\nloss = np.zeros((N, d1))\nfor 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```\n\nExample 2:\n\n```\n// weighted negative log likelihood loss, sum reduction\nN, C, d1 = 2, 3, 2\ninput = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\ntarget = [[2, 1], [0, 2]]\nweight = [0.2, 0.3, 0.1]\nloss = np.zeros((N, d1))\nfor 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\nloss = np.sum(loss)\n// print(loss)\n// -1.1\n```\n\nExample 3:\n\n```\n// weighted negative log likelihood loss, mean reduction\nN, C, d1 = 2, 3, 2\ninput = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\ntarget = [[2, 1], [0, 2]]\nweight = [0.2, 0.3, 0.1]\nloss = np.zeros((N, d1))\nweight_total = 0\nfor 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\nloss = np.sum(loss) / weight_total\n// print(loss)\n// -1.57\n```\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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NC\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_ii\",\n)" }, { "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)\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(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_mean_weight_negative_ii\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_weight\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_weight_ii\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2\",\n)" }, { "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(\n input, target, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_no_weight_reduction_mean_ii\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_reduction_mean\",\n)" }, { "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)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_reduction_sum\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight_reduction_mean\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight_reduction_sum\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight_reduction_sum_ii\",\n)" }, { "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)\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(\n np.int64\n)\ntarget[0][0][0][0] = -5\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3_none_no_weight_negative_ii\",\n)" }, { "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)\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)\ntarget[0] = 10\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3_sum_weight_high_ii\",\n)" }, { "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)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3d4d5_mean_weight\",\n)" }, { "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)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3d4d5_none_no_weight\",\n)" } ] }, { "name": "NegativeLogLikelihoodLoss", "module": "ai.onnx", "version": 22, "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```\nloss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k].\n```\n\nWhen an optional \"weight\" is provided, the sample loss is calculated as:\n\n```\nloss[n][d_1][d_2]...[d_k] = -input[n][c][d_1][d_2]...[d_k] * weight[c].\n```\n\nloss is zero for the case when target-value equals ignore_index.\n\n```\nloss[n][d_1][d_2]...[d_k] = 0, when target[n][d_1][d_2]...[d_k] = ignore_index\n```\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```\nmean(loss), if \"weight\" is not provided,\n```\n\nor if weight is provided,\n\n```\nsum(loss) / sum(weight[target[n][d_1][d_2]...[d_k]]]), for all samples.\n```\n\nIf \"reduction\" attribute is set to \"sum\", the output is a scalar: `sum(loss)`.\n\nSee also https://pytorch.org/docs/stable/nn.html#torch.nn.NLLLoss.\n\nExample 1:\n\n```\n// negative log likelihood loss, \"none\" reduction\nN, C, d1 = 2, 3, 2\ninput = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\ntarget = [[2, 1], [0, 2]]\n\nloss = np.zeros((N, d1))\nfor 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```\n\nExample 2:\n\n```\n// weighted negative log likelihood loss, sum reduction\nN, C, d1 = 2, 3, 2\ninput = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\ntarget = [[2, 1], [0, 2]]\nweight = [0.2, 0.3, 0.1]\nloss = np.zeros((N, d1))\nfor 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\nloss = np.sum(loss)\n// print(loss)\n// -1.1\n```\n\nExample 3:\n\n```\n// weighted negative log likelihood loss, mean reduction\nN, C, d1 = 2, 3, 2\ninput = [[[1.0, 2.0], [2.0, 2.0], [3.0, 2.0]],\n [[0.0, 1.0], [2.0, 2.0], [1.0, 2]]]\ntarget = [[2, 1], [0, 2]]\nweight = [0.2, 0.3, 0.1]\nloss = np.zeros((N, d1))\nweight_total = 0\nfor 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\nloss = np.sum(loss) / weight_total\n// print(loss)\n// -1.57\n```\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(bfloat16)", "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NC\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_ii\",\n)" }, { "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)\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(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_mean_weight_negative_ii\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_weight\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1_weight_ii\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2\",\n)" }, { "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(\n input, target, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_no_weight_reduction_mean_ii\",\n)" }, { "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(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_reduction_mean\",\n)" }, { "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)).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, weight=None, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_reduction_sum\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight_reduction_mean\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight_reduction_sum\",\n)" }, { "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(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2_with_weight_reduction_sum_ii\",\n)" }, { "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)\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(\n np.int64\n)\ntarget[0][0][0][0] = -5\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3_none_no_weight_negative_ii\",\n)" }, { "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)\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)\ntarget[0] = 10\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3_sum_weight_high_ii\",\n)" }, { "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)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, weight=weight, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target, weight],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3d4d5_mean_weight\",\n)" }, { "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)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\n\nnegative_log_likelihood_loss = compute_negative_log_likelihood_loss(\n input, target, reduction=reduction\n)\n\nexpect(\n node,\n inputs=[input, target],\n outputs=[negative_log_likelihood_loss],\n name=\"test_nllloss_NCd1d2d3d4d5_none_no_weight\",\n)" } ] }, { "name": "NhwcConv", "module": "com.microsoft", "version": 1, "attributes": [ { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "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 }, { "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)" ] } ] }, { "name": "NhwcFusedConv", "module": "com.microsoft", "version": 1, "description": "NhwcFusedConv is a Conv operator with optional activation and add operators fused in.\n", "attributes": [ { "name": "activation", "type": "string", "required": false }, { "name": "activation_params", "type": "float32[]", "required": false }, { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "name": "dilations", "type": "int64[]", "required": false }, { "name": "group", "type": "int64", "required": false, "default": 1 }, { "name": "kernel_shape", "type": "int64[]", "required": false }, { "name": "pads", "type": "int64[]", "required": false }, { "name": "strides", "type": "int64[]", "required": false } ], "inputs": [ { "name": "X", "type": "T", "description": "Input activation tensor in channels-last layout. For 2D convolution this is [N, H, W, C], where N is batch size, H/W are spatial dimensions, and C is the number of input channels." }, { "name": "W", "type": "T", "description": "Convolution weight tensor in the standard ONNX Conv filter layout [M, C/group, kH, kW], where M is the number of output channels." }, { "name": "B", "type": "T", "option": "optional", "description": "Optional 1D bias tensor of shape [M]." }, { "name": "Z", "type": "T", "option": "optional", "description": "Optional residual/add tensor in the same channels-last layout and shape as the output tensor Y. For 2D convolution this is [N, out_H, out_W, M]." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "Y", "type": "T", "description": "Output tensor in channels-last layout. For 2D convolution this is [N, out_H, out_W, M], where M is the number of output channels." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain input and output types to float tensors", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] } ] }, { "name": "NhwcMaxPool", "module": "com.microsoft", "version": 1, "attributes": [ { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "name": "ceil_mode", "type": "int64", "required": false }, { "name": "dilations", "type": "int64[]", "required": false }, { "name": "kernel_shape", "type": "int64[]", "required": true }, { "name": "pads", "type": "int64[]", "required": false }, { "name": "strides", "type": "int64[]", "required": false } ], "inputs": [ { "name": "x", "type": "T" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "y", "type": "T" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "", "type_param_str": "T", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)" ] } ] }, { "name": "NonMaxSuppression", "module": "ai.onnx", "version": 10, "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.\nBoxes are suppressed if their IOU with a previously selected box is strictly greater than iou_threshold (i.e., boxes with IOU exactly equal to the threshold are kept).\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=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n center_point_box=1,\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_center_point_box_format\",\n)" }, { "summary": "nonmaxsuppression_flipped_coordinates", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_flipped_coordinates\",\n)" }, { "summary": "nonmaxsuppression_identical_boxes", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\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 [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 ]\n).astype(np.float32)\nscores = np.array(\n [[[0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]]]\n).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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_identical_boxes\",\n)" }, { "summary": "nonmaxsuppression_iou_threshold_boundary", "code": "\"\"\"Test boundary condition where IoU exactly equals threshold.\n\nThis test verifies that the comparison is strict (>), not inclusive (>=).\nWhen IoU exactly equals the threshold, boxes should be KEPT, not suppressed.\nThis follows PyTorch's NMS implementation.\n\"\"\"\nnode = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\n# Two boxes with 50% overlap in each dimension\n# box1=[0,0,1,1], box2=[0.5,0.5,1.5,1.5]\n# Intersection area = 0.5 * 0.5 = 0.25\n# Union area = 1.0 + 1.0 - 0.25 = 1.75\n# IoU = 0.25 / 1.75 (exact value computed below as float32)\nboxes = np.array(\n [\n [\n [0.0, 0.0, 1.0, 1.0], # box 0\n [0.5, 0.5, 1.5, 1.5], # box 1 - overlaps box 0\n ]\n ]\n).astype(np.float32)\nscores = np.array([[[0.9, 0.8]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\n# Compute the exact IoU value and use it as threshold\n# This ensures the threshold exactly equals the IoU\nexact_iou = np.float32(0.25 / 1.75)\niou_threshold = np.array([exact_iou]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\n# Both boxes should be selected because IoU == threshold (not > threshold)\nselected_indices = np.array([[0, 0, 0], [0, 0, 1]]).astype(np.int64)\n\nexpect(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_iou_threshold_boundary\",\n)" }, { "summary": "nonmaxsuppression_limit_output_size", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_limit_output_size\",\n)" }, { "summary": "nonmaxsuppression_single_box", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array([[[0.0, 0.0, 1.0, 1.0]]]).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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_single_box\",\n)" }, { "summary": "nonmaxsuppression_suppress_by_IOU", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_suppress_by_IOU\",\n)" }, { "summary": "nonmaxsuppression_suppress_by_IOU_and_scores", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_suppress_by_IOU_and_scores\",\n)" }, { "summary": "nonmaxsuppression_two_batches", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ],\n [\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 ],\n ]\n).astype(np.float32)\nscores = np.array(\n [[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]], [[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]\n).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(\n [[0, 0, 3], [0, 0, 0], [1, 0, 3], [1, 0, 0]]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_two_batches\",\n)" }, { "summary": "nonmaxsuppression_two_classes", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\n).astype(np.float32)\nscores = np.array(\n [[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3], [0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]\n).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(\n [[0, 0, 3], [0, 0, 0], [0, 1, 3], [0, 1, 0]]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_two_classes\",\n)" } ] }, { "name": "NonMaxSuppression", "module": "ai.onnx", "version": 11, "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.\nBoxes are suppressed if their IOU with a previously selected box is strictly greater than iou_threshold (i.e., boxes with IOU exactly equal to the threshold are kept).\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. Boxes with IoU strictly greater than this threshold are suppressed. 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=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n center_point_box=1,\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_center_point_box_format\",\n)" }, { "summary": "nonmaxsuppression_flipped_coordinates", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_flipped_coordinates\",\n)" }, { "summary": "nonmaxsuppression_identical_boxes", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\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 [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 ]\n).astype(np.float32)\nscores = np.array(\n [[[0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]]]\n).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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_identical_boxes\",\n)" }, { "summary": "nonmaxsuppression_iou_threshold_boundary", "code": "\"\"\"Test boundary condition where IoU exactly equals threshold.\n\nThis test verifies that the comparison is strict (>), not inclusive (>=).\nWhen IoU exactly equals the threshold, boxes should be KEPT, not suppressed.\nThis follows PyTorch's NMS implementation.\n\"\"\"\nnode = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\n# Two boxes with 50% overlap in each dimension\n# box1=[0,0,1,1], box2=[0.5,0.5,1.5,1.5]\n# Intersection area = 0.5 * 0.5 = 0.25\n# Union area = 1.0 + 1.0 - 0.25 = 1.75\n# IoU = 0.25 / 1.75 (exact value computed below as float32)\nboxes = np.array(\n [\n [\n [0.0, 0.0, 1.0, 1.0], # box 0\n [0.5, 0.5, 1.5, 1.5], # box 1 - overlaps box 0\n ]\n ]\n).astype(np.float32)\nscores = np.array([[[0.9, 0.8]]]).astype(np.float32)\nmax_output_boxes_per_class = np.array([3]).astype(np.int64)\n# Compute the exact IoU value and use it as threshold\n# This ensures the threshold exactly equals the IoU\nexact_iou = np.float32(0.25 / 1.75)\niou_threshold = np.array([exact_iou]).astype(np.float32)\nscore_threshold = np.array([0.0]).astype(np.float32)\n# Both boxes should be selected because IoU == threshold (not > threshold)\nselected_indices = np.array([[0, 0, 0], [0, 0, 1]]).astype(np.int64)\n\nexpect(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_iou_threshold_boundary\",\n)" }, { "summary": "nonmaxsuppression_limit_output_size", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_limit_output_size\",\n)" }, { "summary": "nonmaxsuppression_single_box", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array([[[0.0, 0.0, 1.0, 1.0]]]).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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_single_box\",\n)" }, { "summary": "nonmaxsuppression_suppress_by_IOU", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_suppress_by_IOU\",\n)" }, { "summary": "nonmaxsuppression_suppress_by_IOU_and_scores", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\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(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_suppress_by_IOU_and_scores\",\n)" }, { "summary": "nonmaxsuppression_two_batches", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ],\n [\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 ],\n ]\n).astype(np.float32)\nscores = np.array(\n [[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]], [[0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]\n).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(\n [[0, 0, 3], [0, 0, 0], [1, 0, 3], [1, 0, 0]]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_two_batches\",\n)" }, { "summary": "nonmaxsuppression_two_classes", "code": "node = onnx.helper.make_node(\n \"NonMaxSuppression\",\n inputs=[\n \"boxes\",\n \"scores\",\n \"max_output_boxes_per_class\",\n \"iou_threshold\",\n \"score_threshold\",\n ],\n outputs=[\"selected_indices\"],\n)\nboxes = np.array(\n [\n [\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 ]\n ]\n).astype(np.float32)\nscores = np.array(\n [[[0.9, 0.75, 0.6, 0.95, 0.5, 0.3], [0.9, 0.75, 0.6, 0.95, 0.5, 0.3]]]\n).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(\n [[0, 0, 3], [0, 0, 0], [0, 1, 3], [0, 1, 0]]\n).astype(np.int64)\n\nexpect(\n node,\n inputs=[\n boxes,\n scores,\n max_output_boxes_per_class,\n iou_threshold,\n score_threshold,\n ],\n outputs=[selected_indices],\n name=\"test_nonmaxsuppression_two_classes\",\n)" } ] }, { "name": "NonZero", "module": "ai.onnx", "version": 9, "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 but for scalar input, NonZero produces output shape (0, N) instead of (1, N), which is different from Numpy's behavior.\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(\n np.nonzero(condition), dtype=np.int64\n) # expected output [[0, 1, 1], [0, 0, 1]]\nexpect(node, inputs=[condition], outputs=[result], name=\"test_nonzero_example\")" } ] }, { "name": "NonZero", "module": "ai.onnx", "version": 13, "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 but for scalar input, NonZero produces output shape (0, N) instead of (1, N), which is different from Numpy's behavior.\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(\n np.nonzero(condition), dtype=np.int64\n) # expected output [[0, 1, 1], [0, 0, 1]]\nexpect(node, inputs=[condition], outputs=[result], name=\"test_nonzero_example\")" } ] }, { "name": "Normalizer", "module": "ai.onnx.ml", "version": 1, "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, "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)], 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)], 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)], name=\"test_not_4d\")" } ] }, { "name": "OneHot", "module": "ai.onnx", "version": 9, "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 or rank 1 tensor containing exactly one element, 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], [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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_with_axis\",\n)" }, { "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], [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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_with_negative_axis\",\n)" }, { "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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_negative_indices\",\n)" }, { "summary": "with_out_of_range_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)\n# Indices outside [-depth, depth-1] map to an all-off_value row.\nindices = np.array([5, -6, -1], dtype=np.int64)\n\n# print(y)\n# [[1. 1. 1. 1. 1.]\n# [1. 1. 1. 1. 1.]\n# [1. 1. 1. 1. 3.]]\n\ndepth = np.float32(5)\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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_out_of_range_indices\",\n)" }, { "summary": "without_axis", "code": "on_value = 5\noff_value = 2\noutput_type = np.int32\nnode = onnx.helper.make_node(\n \"OneHot\", inputs=[\"indices\", \"depth\", \"values\"], 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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_without_axis\",\n)" } ] }, { "name": "OneHot", "module": "ai.onnx", "version": 11, "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 or Rank 1 tensor containing exactly one element, 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], [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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_with_axis\",\n)" }, { "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], [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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_with_negative_axis\",\n)" }, { "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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_negative_indices\",\n)" }, { "summary": "with_out_of_range_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)\n# Indices outside [-depth, depth-1] map to an all-off_value row.\nindices = np.array([5, -6, -1], dtype=np.int64)\n\n# print(y)\n# [[1. 1. 1. 1. 1.]\n# [1. 1. 1. 1. 1.]\n# [1. 1. 1. 1. 3.]]\n\ndepth = np.float32(5)\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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_out_of_range_indices\",\n)" }, { "summary": "without_axis", "code": "on_value = 5\noff_value = 2\noutput_type = np.int32\nnode = onnx.helper.make_node(\n \"OneHot\", inputs=[\"indices\", \"depth\", \"values\"], 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(\n node,\n inputs=[indices, depth, values],\n outputs=[y],\n name=\"test_onehot_without_axis\",\n)" } ] }, { "name": "OneHotEncoder", "module": "ai.onnx.ml", "version": 1, "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, "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", "type": "type_proto", "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, "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": "OptionalGetElement", "module": "ai.onnx", "version": 18, "description": "If the input is a tensor or sequence type, it returns the input.\nIf the input is an optional type, it outputs the element in the input.\nIt is an error if the input is an empty optional-type (i.e. does not have an element) and 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))", "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 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, "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\n\ntensor_type_proto = onnx.helper.make_tensor_type_proto(\n elem_type=onnx.TensorProto.INT32, shape=[]\n)\noptional_type_proto = onnx.helper.make_optional_type_proto(tensor_type_proto)\n\n# OptionalHasElement takes a tensor or optional as input\nfor input_type_proto in [tensor_type_proto, optional_type_proto]:\n input_name_options = {\n \"empty\": \"optional_input\",\n \"empty_no_input_name\": \"\",\n \"empty_no_input\": None,\n }\n for test_name_surfix, input_name in input_name_options.items():\n if input_type_proto == tensor_type_proto and input_name:\n # the input tensor cannot be empty if input name is provided.\n continue\n node = onnx.helper.make_node(\n \"OptionalHasElement\",\n inputs=[] if input_name is None else [input_name],\n outputs=[\"output\"],\n )\n output = optional_has_element_reference_implementation(optional)\n test_name = (\n \"test_optional_has_element_\"\n + test_name_surfix\n + (\n \"_optional_input\"\n if input_type_proto == optional_type_proto\n else \"_tensor_input\"\n )\n )\n expect(\n node,\n inputs=[optional] if input_name else [],\n outputs=[output],\n input_type_protos=[input_type_proto] if input_name else [],\n name=test_name,\n )" }, { "summary": "get_element_sequence", "code": "optional = [np.array([1, 2, 3, 4]).astype(np.int32)]\ntensor_type_proto = onnx.helper.make_tensor_type_proto(\n elem_type=onnx.TensorProto.INT32,\n shape=[\n 4,\n ],\n)\nseq_type_proto = onnx.helper.make_sequence_type_proto(tensor_type_proto)\noptional_type_proto = onnx.helper.make_optional_type_proto(seq_type_proto)\n\nnode = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"optional_input\"], outputs=[\"output\"]\n)\noutput = optional_get_element_reference_implementation(optional)\nexpect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[optional_type_proto],\n name=\"test_optional_get_element_optional_sequence\",\n)\nexpect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[seq_type_proto],\n name=\"test_optional_get_element_sequence\",\n)" }, { "summary": "get_element_tensor", "code": "optional = np.array([1, 2, 3, 4]).astype(np.float32)\ntensor_type_proto = onnx.helper.make_tensor_type_proto(\n elem_type=onnx.TensorProto.FLOAT,\n shape=[\n 4,\n ],\n)\noptional_type_proto = onnx.helper.make_optional_type_proto(tensor_type_proto)\n\nnode = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"optional_input\"], outputs=[\"output\"]\n)\noutput = optional_get_element_reference_implementation(optional)\nexpect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[optional_type_proto],\n name=\"test_optional_get_element_optional_tensor\",\n)\nexpect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[tensor_type_proto],\n name=\"test_optional_get_element_tensor\",\n)" }, { "summary": "optionalhaselement", "code": "optional = np.array([1, 2, 3, 4]).astype(np.float32)\ntensor_type_proto = onnx.helper.make_tensor_type_proto(\n elem_type=onnx.TensorProto.FLOAT,\n shape=[\n 4,\n ],\n)\noptional_type_proto = onnx.helper.make_optional_type_proto(tensor_type_proto)\n\n# OptionalHasElement takes a tensor or optional as input\nfor input_type_protos in [tensor_type_proto, optional_type_proto]:\n node = onnx.helper.make_node(\n \"OptionalHasElement\", inputs=[\"optional_input\"], outputs=[\"output\"]\n )\n output = optional_has_element_reference_implementation(optional)\n test_name = \"test_optional_has_element_\" + (\n \"optional_input\"\n if input_type_protos == optional_type_proto\n else \"tensor_input\"\n )\n expect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[optional_type_proto],\n name=test_name,\n )" } ] }, { "name": "OptionalHasElement", "module": "ai.onnx", "version": 18, "description": "Returns true if (1) the input is an optional-type and contains an element,\nor, (2) the input is a tensor or sequence type.\nIf the input is not provided or is an empty optional-type, this op returns false.\n", "inputs": [ { "name": "input", "type": "O", "option": "optional", "description": "The optional input." } ], "min_input": 0, "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, "inputs_range": "0 - 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))", "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 to a boolean tensor.", "type_param_str": "B", "allowed_type_strs": [ "tensor(bool)" ] } ], "examples": [ { "summary": "empty", "code": "optional = None\n\ntensor_type_proto = onnx.helper.make_tensor_type_proto(\n elem_type=onnx.TensorProto.INT32, shape=[]\n)\noptional_type_proto = onnx.helper.make_optional_type_proto(tensor_type_proto)\n\n# OptionalHasElement takes a tensor or optional as input\nfor input_type_proto in [tensor_type_proto, optional_type_proto]:\n input_name_options = {\n \"empty\": \"optional_input\",\n \"empty_no_input_name\": \"\",\n \"empty_no_input\": None,\n }\n for test_name_surfix, input_name in input_name_options.items():\n if input_type_proto == tensor_type_proto and input_name:\n # the input tensor cannot be empty if input name is provided.\n continue\n node = onnx.helper.make_node(\n \"OptionalHasElement\",\n inputs=[] if input_name is None else [input_name],\n outputs=[\"output\"],\n )\n output = optional_has_element_reference_implementation(optional)\n test_name = (\n \"test_optional_has_element_\"\n + test_name_surfix\n + (\n \"_optional_input\"\n if input_type_proto == optional_type_proto\n else \"_tensor_input\"\n )\n )\n expect(\n node,\n inputs=[optional] if input_name else [],\n outputs=[output],\n input_type_protos=[input_type_proto] if input_name else [],\n name=test_name,\n )" }, { "summary": "get_element_sequence", "code": "optional = [np.array([1, 2, 3, 4]).astype(np.int32)]\ntensor_type_proto = onnx.helper.make_tensor_type_proto(\n elem_type=onnx.TensorProto.INT32,\n shape=[\n 4,\n ],\n)\nseq_type_proto = onnx.helper.make_sequence_type_proto(tensor_type_proto)\noptional_type_proto = onnx.helper.make_optional_type_proto(seq_type_proto)\n\nnode = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"optional_input\"], outputs=[\"output\"]\n)\noutput = optional_get_element_reference_implementation(optional)\nexpect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[optional_type_proto],\n name=\"test_optional_get_element_optional_sequence\",\n)\nexpect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[seq_type_proto],\n name=\"test_optional_get_element_sequence\",\n)" }, { "summary": "get_element_tensor", "code": "optional = np.array([1, 2, 3, 4]).astype(np.float32)\ntensor_type_proto = onnx.helper.make_tensor_type_proto(\n elem_type=onnx.TensorProto.FLOAT,\n shape=[\n 4,\n ],\n)\noptional_type_proto = onnx.helper.make_optional_type_proto(tensor_type_proto)\n\nnode = onnx.helper.make_node(\n \"OptionalGetElement\", inputs=[\"optional_input\"], outputs=[\"output\"]\n)\noutput = optional_get_element_reference_implementation(optional)\nexpect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[optional_type_proto],\n name=\"test_optional_get_element_optional_tensor\",\n)\nexpect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[tensor_type_proto],\n name=\"test_optional_get_element_tensor\",\n)" }, { "summary": "optionalhaselement", "code": "optional = np.array([1, 2, 3, 4]).astype(np.float32)\ntensor_type_proto = onnx.helper.make_tensor_type_proto(\n elem_type=onnx.TensorProto.FLOAT,\n shape=[\n 4,\n ],\n)\noptional_type_proto = onnx.helper.make_optional_type_proto(tensor_type_proto)\n\n# OptionalHasElement takes a tensor or optional as input\nfor input_type_protos in [tensor_type_proto, optional_type_proto]:\n node = onnx.helper.make_node(\n \"OptionalHasElement\", inputs=[\"optional_input\"], outputs=[\"output\"]\n )\n output = optional_has_element_reference_implementation(optional)\n test_name = \"test_optional_has_element_\" + (\n \"optional_input\"\n if input_type_protos == optional_type_proto\n else \"tensor_input\"\n )\n expect(\n node,\n inputs=[optional],\n outputs=[output],\n input_type_protos=[optional_type_proto],\n name=test_name,\n )" } ] }, { "name": "Or", "module": "ai.onnx", "version": 1, "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], 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], 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], 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], 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], 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], 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], 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], name=\"test_or_bcast4v4d\")" } ] }, { "name": "Or", "module": "ai.onnx", "version": 7, "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], 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], 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], 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], 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], 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], 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], 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], name=\"test_or_bcast4v4d\")" } ] }, { "name": "PRelu", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_prelu_broadcast\")" } ], "category": "Activation" }, { "name": "PRelu", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_prelu_broadcast\")" } ], "category": "Activation" }, { "name": "PRelu", "module": "ai.onnx", "version": 7, "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\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 than 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], 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], name=\"test_prelu_broadcast\")" } ], "category": "Activation" }, { "name": "PRelu", "module": "ai.onnx", "version": 9, "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\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 than 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], 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], name=\"test_prelu_broadcast\")" } ], "category": "Activation" }, { "name": "PRelu", "module": "ai.onnx", "version": 16, "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 than 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], 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], name=\"test_prelu_broadcast\")" } ], "category": "Activation" }, { "name": "PackedAttention", "module": "com.microsoft", "version": 1, "description": "This is the packed version of Attention.\n\nSequences in one batch usually don't have same length and they are padded to have same length,\ne.g., below is a batch with 3 sequences and tokens* are padded.\n Sequence_0: 0, 1*, 2*, 3*\n Sequence_1: 4, 5, 6*, 7*\n Sequence_2: 8, 9, 10, 11\n\nPackedAttention is designed to takes in packed input, i.e., only the real tokens without padding.\nAn input as above will be packed into 3 tensors like below:\n - input ([h0, h4, h5, h8, h9, h10, h11])\n - token_offset: 0, 4, 5, 8, 9, 10, 11, 1*, 2*, 3*, 6*, 7*\n - cumulated_token_count: 0, 1, 1+2, 1+2+4\n\nInput tensors contains the hidden embedding of real tokens.\nToken_offset records the offset of token in the unpacked input.\ncumulated_token_count records cumulated length of each sequence length.\n\nThe operator only supports BERT like model with padding on right now.\n\n", "attributes": [ { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "qkv_hidden_sizes", "type": "int64[]", "required": false, "description": "Hidden dimension of Q, K, V: hidden_size, hidden_size and v_hidden_size" }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1/sqrt(head_size)" } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor with shape (token_count, input_hidden_size)" }, { "name": "weights", "type": "T", "description": "Merged Q/K/V weights with shape (input_hidden_size, hidden_size + hidden_size + v_hidden_size)" }, { "name": "bias", "type": "T", "description": "Bias tensor with shape (hidden_size + hidden_size + v_hidden_size) for input projection" }, { "name": "token_offset", "type": "M", "description": "In packing mode, it specifies the offset of each token(batch_size, sequence_length)." }, { "name": "cumulative_sequence_length", "type": "M", "description": "A tensor with shape (batch_size + 1). It specifies the cumulative sequence length." }, { "name": "attention_bias", "type": "T", "option": "optional", "description": "A tensor with shape (batch_size or 1, num_heads or 1, sequence_length, sequence_length).It specifies the additional bias to QxK'" } ], "min_input": 5, "max_input": 6, "outputs": [ { "name": "output", "type": "T", "description": "2D output tensor with shape (token_count, v_hidden_size)" } ], "min_output": 1, "max_output": 1, "inputs_range": "5 - 6", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain mask index to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "PackedMultiHeadAttention", "module": "com.microsoft", "version": 1, "description": "This is the packed version of MultiHeadAttention.\n\nSequences in one batch usually don't have same length and they are padded to have same length,\ne.g., below is a batch with 3 sequences and * is padding token.\n Sequence_0: 0, 1*, 2*, 3*\n Sequence_1: 4, 5, 6*, 7*\n Sequence_2: 8, 9, 10, 11\n\nPackedMultiHeadAttention is designed to takes in packed input, i.e., only the real tokens without padding.\nAn input as above will be packed into 3 tensors like below:\n - query ([q0, q4, q5, q8, q9, q10, q11])\n - key ([k0, k4, k5, k8, k9, k10, k11])\n - value ([v0, v4, v5, v8, v9, v10, v11])\n - token_offset: 0, 4, 5, 8, 9, 10, 11, 1*, 2*, 3*, 6*, 7*\n - cumulative_sequence_length: 0, 1, 1+2, 1+2+4\n\nThe query, key and value tensors contain result of hidden embedding of real tokens after input projections.\nToken_offset records the offset of token in the unpacked input.\ncumulative_sequence_length records cumulated length of each sequence length.\n\nThe operator only supports BERT like model with padding on right now.\n", "attributes": [ { "name": "mask_filter_value", "type": "float32", "required": false, "description": "The value to be filled in the attention mask. Default value is -10000.0f" }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1/sqrt(head_size)" } ], "inputs": [ { "name": "query", "type": "T", "description": "Query with shape (token_count, hidden_size) or packed qkv with shape (token_count, num_heads, 3, head_size)" }, { "name": "key", "type": "T", "option": "optional", "description": "Key with shape (token_count, hidden_size)" }, { "name": "value", "type": "T", "option": "optional", "description": "Value with shape (token_count, v_hidden_size)" }, { "name": "bias", "type": "T", "option": "optional", "description": "Bias tensor with shape (hidden_size + hidden_size + v_hidden_size) from input projection" }, { "name": "token_offset", "type": "M", "description": "Offset of each token before packing, with shape (batch_size, sequence_length)." }, { "name": "cumulative_sequence_length", "type": "M", "description": "A tensor with shape (batch_size + 1). It specifies the cumulative sequence length." }, { "name": "attention_bias", "type": "T", "option": "optional", "description": "It specifies the additional bias to QxK'. The shape is (batch_size or 1, num_heads or 1, sequence_length, sequence_length)" } ], "min_input": 6, "max_input": 7, "outputs": [ { "name": "output", "type": "T", "description": "output tensor with shape (token_count, v_hidden_size)" } ], "min_output": 1, "max_output": 1, "inputs_range": "6 - 7", "type_constraints": [ { "description": "Constrain input and output to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain mask, offset and sequence length to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "Pad", "module": "ai.onnx", "version": 1, "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\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "ai.onnx", "version": 2, "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\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "ai.onnx", "version": 11, "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\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "ai.onnx", "version": 13, "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\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "ai.onnx", "version": 18, "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\nInsert 0 pads to the beginning of the second dimension.\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'constant'\n\nconstant_value = 0.0\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'reflect'\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'edge'\n\noutput = [\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 * num_axes] where `num_axes` refers to the number of elements in the `axes` input or the input rank if `axes` are not provided explicitly. `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 `axes[i]` and xi_end, the number of pad values added at the end of axis `axes[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)." }, { "name": "axes", "type": "Tind", "option": "optional", "description": "1-D tensor of axes that `pads` 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. If not provided, all axes are assumed (`[0, 1, ..., input_rank-1]`)." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Tensor after padding." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "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": "constant_pad", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "ai.onnx", "version": 19, "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 four 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\n4) `wrap` - wrap-around padding as if the data tensor forms a torus\n\n\nExample 1 (`constant` mode):\n\nInsert 0 pads to the beginning of the second dimension.\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'constant'\n\nconstant_value = 0.0\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'reflect'\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'edge'\n\noutput = [\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\nExample 4 (`wrap` mode):\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [2, 1, 1, 1]\n\nmode = 'wrap'\n\noutput = [\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n]\n```\n", "attributes": [ { "name": "mode", "type": "string", "required": false, "default": "constant", "description": "Supported modes: `constant`(default), `reflect`, `edge`, `wrap`" } ], "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 * num_axes] where `num_axes` refers to the number of elements in the `axes` input or the input rank if `axes` are not provided explicitly. `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 `axes[i]` and xi_end, the number of pad values added at the end of axis `axes[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)." }, { "name": "axes", "type": "Tind", "option": "optional", "description": "1-D tensor of axes that `pads` 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. If not provided, all axes are assumed (`[0, 1, ..., input_rank-1]`)." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Tensor after padding." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "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": "constant_pad", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "ai.onnx", "version": 21, "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 four 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\n4) `wrap` - wrap-around padding as if the data tensor forms a torus\n\n\nExample 1 (`constant` mode):\n\nInsert 0 pads to the beginning of the second dimension.\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'constant'\n\nconstant_value = 0.0\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'reflect'\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'edge'\n\noutput = [\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\nExample 4 (`wrap` mode):\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [2, 1, 1, 1]\n\nmode = 'wrap'\n\noutput = [\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n]\n```\n", "attributes": [ { "name": "mode", "type": "string", "required": false, "default": "constant", "description": "Supported modes: `constant`(default), `reflect`, `edge`, `wrap`" } ], "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 * num_axes] where `num_axes` refers to the number of elements in the `axes` input or the input rank if `axes` are not provided explicitly. `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 `axes[i]` and xi_end, the number of pad values added at the end of axis `axes[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)." }, { "name": "axes", "type": "Tind", "option": "optional", "description": "1-D tensor of axes that `pads` 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. If not provided, all axes are assumed (`[0, 1, ..., input_rank-1]`)." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Tensor after padding." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain input and output types to all tensor types up to IRv10.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] }, { "description": "Constrain indices to integer types", "type_param_str": "Tind", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "constant_pad", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "ai.onnx", "version": 23, "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 four 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\n4) `wrap` - wrap-around padding as if the data tensor forms a torus\n\n\nExample 1 (`constant` mode):\n\nInsert 0 pads to the beginning of the second dimension.\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'constant'\n\nconstant_value = 0.0\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'reflect'\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'edge'\n\noutput = [\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\nExample 4 (`wrap` mode):\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [2, 1, 1, 1]\n\nmode = 'wrap'\n\noutput = [\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n]\n```\n", "attributes": [ { "name": "mode", "type": "string", "required": false, "default": "constant", "description": "Supported modes: `constant`(default), `reflect`, `edge`, `wrap`" } ], "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 * num_axes] where `num_axes` refers to the number of elements in the `axes` input or the input rank if `axes` are not provided explicitly. `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 `axes[i]` and xi_end, the number of pad values added at the end of axis `axes[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)." }, { "name": "axes", "type": "Tind", "option": "optional", "description": "1-D tensor of axes that `pads` 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. If not provided, all axes are assumed (`[0, 1, ..., input_rank-1]`)." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Tensor after padding." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain input and output types to all tensor types up to IRv11.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] }, { "description": "Constrain indices to integer types", "type_param_str": "Tind", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "constant_pad", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "ai.onnx", "version": 24, "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 four 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\n4) `wrap` - wrap-around padding as if the data tensor forms a torus\n\n\nExample 1 (`constant` mode):\n\nInsert 0 pads to the beginning of the second dimension.\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'constant'\n\nconstant_value = 0.0\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'reflect'\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'edge'\n\noutput = [\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\nExample 4 (`wrap` mode):\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [2, 1, 1, 1]\n\nmode = 'wrap'\n\noutput = [\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n]\n```\n", "attributes": [ { "name": "mode", "type": "string", "required": false, "default": "constant", "description": "Supported modes: `constant`(default), `reflect`, `edge`, `wrap`" } ], "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 * num_axes] where `num_axes` refers to the number of elements in the `axes` input or the input rank if `axes` are not provided explicitly. `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 `axes[i]` and xi_end, the number of pad values added at the end of axis `axes[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)." }, { "name": "axes", "type": "Tind", "option": "optional", "description": "1-D tensor of axes that `pads` 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. If not provided, all axes are assumed (`[0, 1, ..., input_rank-1]`)." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Tensor after padding." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain input and output types to all tensor types up to IRv12.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] }, { "description": "Constrain indices to integer types", "type_param_str": "Tind", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "constant_pad", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "ai.onnx", "version": 25, "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 four 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\n4) `wrap` - wrap-around padding as if the data tensor forms a torus\n\n\nExample 1 (`constant` mode):\n\nInsert 0 pads to the beginning of the second dimension.\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'constant'\n\nconstant_value = 0.0\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'reflect'\n\noutput = [\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\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [0, 2, 0, 0]\n\nmode = 'edge'\n\noutput = [\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\nExample 4 (`wrap` mode):\n\n```\ndata = [\n [1.0, 1.2],\n [2.3, 3.4],\n [4.5, 5.7],\n]\n\npads = [2, 1, 1, 1]\n\nmode = 'wrap'\n\noutput = [\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n [3.4, 2.3, 3.4, 2.3],\n [5.7, 4.5, 5.7, 4.5],\n [1.2, 1.0, 1.2, 1.0],\n]\n```\n", "attributes": [ { "name": "mode", "type": "string", "required": false, "default": "constant", "description": "Supported modes: `constant`(default), `reflect`, `edge`, `wrap`" } ], "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 * num_axes] where `num_axes` refers to the number of elements in the `axes` input or the input rank if `axes` are not provided explicitly. `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 `axes[i]` and xi_end, the number of pad values added at the end of axis `axes[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)." }, { "name": "axes", "type": "Tind", "option": "optional", "description": "1-D tensor of axes that `pads` 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. If not provided, all axes are assumed (`[0, 1, ..., input_rank-1]`)." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "Tensor after padding." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain input and output types to all tensor types up to IRv13.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] }, { "description": "Constrain indices to integer types", "type_param_str": "Tind", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "constant_pad", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\"], outputs=[\"y\"], 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(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\ny = pad_impl(x, pads, \"constant\", 1.2)\n\nexpect(node, inputs=[x, pads, value], outputs=[y], name=\"test_constant_pad\")" }, { "summary": "constant_pad_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([1, 3], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [1, 3],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_axes\",\n)" }, { "summary": "constant_pad_negative_axes", "code": "node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\", \"value\", \"axes\"], outputs=[\"y\"], mode=\"constant\"\n)\nx = np.random.randn(1, 3, 4, 5).astype(np.float32)\npads = np.array([0, 3, 0, 4]).astype(\n np.int64\n) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\nvalue = np.float32(1.2)\naxes = np.array([-3, -1], dtype=np.int64)\ny = pad_impl(\n x,\n pads,\n \"constant\",\n 1.2,\n [-3, -1],\n)\n\nexpect(\n node,\n inputs=[x, pads, value, axes],\n outputs=[y],\n name=\"test_constant_pad_negative_axes\",\n)" }, { "summary": "reflection_edge_and_wrap_pad", "code": "for mode in (\"edge\", \"reflect\", \"wrap\"):\n node = onnx.helper.make_node(\n \"Pad\", inputs=[\"x\", \"pads\"], outputs=[\"y\"], 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(\n np.int64\n ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...]\n y = pad_impl(x, pads, mode)\n\n expect(node, inputs=[x, pads], outputs=[y], name=f\"test_{mode}_pad\")" } ], "category": "Tensor" }, { "name": "Pad", "module": "com.microsoft", "version": 1, "description": "Given `data` tensor, pads, mode, and value.\n Example:\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) - pads with a given constant value, `reflect` - pads with the reflection of the vector mirrored on the first and last values of the vector along each axis, `edge` - pads with the edge values of array" } ], "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] or a 2D tensor of shape [1, 2 * input_rank]. `pads` format (1D example) should be as follow [x1_begin, x2_begin,...,x1_end, x2_end,...], where xi_begin is 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": "T", "option": "optional", "description": "(Optional) A scalar or rank 1 tensor containing a single value to be filled if the mode chosen is `constant` (by default it is 0.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 types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "category": "Tensor" }, { "name": "PagedAttention", "module": "com.microsoft", "version": 1, "description": "Paged Attention.\n\nThis op leverages a block-based KV cache to enable continuous batching for LLMs. Currently, it is designed to work with\nthe CUDA Execution Provider only.\n\nIn other attention ops, batch entries typically aren't of the same length, so they are padded.\nBelow is a batch with 3 sequences where * denotes a padding token.\n Sequence_0: 0, 1*, 2*, 3*\n Sequence_1: 4, 5, 6*, 7*\n Sequence_2: 8, 9, 10, 11\n\nPagedAttention is designed to take in packed input, i.e., only the real tokens without padding.\nFor example, the input shown above will be packed into 3 tensors like below:\n - query ([q0, q4, q5, q8, q9, q10, q11])\n - key ([k0, k4, k5, k8, k9, k10, k11])\n - value ([v0, v4, v5, v8, v9, v10, v11])\n - cumulative_sequence_length: 0, 1, 1+2, 1+2+4\nThis packing omits padding tokens.\n\nThe query, key and value tensors contain result of hidden embedding of real tokens after input projections.\ncumulative_sequence_length records cumulated length of each sequence length.\n\n", "attributes": [ { "name": "do_rotary", "type": "int64", "required": false, "description": "Whether to use rotary position embedding. Default value is 0." }, { "name": "kv_num_heads", "type": "int64", "required": true, "description": "Number of attention heads for k and v" }, { "name": "local_window_size", "type": "int64", "required": false, "default": -1, "description": "left_window_size for local attention (like Mistral). Default value is -1 meaning unused." }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads for q" }, { "name": "rotary_interleaved", "type": "int64", "required": false, "description": "Rotate using interleaved pattern. Default value is 0 (False)." }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1/sqrt(head_size)" }, { "name": "softcap", "type": "float32", "required": false, "description": "Softcap value for attention weights. Default value is 0." } ], "inputs": [ { "name": "query", "type": "T", "description": "Query with shape (num_tokens, hidden_size), or packed QKV with shape (num_tokens, d) where d is (num_heads * head_size + 2 * kv_num_heads * head_size)." }, { "name": "key", "type": "T", "option": "optional", "description": "Key with shape (num_tokens, kv_hidden_size) " }, { "name": "value", "type": "T", "option": "optional", "description": "Value with shape (num_tokens, kv_hidden_size)" }, { "name": "key_cache", "type": "T", "description": "Block-based key cache with shape (num_blocks, block_size, kv_num_heads, head_size). This is updated in place within the op." }, { "name": "value_cache", "type": "T", "description": "Block-based value cache with shape (num_blocks, block_size, kv_num_heads, head_size). This is updated in place within the op. This should be the same shape as key_cache." }, { "name": "cumulative_sequence_length", "type": "S", "description": "A tensor with shape (batch_size + 1). It specifies the cumulative sequence lengths between the packed entries in Q/K/V." }, { "name": "past_seqlens", "type": "S", "description": "A tensor with shape (batch_size). It specifies the past lengths of cached sequence in the KV cache." }, { "name": "block_table", "type": "S", "description": "2D tensor with shape (batch_size, max_blocks_per_sequence) that maps each sequence in the batch to itscorresponding blocks in the KV cache." }, { "name": "cos_cache", "type": "T", "option": "optional", "description": "2D tensor with shape (max total seqlen, head_size / 2)." }, { "name": "sin_cache", "type": "T", "option": "optional", "description": "2D tensor with shape (max total seqlen, head_size / 2)." } ], "min_input": 8, "max_input": 10, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (num_tokens, hidden_size)" }, { "name": "key_cache_out", "type": "T", "option": "optional", "description": "Block-based key cache with shape (num_blocks, block_size, kv_num_heads, head_size). This is always the same tensor as key_cache." }, { "name": "value_cache_out", "type": "T", "option": "optional", "description": "Block-based value cache with shape (num_blocks, block_size, kv_num_heads, head_size). This is always the same tensor as value_cache." } ], "min_output": 1, "max_output": 3, "inputs_range": "8 - 10", "outputs_range": "1 - 3", "type_constraints": [ { "description": "Constrain input and output to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain Positional inputs to int tensor.", "type_param_str": "S", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "ParametricSoftplus", "module": "ai.onnx", "version": 1, "description": "ParametricSoftplus takes one input data (Tensor) and produces one output data\n(Tensor) where the softplus function, y = alpha * ln(exp(beta * x) + 1), is applied to\nthe tensor elementwise.\n", "attributes": [ { "name": "alpha", "type": "float32", "required": false, "description": "Value of alpha" }, { "name": "beta", "type": "float32", "required": false, "description": "Value of beta" } ], "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)" ] } ] }, { "name": "ParametricSoftplus", "module": "ai.onnx", "version": 10, "description": "ParametricSoftplus takes one input data (Tensor) and produces one output data\n(Tensor) where the softplus function, y = alpha * ln(exp(beta * x) + 1), is applied to\nthe tensor elementwise.\n", "attributes": [ { "name": "alpha", "type": "float32", "required": false, "description": "Value of alpha" }, { "name": "beta", "type": "float32", "required": false, "description": "Value of beta" } ], "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)" ] } ] }, { "name": "Pow", "module": "ai.onnx", "version": 1, "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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], name=\"test_pow_types_int32_int32\")" } ] }, { "name": "Pow", "module": "ai.onnx", "version": 7, "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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], name=\"test_pow_types_int32_int32\")" } ] }, { "name": "Pow", "module": "ai.onnx", "version": 12, "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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], name=\"test_pow_types_int32_int32\")" } ] }, { "name": "Pow", "module": "ai.onnx", "version": 13, "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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], name=\"test_pow_types_int32_int32\")" } ] }, { "name": "Pow", "module": "ai.onnx", "version": 15, "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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], 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], name=\"test_pow_types_int32_int32\")" } ] }, { "name": "PyramidROIAlign_TRT", "module": "ai.onnx", "version": 1, "description": "Pyramid ROI Align TensorRT Plugin.", "attributes": [ { "name": "plugin_version", "type": "string", "required": true, "description": "Version number of the TRT plugin." }, { "name": "pooled_size", "type": "int64", "required": true, "description": "Pooled size." } ], "inputs": [ { "name": "boxes", "type": "T", "description": "The boxes input tensor." }, { "name": "feature_map_0", "type": "T", "description": "The first feature map input tensor." }, { "name": "feature_map_1", "type": "T", "description": "The second feature map input tensor." }, { "name": "feature_map_2", "type": "T", "description": "The third feature map input tensor." }, { "name": "feature_map_3", "type": "T", "description": "The fourth feature map input tensor." } ], "min_input": 5, "max_input": 5, "outputs": [ { "name": "patches", "type": "T", "description": "The cropped patches 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(float)" ] } ] }, { "name": "QAttention", "module": "com.microsoft", "version": 1, "description": "Quantization of Multi-Head Self Attention.", "attributes": [ { "name": "do_rotary", "type": "int64", "required": false, "description": "Whether to use rotary position embedding. Default value is 0." }, { "name": "mask_filter_value", "type": "float32", "required": false, "description": "The value to be filled in the attention mask. Default value is -10000.0f" }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "past_present_share_buffer", "type": "int64", "required": false, "description": "Corresponding past and present are same tensor, its shape is (2, batch_size, num_heads, max_sequence_length, head_size)" }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1/sqrt(head_size)" }, { "name": "unidirectional", "type": "int64", "required": false, "description": "Whether every token can only attend to previous tokens. Default value is 0." } ], "inputs": [ { "name": "input", "type": "T1", "description": "3D input tensor with shape (batch_size, sequence_length, input_hidden_size)" }, { "name": "weight", "type": "T2", "description": "2D input tensor with shape (input_hidden_size, 3 * hidden_size), hidden_size = num_heads * head_size" }, { "name": "bias", "type": "T3", "description": "1D input tensor with shape (3 * hidden_size)" }, { "name": "input_scale", "type": "T3", "description": "scale of quantized input tensor. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "weight_scale", "type": "T3", "description": "scale of weight scale. It's a scalar or a 1D tensor, which means a per-tensor/per-column quantization.Its size should be 3 * hidden_size if it is per-column quantization" }, { "name": "mask_index", "type": "T4", "option": "optional", "description": "Attention mask index with shape (batch_size)" }, { "name": "input_zero_point", "type": "T1", "option": "optional", "description": "zero point of quantized input tensor.It's a scalar, which means a per-tensor/layer quantization." }, { "name": "weight_zero_point", "type": "T2", "option": "optional", "description": "zero point of quantized weight tensor. It's a scalar or a 1D tensor, which means a per-tensor/per-column quantization.Its size should be 3 * hidden_size if it is per-column quantization" }, { "name": "past", "type": "T3", "option": "optional", "description": "past state for key and value with shape (2, batch_size, num_heads, past_sequence_length, head_size)." } ], "min_input": 5, "max_input": 9, "outputs": [ { "name": "output", "type": "T3", "description": "3D output tensor with shape (batch_size, sequence_length, hidden_size)" }, { "name": "present", "type": "T3", "option": "optional", "description": "present state for key and value with shape (2, batch_size, num_heads, past_sequence_length + sequence_length, head_size)" } ], "min_output": 1, "max_output": 2, "inputs_range": "5 - 9", "outputs_range": "1 - 2", "type_constraints": [ { "description": "Constrain input and output types to int8 tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)" ] }, { "description": "Constrain input and output types to int8 tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)" ] }, { "description": "Constrain input and output types to float tensors.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain mask index to integer types", "type_param_str": "T4", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "QEmbedLayerNormalization", "module": "com.microsoft", "version": 1, "status": "experimental", "description": "QEmbedLayerNormalization is the quantized fusion of embedding layer in BERT model, with optional mask processing.\nThe embedding layer takes input_ids (word IDs) and segment_ids (sentence IDs) to look up word_embedding, position_embedding,\nand segment_emedding; the embeddings are added then applied layer normalization using gamma and beta tensors. The input_ids\nand segment_ids remain int32. All embeddings, gamma, and beta tensors are converted to int8/uint8. The last input mask is optional.\nIf mask is provided, mask index (that is position of first 0 in mask, or number of words will be calculated.", "attributes": [ { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999960041972e-13, "description": "The epsilon value to use to avoid division by zero." } ], "inputs": [ { "name": "input_ids", "type": "T1", "description": "2D words IDs with shape (batch_size, sequence_length)" }, { "name": "segment_ids", "type": "T1", "option": "optional", "description": "2D segment IDs with shape (batch_size, sequence_length)" }, { "name": "word_embedding_quant", "type": "T2", "description": "2D with shape (,hidden_size)" }, { "name": "position_embedding_quant", "type": "T2", "description": "2D with shape (, hidden_size)" }, { "name": "segment_embedding", "type": "T2", "option": "optional", "description": "2D with shape (, hidden_size)" }, { "name": "gamma_quant", "type": "T2", "description": "1D gamma tensor for layer normalization with shape (hidden_size)" }, { "name": "beta_quant", "type": "T2", "description": "1D beta tensor for layer normalization with shape (hidden_size)" }, { "name": "mask", "type": "T1", "option": "optional", "description": "Mask" }, { "name": "word_embedding_scale", "type": "T", "description": "Scale for word embeddings" }, { "name": "position_embedding_scale", "type": "T", "description": "Scale for position embeddings" }, { "name": "segment_embedding_scale", "type": "T", "option": "optional", "description": "Scale for segment embeddings" }, { "name": "gamma_scale", "type": "T", "description": "Scale for 1D gamma tensor" }, { "name": "beta_scale", "type": "T", "description": "Scale for 1D beta tensor" }, { "name": "word_embedding_zero_point", "type": "T2", "description": "Zero point for word embeddings" }, { "name": "position_embedding_zero_point", "type": "T2", "description": "Zero point for position embeddings" }, { "name": "segment_embedding_zero_point", "type": "T2", "option": "optional", "description": "Zero Point for segment embeddings" }, { "name": "gamma_zero_point", "type": "T2", "description": "Zero Point for 1D gamma tensor" }, { "name": "beta_zero_point", "type": "T2", "description": "Zero Point for 1D beta tensor" } ], "min_input": 18, "max_input": 18, "outputs": [ { "name": "layernorm_out", "type": "T", "description": "LayerNorm Output" }, { "name": "mask_index_out", "type": "T1", "description": "Mask Index Output" } ], "min_output": 2, "max_output": 2, "type_constraints": [ { "description": "Constrain mask index to integer types", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int32)" ] }, { "description": "Constrain input and output types to int8 tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)" ] }, { "description": "Constrain input and output types to float32 tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "QGemm", "module": "com.microsoft", "version": 1, "description": "Quantized Gemm", "attributes": [ { "name": "alpha", "type": "float32", "required": false, "default": 1.0, "description": "Scalar multiplier for the product of input tensors A * B." }, { "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": "TA", "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": "a_scale", "type": "T", "description": "Scale of quantized input 'A'. It is a scalar,which means a per-tensor quantization." }, { "name": "a_zero_point", "type": "TA", "description": "Zero point tensor for input 'A'. It is a scalar." }, { "name": "B", "type": "TB", "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": "b_scale", "type": "T", "description": "Scale of quantized input 'B'. It could be a scalar or a 1-D tensor, which means a per-tensor or per-column quantization. If it's a 1-D tensor, its number of elements should be equal to the number of columns of input 'B'." }, { "name": "b_zero_point", "type": "TB", "description": "Zero point tensor for input 'B'. It's optional and default value is 0. It could be a scalar or a 1-D tensor, which means a per-tensor or per-column quantization. If it's a 1-D tensor, its number of elements should be equal to the number of columns of input 'B'." }, { "name": "C", "type": "TC", "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). Its type is int32_t and must be quantized with zero_point = 0 and scale = alpha / beta * a_scale * b_scale." }, { "name": "y_scale", "type": "T", "option": "optional", "description": "Scale of output 'Y'. It is a scalar, which means a per-tensor quantization. It is optional. The output is full precision(float32) if it is not provided. Or the output is quantized." }, { "name": "y_zero_point", "type": "TYZ", "option": "optional", "description": "Zero point tensor for output 'Y'. It is a scalar, which means a per-tensor quantization. It is optional. The output is full precision(float32) if it is not provided. Or the output is quantized." } ], "min_input": 6, "max_input": 9, "outputs": [ { "name": "Y", "type": "TY", "description": "Output tensor of shape (M, N)." } ], "min_output": 1, "max_output": 1, "inputs_range": "6 - 9", "type_constraints": [ { "description": "Constrain scale types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain input A and its zero point types to 8 bit tensors.", "type_param_str": "TA", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] }, { "description": "Constrain input B and its zero point types to 8 bit tensors.", "type_param_str": "TB", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] }, { "description": "Constrain input C to 32 bit integer tensors.", "type_param_str": "TC", "allowed_type_strs": [ "tensor(int32)" ] }, { "description": "Constrain output zero point types to 8 bit tensors.", "type_param_str": "TYZ", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] }, { "description": "Constrain output type to float32 or 8 bit tensors.", "type_param_str": "TY", "allowed_type_strs": [ "tensor(float)", "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearAdd", "module": "com.microsoft", "version": 1, "description": "Performs element-wise binary addition on 8 bit data types (with Numpy-style broadcasting support).\n\nC = (A_scale * (A - A_zero_point) + B_scale * (B - B_zero_point))/C_scale + C_zero_point\n", "inputs": [ { "name": "A", "type": "T", "description": "First operand." }, { "name": "A_scale", "type": "tensor(float)", "description": "Input A's scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "A_zero_point", "type": "T", "option": "optional", "description": "Input A zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "B", "type": "T", "description": "Second operand." }, { "name": "B_scale", "type": "tensor(float)", "description": "Input B's scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "B_zero_point", "type": "T", "option": "optional", "description": "Input B zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "C_scale", "type": "tensor(float)", "description": "Output scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "C_zero_point", "type": "T", "option": "optional", "description": "Output zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." } ], "min_input": 7, "max_input": 8, "outputs": [ { "name": "C", "type": "T", "description": "Result, has same element type as two inputs" } ], "min_output": 1, "max_output": 1, "inputs_range": "7 - 8", "type_constraints": [ { "description": "Constrain input and output types to 8 bit signed and unsigned tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearAveragePool", "module": "com.microsoft", "version": 1, "description": "QLinearAveragePool 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\nThe output of each pooling window is divided by the number of elements (exclude pad when attribute count_include_pad is zero).\n\nInput and output scales and zero points are used to convert the output to a new quantization range.\nOutput = Dequantize(Input) -> AveragePool on fp32 data -> Quantize(output)\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": "channels_last", "type": "int64", "required": false, "description": "Works on NHWC layout or not? Default not." }, { "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 ...]." }, { "name": "x_scale", "type": "tensor(float)", "description": "Input scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "x_zero_point", "type": "T", "option": "optional", "description": "Input zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "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": "T", "option": "optional", "description": "Output zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." } ], "min_input": 4, "max_input": 5, "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, "inputs_range": "4 - 5", "type_constraints": [ { "description": "Constrain input and output types to 8 bit tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearAveragePool", "module": "com.ms.internal.nhwc", "version": 1, "description": "QLinearAveragePool 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\nThe output of each pooling window is divided by the number of elements (exclude pad when attribute count_include_pad is zero).\n\nInput and output scales and zero points are used to convert the output to a new quantization range.\nOutput = Dequantize(Input) -> AveragePool on fp32 data -> Quantize(output)\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": "channels_last", "type": "int64", "required": false, "description": "Works on NHWC layout or not? Default not." }, { "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 ...]." }, { "name": "x_scale", "type": "tensor(float)", "description": "Input scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "x_zero_point", "type": "T", "option": "optional", "description": "Input zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "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": "T", "option": "optional", "description": "Output zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." } ], "min_input": 4, "max_input": 5, "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, "inputs_range": "4 - 5", "type_constraints": [ { "description": "Constrain input and output types to 8 bit tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearConcat", "module": "com.microsoft", "version": 1, "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" } ], "inputs": [ { "name": "Y_scale", "type": "TF", "description": "Y's scale." }, { "name": "Y_zero_point", "type": "T8", "description": "Y's zero point." }, { "name": "inputs", "type": "TV", "list": true, "description": "List of tensors/scale/zero_point for concatenation" } ], "min_input": 3, "max_input": 2147483647, "outputs": [ { "name": "Y", "type": "T8", "description": "Concatenated tensor" } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - ∞", "type_constraints": [ { "description": "Constrain input and output types to 8 bit signed and unsigned tensors.", "type_param_str": "T8", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] }, { "description": "Constrain scale types to any float tensor type.", "type_param_str": "TF", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Sequence of (Tensor, Scale, ZeroPoint) tuples. The type is sequence of (T8, TF, T8).", "type_param_str": "TV", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)", "tensor(float)" ] } ] }, { "name": "QLinearConv", "module": "ai.onnx", "version": 10, "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(\n \"QLinearConv\",\n inputs=[\n \"x\",\n \"x_scale\",\n \"x_zero_point\",\n \"w\",\n \"w_scale\",\n \"w_zero_point\",\n \"y_scale\",\n \"y_zero_point\",\n ],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n [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],\n ],\n dtype=np.uint8,\n).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(\n [\n [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],\n ],\n dtype=np.uint8,\n).reshape((1, 1, 7, 7))\n\nexpect(\n node,\n inputs=[\n x,\n x_scale,\n x_zero_point,\n w,\n w_scale,\n w_zero_point,\n y_scale,\n y_zero_point,\n ],\n outputs=[output],\n name=\"test_qlinearconv\",\n)" } ] }, { "name": "QLinearConv", "module": "com.microsoft", "version": 1, "attributes": [ { "name": "auto_pad", "type": "string", "required": false, "default": "NOTSET" }, { "name": "channels_last", "type": "int64", "required": false }, { "name": "dilations", "type": "int64[]", "required": false }, { "name": "group", "type": "int64", "required": false, "default": 1 }, { "name": "kernel_shape", "type": "int64[]", "required": false }, { "name": "pads", "type": "int64[]", "required": false }, { "name": "strides", "type": "int64[]", "required": false } ], "inputs": [ { "name": "x", "type": "T1" }, { "name": "x_scale", "type": "tensor(float)" }, { "name": "x_zero_point", "type": "T1" }, { "name": "w", "type": "T2" }, { "name": "w_scale", "type": "tensor(float)" }, { "name": "w_zero_point", "type": "T2" }, { "name": "y_scale", "type": "tensor(float)" }, { "name": "y_zero_point", "type": "T3" }, { "name": "B", "type": "T4", "option": "optional" } ], "min_input": 8, "max_input": 9, "outputs": [ { "name": "y", "type": "T3" } ], "min_output": 1, "max_output": 1, "inputs_range": "8 - 9", "type_constraints": [ { "description": "", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)" ] }, { "description": "", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)" ] }, { "description": "", "type_param_str": "T3", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)" ] }, { "description": "", "type_param_str": "T4", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "QLinearConv", "module": "com.ms.internal.nhwc", "version": 10, "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)" ] } ] }, { "name": "QLinearConvTranspose", "module": "com.ms.internal.nhwc", "version": 1, "description": "Similar to ConvTranspose in onnx, but with quantization.\nThe 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" }, { "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" }, { "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)" }, { "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 (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. " }, { "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)" ] } ] }, { "name": "QLinearGlobalAveragePool", "module": "com.microsoft", "version": 1, "description": "QLinearGlobalAveragePool consumes an input tensor X and applies Average pooling across\nthe values in the same channel. This is equivalent to AveragePool with kernel size\nequal to the spatial dimension of input tensor. Input is of type uint8_t or int8_t.\n", "attributes": [ { "name": "channels_last", "type": "int64", "required": false } ], "inputs": [ { "name": "X", "type": "T", "description": "Input data tensor from the previous operator; According to channels_last, dimensions for image case are (N x C x H x W), or (N x H x W x C) 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), or (N x D1 X D2 ... Dn x C) where N is the batch size." }, { "name": "x_scale", "type": "tensor(float)", "description": "Scale of quantized input 'X'. It must be a scalar." }, { "name": "x_zero_point", "type": "T", "description": "Zero point tensor for input 'X'. It must be a scalar." }, { "name": "y_scale", "type": "tensor(float)", "description": "Scale of quantized output 'Y'. It must be a scalar." }, { "name": "y_zero_point", "type": "T", "description": "Zero point tensor for output 'Y'. It must be a scalar." } ], "min_input": 5, "max_input": 5, "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. with the N and C value keep it value, while the otherdimensions are all 1." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to signed/unsigned int8 tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearLeakyRelu", "module": "com.microsoft", "version": 1, "description": "QLinearLeakyRelu takes quantized input data (Tensor), an argument alpha, and quantize parameter for output,\nand produces one output data (Tensor) where the function `f(x) = quantize(alpha * dequantize(x)) for dequantize(x) < 0`,\n`f(x) = quantize(dequantize(x)) for dequantize(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" }, { "name": "X_scale", "type": "tensor(float)", "description": "Input X's scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "X_zero_point", "type": "T", "option": "optional", "description": "Input X's zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "Y_scale", "type": "tensor(float)", "description": "Output Y's scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "Y_zero_point", "type": "T", "option": "optional", "description": "Output Y's zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." } ], "min_input": 4, "max_input": 5, "outputs": [ { "name": "Y", "type": "T", "description": "Output tensor" } ], "min_output": 1, "max_output": 1, "inputs_range": "4 - 5", "type_constraints": [ { "description": "Constrain input and output types to 8 bit tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearMatMul", "module": "ai.onnx", "version": 10, "description": "Matrix product that behaves like [numpy.matmul](https://numpy.org/doc/stable/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": "int", "code": "for quant_type_name in [\"uint8\", \"int8\"]:\n quant_type = getattr(np, quant_type_name)\n for dtype_name in [\"float32\", \"float16\"]:\n dtype = getattr(np, dtype_name)\n node = onnx.helper.make_node(\n \"QLinearMatMul\",\n inputs=[\n \"a\",\n \"a_scale\",\n \"a_zero_point\",\n \"b\",\n \"b_scale\",\n \"b_zero_point\",\n \"y_scale\",\n \"y_zero_point\",\n ],\n outputs=[\"y\"],\n )\n\n # 2D\n a = np.array([[208, 236, 0, 238], [3, 214, 255, 29]])\n if quant_type == np.int8:\n a -= 127\n a = a.astype(quant_type)\n\n a_scale = np.array([0.0066], dtype=dtype)\n a_zero_point = np.array(\n [113 - 127] if quant_type == np.int8 else [113], dtype=quant_type\n )\n\n b = np.array(\n [[152, 51, 244], [60, 26, 255], [0, 127, 246], [127, 254, 247]]\n )\n if quant_type == np.int8:\n b -= 127\n b = b.astype(quant_type)\n\n b_scale = np.array([0.00705], dtype=dtype)\n b_zero_point = np.array(\n [114 - 127] if quant_type == np.int8 else [114], dtype=quant_type\n )\n\n y_scale = np.array([0.0107], dtype=dtype)\n y_zero_point = np.array(\n [118 - 127] if quant_type == np.int8 else [118], dtype=quant_type\n )\n\n if quant_type == np.int8:\n output = np.array([[41, -12, -9], [1, -75, -128]])\n else:\n output = np.array([[168, 115, 255], [1, 66, 151]])\n output = output.astype(quant_type)\n\n expect(\n node,\n inputs=[\n a,\n a_scale,\n a_zero_point,\n b,\n b_scale,\n b_zero_point,\n y_scale,\n y_zero_point,\n ],\n outputs=[output],\n name=f\"test_qlinearmatmul_2D_{quant_type_name}_{dtype_name}\",\n )\n\n # 3D\n a = np.array(\n [\n [[208, 236, 0, 238], [3, 214, 255, 29]],\n [[208, 236, 0, 238], [3, 214, 255, 29]],\n ],\n )\n if quant_type == np.int8:\n a -= 127\n a = a.astype(quant_type)\n\n a_scale = np.array([0.0066], dtype=dtype)\n a_zero_point = np.array(\n [113 - 127] if quant_type == np.int8 else [113], dtype=quant_type\n )\n\n b = np.array(\n [\n [[152, 51, 244], [60, 26, 255], [0, 127, 246], [127, 254, 247]],\n [[152, 51, 244], [60, 26, 255], [0, 127, 246], [127, 254, 247]],\n ],\n )\n if quant_type == np.int8:\n b -= 127\n b = b.astype(quant_type)\n\n b_scale = np.array([0.00705], dtype=dtype)\n b_zero_point = np.array([114], dtype=quant_type)\n\n y_scale = np.array([0.0107], dtype=dtype)\n y_zero_point = np.array(\n [118 - 127] if quant_type == np.int8 else [118], dtype=quant_type\n )\n\n if quant_type == np.int8:\n output = np.array(\n [\n [[-86, -128, -128], [115, 39, -121]],\n [[-86, -128, -128], [115, 39, -121]],\n ]\n )\n else:\n output = np.array(\n [\n [[168, 115, 255], [1, 66, 151]],\n [[168, 115, 255], [1, 66, 151]],\n ]\n )\n output = output.astype(quant_type)\n\n expect(\n node,\n inputs=[\n a,\n a_scale,\n a_zero_point,\n b,\n b_scale,\n b_zero_point,\n y_scale,\n y_zero_point,\n ],\n outputs=[output],\n name=f\"test_qlinearmatmul_3D_{quant_type_name}_{dtype_name}\",\n )" } ] }, { "name": "QLinearMatMul", "module": "ai.onnx", "version": 21, "description": "Matrix product that behaves like [numpy.matmul](https://numpy.org/doc/stable/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": "TS", "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": "TS", "description": "scale of quantized input b" }, { "name": "b_zero_point", "type": "T2", "description": "zero point of quantized input b" }, { "name": "y_scale", "type": "TS", "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 scales.", "type_param_str": "TS", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "The type of input a and its zeropoint.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "description": "The type of input b and its zeropoint.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "description": "The type of the output and its zeropoint.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] } ], "examples": [ { "summary": "int", "code": "for quant_type_name in [\"uint8\", \"int8\"]:\n quant_type = getattr(np, quant_type_name)\n for dtype_name in [\"float32\", \"float16\"]:\n dtype = getattr(np, dtype_name)\n node = onnx.helper.make_node(\n \"QLinearMatMul\",\n inputs=[\n \"a\",\n \"a_scale\",\n \"a_zero_point\",\n \"b\",\n \"b_scale\",\n \"b_zero_point\",\n \"y_scale\",\n \"y_zero_point\",\n ],\n outputs=[\"y\"],\n )\n\n # 2D\n a = np.array([[208, 236, 0, 238], [3, 214, 255, 29]])\n if quant_type == np.int8:\n a -= 127\n a = a.astype(quant_type)\n\n a_scale = np.array([0.0066], dtype=dtype)\n a_zero_point = np.array(\n [113 - 127] if quant_type == np.int8 else [113], dtype=quant_type\n )\n\n b = np.array(\n [[152, 51, 244], [60, 26, 255], [0, 127, 246], [127, 254, 247]]\n )\n if quant_type == np.int8:\n b -= 127\n b = b.astype(quant_type)\n\n b_scale = np.array([0.00705], dtype=dtype)\n b_zero_point = np.array(\n [114 - 127] if quant_type == np.int8 else [114], dtype=quant_type\n )\n\n y_scale = np.array([0.0107], dtype=dtype)\n y_zero_point = np.array(\n [118 - 127] if quant_type == np.int8 else [118], dtype=quant_type\n )\n\n if quant_type == np.int8:\n output = np.array([[41, -12, -9], [1, -75, -128]])\n else:\n output = np.array([[168, 115, 255], [1, 66, 151]])\n output = output.astype(quant_type)\n\n expect(\n node,\n inputs=[\n a,\n a_scale,\n a_zero_point,\n b,\n b_scale,\n b_zero_point,\n y_scale,\n y_zero_point,\n ],\n outputs=[output],\n name=f\"test_qlinearmatmul_2D_{quant_type_name}_{dtype_name}\",\n )\n\n # 3D\n a = np.array(\n [\n [[208, 236, 0, 238], [3, 214, 255, 29]],\n [[208, 236, 0, 238], [3, 214, 255, 29]],\n ],\n )\n if quant_type == np.int8:\n a -= 127\n a = a.astype(quant_type)\n\n a_scale = np.array([0.0066], dtype=dtype)\n a_zero_point = np.array(\n [113 - 127] if quant_type == np.int8 else [113], dtype=quant_type\n )\n\n b = np.array(\n [\n [[152, 51, 244], [60, 26, 255], [0, 127, 246], [127, 254, 247]],\n [[152, 51, 244], [60, 26, 255], [0, 127, 246], [127, 254, 247]],\n ],\n )\n if quant_type == np.int8:\n b -= 127\n b = b.astype(quant_type)\n\n b_scale = np.array([0.00705], dtype=dtype)\n b_zero_point = np.array([114], dtype=quant_type)\n\n y_scale = np.array([0.0107], dtype=dtype)\n y_zero_point = np.array(\n [118 - 127] if quant_type == np.int8 else [118], dtype=quant_type\n )\n\n if quant_type == np.int8:\n output = np.array(\n [\n [[-86, -128, -128], [115, 39, -121]],\n [[-86, -128, -128], [115, 39, -121]],\n ]\n )\n else:\n output = np.array(\n [\n [[168, 115, 255], [1, 66, 151]],\n [[168, 115, 255], [1, 66, 151]],\n ]\n )\n output = output.astype(quant_type)\n\n expect(\n node,\n inputs=[\n a,\n a_scale,\n a_zero_point,\n b,\n b_scale,\n b_zero_point,\n y_scale,\n y_zero_point,\n ],\n outputs=[output],\n name=f\"test_qlinearmatmul_3D_{quant_type_name}_{dtype_name}\",\n )" } ] }, { "name": "QLinearMul", "module": "com.microsoft", "version": 1, "description": "Performs element-wise binary multiplication on 8 bit data types (with Numpy-style broadcasting support).\n\nC = ((A - A_zero_point) * (B - B_zero_point)) * (A_scale * B_scale)/C_scale + C_zero_point\n", "inputs": [ { "name": "A", "type": "T", "description": "First operand." }, { "name": "A_scale", "type": "tensor(float)", "description": "Input A's scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "A_zero_point", "type": "T", "option": "optional", "description": "Input A zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "B", "type": "T", "description": "Second operand." }, { "name": "B_scale", "type": "tensor(float)", "description": "Input B's scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "B_zero_point", "type": "T", "option": "optional", "description": "Input B zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "C_scale", "type": "tensor(float)", "description": "Output scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "C_zero_point", "type": "T", "option": "optional", "description": "Output zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." } ], "min_input": 7, "max_input": 8, "outputs": [ { "name": "C", "type": "T", "description": "Result, has same element type as two inputs" } ], "min_output": 1, "max_output": 1, "inputs_range": "7 - 8", "type_constraints": [ { "description": "Constrain input and output types to 8 bit signed and unsigned tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearReduceMean", "module": "com.microsoft", "version": 1, "description": "Computes the mean of the low-precision input tensor's element along the provided axes.\nThe resulting tensor has the same rank as the input if keepdims equal 1. If keepdims equal 0,\nthen the resulting tensor have the reduced dimension pruned. The above behavior is similar to numpy,\nwith the exception that numpy default keepdims to False instead of True.\nInput and Output scales and zero points are used to requantize the output in a new range.\nThis helps to improve accuracy as after ReduceMean operation the range of the output is expected to decrease.\n\n```\n\"Output = Dequantize(Input) -> ReduceMean on fp32 data -> Quantize(output)\",\n\n```\n", "attributes": [ { "name": "axes", "type": "int64[]", "required": true, "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": true, "description": "Keep the reduced dimension or not, default 1 mean keep reduced dimension." } ], "inputs": [ { "name": "data", "type": "T", "description": "An input tensor." }, { "name": "data_scale", "type": "tensor(float)", "description": "Input scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "data_zero_point", "type": "T", "option": "optional", "description": "Input zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "reduced_scale", "type": "tensor(float)", "description": "Output scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "reduced_zero_point", "type": "T", "option": "optional", "description": "Output zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." } ], "min_input": 4, "max_input": 5, "outputs": [ { "name": "reduced", "type": "T", "description": "Reduced output tensor." } ], "min_output": 1, "max_output": 1, "inputs_range": "4 - 5", "type_constraints": [ { "description": "Constrain input types to 8 bit signed and unsigned tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearSigmoid", "module": "com.microsoft", "version": 1, "description": "QLinearSigmoid takes quantized input data (Tensor), and quantize parameter for output, and produces one output data\n(Tensor) where the function `f(x) = quantize(Sigmoid(dequantize(x)))`, is applied to the data tensor elementwise.\nWwhere the function `Sigmoid(x) = 1 / (1 + exp(-x))` ", "inputs": [ { "name": "X", "type": "T", "description": "Input tensor" }, { "name": "X_scale", "type": "tensor(float)", "description": "Input X's scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "X_zero_point", "type": "T", "option": "optional", "description": "Input X's zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "Y_scale", "type": "tensor(float)", "description": "Output Y's scale. It's a scalar, which means a per-tensor/layer quantization." }, { "name": "Y_zero_point", "type": "T", "option": "optional", "description": "Output Y's zero point. Default value is 0 if it's not specified. It's a scalar, which means a per-tensor/layer quantization." } ], "min_input": 4, "max_input": 5, "outputs": [ { "name": "Y", "type": "T", "description": "Output tensor" } ], "min_output": 1, "max_output": 1, "inputs_range": "4 - 5", "type_constraints": [ { "description": "Constrain input and output types to 8 bit tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearSoftmax", "module": "com.microsoft", "version": 1, "description": "QLinearSoftmax computes the normalized exponential values for the given input:\nSoftmax(input, axis) = Exp(input) / ReduceSum(Exp(input), axis=axis, keepdims=1)\nThe input does not need to explicitly be a 2D vector. The \"axis\" attribute\nindicates the dimension along which QLinearSoftmax will be performed for onnx v.13+.\nor the dimension coerced to NxD Matrix for onnx v.12-.\nThe output tensor has the same shape.\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": -1, "description": "apply softmax to elements for dimensions axis,or all dims along with axis according to op-version" }, { "name": "opset", "type": "int64", "required": true, "description": "opset version of corresponding SoftMax." } ], "inputs": [ { "name": "X", "type": "T", "description": "The input tensor" }, { "name": "X_scale", "type": "tensor(float)", "description": "Scale of quantized input 'X'. It must be a scalar." }, { "name": "x_zero_point", "type": "T", "option": "optional", "description": "Zero point tensor for input 'X'.It must be a scalar." }, { "name": "y_scale", "type": "tensor(float)", "description": "Scale of quantized output 'Y'. It must be a scalar." }, { "name": "y_zero_point", "type": "T", "description": "Zero point tensor for output 'Y'. It must be a scalar." } ], "min_input": 5, "max_input": 5, "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. " } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to signed/unsigned int8 tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QLinearWhere", "module": "com.microsoft", "version": 1, "description": "Return elements, either from X or Y, depending on condition.", "inputs": [ { "name": "condition", "type": "B", "description": " When True (nonzero), yield x, otherwise yield y" }, { "name": "X", "type": "T", "description": "Y's zero point." }, { "name": "x_scale", "type": "TF", "description": "X's scale." }, { "name": "x_zero_point", "type": "T", "description": "X's zero point." }, { "name": "Y", "type": "T", "description": "Y's zero point." }, { "name": "y_scale", "type": "TF", "description": "Y's scale." }, { "name": "y_zero_point", "type": "T", "description": "Y's zero point." }, { "name": "z_scale", "type": "TF", "description": "Z's scale." }, { "name": "z_zero_point", "type": "T", "description": "Z's zero point." } ], "min_input": 9, "max_input": 9, "outputs": [ { "name": "Z", "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 input and output types to 8 bit signed and unsigned tensors.", "type_param_str": "B", "allowed_type_strs": [ "tensor(bool)" ] }, { "description": "Constrain scale types to any float tensor type.", "type_param_str": "TF", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain input and output types to 8 bit signed and unsigned tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(int8)" ] } ] }, { "name": "QMoE", "module": "com.microsoft", "version": 1, "description": "Quantized mixture of experts (MoE).\n\n The quantized weights are stored in column major order per expert.\n The quantization block size can be specified. If not provided, column wise quantization is used.\n\n The formula of linear dequantization of the quantized weights using scale and (optionally) zero-point is:\n dequantized_weight = (quantized_weight - zero_point) * scale\n When zero_point is not provided, the default value is 2^(bits-1): 2 for 2 bits, 8 for 4 bits, 128 for 8 bits.\n\n If block_size is provided, both hidden_size and inter_size must be divisible by the block size, and\n the dequantization is performed per block of size block_size along the K (input feature) dimension.\n\n If block_size and zero_point are provided, both hidden_size and inter_size must be divisible by block_size * pack_size,\n where pack_size = 8 / expert_weight_bits.\n\n The SwiGLU (Swish-Gated Linear Unit) activation function is like:\n g = xW + b\n l = xV + c\n G = clamp(g, max=limit)\n L = clamp(l, min=-limit, max=limit)\n swiglu = G * sigmoid(alpha * G) * (L + beta)\n where x is the input, W and V are weight matrices, b and c are bias vectors, and alpha, beta and limit are constant float parameters.\n When swiglu_fusion=0, two GEMMs are not fused, and they are FC1 and FC3 in the inputs.\n When swiglu_fusion=1, two GEMMs are fused so that g and l are computed in a single GEMM (FC1), and g and l are interleaved on each row of size 2 * inter_size.\n When swiglu_fusion=2, two GEMMs are fused, and g and l are concatenated on each row.\n ", "attributes": [ { "name": "activation_alpha", "type": "float32", "required": false, "default": 1.0, "description": "Alpha parameter used in activation function." }, { "name": "activation_beta", "type": "float32", "required": false, "description": "Beta parameter used in activation function." }, { "name": "activation_type", "type": "string", "required": false, "default": "relu", "description": "Activation function to use. Choose from relu, gelu, silu, swiglu and identity. Default is relu" }, { "name": "block_size", "type": "int64", "required": false, "description": "Size of each quantization block along the K (input feature) dimension. Must be power of two and \u2265 16 (e.g., 16, 32, 64, 128). If provided, both hidden_size and inter_size must be divisible by the block size. Otherwise, there is no blocking and a whole column shares one scaling factor. " }, { "name": "expert_weight_bits", "type": "int64", "required": false, "default": 4, "description": "Number of bits used in quantized weights. Supported values are 2, 4, and 8. Default is 4 bits" }, { "name": "k", "type": "int64", "required": false, "default": 1, "description": "Number of top experts to select from expert pool" }, { "name": "normalize_routing_weights", "type": "int64", "required": false, "description": "Whether to normalize routing weights" }, { "name": "quant_type", "type": "string", "required": false, "default": "int", "description": "Quantization type: 'int' for integer quantization (default), 'fp4' for MXFP4 quantization, 'fp8' for FP8 e4m3 weight-only quantization, or 'wfp4afp8' for MXFP4 weight with FP8 activation. When quant_type is 'fp4', weights are stored in MXFP4 format (2 values per byte), fc*_scales inputs contain MXFP4 block scales, and fc*_global_scale inputs must be provided." }, { "name": "swiglu_fusion", "type": "int64", "required": false, "description": "0: not fused, 1: fused and interleaved. 2: fused and not interleaved." }, { "name": "swiglu_limit", "type": "float32", "required": false, "description": "The limit used to clamp inputs in SwiGLU. It is infinite when limit is not provided." }, { "name": "use_sparse_mixer", "type": "int64", "required": false, "description": "Whether to use sparse mixer" }, { "name": "weights_prepacked", "type": "int64", "required": false, "default": -1, "description": "Only meaningful when quant_type='int'. Tri-state control over the layout of the int4/int8 fc1/fc2 weight initializers. The concrete prepacked layouts selected by -1 and 1 are determined by the execution provider. 0: the initializers are raw, un-prepacked [E, N, K/pack] tensors as produced by quantize_matmul_{4,8}bits. Defaults to -1." } ], "inputs": [ { "name": "input", "type": "T", "description": "2D tensor with shape (num_tokens, hidden_size), or 3D tensor with shape (batch_size, sequence_length, hidden_size)" }, { "name": "router_probs", "type": "T", "description": "2D tensor with shape (num_tokens, num_experts)" }, { "name": "fc1_experts_weights", "type": "T1", "description": "3D tensor with shape (num_experts, fusion_size * inter_size, hidden_size / pack_size), The fusion_size is 2 for fused swiglu, or 1 otherwise. The pack_size is 8 / expert_weight_bits." }, { "name": "fc1_scales", "type": "T2", "option": "optional", "description": "Optional weight scales. For quant_type='int', this is a 2D tensor with shape (num_experts, fusion_size * inter_size), or a 3D tensor with shape (num_experts, fusion_size * inter_size, hidden_size / block_size) when block_size is provided. For quant_type='fp4' or 'wfp4afp8', this is a float8e8m0 MXFP block-scale tensor with shape (num_experts, fusion_size * inter_size, hidden_size / 32). Not used for quant_type='fp8'." }, { "name": "fc1_experts_bias", "type": "T", "option": "optional", "description": "2D optional tensor with shape (num_experts, fusion_size * inter_size)" }, { "name": "fc2_experts_weights", "type": "T1", "description": "3D tensor with shape (num_experts, hidden_size, inter_size / pack_size)" }, { "name": "fc2_scales", "type": "T2", "option": "optional", "description": "Optional weight scales. For quant_type='int', this is a 2D tensor with shape (num_experts, hidden_size), or a 3D tensor with shape (num_experts, hidden_size, inter_size / block_size) when block_size is provided. For quant_type='fp4' or 'wfp4afp8', this is a float8e8m0 MXFP block-scale tensor with shape (num_experts, hidden_size, inter_size / 32). Not used for quant_type='fp8'." }, { "name": "fc2_experts_bias", "type": "T", "option": "optional", "description": "2D optional tensor with shape (num_experts, hidden_size)" }, { "name": "fc3_experts_weights", "type": "T1", "option": "optional", "description": "3D optional tensor with shape (num_experts, inter_size, hidden_size / pack_size)" }, { "name": "fc3_scales", "type": "T2", "option": "optional", "description": "Optional weight scales. For quant_type='int', this is a 2D tensor with shape (num_experts, inter_size), or a 3D tensor with shape (num_experts, inter_size, hidden_size / block_size) when block_size is provided. For quant_type='fp4' or 'wfp4afp8', this is a float8e8m0 MXFP block-scale tensor with shape (num_experts, inter_size, hidden_size / 32). Not used for quant_type='fp8'." }, { "name": "fc3_experts_bias", "type": "T", "option": "optional", "description": "2D optional tensor with shape (num_experts, inter_size)" }, { "name": "fc1_zero_points", "type": "T1", "option": "optional", "description": "2D tensor with shape (num_experts, fusion_size * inter_size / pack_size), or 3D tensor with shape (num_experts, fusion_size * inter_size, hidden_size / block_size / pack_size) when block_size is provided." }, { "name": "fc2_zero_points", "type": "T1", "option": "optional", "description": "2D tensor with shape (num_experts, hidden_size / pack_size), or 3D tensor with shape (num_experts, hidden_size, inter_size / block_size / pack_size) when block_size is provided." }, { "name": "fc3_zero_points", "type": "T1", "option": "optional", "description": "2D optional tensor with shape (num_experts, inter_size / pack_size), or 3D optional tensor with shape (num_experts, inter_size, hidden_size / block_size / pack_size) when block_size is provided." }, { "name": "router_weights", "type": "T", "option": "optional", "description": "2D optional tensor with shape (num_tokens, num_experts). When provided, router_probs is used only for Top-K expert selection, and router_weights is used for aggregating expert outputs (the values at the selected expert indices are gathered and used as mixing weights). This enables DeepSeek-style noaux_tc routing where different tensors are used for selection and aggregation. When not provided, router_probs is used for both selection and aggregation (backward compatible)." }, { "name": "fc1_global_scale", "type": "T4", "option": "optional", "description": "1D optional tensor with shape (num_experts,). Per-expert global weight scale for FC1. Required when quant_type is 'fp4', 'fp8', or 'wfp4afp8'." }, { "name": "fc2_global_scale", "type": "T4", "option": "optional", "description": "1D optional tensor with shape (num_experts,). Per-expert global weight scale for FC2. Required when quant_type is 'fp4', 'fp8', or 'wfp4afp8'." }, { "name": "fc1_act_scale", "type": "T4", "option": "optional", "description": "1D optional tensor with shape (1,) or (num_experts,). Activation scale for FC1 FP8 activation modes." }, { "name": "fc2_act_scale", "type": "T4", "option": "optional", "description": "1D optional tensor with shape (1,) or (num_experts,). Activation scale for FC2 FP8 activation modes." }, { "name": "fc1_act_block_scale", "type": "T2", "option": "optional", "description": "3D optional float8e8m0 MXFP activation block-scale tensor for FC1 FP8 activation modes." }, { "name": "fc2_act_block_scale", "type": "T2", "option": "optional", "description": "3D optional float8e8m0 MXFP activation block-scale tensor for FC2 FP8 activation modes." } ], "min_input": 6, "max_input": 21, "outputs": [ { "name": "output", "type": "T", "description": "output tensor with same shape of input" } ], "min_output": 1, "max_output": 1, "inputs_range": "6 - 21", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain quantized weight types. Integer and FP4 weights use uint8. FP8 weights use float8e4m3fn.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(uint8)", "tensor(float8e4m3fn)" ] }, { "description": "Constrain scale types. Float tensors are used for integer quantization scales. Float8e8m0 tensors are used for MXFP block scales.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(float8e8m0)" ] }, { "description": "Constrain FP4 global scale type to float32 tensors.", "type_param_str": "T4", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "QOrderedAttention", "module": "com.microsoft", "version": 1, "description": "Quantized version of simplified Multi-Head Self Attention(using int8 with specific matrix Layout).\nMulti-Head Self Attention that can be either unidirectional (like GPT-2) or bidirectional (like BERT).\nThe mask_index input is optional. Besides raw attention mask with shape (batch_size, past_sequence_length + sequence_length)\nor (batch_size, sequence_length, past_sequence_length + sequence_length) with value 0 for masked and 1 otherwise,\nwe also support other two formats: When input has right-side padding, mask_index is one dimension with shape (batch_size),\nwhere value of each element is the end position, or valid length of actual sequence excluding padding. When input has\nleft-side padding, mask_index has shape (2 * batch_size), where the values are the exclusive end positions followed by\nthe inclusive start positions. When unidirectional is 1, and each token only attend to previous tokens. For GPT-2, both past\nand present state are optional. Present state could appear in output even when past state is not in input.\nCurrent version does not support past/present, attention_bias and qkv_hidden_sizes.\nTODO: Support them if needed in the future.\n", "attributes": [ { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "order_input", "type": "int64", "required": true, "description": "cublasLt order of input matrix. See the schema of QuantizeWithOrder for order definition." }, { "name": "order_output", "type": "int64", "required": true, "description": "cublasLt order of global bias" }, { "name": "order_weight", "type": "int64", "required": true, "description": "cublasLt order of weight matrix" }, { "name": "qkv_hidden_sizes", "type": "int64[]", "required": false, "description": "Hidden layer sizes of Q, K, V paths in Attention" }, { "name": "unidirectional", "type": "int64", "required": false, "description": "Whether every token can only attend to previous tokens. Default value is 0." } ], "inputs": [ { "name": "input", "type": "Q", "description": "3D input tensor with shape (batch_size, sequence_length, input_hidden_size)" }, { "name": "scale_input", "type": "S", "description": "scale of the input, scalar value (per tensor) currently." }, { "name": "scale_Q_gemm", "type": "S", "description": "scale of the gemm - scalar (per-tensor quantization)" }, { "name": "scale_K_gemm", "type": "S", "description": "scale of the gemm - scalar (per-tensor quantization)" }, { "name": "scale_V_gemm", "type": "S", "description": "scale of the gemm - scalar (per-tensor quantization)" }, { "name": "Q_weight", "type": "Q", "description": "2D input tensor with shape (input_hidden_size, hidden_size), where hidden_size = num_heads * head_size" }, { "name": "K_weight", "type": "Q", "description": "2D input tensor with shape (input_hidden_size, hidden_size), where hidden_size = num_heads * head_size" }, { "name": "V_weight", "type": "Q", "description": "2D input tensor with shape (input_hidden_size, hidden_size), where hidden_size = num_heads * head_size" }, { "name": "scale_Q_weight", "type": "S", "description": "scale of the weight (scalar for per-tensor quantization or 1-D of dims [hidden_size] for per-channel quantization)" }, { "name": "scale_K_weight", "type": "S", "description": "scale of the weight (scalar for per-tensor quantization or 1-D of dims [hidden_size] for per-channel quantization)" }, { "name": "scale_V_weight", "type": "S", "description": "scale of the weight (scalar for per-tensor quantization or 1-D of dims [hidden_size] for per-channel quantization)" }, { "name": "Q_bias", "type": "S", "description": "1D input tensor with shape (hidden_size)" }, { "name": "K_bias", "type": "S", "description": "1D input tensor with shape (hidden_size)" }, { "name": "V_bias", "type": "S", "description": "1D input tensor with shape (hidden_size)" }, { "name": "scale_QKT_gemm", "type": "S", "option": "optional", "description": "scale of the gemm - scalar (per-tensor quantization)" }, { "name": "scale_QKT_softmax", "type": "S", "option": "optional", "description": "scale of the softmax result - scalar (per-tensor quantization)" }, { "name": "scale_values_gemm", "type": "S", "description": "scale of the gemm - scalar (per-tensor quantization). Also this is the output scale for the operator." }, { "name": "mask_index", "type": "G", "option": "optional", "description": "Attention mask with shape (batch_size, 1, max_sequence_length, max_sequence_length), (batch_size, past_sequence_length + sequence_length)or (batch_size, sequence_length, past_sequence_length + sequence_length), or index with shape (batch_size) or (2 * batch_size)." }, { "name": "past", "type": "Q", "option": "optional", "description": "past state for key and value with shape (2, batch_size, num_heads, past_sequence_length, head_size)." }, { "name": "attention_bias", "type": "S", "option": "optional", "description": "additional add to QxK' with shape (batch_size or 1, num_heads or 1, sequence_length, total_sequence_length)." } ], "min_input": 17, "max_input": 20, "outputs": [ { "name": "output", "type": "Q", "description": "3D output tensor with shape (batch_size, sequence_length, hidden_size)" } ], "min_output": 1, "max_output": 1, "inputs_range": "17 - 20", "type_constraints": [ { "description": "Constrain input and output types to int8 tensors.", "type_param_str": "Q", "allowed_type_strs": [ "tensor(int8)" ] }, { "description": "Constrain scales to float32 tensors.", "type_param_str": "S", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain to integer types", "type_param_str": "G", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "QOrderedGelu", "module": "com.microsoft", "version": 1, "description": "Ordered Quantize Gelu.", "attributes": [ { "name": "order_X", "type": "int64", "required": false, "description": "cublasLt order of input X. Optional. See the schema of QuantizeWithOrder for order definition." }, { "name": "order_Y", "type": "int64", "required": false, "description": "cublasLt order of matrix Y, must be same as order_X if specified together. Optional." } ], "inputs": [ { "name": "X", "type": "Q", "description": "N-dimensional input A" }, { "name": "scale_X", "type": "S", "description": "scale of the input A" }, { "name": "scale_Y", "type": "S", "description": "scale of the output Y" } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "Y", "type": "Q", "description": "Output of the Gelu" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to int8 tensors.", "type_param_str": "Q", "allowed_type_strs": [ "tensor(int8)" ] }, { "description": "Constrain scales to float32", "type_param_str": "S", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "QOrderedLayerNormalization", "module": "com.microsoft", "version": 1, "description": "QOrderedLayerNormalization", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": -1, "description": "The first normalization dimension: normalization will be performed along dimensions axis : rank(inputs)." }, { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "The epsilon value to use to avoid division by zero." }, { "name": "order_X", "type": "int64", "required": false, "default": 1, "description": "cublasLt order of input X. Default is ROW MAJOR. See the schema of QuantizeWithOrder for order definition." }, { "name": "order_Y", "type": "int64", "required": false, "default": 1, "description": "cublasLt order of matrix Y, must be same as order_X. Default is ROW MAJOR." } ], "inputs": [ { "name": "X", "type": "Q", "description": "Input data tensor from the previous layer." }, { "name": "scale_X", "type": "S", "description": "scale of the quantized X" }, { "name": "scale", "type": "F", "description": "Scale tensor, i.e., gamma vector." }, { "name": "B", "type": "F", "option": "optional", "description": "Bias tensor." }, { "name": "scale_Y", "type": "S", "description": "scale of the quantized X" } ], "min_input": 5, "max_input": 5, "outputs": [ { "name": "Y", "type": "Q", "description": "Output data tensor." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input gamma and bias could be float16/float tensors. float may get better precision, float16 runs faster.", "type_param_str": "F", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] }, { "description": "quantization scale must be float tensors.", "type_param_str": "S", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "quantization tensor must be int8 tensors.", "type_param_str": "Q", "allowed_type_strs": [ "tensor(int8)" ] } ] }, { "name": "QOrderedLongformerAttention", "module": "com.microsoft", "version": 1, "description": "Quantized version of Longformer Self Attention (using int8 with specific matrix Layout).", "attributes": [ { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads" }, { "name": "order_global_weight", "type": "int64", "required": true, "description": "cublasLt order of weight matrix" }, { "name": "order_input", "type": "int64", "required": true, "description": "cublasLt order of input matrix. See the schema of QuantizeWithOrder for order definition." }, { "name": "order_output", "type": "int64", "required": true, "description": "cublasLt order of global bias" }, { "name": "order_weight", "type": "int64", "required": true, "description": "cublasLt order of weight matrix" }, { "name": "window", "type": "int64", "required": true, "description": "One sided attention windows length W, or half of total window length" } ], "inputs": [ { "name": "input", "type": "Q", "description": "3D input tensor with shape (batch_size, sequence_length, hidden_size), hidden_size = num_heads * head_size" }, { "name": "scale_input", "type": "S", "description": "scale of the input" }, { "name": "weight", "type": "Q", "description": "2D input tensor with shape (hidden_size, 3 * hidden_size)" }, { "name": "scale_weight", "type": "S", "description": "scale of the weight" }, { "name": "bias", "type": "S", "description": "1D input tensor with shape (3 * hidden_size), fp32 only currently." }, { "name": "scale_bias", "type": "S", "description": "reserved. (not used as add bias need float value in cublasLt for normal order.)" }, { "name": "scale_qkv_gemm", "type": "S", "description": "scale of the output for fused kqv gemm" }, { "name": "mask", "type": "F", "description": "Attention mask with shape (batch_size, sequence_length)" }, { "name": "global_weight", "type": "Q", "description": "2D input tensor with shape (hidden_size, 3 * hidden_size)" }, { "name": "scale_global_weight", "type": "S", "description": "scale of the global_weight" }, { "name": "global_bias", "type": "S", "description": "1D input tensor with shape (3 * hidden_size)" }, { "name": "scale_global_gemm", "type": "S", "description": "scale of the global_qkv_gemm" }, { "name": "global", "type": "G", "description": "Global attention flags with shape (batch_size, sequence_length)" }, { "name": "scale_output", "type": "S", "description": "scale of the output" } ], "min_input": 14, "max_input": 14, "outputs": [ { "name": "output", "type": "Q", "description": "3D output tensor with shape (batch_size, sequence_length, hidden_size)" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to int8 tensors.", "type_param_str": "Q", "allowed_type_strs": [ "tensor(int8)" ] }, { "description": "Constrain scales to float32 tensors.", "type_param_str": "S", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain to integer types", "type_param_str": "G", "allowed_type_strs": [ "tensor(int32)" ] }, { "description": "Be compatible with float version.", "type_param_str": "F", "allowed_type_strs": [ "tensor(float16)" ] } ], "category": "Attention" }, { "name": "QOrderedMatMul", "module": "com.microsoft", "version": 1, "description": "Quantize (Int8) MatMul with order. Implement Y = alpha * A * B + bias + beta * C. Matrix A, B, C, Y are all int8 matrix.\nTwo type of order combination supported:\n *) When order_B is ORDER_COL, order_A must be ORDER_ROW.\n bias is vector of {#cols of Y} of float32, C should be batch 1/batch_A. B could be of batch 1 or batch_A.\n Note B is reorder to ORDER_COL, or Transposed. Not Transposed first and then Reordered here.\n *) When order_B is specify ORDER_COL4_4R2_8C or ORDER_COL32_2R_4R4, orderA must be ORDER_COL32.\n MatMul will be implemented using alpha(A * B) + beta * C => Y.\n bias is not supported here. B in fact is transposed first then reordered into ORDER_COL4_4R2_8C or ORDER_COL32_2R_4R4 here.\norder_Y and order_C will be same as order_A.\nSupport per column quantized weight, ie, scale_B is 1-D vector of size [#cols of matrix B].\n", "attributes": [ { "name": "order_A", "type": "int64", "required": true, "description": "cublasLt order of matrix A. See the schema of QuantizeWithOrder for order definition." }, { "name": "order_B", "type": "int64", "required": true, "description": "cublasLt order of matrix B" }, { "name": "order_Y", "type": "int64", "required": true, "description": "cublasLt order of matrix Y and optional matrix C" } ], "inputs": [ { "name": "A", "type": "Q", "description": "3-dimensional matrix A" }, { "name": "scale_A", "type": "S", "description": "scale of the input A." }, { "name": "B", "type": "Q", "description": "2-dimensional matrix B. Transposed if order_B is ORDER_COL." }, { "name": "scale_B", "type": "S", "description": "scale of the input B. Scalar or 1-D float32." }, { "name": "scale_Y", "type": "S", "description": "scale of the output Y." }, { "name": "bias", "type": "S", "option": "optional", "description": "1d bias, not scaled with scale_Y." }, { "name": "C", "type": "Q", "option": "optional", "description": "3d or 2d matrix C. if 2d expand to 3d first. Shape[0] should be 1 or same as A.shape[0] " }, { "name": "scale_C", "type": "S", "option": "optional", "description": "scale of the input A." } ], "min_input": 5, "max_input": 8, "outputs": [ { "name": "Y", "type": "Q", "description": "Matrix multiply results from A * B" } ], "min_output": 1, "max_output": 1, "inputs_range": "5 - 8", "type_constraints": [ { "description": "Constrain input and output types to int8 tensors.", "type_param_str": "Q", "allowed_type_strs": [ "tensor(int8)" ] }, { "description": "Constrain bias and scales to float32", "type_param_str": "S", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "QuantizeBFP", "module": "com.microsoft", "version": 1, "description": "The BFP quantization operator. It consumes a full precision tensor and computes an BFP tensor.\nMore documentation on the BFP format can be found in this paper: https://www.microsoft.com/en-us/research/publication/pushing-the-limits-of-narrow-precision-inferencing-at-cloud-scale-with-microsoft-floating-point/", "attributes": [ { "name": "bfp_type", "type": "int64", "required": true, "description": "The type of BFP - must match with the BFPType enum" }, { "name": "block_dim", "type": "int64", "required": false, "default": -1, "description": "Each bounding box spans this dimension.Typically, the block dimension corresponds to the reduction dimension of the matrix multipication that consumes the output of this operator.For example, for a 2D matrix multiplication A@W, QuantizeBFP(A) would use block_dim 1 and QuantizeBFP(W) would use block_dim 0.The default is the last dimension." } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D full precision input tensor to be quantized." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "y", "type": "T2", "description": "1-D, contiguous BFP data" }, { "name": "shape", "type": "T3", "description": "Shape of x" }, { "name": "strides", "type": "T3", "description": "Strides of x" } ], "min_output": 3, "max_output": 3, "type_constraints": [ { "description": "Constrain the input to float and bfloat.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain y to uint8.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(uint8)" ] }, { "description": "Constrain shape and strides to uint64.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(int64)" ] } ] }, { "name": "QuantizeLinear", "module": "ai.onnx", "version": 10, "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 the nearest 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(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n [\n [[-162, 10], [-100, 232], [-20, -50]],\n [[-76, 0], [0, 252], [32, -44]],\n [[245, -485], [-960, -270], [-375, -470]],\n ],\n ],\n dtype=np.float32,\n)\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(\n np.uint8\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_axis\",\n)" }, { "summary": "blocked_asymmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [6.0, 12.0, 50.0, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\ny_zero_point = np.array(\n [\n [0, 1],\n [1, 0],\n [2, 3],\n ],\n dtype=np.uint8,\n)\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\nassert y_scale.shape == y_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\ny_zero_point_elementwise = np.repeat(\n y_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = np.rint(x / y_scale_elementwise + y_zero_point_elementwise).astype(np.uint8)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_blocked_asymmetric\",\n)" }, { "summary": "blocked_symmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n output_dtype=TensorProto.INT16,\n)\n\nx = np.array(\n [\n [6.0, -8, -10, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\n\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\n\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\n\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\n\ny_val = np.clip(\n np.rint(x / y_scale_elementwise), a_min=-32768, a_max=32767\n).astype(np.int16)\ny = make_tensor(\n \"y\",\n TensorProto.INT16,\n x.shape,\n y_val,\n)\nexpect(\n node,\n inputs=[x, y_scale],\n outputs=[y],\n name=\"test_quantizelinear_blocked_symmetric\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e4m3fn\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E5M2, [1], [0.0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [-0.0, -2.5, -4.8, -8.6],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\",\n TensorProto.FLOAT4E2M1,\n y_scale.shape,\n np.zeros_like(y_scale),\n)\ny = make_tensor(\n \"y\",\n TensorProto.FLOAT4E2M1,\n x.shape,\n [0, 1, 2, 4, -6, -6, 2, 3, 0, -0.5, -1, -2],\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -514.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65022.0,\n -66046.0,\n 65023.0,\n -66047.0,\n 65024.0,\n -66048.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.int16(256)\ny = np.array(\n [\n 256,\n -1,\n 258,\n 254,\n 257,\n 255,\n 258,\n 254,\n 32767,\n -32767,\n 32767,\n -32768,\n 32767,\n -32768,\n 32767,\n -32768,\n ]\n).astype(np.int16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-4.0, -3.0, 1.0, 2.0],\n [-0.0, -2.5, -4.8, -8.6],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT2, x.shape, [0, 1, 1, 1, -1, -1, 0, 1, 0, -1, -1, -2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT4, x.shape, [1, 2, 3, 5, -8, -6, 3, 4, 4, 5, 5, 7]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int4\",\n)" }, { "summary": "quantizelinear", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -128.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65536.0,\n -65534.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.uint16(32767)\ny = np.array(\n [\n 32767,\n 32703,\n 32769,\n 32765,\n 32768,\n 32766,\n 32769,\n 32765,\n 65535,\n 0,\n 65535,\n 0,\n ]\n).astype(np.uint16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-2.0, -1.0, 1.0, 3.0],\n [4.0, 5.0, 6.0, 7.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT2, x.shape, [0, 1, 2, 3, 0, 0, 0, 1, 1, 1, 2, 2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT4, x.shape, [1, 2, 3, 5, 0, 0, 3, 4, 4, 5, 5, 11]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint4\",\n)" } ] }, { "name": "QuantizeLinear", "module": "ai.onnx", "version": 13, "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 the nearest 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(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n [\n [[-162, 10], [-100, 232], [-20, -50]],\n [[-76, 0], [0, 252], [32, -44]],\n [[245, -485], [-960, -270], [-375, -470]],\n ],\n ],\n dtype=np.float32,\n)\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(\n np.uint8\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_axis\",\n)" }, { "summary": "blocked_asymmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [6.0, 12.0, 50.0, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\ny_zero_point = np.array(\n [\n [0, 1],\n [1, 0],\n [2, 3],\n ],\n dtype=np.uint8,\n)\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\nassert y_scale.shape == y_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\ny_zero_point_elementwise = np.repeat(\n y_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = np.rint(x / y_scale_elementwise + y_zero_point_elementwise).astype(np.uint8)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_blocked_asymmetric\",\n)" }, { "summary": "blocked_symmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n output_dtype=TensorProto.INT16,\n)\n\nx = np.array(\n [\n [6.0, -8, -10, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\n\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\n\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\n\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\n\ny_val = np.clip(\n np.rint(x / y_scale_elementwise), a_min=-32768, a_max=32767\n).astype(np.int16)\ny = make_tensor(\n \"y\",\n TensorProto.INT16,\n x.shape,\n y_val,\n)\nexpect(\n node,\n inputs=[x, y_scale],\n outputs=[y],\n name=\"test_quantizelinear_blocked_symmetric\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e4m3fn\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E5M2, [1], [0.0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [-0.0, -2.5, -4.8, -8.6],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\",\n TensorProto.FLOAT4E2M1,\n y_scale.shape,\n np.zeros_like(y_scale),\n)\ny = make_tensor(\n \"y\",\n TensorProto.FLOAT4E2M1,\n x.shape,\n [0, 1, 2, 4, -6, -6, 2, 3, 0, -0.5, -1, -2],\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -514.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65022.0,\n -66046.0,\n 65023.0,\n -66047.0,\n 65024.0,\n -66048.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.int16(256)\ny = np.array(\n [\n 256,\n -1,\n 258,\n 254,\n 257,\n 255,\n 258,\n 254,\n 32767,\n -32767,\n 32767,\n -32768,\n 32767,\n -32768,\n 32767,\n -32768,\n ]\n).astype(np.int16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-4.0, -3.0, 1.0, 2.0],\n [-0.0, -2.5, -4.8, -8.6],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT2, x.shape, [0, 1, 1, 1, -1, -1, 0, 1, 0, -1, -1, -2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT4, x.shape, [1, 2, 3, 5, -8, -6, 3, 4, 4, 5, 5, 7]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int4\",\n)" }, { "summary": "quantizelinear", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -128.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65536.0,\n -65534.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.uint16(32767)\ny = np.array(\n [\n 32767,\n 32703,\n 32769,\n 32765,\n 32768,\n 32766,\n 32769,\n 32765,\n 65535,\n 0,\n 65535,\n 0,\n ]\n).astype(np.uint16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-2.0, -1.0, 1.0, 3.0],\n [4.0, 5.0, 6.0, 7.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT2, x.shape, [0, 1, 2, 3, 0, 0, 0, 1, 1, 1, 2, 2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT4, x.shape, [1, 2, 3, 5, 0, 0, 3, 4, 4, 5, 5, 11]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint4\",\n)" } ] }, { "name": "QuantizeLinear", "module": "ai.onnx", "version": 19, "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 the nearest even. Refer to https://en.wikipedia.org/wiki/Rounding for details.\n'y_zero_point' and 'y' must have same type.\n'y_zero_point' is usually not used for quantization to float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz,\nbut the quantization formula remains the same for consistency and\nthe type of the attribute 'y_zero_point' still determines the quantization 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)." }, { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 quantization (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. All cases are fully described in two tables inserted in the operator description." } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D full precision Input tensor to be quantized." }, { "name": "y_scale", "type": "T1", "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, float16, bfloat16 or int32 tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(int32)" ] }, { "description": "Constrain 'y_zero_point' and 'y' to 8-bit integer/float tensor.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n [\n [[-162, 10], [-100, 232], [-20, -50]],\n [[-76, 0], [0, 252], [32, -44]],\n [[245, -485], [-960, -270], [-375, -470]],\n ],\n ],\n dtype=np.float32,\n)\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(\n np.uint8\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_axis\",\n)" }, { "summary": "blocked_asymmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [6.0, 12.0, 50.0, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\ny_zero_point = np.array(\n [\n [0, 1],\n [1, 0],\n [2, 3],\n ],\n dtype=np.uint8,\n)\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\nassert y_scale.shape == y_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\ny_zero_point_elementwise = np.repeat(\n y_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = np.rint(x / y_scale_elementwise + y_zero_point_elementwise).astype(np.uint8)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_blocked_asymmetric\",\n)" }, { "summary": "blocked_symmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n output_dtype=TensorProto.INT16,\n)\n\nx = np.array(\n [\n [6.0, -8, -10, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\n\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\n\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\n\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\n\ny_val = np.clip(\n np.rint(x / y_scale_elementwise), a_min=-32768, a_max=32767\n).astype(np.int16)\ny = make_tensor(\n \"y\",\n TensorProto.INT16,\n x.shape,\n y_val,\n)\nexpect(\n node,\n inputs=[x, y_scale],\n outputs=[y],\n name=\"test_quantizelinear_blocked_symmetric\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e4m3fn\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E5M2, [1], [0.0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [-0.0, -2.5, -4.8, -8.6],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\",\n TensorProto.FLOAT4E2M1,\n y_scale.shape,\n np.zeros_like(y_scale),\n)\ny = make_tensor(\n \"y\",\n TensorProto.FLOAT4E2M1,\n x.shape,\n [0, 1, 2, 4, -6, -6, 2, 3, 0, -0.5, -1, -2],\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -514.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65022.0,\n -66046.0,\n 65023.0,\n -66047.0,\n 65024.0,\n -66048.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.int16(256)\ny = np.array(\n [\n 256,\n -1,\n 258,\n 254,\n 257,\n 255,\n 258,\n 254,\n 32767,\n -32767,\n 32767,\n -32768,\n 32767,\n -32768,\n 32767,\n -32768,\n ]\n).astype(np.int16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-4.0, -3.0, 1.0, 2.0],\n [-0.0, -2.5, -4.8, -8.6],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT2, x.shape, [0, 1, 1, 1, -1, -1, 0, 1, 0, -1, -1, -2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT4, x.shape, [1, 2, 3, 5, -8, -6, 3, 4, 4, 5, 5, 7]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int4\",\n)" }, { "summary": "quantizelinear", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -128.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65536.0,\n -65534.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.uint16(32767)\ny = np.array(\n [\n 32767,\n 32703,\n 32769,\n 32765,\n 32768,\n 32766,\n 32769,\n 32765,\n 65535,\n 0,\n 65535,\n 0,\n ]\n).astype(np.uint16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-2.0, -1.0, 1.0, 3.0],\n [4.0, 5.0, 6.0, 7.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT2, x.shape, [0, 1, 2, 3, 0, 0, 0, 1, 1, 1, 2, 2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT4, x.shape, [1, 2, 3, 5, 0, 0, 3, 4, 4, 5, 5, 11]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint4\",\n)" } ] }, { "name": "QuantizeLinear", "module": "ai.onnx", "version": 21, "description": "The linear quantization operator consumes a high-precision tensor, a scale, and a zero point to compute the\nlow-precision/quantized tensor. The scale factor and zero point must have the same shape, determining the quantization\ngranularity. The quantization formula is `y = saturate((x / y_scale) + y_zero_point)`.\nSaturation is done according to:\n- uint16: [0, 65535]\n- int16: [-32768, 32767]\n- uint8: [0, 255]\n- int8: [-128, 127]\n- uint4: [0, 15]\n- int4: [-8, 7]\nFor `(x / y_scale)`, it rounds to the nearest even. Refer to https://en.wikipedia.org/wiki/Rounding for details.\n`y_zero_point` and `y` must have the same type. `y_zero_point` is usually not used for quantization to float8 types, but the quantization\nformula remains the same for consistency, and the type of the attribute `y_zero_point` still determines the quantization type.\nThere are three supported quantization granularities, determined by the shape of `y_scale`.\nIn all cases, `y_zero_point` must have the same shape as `y_scale`.\n- Per-tensor (per-layer) quantization: `y_scale` is a scalar.\n- Per-axis quantization: The scale must be a 1-D tensor, with the length of the quantization axis. For an input shape\n `(D0, ..., Di, ..., Dn)` and `axis=i`, `y_scale` is a 1-D tensor of length `Di`.\n- Blocked quantization: The scale's shape is identical to the input's shape, except for one dimension, in which\n blocking is performed. Given `x` shape `(D0, ..., Di, ..., Dn)`, `axis=i`, and block size `B`: `y_scale` shape is\n `(D0, ..., ceil(Di/B), ..., Dn)`.\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Used only for per-axis and blocked quantization. Negative value means counting dimensions from the back. Accepted range is `[-r, r-1]` where `r = rank(input)`. When the rank of the input is 1, per-tensor quantization is applied, rendering the axis unnecessary in this scenario." }, { "name": "block_size", "type": "int64", "required": false, "description": "(Optional) The size of the quantization block (number of times every scale is replicated). Used only for blocked quantization. The block size is a positive integer. Given `x` shape `(D0, ..., Di, ..., Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`" }, { "name": "output_dtype", "type": "int64", "required": false, "description": "(Optional) The output data type. If not supplied, the output data type is inferred from `y_zero_point` data type (`T2`). If neither `output_dtype` nor `y_zero_point` are supplied, output data type is uint8. If both `output_dtype` and `y_zero_point` are specified, `output_dtype` must be `T2`." }, { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 quantization (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. All cases are fully described in two tables inserted in the operator description." } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D full precision Input tensor to be quantized." }, { "name": "y_scale", "type": "T1", "description": "Scale for doing quantization to get `y`. For per-tensor/layer quantization the scale is a scalar, for per-axis quantization it is a 1-D Tensor and for blocked quantization it has the same shape as the input, except for one dimension in which blocking is performed." }, { "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": "The type of the input 'x'.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(int32)" ] }, { "description": "The type of the input `y_zero_point` and the output `y`.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n [\n [[-162, 10], [-100, 232], [-20, -50]],\n [[-76, 0], [0, 252], [32, -44]],\n [[245, -485], [-960, -270], [-375, -470]],\n ],\n ],\n dtype=np.float32,\n)\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(\n np.uint8\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_axis\",\n)" }, { "summary": "blocked_asymmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [6.0, 12.0, 50.0, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\ny_zero_point = np.array(\n [\n [0, 1],\n [1, 0],\n [2, 3],\n ],\n dtype=np.uint8,\n)\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\nassert y_scale.shape == y_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\ny_zero_point_elementwise = np.repeat(\n y_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = np.rint(x / y_scale_elementwise + y_zero_point_elementwise).astype(np.uint8)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_blocked_asymmetric\",\n)" }, { "summary": "blocked_symmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n output_dtype=TensorProto.INT16,\n)\n\nx = np.array(\n [\n [6.0, -8, -10, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\n\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\n\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\n\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\n\ny_val = np.clip(\n np.rint(x / y_scale_elementwise), a_min=-32768, a_max=32767\n).astype(np.int16)\ny = make_tensor(\n \"y\",\n TensorProto.INT16,\n x.shape,\n y_val,\n)\nexpect(\n node,\n inputs=[x, y_scale],\n outputs=[y],\n name=\"test_quantizelinear_blocked_symmetric\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e4m3fn\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E5M2, [1], [0.0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [-0.0, -2.5, -4.8, -8.6],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\",\n TensorProto.FLOAT4E2M1,\n y_scale.shape,\n np.zeros_like(y_scale),\n)\ny = make_tensor(\n \"y\",\n TensorProto.FLOAT4E2M1,\n x.shape,\n [0, 1, 2, 4, -6, -6, 2, 3, 0, -0.5, -1, -2],\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -514.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65022.0,\n -66046.0,\n 65023.0,\n -66047.0,\n 65024.0,\n -66048.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.int16(256)\ny = np.array(\n [\n 256,\n -1,\n 258,\n 254,\n 257,\n 255,\n 258,\n 254,\n 32767,\n -32767,\n 32767,\n -32768,\n 32767,\n -32768,\n 32767,\n -32768,\n ]\n).astype(np.int16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-4.0, -3.0, 1.0, 2.0],\n [-0.0, -2.5, -4.8, -8.6],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT2, x.shape, [0, 1, 1, 1, -1, -1, 0, 1, 0, -1, -1, -2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT4, x.shape, [1, 2, 3, 5, -8, -6, 3, 4, 4, 5, 5, 7]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int4\",\n)" }, { "summary": "quantizelinear", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -128.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65536.0,\n -65534.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.uint16(32767)\ny = np.array(\n [\n 32767,\n 32703,\n 32769,\n 32765,\n 32768,\n 32766,\n 32769,\n 32765,\n 65535,\n 0,\n 65535,\n 0,\n ]\n).astype(np.uint16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-2.0, -1.0, 1.0, 3.0],\n [4.0, 5.0, 6.0, 7.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT2, x.shape, [0, 1, 2, 3, 0, 0, 0, 1, 1, 1, 2, 2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT4, x.shape, [1, 2, 3, 5, 0, 0, 3, 4, 4, 5, 5, 11]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint4\",\n)" } ] }, { "name": "QuantizeLinear", "module": "ai.onnx", "version": 23, "description": "The linear quantization operator consumes a high-precision tensor, a scale, and a zero point to compute the\nlow-precision/quantized tensor. The scale factor and zero point must have the same shape, determining the quantization\ngranularity. The quantization formula is `y = saturate((x / y_scale) + y_zero_point)`.\n\nSaturation is done according to:\n- uint16: [0, 65535]\n- int16: [-32768, 32767]\n- uint8: [0, 255]\n- int8: [-128, 127]\n- uint4: [0, 15]\n- int4: [-8, 7]\n\nFor `(x / y_scale)`, it rounds to the nearest even. Refer to https://en.wikipedia.org/wiki/Rounding for details.\n\n`y_zero_point` and `y` must have the same type. `y_zero_point` is usually not used for quantization to float8 and 4bit types, but the quantization\nformula remains the same for consistency, and the type of the attribute `y_zero_point` still determines the quantization type.\n`x` and `y_scale` are allowed to have different types. The type of `y_scale` determines the precision of the division operation between `x` and\n`y_scale`, unless the `precision` attribute is specified.\n\nThere are three supported quantization granularities, determined by the shape of `y_scale`.\nIn all cases, `y_zero_point` must have the same shape as `y_scale`.\n- Per-tensor (per-layer) quantization: `y_scale` is a scalar.\n- Per-axis quantization: The scale must be a 1-D tensor, with the length of the quantization axis. For an input shape\n `(D0, ..., Di, ..., Dn)` and `axis=i`, `y_scale` is a 1-D tensor of length `Di`.\n- Blocked quantization: The scale's shape is identical to the input's shape, except for one dimension, in which\n blocking is performed. Given `x` shape `(D0, ..., Di, ..., Dn)`, `axis=i`, and block size `B`: `y_scale` shape is\n `(D0, ..., ceil(Di/B), ..., Dn)`.\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Used only for per-axis and blocked quantization. Negative value means counting dimensions from the back. Accepted range is `[-r, r-1]` where `r = rank(input)`. When the rank of the input is 1, per-tensor quantization is applied, rendering the axis unnecessary in this scenario." }, { "name": "block_size", "type": "int64", "required": false, "description": "(Optional) The size of the quantization block (number of times every scale is replicated). Used only for blocked quantization. The block size is a positive integer. Given `x` shape `(D0, ..., Di, ..., Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`" }, { "name": "output_dtype", "type": "int64", "required": false, "description": "(Optional) The output data type. If not supplied, the output data type is inferred from `y_zero_point` data type (`T3`). If neither `output_dtype` nor `y_zero_point` are supplied, output data type is uint8. If both `output_dtype` and `y_zero_point` are specified, `output_dtype` must be `T3`." }, { "name": "precision", "type": "int64", "required": false, "description": "(Optional) The precision of the division operation between `x` and `y_scale`. If not provided, it will be the same as the type of `y_scale`." }, { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 quantization (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. All cases are fully described in two tables inserted in the operator description." } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D full precision Input tensor to be quantized." }, { "name": "y_scale", "type": "T2", "description": "Scale for doing quantization to get `y`. For per-tensor/layer quantization the scale is a scalar, for per-axis quantization it is a 1-D Tensor and for blocked quantization it has the same shape as the input, except for one dimension in which blocking is performed." }, { "name": "y_zero_point", "type": "T3", "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": "T3", "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": "The type of the input 'x'.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(int32)" ] }, { "description": "The type of the input 'y_scale'.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(int32)" ] }, { "description": "The type of the input `y_zero_point` and the output `y`.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n [\n [[-162, 10], [-100, 232], [-20, -50]],\n [[-76, 0], [0, 252], [32, -44]],\n [[245, -485], [-960, -270], [-375, -470]],\n ],\n ],\n dtype=np.float32,\n)\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(\n np.uint8\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_axis\",\n)" }, { "summary": "blocked_asymmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [6.0, 12.0, 50.0, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\ny_zero_point = np.array(\n [\n [0, 1],\n [1, 0],\n [2, 3],\n ],\n dtype=np.uint8,\n)\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\nassert y_scale.shape == y_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\ny_zero_point_elementwise = np.repeat(\n y_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = np.rint(x / y_scale_elementwise + y_zero_point_elementwise).astype(np.uint8)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_blocked_asymmetric\",\n)" }, { "summary": "blocked_symmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n output_dtype=TensorProto.INT16,\n)\n\nx = np.array(\n [\n [6.0, -8, -10, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\n\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\n\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\n\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\n\ny_val = np.clip(\n np.rint(x / y_scale_elementwise), a_min=-32768, a_max=32767\n).astype(np.int16)\ny = make_tensor(\n \"y\",\n TensorProto.INT16,\n x.shape,\n y_val,\n)\nexpect(\n node,\n inputs=[x, y_scale],\n outputs=[y],\n name=\"test_quantizelinear_blocked_symmetric\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e4m3fn\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E5M2, [1], [0.0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [-0.0, -2.5, -4.8, -8.6],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\",\n TensorProto.FLOAT4E2M1,\n y_scale.shape,\n np.zeros_like(y_scale),\n)\ny = make_tensor(\n \"y\",\n TensorProto.FLOAT4E2M1,\n x.shape,\n [0, 1, 2, 4, -6, -6, 2, 3, 0, -0.5, -1, -2],\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -514.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65022.0,\n -66046.0,\n 65023.0,\n -66047.0,\n 65024.0,\n -66048.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.int16(256)\ny = np.array(\n [\n 256,\n -1,\n 258,\n 254,\n 257,\n 255,\n 258,\n 254,\n 32767,\n -32767,\n 32767,\n -32768,\n 32767,\n -32768,\n 32767,\n -32768,\n ]\n).astype(np.int16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-4.0, -3.0, 1.0, 2.0],\n [-0.0, -2.5, -4.8, -8.6],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT2, x.shape, [0, 1, 1, 1, -1, -1, 0, 1, 0, -1, -1, -2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT4, x.shape, [1, 2, 3, 5, -8, -6, 3, 4, 4, 5, 5, 7]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int4\",\n)" }, { "summary": "quantizelinear", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -128.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65536.0,\n -65534.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.uint16(32767)\ny = np.array(\n [\n 32767,\n 32703,\n 32769,\n 32765,\n 32768,\n 32766,\n 32769,\n 32765,\n 65535,\n 0,\n 65535,\n 0,\n ]\n).astype(np.uint16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-2.0, -1.0, 1.0, 3.0],\n [4.0, 5.0, 6.0, 7.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT2, x.shape, [0, 1, 2, 3, 0, 0, 0, 1, 1, 1, 2, 2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT4, x.shape, [1, 2, 3, 5, 0, 0, 3, 4, 4, 5, 5, 11]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint4\",\n)" } ] }, { "name": "QuantizeLinear", "module": "ai.onnx", "version": 24, "description": "The linear quantization operator consumes a high-precision tensor, a scale, and a zero point to compute the\nlow-precision/quantized tensor. The scale factor and zero point must have the same shape, determining the quantization\ngranularity. The quantization formula is `y = saturate((x / y_scale) + y_zero_point)`.\n\nSaturation is done according to:\n- uint16: [0, 65535]\n- int16: [-32768, 32767]\n- uint8: [0, 255]\n- int8: [-128, 127]\n- uint4: [0, 15]\n- int4: [-8, 7]\n\nFor `(x / y_scale)`, it rounds to the nearest even. Refer to https://en.wikipedia.org/wiki/Rounding for details.\n\n`y_zero_point` and `y` must have the same type. `y_zero_point` is usually not used for quantization to float8 and 4bit types, but the quantization\nformula remains the same for consistency, and the type of the attribute `y_zero_point` still determines the quantization type.\n`x` and `y_scale` are allowed to have different types. The type of `y_scale` determines the precision of the division operation between `x` and\n`y_scale`, unless the `precision` attribute is specified.\n\nThere are three supported quantization granularities, determined by the shape of `y_scale`.\nIn all cases, `y_zero_point` must have the same shape as `y_scale`.\n- Per-tensor (per-layer) quantization: `y_scale` is a scalar.\n- Per-axis quantization: The scale must be a 1-D tensor, with the length of the quantization axis. For an input shape\n `(D0, ..., Di, ..., Dn)` and `axis=i`, `y_scale` is a 1-D tensor of length `Di`.\n- Blocked quantization: The scale's shape is identical to the input's shape, except for one dimension, in which\n blocking is performed. Given `x` shape `(D0, ..., Di, ..., Dn)`, `axis=i`, and block size `B`: `y_scale` shape is\n `(D0, ..., ceil(Di/B), ..., Dn)`.\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Used only for per-axis and blocked quantization. Negative value means counting dimensions from the back. Accepted range is `[-r, r-1]` where `r = rank(input)`. When the rank of the input is 1, per-tensor quantization is applied, rendering the axis unnecessary in this scenario." }, { "name": "block_size", "type": "int64", "required": false, "description": "(Optional) The size of the quantization block (number of times every scale is replicated). Used only for blocked quantization. The block size is a positive integer. Given `x` shape `(D0, ..., Di, ..., Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`" }, { "name": "output_dtype", "type": "int64", "required": false, "description": "(Optional) The output data type. If not supplied, the output data type is inferred from `y_zero_point` data type (`T3`). If neither `output_dtype` nor `y_zero_point` are supplied, output data type is uint8. If both `output_dtype` and `y_zero_point` are specified, `output_dtype` must be `T3`." }, { "name": "precision", "type": "int64", "required": false, "description": "(Optional) The precision of the division operation between `x` and `y_scale`. If not provided, it will be the same as the type of `y_scale`." }, { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 quantization (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. All cases are fully described in two tables inserted in the operator description." } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D full precision Input tensor to be quantized." }, { "name": "y_scale", "type": "T2", "description": "Scale for doing quantization to get `y`. For per-tensor/layer quantization the scale is a scalar, for per-axis quantization it is a 1-D Tensor and for blocked quantization it has the same shape as the input, except for one dimension in which blocking is performed." }, { "name": "y_zero_point", "type": "T3", "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": "T3", "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": "The type of the input 'x'.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(int32)" ] }, { "description": "The type of the input 'y_scale'.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(int32)", "tensor(float8e8m0)" ] }, { "description": "The type of the input `y_zero_point` and the output `y`.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n [\n [[-162, 10], [-100, 232], [-20, -50]],\n [[-76, 0], [0, 252], [32, -44]],\n [[245, -485], [-960, -270], [-375, -470]],\n ],\n ],\n dtype=np.float32,\n)\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(\n np.uint8\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_axis\",\n)" }, { "summary": "blocked_asymmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [6.0, 12.0, 50.0, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\ny_zero_point = np.array(\n [\n [0, 1],\n [1, 0],\n [2, 3],\n ],\n dtype=np.uint8,\n)\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\nassert y_scale.shape == y_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\ny_zero_point_elementwise = np.repeat(\n y_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = np.rint(x / y_scale_elementwise + y_zero_point_elementwise).astype(np.uint8)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_blocked_asymmetric\",\n)" }, { "summary": "blocked_symmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n output_dtype=TensorProto.INT16,\n)\n\nx = np.array(\n [\n [6.0, -8, -10, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\n\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\n\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\n\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\n\ny_val = np.clip(\n np.rint(x / y_scale_elementwise), a_min=-32768, a_max=32767\n).astype(np.int16)\ny = make_tensor(\n \"y\",\n TensorProto.INT16,\n x.shape,\n y_val,\n)\nexpect(\n node,\n inputs=[x, y_scale],\n outputs=[y],\n name=\"test_quantizelinear_blocked_symmetric\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e4m3fn\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E5M2, [1], [0.0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [-0.0, -2.5, -4.8, -8.6],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\",\n TensorProto.FLOAT4E2M1,\n y_scale.shape,\n np.zeros_like(y_scale),\n)\ny = make_tensor(\n \"y\",\n TensorProto.FLOAT4E2M1,\n x.shape,\n [0, 1, 2, 4, -6, -6, 2, 3, 0, -0.5, -1, -2],\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -514.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65022.0,\n -66046.0,\n 65023.0,\n -66047.0,\n 65024.0,\n -66048.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.int16(256)\ny = np.array(\n [\n 256,\n -1,\n 258,\n 254,\n 257,\n 255,\n 258,\n 254,\n 32767,\n -32767,\n 32767,\n -32768,\n 32767,\n -32768,\n 32767,\n -32768,\n ]\n).astype(np.int16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-4.0, -3.0, 1.0, 2.0],\n [-0.0, -2.5, -4.8, -8.6],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT2, x.shape, [0, 1, 1, 1, -1, -1, 0, 1, 0, -1, -1, -2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT4, x.shape, [1, 2, 3, 5, -8, -6, 3, 4, 4, 5, 5, 7]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int4\",\n)" }, { "summary": "quantizelinear", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -128.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65536.0,\n -65534.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.uint16(32767)\ny = np.array(\n [\n 32767,\n 32703,\n 32769,\n 32765,\n 32768,\n 32766,\n 32769,\n 32765,\n 65535,\n 0,\n 65535,\n 0,\n ]\n).astype(np.uint16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-2.0, -1.0, 1.0, 3.0],\n [4.0, 5.0, 6.0, 7.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT2, x.shape, [0, 1, 2, 3, 0, 0, 0, 1, 1, 1, 2, 2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT4, x.shape, [1, 2, 3, 5, 0, 0, 3, 4, 4, 5, 5, 11]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint4\",\n)" } ] }, { "name": "QuantizeLinear", "module": "ai.onnx", "version": 25, "description": "The linear quantization operator consumes a high-precision tensor, a scale, and a zero point to compute the\nlow-precision/quantized tensor. The scale factor and zero point must have the same shape, determining the quantization\ngranularity. The quantization formula is `y = saturate((x / y_scale) + y_zero_point)`.\n\nSaturation is done according to:\n- uint16: [0, 65535]\n- int16: [-32768, 32767]\n- uint8: [0, 255]\n- int8: [-128, 127]\n- uint4: [0, 15]\n- int4: [-8, 7]\n- uint2: [0, 3]\n- int2: [-2, 1]\n\nFor `(x / y_scale)`, it rounds to the nearest even. Refer to https://en.wikipedia.org/wiki/Rounding for details.\n\n`y_zero_point` and `y` must have the same type. `y_zero_point` is usually not used for quantization to float8 and 4bit types, but the quantization\nformula remains the same for consistency, and the type of the attribute `y_zero_point` still determines the quantization type.\n`x` and `y_scale` are allowed to have different types. The type of `y_scale` determines the precision of the division operation between `x` and\n`y_scale`, unless the `precision` attribute is specified.\n\nThere are three supported quantization granularities, determined by the shape of `y_scale`.\nIn all cases, `y_zero_point` must have the same shape as `y_scale`.\n- Per-tensor (per-layer) quantization: `y_scale` is a scalar.\n- Per-axis quantization: The scale must be a 1-D tensor, with the length of the quantization axis. For an input shape\n `(D0, ..., Di, ..., Dn)` and `axis=i`, `y_scale` is a 1-D tensor of length `Di`.\n- Blocked quantization: The scale's shape is identical to the input's shape, except for one dimension, in which\n blocking is performed. Given `x` shape `(D0, ..., Di, ..., Dn)`, `axis=i`, and block size `B`: `y_scale` shape is\n `(D0, ..., ceil(Di/B), ..., Dn)`.\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": 1, "description": "(Optional) The axis of the dequantizing dimension of the input tensor. Used only for per-axis and blocked quantization. Negative value means counting dimensions from the back. Accepted range is `[-r, r-1]` where `r = rank(input)`. When the rank of the input is 1, per-tensor quantization is applied, rendering the axis unnecessary in this scenario." }, { "name": "block_size", "type": "int64", "required": false, "description": "(Optional) The size of the quantization block (number of times every scale is replicated). Used only for blocked quantization. The block size is a positive integer. Given `x` shape `(D0, ..., Di, ..., Dn)`, `y_scale` shape `(S0, ... Si, ...Sn)` and `axis=i`, the accepted range is `[ceil(Di/Si), ceil(Di/(Si-1))-1]`" }, { "name": "output_dtype", "type": "int64", "required": false, "description": "(Optional) The output data type. If not supplied, the output data type is inferred from `y_zero_point` data type (`T3`). If neither `output_dtype` nor `y_zero_point` are supplied, output data type is uint8. If both `output_dtype` and `y_zero_point` are specified, `output_dtype` must be `T3`." }, { "name": "precision", "type": "int64", "required": false, "description": "(Optional) The precision of the division operation between `x` and `y_scale`. If not provided, it will be the same as the type of `y_scale`." }, { "name": "saturate", "type": "int64", "required": false, "default": 1, "description": "The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 quantization (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. All cases are fully described in two tables inserted in the operator description." } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D full precision Input tensor to be quantized." }, { "name": "y_scale", "type": "T2", "description": "Scale for doing quantization to get `y`. For per-tensor/layer quantization the scale is a scalar, for per-axis quantization it is a 1-D Tensor and for blocked quantization it has the same shape as the input, except for one dimension in which blocking is performed." }, { "name": "y_zero_point", "type": "T3", "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": "T3", "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": "The type of the input 'x'.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(int32)" ] }, { "description": "The type of the input 'y_scale'.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)", "tensor(int32)", "tensor(float8e8m0)" ] }, { "description": "The type of the input `y_zero_point` and the output `y`.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(uint2)", "tensor(int2)" ] } ], "examples": [ { "summary": "axis", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n [\n [[-162, 10], [-100, 232], [-20, -50]],\n [[-76, 0], [0, 252], [32, -44]],\n [[245, -485], [-960, -270], [-375, -470]],\n ],\n ],\n dtype=np.float32,\n)\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(\n np.uint8\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_axis\",\n)" }, { "summary": "blocked_asymmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n)\n\nx = np.array(\n [\n [6.0, 12.0, 50.0, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\ny_zero_point = np.array(\n [\n [0, 1],\n [1, 0],\n [2, 3],\n ],\n dtype=np.uint8,\n)\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\nassert y_scale.shape == y_zero_point.shape\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\ny_zero_point_elementwise = np.repeat(\n y_zero_point, repeats=repeats, axis=block_axis\n)\n\ny = np.rint(x / y_scale_elementwise + y_zero_point_elementwise).astype(np.uint8)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_blocked_asymmetric\",\n)" }, { "summary": "blocked_symmetric", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\"],\n outputs=[\"y\"],\n axis=1,\n block_size=2,\n output_dtype=TensorProto.INT16,\n)\n\nx = np.array(\n [\n [6.0, -8, -10, 5.0],\n [1.0, 8.0, 4.0, 5.0],\n [0.0, 20.0, 10.0, 4.0],\n ],\n dtype=np.float32,\n)\n\ny_scale = np.array(\n [\n [1.5, 2.5],\n [3.0, 4.9],\n [5.1, 6.9],\n ],\n dtype=np.float32,\n)\n\n# x.shape = (3, 4)\n# y_scale.shape = (3, 2)\n\nblock_axis = 1\n# The block shape is [x.shape[i] // y_scale.shape[i] for i in range(len(x.shape))] = (1, 2)\nassert all(\n x.shape[i] == y_scale.shape[i]\n for i in range(len(x.shape))\n if i != block_axis\n)\nassert x.shape[block_axis] % y_scale.shape[block_axis] == 0\nrepeats = x.shape[block_axis] // y_scale.shape[block_axis]\n\n# Create element-wise scale and zero point\ny_scale_elementwise = np.repeat(y_scale, repeats=repeats, axis=block_axis)\n\ny_val = np.clip(\n np.rint(x / y_scale_elementwise), a_min=-32768, a_max=32767\n).astype(np.int16)\ny = make_tensor(\n \"y\",\n TensorProto.INT16,\n x.shape,\n y_val,\n)\nexpect(\n node,\n inputs=[x, y_scale],\n outputs=[y],\n name=\"test_quantizelinear_blocked_symmetric\",\n)" }, { "summary": "e4m3fn", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E4M3FN, [1], [0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E4M3FN, [5], [0, 0.5, 1, 448, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e4m3fn\",\n)" }, { "summary": "e5m2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array([0.0, 1.0, 2.0, 100000.0, 200.0]).astype(np.float32)\ny_scale = np.float32(2)\ny_zero_point = make_tensor(\"y_zero_point\", TensorProto.FLOAT8E5M2, [1], [0.0])\ny = make_tensor(\"y\", TensorProto.FLOAT8E5M2, [5], [0, 0.5, 1, 49152, 96])\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_e5m2\",\n)" }, { "summary": "float4e2m1", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [-0.0, -2.5, -4.8, -8.6],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\",\n TensorProto.FLOAT4E2M1,\n y_scale.shape,\n np.zeros_like(y_scale),\n)\ny = make_tensor(\n \"y\",\n TensorProto.FLOAT4E2M1,\n x.shape,\n [0, 1, 2, 4, -6, -6, 2, 3, 0, -0.5, -1, -2],\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_float4e2m1\",\n)" }, { "summary": "int16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -514.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65022.0,\n -66046.0,\n 65023.0,\n -66047.0,\n 65024.0,\n -66048.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.int16(256)\ny = np.array(\n [\n 256,\n -1,\n 258,\n 254,\n 257,\n 255,\n 258,\n 254,\n 32767,\n -32767,\n 32767,\n -32768,\n 32767,\n -32768,\n 32767,\n -32768,\n ]\n).astype(np.int16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int16\",\n)" }, { "summary": "int2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-4.0, -3.0, 1.0, 2.0],\n [-0.0, -2.5, -4.8, -8.6],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT2, x.shape, [0, 1, 1, 1, -1, -1, 0, 1, 0, -1, -1, -2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int2\",\n)" }, { "summary": "int4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.INT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.INT4, x.shape, [1, 2, 3, 5, -8, -6, 3, 4, 4, 5, 5, 7]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_int4\",\n)" }, { "summary": "quantizelinear", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\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(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear\",\n)" }, { "summary": "uint16", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n)\n\nx = np.array(\n [\n 0.0,\n -128.0,\n 3.0,\n -3.0,\n 2.9,\n -2.9,\n 3.1,\n -3.1,\n 65536.0,\n -65534.0,\n 70000.0,\n -70000.0,\n ]\n).astype(np.float32)\ny_scale = np.float32(2.0)\ny_zero_point = np.uint16(32767)\ny = np.array(\n [\n 32767,\n 32703,\n 32769,\n 32765,\n 32768,\n 32766,\n 32769,\n 32765,\n 65535,\n 0,\n 65535,\n 0,\n ]\n).astype(np.uint16)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint16\",\n)" }, { "summary": "uint2", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-2.0, -1.0, 1.0, 3.0],\n [4.0, 5.0, 6.0, 7.0],\n ],\n dtype=np.float32,\n)\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT2, y_scale.shape, np.zeros_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT2, x.shape, [0, 1, 2, 3, 0, 0, 0, 1, 1, 1, 2, 2]\n)\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint2\",\n)" }, { "summary": "uint4", "code": "node = onnx.helper.make_node(\n \"QuantizeLinear\",\n inputs=[\"x\", \"y_scale\", \"y_zero_point\"],\n outputs=[\"y\"],\n axis=0,\n)\n\nx = np.array(\n [\n [0.0, 2.5, 4.8, 8.6],\n [-30, -20, 6, 9],\n [12, 15, 16, 40],\n ]\n).astype(np.float32)\n\ny_scale = np.asarray([2.0, 3.0, 4.0], dtype=np.float32)\ny_zero_point = make_tensor(\n \"y_zero_point\", TensorProto.UINT4, y_scale.shape, np.ones_like(y_scale)\n)\ny = make_tensor(\n \"y\", TensorProto.UINT4, x.shape, [1, 2, 3, 5, 0, 0, 3, 4, 4, 5, 5, 11]\n)\n\nexpect(\n node,\n inputs=[x, y_scale, y_zero_point],\n outputs=[y],\n name=\"test_quantizelinear_uint4\",\n)" } ] }, { "name": "QuantizeLinear", "module": "com.microsoft", "version": 1, "description": "The linear quantization operator. It consumes a full precision data, 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, [-128, 127] if it's int8,\n[0, 65,535] if it's uint16, and [-32,768, 32,767] if it's int16. For (x / y_scale), it's rounding to nearest ties to even.\nRefer 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 1-D tensor (per 'axis').", "attributes": [ { "name": "axis", "type": "int64", "required": false, "description": "The axis along which same quantization parameters are applied. It's optional.If it's not specified, it means per-tensor quantization and input 'x_scale' and 'x_zero_point' must be scalars.If it's specified, it means per 'axis' quantization and input 'x_scale' and 'x_zero_point' must be 1-D tensors." } ], "inputs": [ { "name": "x", "type": "T1", "description": "N-D full precision Input tensor to be quantized." }, { "name": "y_scale", "type": "T1", "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', 'y_scale' to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] }, { "description": "Constrain 'y_zero_point' and 'y' to 8-bit and 16-bit integer tensors.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)", "tensor(int16)", "tensor(uint16)", "tensor(int4)", "tensor(uint4)" ] } ] }, { "name": "QuantizeWithOrder", "module": "com.microsoft", "version": 1, "description": "Quantize input matrix to specific layout used in cublaslt.", "attributes": [ { "name": "order_input", "type": "int64", "required": true, "description": "cublasLt order of input matrix. ORDER_COL = 0, ORDER_ROW = 1, ORDER_COL32 = 2, ORDER_COL4_4R2_8C = 3, ORDER_COL32_2R_4R4 = 4. Please refer https://docs.nvidia.com/cuda/cublas/index.html#cublasLtOrder_t for their meaning." }, { "name": "order_output", "type": "int64", "required": true, "description": "cublasLt order of output matrix." } ], "inputs": [ { "name": "input", "type": "F", "description": "TODO: input tensor of (ROWS, COLS). if less than 2d, will broadcast to (1, X). If 3d, it is treated as (B, ROWS, COS)" }, { "name": "scale_input", "type": "S", "description": "scale of the input" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "output", "type": "Q", "description": "output tensor" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to int8 tensors.", "type_param_str": "Q", "allowed_type_strs": [ "tensor(int8)" ] }, { "description": "Constrain to float types", "type_param_str": "F", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] }, { "description": "Constrain Scale to float32 types", "type_param_str": "S", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "QuickGelu", "module": "com.microsoft", "version": 1, "description": "Compute x * Sigmoid(alpha * x).", "attributes": [ { "name": "alpha", "type": "float32", "required": false, "default": 1.7020000219345093, "description": "Alpha value." } ], "inputs": [ { "name": "X", "type": "T", "description": "The input data as Tensor." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "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)", "tensor(bfloat16)" ] } ], "category": "Activation" }, { "name": "RMSNormalization", "module": "ai.onnx", "version": 23, "description": "This is RMS normalization defined in ONNX as function as described in the paper https://arxiv.org/pdf/1910.07467.\n The overall computation can be split into two stages. The root mean squared norm is taken over the last D dimensions,\n where D is the dimension of normalized_shape. For example, if normalized_shape is (3, 5) (a 2-dimensional shape),\n the rms norm is computed over the last 2 dimensions of the input. The computation required by standardization can be\n described by the following equations.\n ```\n XSquared = Mul(X, X)\n XSquaredMean = ReduceMean(XSquared)\n MeanSquareEpsilon = Add(XSquaredMean, epsilon)\n RMS = Sqrt(MeanSquareEpsilon)\n Normalized = Div(X, RMS)\n ```\n where `normalized_axes` is `[axis, ..., rank of X - 1]`. The variables `RMS` stand for root mean square,\n Depending on `stash_type` attribute, the actual computation\n must happen in different floating-point precision.\n For example, if `stash_type` is 1, this operator casts\n all input variables to 32-bit float, perform the computation, and\n finally cast `Normalized` back to the original type of `X`.\n The second stage then scales the outcome of the first stage using:\n ```\n Y= Mul(Normalized, Scale)\n ```\n Let `d[i]` indicate the i-th dimension of `X`.\n If `X`'s shape is `[d[0], ..., d[axis-1], d[axis], ..., d[rank-1]]`,\n the shape of `RMS` is `[d[0], ..., d[axis-1], 1, ..., 1]`.\n `Y` and `X` have the same shape. This operator supports unidirectional broadcasting\n (`Scale` should be unidirectional broadcastable to tensor `X`);\n for more details please check [the doc](https://github.com/onnx/onnx/blob/master/docs/Broadcasting.md).\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": -1, "description": "The first normalization dimension. If rank(X) is r, axis' allowed range is [-r, r). Negative value means counting dimensions from the back." }, { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "The epsilon value to use to avoid division by zero." }, { "name": "stash_type", "type": "int64", "required": false, "default": 1, "description": "The floating-point precision used in stage one of the computation." } ], "inputs": [ { "name": "X", "type": "T", "description": "The input tensor to be normalized. In general, the shape is (D1, D2, ... , Dn) for n-dimensional data, where the root mean squared norm is taken over the last D dimensions, D is determined by the axis attribute." }, { "name": "scale", "type": "V", "description": "Scale tensor. Scale tensor shape should be broadcastable to the normalized shape." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "V", "description": "Output data tensor. Same shape as X" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input X type to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] }, { "description": "Constrain output Y and scale type to float tensors.", "type_param_str": "V", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] } ], "examples": [ { "summary": "d", "code": "X = np.random.randn(3, 4).astype(np.float32)\n\ndef case(axis: int) -> None:\n normalized_shape = calculate_normalized_shape(X.shape, axis)\n W = np.random.randn(*normalized_shape).astype(np.float32)\n Y = _rms_normalization(X, W, axis=axis)\n\n node = onnx.helper.make_node(\n \"RMSNormalization\",\n inputs=[\"X\", \"W\"],\n outputs=[\"Y\"],\n axis=axis,\n )\n\n if axis < 0:\n name = f\"test_rms_normalization_2d_axis_negative_{-axis}\"\n else:\n name = f\"test_rms_normalization_2d_axis{axis}\"\n\n expect(node, inputs=[X, W], outputs=[Y], name=name)\n\nfor i in range(len(X.shape)):\n case(i)\n case(i - len(X.shape))" }, { "summary": "d_epsilon", "code": "epsilon = 1e-1\nX = np.random.randn(2, 3, 5).astype(np.float32)\n\ndef case(axis: int) -> None:\n normalized_shape = calculate_normalized_shape(X.shape, axis)\n W = np.random.randn(*normalized_shape).astype(np.float32)\n Y = _rms_normalization(X, W, axis=axis, epsilon=epsilon)\n node = onnx.helper.make_node(\n \"RMSNormalization\",\n inputs=[\"X\", \"W\"],\n outputs=[\"Y\"],\n axis=axis,\n epsilon=epsilon,\n )\n\n if axis < 0:\n name = f\"test_rms_normalization_3d_axis_negative_{-axis}_epsilon\"\n else:\n name = f\"test_rms_normalization_3d_axis{axis}_epsilon\"\n\n expect(node, inputs=[X, W], outputs=[Y], name=name)\n\nfor i in range(len(X.shape)):\n case(i)\n case(i - len(X.shape))" }, { "summary": "default_axis", "code": "X = np.random.randn(2, 3, 4, 5).astype(np.float32)\n\n# Default axis in RMSNormalization is -1.\nnormalized_shape = calculate_normalized_shape(X.shape, -1)\nW = np.random.randn(*normalized_shape).astype(np.float32)\n# Axis is default to -1 in the reference implementation.\nY = _rms_normalization(X, W)\n\n# Not specifying axis attribute means -1.\nnode = onnx.helper.make_node(\n \"RMSNormalization\",\n inputs=[\"X\", \"W\"],\n outputs=[\"Y\"],\n)\n\nexpect(\n node,\n inputs=[X, W],\n outputs=[Y],\n name=\"test_rms_normalization_default_axis\",\n)" }, { "summary": "rmsnormalization", "code": "X = np.random.randn(2, 3, 4, 5).astype(np.float32)\n\ndef case(axis: int) -> None:\n normalized_shape = calculate_normalized_shape(X.shape, axis)\n W = np.random.randn(*normalized_shape).astype(np.float32)\n Y = _rms_normalization(X, W, axis=axis)\n\n node = onnx.helper.make_node(\n \"RMSNormalization\",\n inputs=[\"X\", \"W\"],\n outputs=[\"Y\"],\n axis=axis,\n )\n\n if axis < 0:\n name = f\"test_rms_normalization_4d_axis_negative_{-axis}\"\n else:\n name = f\"test_rms_normalization_4d_axis{axis}\"\n\n expect(node, inputs=[X, W], outputs=[Y], name=name)\n\nfor i in range(len(X.shape)):\n case(i)\n case(i - len(X.shape))" } ] }, { "name": "RNN", "module": "ai.onnx", "version": 1, "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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 = RNNHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_simple_rnn_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n \"RNN\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], 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 = RNNHelper(X=input, W=W, R=R)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_simple_rnn_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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 = RNNHelper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_simple_rnn_with_initial_bias\",\n)" }, { "summary": "seq_length", "code": "input = np.array(\n [\n [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],\n [[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]],\n ]\n).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 = RNNHelper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_rnn_seq_length\",\n)" } ], "category": "Layer" }, { "name": "RNN", "module": "ai.onnx", "version": 7, "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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 = RNNHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_simple_rnn_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n \"RNN\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], 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 = RNNHelper(X=input, W=W, R=R)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_simple_rnn_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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 = RNNHelper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_simple_rnn_with_initial_bias\",\n)" }, { "summary": "seq_length", "code": "input = np.array(\n [\n [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],\n [[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]],\n ]\n).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 = RNNHelper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_rnn_seq_length\",\n)" } ], "category": "Layer" }, { "name": "RNN", "module": "ai.onnx", "version": 14, "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* `i` - input gate\n* `t` - time step (t-1 means previous time step)\n* `Wi` - W parameter weight matrix for input gate\n* `Ri` - R recurrence weight matrix for input gate\n* `Wbi` - W parameter bias vector for input gate\n* `Rbi` - R parameter bias vector for input gate\n* `WBi` - W parameter weight matrix for backward input gate\n* `RBi` - R recurrence weight matrix for backward input gate\n* `WBbi` - WR bias vectors for backward input gate\n* `RBbi` - RR bias vectors for backward input gate\n* `H` - Hidden state\n* `num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n* Relu(x) - max(0, x)\n* Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n* Sigmoid(x) - 1/(1 + e^{-x})\n\nNOTE: Below are optional\n\n* Affine(x) - alpha*x + beta\n* LeakyRelu(x) - x if x >= 0 else alpha * x\n* ThresholdedRelu(x) - x if x >= alpha else 0\n* ScaledTanh(x) - alpha*Tanh(beta*x)\n* HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n* Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n* Softsign(x) - x/(1 + |x|)\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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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 = RNNHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_simple_rnn_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n \"RNN\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], 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 = RNNHelper(X=input, W=W, R=R)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_simple_rnn_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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 = RNNHelper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_simple_rnn_with_initial_bias\",\n)" }, { "summary": "seq_length", "code": "input = np.array(\n [\n [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],\n [[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]],\n ]\n).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 = RNNHelper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_rnn_seq_length\",\n)" } ], "category": "Layer" }, { "name": "RNN", "module": "ai.onnx", "version": 22, "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* `i` - input gate\n* `t` - time step (t-1 means previous time step)\n* `Wi` - W parameter weight matrix for input gate\n* `Ri` - R recurrence weight matrix for input gate\n* `Wbi` - W parameter bias vector for input gate\n* `Rbi` - R parameter bias vector for input gate\n* `WBi` - W parameter weight matrix for backward input gate\n* `RBi` - R recurrence weight matrix for backward input gate\n* `WBbi` - WR bias vectors for backward input gate\n* `RBbi` - RR bias vectors for backward input gate\n* `H` - Hidden state\n* `num_directions` - 2 if direction == bidirectional else 1\n\nActivation functions:\n\n* Relu(x) - max(0, x)\n* Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})\n* Sigmoid(x) - 1/(1 + e^{-x})\n\nNOTE: Below are optional\n\n* Affine(x) - alpha*x + beta\n* LeakyRelu(x) - x if x >= 0 else alpha * x\n* ThresholdedRelu(x) - x if x >= alpha else 0\n* ScaledTanh(x) - alpha*Tanh(beta*x)\n* HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)\n* Elu(x) - x if x >= 0 else alpha*(e^x - 1)\n* Softsign(x) - x/(1 + |x|)\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(bfloat16)", "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.0, 2.0]], [[3.0, 4.0]], [[5.0, 6.0]]]).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 = RNNHelper(X=input, W=W, R=R, layout=layout)\nY, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y.astype(np.float32), Y_h.astype(np.float32)],\n name=\"test_simple_rnn_batchwise\",\n)" }, { "summary": "defaults", "code": "input = np.array([[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]]).astype(np.float32)\n\ninput_size = 2\nhidden_size = 4\nweight_scale = 0.1\n\nnode = onnx.helper.make_node(\n \"RNN\", inputs=[\"X\", \"W\", \"R\"], outputs=[\"\", \"Y_h\"], 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 = RNNHelper(X=input, W=W, R=R)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_simple_rnn_defaults\",\n)" }, { "summary": "initial_bias", "code": "input = np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]).astype(\n np.float32\n)\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 = RNNHelper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_simple_rnn_with_initial_bias\",\n)" }, { "summary": "seq_length", "code": "input = np.array(\n [\n [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],\n [[10.0, 11.0, 12.0], [13.0, 14.0, 15.0], [16.0, 17.0, 18.0]],\n ]\n).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 = RNNHelper(X=input, W=W, R=R, B=B)\n_, Y_h = rnn.step()\nexpect(\n node,\n inputs=[input, W, R, B],\n outputs=[Y_h.astype(np.float32)],\n name=\"test_rnn_seq_length\",\n)" } ], "category": "Layer" }, { "name": "RandomNormal", "module": "ai.onnx", "version": 1, "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": "RandomNormal", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ] }, { "name": "RandomNormalLike", "module": "ai.onnx", "version": 1, "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": "RandomNormalLike", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ] }, { "name": "RandomUniform", "module": "ai.onnx", "version": 1, "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": "RandomUniform", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ] }, { "name": "RandomUniformLike", "module": "ai.onnx", "version": 1, "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": "RandomUniformLike", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ] }, { "name": "Range", "module": "ai.onnx", "version": 11, "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```\nnumber_of_elements = max( ceil( (limit - start) / delta ) , 0 )\n```\nThe pseudocode determining the contents of the output is shown below:\n```\nfor(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], 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], name=\"test_reciprocal\")" } ] }, { "name": "Reciprocal", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_reciprocal\")" } ] }, { "name": "Reciprocal", "module": "ai.onnx", "version": 13, "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], 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], name=\"test_reciprocal\")" } ] }, { "name": "ReduceL1", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\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 \"ReduceL1\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], 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=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[78.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[3., 7.], [11., 15.], [19., 23.]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_keep_dims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_keep_dims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_negative_axes_keep_dims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_negative_axes_keep_dims_random\",\n)" } ] }, { "name": "ReduceL1", "module": "ai.onnx", "version": 11, "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 = np.array([], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL1\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], 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=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[78.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[3., 7.], [11., 15.], [19., 23.]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_keep_dims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_keep_dims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_negative_axes_keep_dims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_negative_axes_keep_dims_random\",\n)" } ] }, { "name": "ReduceL1", "module": "ai.onnx", "version": 13, "description": "Computes the L1 norm of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 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 \"ReduceL1\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], 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=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[78.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[3., 7.], [11., 15.], [19., 23.]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_keep_dims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_keep_dims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_negative_axes_keep_dims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_negative_axes_keep_dims_random\",\n)" } ] }, { "name": "ReduceL1", "module": "ai.onnx", "version": 18, "description": "Computes the L1 norm of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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 \"ReduceL1\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], 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=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[78.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(a=np.abs(data), axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[3., 7.], [11., 15.], [19., 23.]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_keep_dims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_keep_dims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL1\",\n inputs=[\"data\", \"axes\"],\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=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3.], [7.]], [[11.], [15.]], [[19.], [23.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_negative_axes_keep_dims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l1_negative_axes_keep_dims_random\",\n)" } ] }, { "name": "ReduceL2", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\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 \"ReduceL2\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], 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(a=np.square(data), axis=None, keepdims=keepdims == 1))\n# print(reduced)\n# [[[25.49509757]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(a=np.square(data), axis=None, keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[2.23606798, 5.],\n# [7.81024968, 10.63014581],\n# [13.45362405, 16.2788206]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_do_not_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_keep_dims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_keep_dims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_negative_axes_keep_dims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_negative_axes_keep_dims_random\",\n)" } ] }, { "name": "ReduceL2", "module": "ai.onnx", "version": 11, "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 = np.array([], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL2\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], 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(a=np.square(data), axis=None, keepdims=keepdims == 1))\n# print(reduced)\n# [[[25.49509757]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(a=np.square(data), axis=None, keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[2.23606798, 5.],\n# [7.81024968, 10.63014581],\n# [13.45362405, 16.2788206]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_do_not_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_keep_dims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_keep_dims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_negative_axes_keep_dims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_negative_axes_keep_dims_random\",\n)" } ] }, { "name": "ReduceL2", "module": "ai.onnx", "version": 13, "description": "Computes the L2 norm of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 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 \"ReduceL2\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], 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(a=np.square(data), axis=None, keepdims=keepdims == 1))\n# print(reduced)\n# [[[25.49509757]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(a=np.square(data), axis=None, keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[2.23606798, 5.],\n# [7.81024968, 10.63014581],\n# [13.45362405, 16.2788206]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_do_not_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_keep_dims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_keep_dims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_negative_axes_keep_dims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_negative_axes_keep_dims_random\",\n)" } ] }, { "name": "ReduceL2", "module": "ai.onnx", "version": 18, "description": "Computes the L2 norm of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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 \"ReduceL2\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], 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(a=np.square(data), axis=None, keepdims=keepdims == 1))\n# print(reduced)\n# [[[25.49509757]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(np.sum(a=np.square(data), axis=None, keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 0\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[2.23606798, 5.],\n# [7.81024968, 10.63014581],\n# [13.45362405, 16.2788206]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_do_not_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([2], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_keep_dims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_keep_dims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceL2\",\n inputs=[\"data\", \"axes\"],\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(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n# print(reduced)\n# [[[2.23606798], [5.]]\n# [[7.81024968], [10.63014581]]\n# [[13.45362405], [16.2788206 ]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_negative_axes_keep_dims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sqrt(\n np.sum(a=np.square(data), axis=tuple(axes), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_l2_negative_axes_keep_dims_random\",\n)" } ] }, { "name": "ReduceLogSum", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or undefined otherwise.\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": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = np.log(zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_empty_set\",\n)" }, { "summary": "keepdims", "code": "node = onnx.helper.make_node(\n \"ReduceLogSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, keepdims=True))\naxes = np.array([], dtype=np.int64)\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_default\",\n)" }, { "summary": "negative_axes_keepdims", "code": "axes = np.array([-2], dtype=np.int64)\nnode = onnx.helper.make_node(\n \"ReduceLogSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=True))\n# print(reduced)\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_negative_axes\",\n)" }, { "summary": "nokeepdims", "code": "shape = [3, 4, 5]\naxes = np.array([2, 1], dtype=np.int64)\n\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=0,\n)\ndata = np.random.ranf(shape).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=False))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_desc_axes\",\n)\n\naxes = np.array([0, 1], dtype=np.int64)\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=0,\n)\ndata = np.random.ranf(shape).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=False))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_asc_axes\",\n)" } ] }, { "name": "ReduceLogSum", "module": "ai.onnx", "version": 11, "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": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = np.log(zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_empty_set\",\n)" }, { "summary": "keepdims", "code": "node = onnx.helper.make_node(\n \"ReduceLogSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, keepdims=True))\naxes = np.array([], dtype=np.int64)\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_default\",\n)" }, { "summary": "negative_axes_keepdims", "code": "axes = np.array([-2], dtype=np.int64)\nnode = onnx.helper.make_node(\n \"ReduceLogSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=True))\n# print(reduced)\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_negative_axes\",\n)" }, { "summary": "nokeepdims", "code": "shape = [3, 4, 5]\naxes = np.array([2, 1], dtype=np.int64)\n\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=0,\n)\ndata = np.random.ranf(shape).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=False))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_desc_axes\",\n)\n\naxes = np.array([0, 1], dtype=np.int64)\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=0,\n)\ndata = np.random.ranf(shape).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=False))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_asc_axes\",\n)" } ] }, { "name": "ReduceLogSum", "module": "ai.onnx", "version": 13, "description": "Computes the log sum of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or undefined otherwise.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 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": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = np.log(zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_empty_set\",\n)" }, { "summary": "keepdims", "code": "node = onnx.helper.make_node(\n \"ReduceLogSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, keepdims=True))\naxes = np.array([], dtype=np.int64)\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_default\",\n)" }, { "summary": "negative_axes_keepdims", "code": "axes = np.array([-2], dtype=np.int64)\nnode = onnx.helper.make_node(\n \"ReduceLogSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=True))\n# print(reduced)\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_negative_axes\",\n)" }, { "summary": "nokeepdims", "code": "shape = [3, 4, 5]\naxes = np.array([2, 1], dtype=np.int64)\n\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=0,\n)\ndata = np.random.ranf(shape).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=False))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_desc_axes\",\n)\n\naxes = np.array([0, 1], dtype=np.int64)\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=0,\n)\ndata = np.random.ranf(shape).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=False))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_asc_axes\",\n)" } ] }, { "name": "ReduceLogSum", "module": "ai.onnx", "version": 18, "description": "Computes the log sum of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or undefined otherwise.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = np.log(zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_empty_set\",\n)" }, { "summary": "keepdims", "code": "node = onnx.helper.make_node(\n \"ReduceLogSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, keepdims=True))\naxes = np.array([], dtype=np.int64)\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_default\",\n)" }, { "summary": "negative_axes_keepdims", "code": "axes = np.array([-2], dtype=np.int64)\nnode = onnx.helper.make_node(\n \"ReduceLogSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"]\n)\ndata = np.random.ranf([3, 4, 5]).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=True))\n# print(reduced)\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_negative_axes\",\n)" }, { "summary": "nokeepdims", "code": "shape = [3, 4, 5]\naxes = np.array([2, 1], dtype=np.int64)\n\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=0,\n)\ndata = np.random.ranf(shape).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=False))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_desc_axes\",\n)\n\naxes = np.array([0, 1], dtype=np.int64)\nnode = onnx.helper.make_node(\n \"ReduceLogSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=0,\n)\ndata = np.random.ranf(shape).astype(np.float32)\nreduced = np.log(np.sum(data, axis=tuple(axes), keepdims=False))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_asc_axes\",\n)" } ] }, { "name": "ReduceLogSumExp", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or undefined otherwise.\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 \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(np.exp(data), axis=None, keepdims=keepdims == 1))\n# print(reduced)\n# [[[60.00671387]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=None, keepdims=keepdims == 1))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 0\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_do_not_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = np.log(zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_keepdims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-2], dtype=np.int64)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_negative_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(\n np.sum(np.exp(data), axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceLogSumExp", "module": "ai.onnx", "version": 11, "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 = np.array([], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(np.exp(data), axis=None, keepdims=keepdims == 1))\n# print(reduced)\n# [[[60.00671387]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=None, keepdims=keepdims == 1))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 0\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_do_not_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = np.log(zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_keepdims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-2], dtype=np.int64)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_negative_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(\n np.sum(np.exp(data), axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceLogSumExp", "module": "ai.onnx", "version": 13, "description": "Computes the log sum exponent of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or undefined otherwise.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 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 \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(np.exp(data), axis=None, keepdims=keepdims == 1))\n# print(reduced)\n# [[[60.00671387]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=None, keepdims=keepdims == 1))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 0\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_do_not_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = np.log(zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_keepdims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-2], dtype=np.int64)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_negative_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(\n np.sum(np.exp(data), axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceLogSumExp", "module": "ai.onnx", "version": 18, "description": "Computes the log sum exponent of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or undefined otherwise.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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 \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(np.exp(data), axis=None, keepdims=keepdims == 1))\n# print(reduced)\n# [[[60.00671387]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=None, keepdims=keepdims == 1))\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_default_axes_keepdims_random\",\n)" }, { "summary": "do_not_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 0\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_do_not_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = np.log(zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(np.sum(np.exp(data), axis=tuple(axes), keepdims=keepdims == 1))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_keepdims_random\",\n)" }, { "summary": "negative_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([-2], dtype=np.int64)\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceLogSumExp\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]], dtype=np.double\n)\nreduced = np.log(np.sum(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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_negative_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.double)\nreduced = np.log(\n np.sum(np.exp(data), axis=tuple(axes.tolist()), keepdims=keepdims == 1)\n)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_log_sum_exp_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceMax", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or the minimum value of the data type otherwise.\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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[True],\n# [True],\n# [True],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceMax\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdim_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = -(one / zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMax", "module": "ai.onnx", "version": 11, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or the minimum value of the data type otherwise.\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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[True],\n# [True],\n# [True],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceMax\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdim_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = -(one / zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMax", "module": "ai.onnx", "version": 12, "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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[True],\n# [True],\n# [True],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceMax\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdim_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = -(one / zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMax", "module": "ai.onnx", "version": 13, "description": "Computes the max of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or the minimum value of the data type otherwise.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[True],\n# [True],\n# [True],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceMax\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdim_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = -(one / zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMax", "module": "ai.onnx", "version": 18, "description": "Computes the max of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or the minimum value of the data type otherwise.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[True],\n# [True],\n# [True],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceMax\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdim_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = -(one / zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMax", "module": "ai.onnx", "version": 20, "description": "Computes the max of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or the minimum value of the data type otherwise.\n\n\nIf the input data type is Boolean, the comparison should consider `False < True`.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 numeric and Boolean 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)", "tensor(bool)" ] } ], "examples": [ { "summary": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.maximum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[True],\n# [True],\n# [True],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\nnode = onnx.helper.make_node(\n \"ReduceMax\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.maximum.reduce(data, axis=axes, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdim_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_max_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = -(one / zero) # -inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMax\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_max_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMean", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields undefined.\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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\nreduced = np.mean(data, axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[18.25]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_do_not_keepdims_random\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_keepdims_random\",\n)" }, { "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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceMean", "module": "ai.onnx", "version": 11, "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 = np.array([], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\nreduced = np.mean(data, axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[18.25]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_do_not_keepdims_random\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_keepdims_random\",\n)" }, { "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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceMean", "module": "ai.onnx", "version": 13, "description": "Computes the mean of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields undefined.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\nreduced = np.mean(data, axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[18.25]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_do_not_keepdims_random\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_keepdims_random\",\n)" }, { "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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceMean", "module": "ai.onnx", "version": 18, "description": "Computes the mean of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields undefined.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\nreduced = np.mean(data, axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[18.25]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.mean(data, axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_do_not_keepdims_random\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_keepdims_random\",\n)" }, { "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 \"ReduceMean\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_mean_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceMin", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields plus infinity (if supported by the datatype) or the maximum value of the data type otherwise.\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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[ True],\n# [False],\n# [False],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[1.]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = one / zero # inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMin", "module": "ai.onnx", "version": 11, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields plus infinity (if supported by the datatype) or the maximum value of the data type otherwise.\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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[ True],\n# [False],\n# [False],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[1.]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = one / zero # inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMin", "module": "ai.onnx", "version": 12, "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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[ True],\n# [False],\n# [False],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[1.]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = one / zero # inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMin", "module": "ai.onnx", "version": 13, "description": "Computes the min of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields plus infinity (if supported by the datatype) or the maximum value of the data type otherwise.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[ True],\n# [False],\n# [False],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[1.]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = one / zero # inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMin", "module": "ai.onnx", "version": 18, "description": "Computes the min of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields plus infinity (if supported by the datatype) or the maximum value of the data type otherwise.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[ True],\n# [False],\n# [False],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[1.]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = one / zero # inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceMin", "module": "ai.onnx", "version": 20, "description": "Computes the min of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields plus infinity (if supported by the datatype) or the maximum value of the data type otherwise.\n\n\nIf the input data type is Boolean, the comparison should consider `False < True`.\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 numeric and Boolean 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)", "tensor(bool)" ] } ], "examples": [ { "summary": "bool_inputs", "code": "axes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[True, True], [True, False], [False, True], [False, False]],\n)\nreduced = np.minimum.reduce(data, axis=tuple(axes), keepdims=bool(keepdims))\n# print(reduced)\n# [[ True],\n# [False],\n# [False],\n# [False]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_bool_inputs\",\n)" }, { "summary": "default_axes_keepdims", "code": "shape = [3, 2, 2]\naxes = None\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[5, 1], [20, 2]], [[30, 1], [40, 2]], [[55, 1], [60, 2]]],\n dtype=np.float32,\n)\nreduced = np.minimum.reduce(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[1.]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_min_default_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_do_not_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\none = np.array(np.ones(reduced_shape, dtype=np.float32))\nzero = np.array(np.zeros(reduced_shape, dtype=np.float32))\nreduced = one / zero # inf\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" }, { "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 \"ReduceMin\",\n inputs=[\"data\", \"axes\"],\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.float32,\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_example\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_min_negative_axes_keepdims_random\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 18)],\n)" } ] }, { "name": "ReduceProd", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 1.\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\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[4.790016e+08]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_prod_default_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_prod_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[3., 8.]\n# [35., 48.]\n# [99., 120.]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.ones(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_keepdims_random\",\n)" }, { "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 \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceProd", "module": "ai.onnx", "version": 11, "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\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[4.790016e+08]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_prod_default_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_prod_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[3., 8.]\n# [35., 48.]\n# [99., 120.]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.ones(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_keepdims_random\",\n)" }, { "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 \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceProd", "module": "ai.onnx", "version": 13, "description": "Computes the product of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 1.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 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\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[4.790016e+08]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_prod_default_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_prod_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[3., 8.]\n# [35., 48.]\n# [99., 120.]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.ones(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_keepdims_random\",\n)" }, { "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 \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceProd", "module": "ai.onnx", "version": 18, "description": "Computes the product of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 1.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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\", inputs=[\"data\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=axes, keepdims=keepdims == 1)\n# print(reduced)\n# [[[4.790016e+08]]]\n\nexpect(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_prod_default_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data],\n outputs=[reduced],\n name=\"test_reduce_prod_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[3., 8.]\n# [35., 48.]\n# [99., 120.]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.ones(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_keepdims_random\",\n)" }, { "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 \"ReduceProd\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.prod(data, axis=tuple(axes), keepdims=keepdims == 1)\n# print(reduced)\n# [[[3., 8.]]\n# [[35., 48.]]\n# [[99., 120.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_prod_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceSum", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\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\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.sum(data, axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[78.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_default_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_default_axes_keepdims_random\",\n)" }, { "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\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_do_not_keepdims_random\",\n)" }, { "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)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_axes_input_noop_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.array(data)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_axes_input_noop\",\n)" }, { "summary": "empty_set", "code": "\"\"\"Test case with the reduced-axis of size zero.\"\"\"\nshape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_keepdims_random\",\n)" }, { "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\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_negative_axes_keepdims_random\",\n)" }, { "summary": "non_reduced_axis_zero", "code": "\"\"\"Test case with the non-reduced-axis of size zero.\"\"\"\nshape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 0, 1]\n\nnode = onnx.helper.make_node(\n \"ReduceSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([2], dtype=np.int64)\nreduced = np.array([], dtype=np.float32).reshape(reduced_shape)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_set_non_reduced_axis_zero\",\n)" } ] }, { "name": "ReduceSum", "module": "ai.onnx", "version": 11, "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\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.sum(data, axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[78.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_default_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_default_axes_keepdims_random\",\n)" }, { "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\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_do_not_keepdims_random\",\n)" }, { "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)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_axes_input_noop_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.array(data)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_axes_input_noop\",\n)" }, { "summary": "empty_set", "code": "\"\"\"Test case with the reduced-axis of size zero.\"\"\"\nshape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_keepdims_random\",\n)" }, { "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\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_negative_axes_keepdims_random\",\n)" }, { "summary": "non_reduced_axis_zero", "code": "\"\"\"Test case with the non-reduced-axis of size zero.\"\"\"\nshape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 0, 1]\n\nnode = onnx.helper.make_node(\n \"ReduceSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([2], dtype=np.int64)\nreduced = np.array([], dtype=np.float32).reshape(reduced_shape)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_set_non_reduced_axis_zero\",\n)" } ] }, { "name": "ReduceSum", "module": "ai.onnx", "version": 13, "description": "Computes the sum of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.sum(data, axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[78.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_default_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_default_axes_keepdims_random\",\n)" }, { "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\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_do_not_keepdims_random\",\n)" }, { "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)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_axes_input_noop_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.array(data)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_axes_input_noop\",\n)" }, { "summary": "empty_set", "code": "\"\"\"Test case with the reduced-axis of size zero.\"\"\"\nshape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceSum\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_keepdims_random\",\n)" }, { "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\", inputs=[\"data\", \"axes\"], outputs=[\"reduced\"], keepdims=keepdims\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_negative_axes_keepdims_random\",\n)" }, { "summary": "non_reduced_axis_zero", "code": "\"\"\"Test case with the non-reduced-axis of size zero.\"\"\"\nshape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 0, 1]\n\nnode = onnx.helper.make_node(\n \"ReduceSum\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([2], dtype=np.int64)\nreduced = np.array([], dtype=np.float32).reshape(reduced_shape)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_empty_set_non_reduced_axis_zero\",\n)" } ] }, { "name": "ReduceSumInteger", "module": "com.microsoft", "version": 1, "description": "Computes the sum of the low-precision input tensor's element along the provided axes.\nThe resulting tensor has the same rank as the input if keepdims equal 1. If keepdims equal 0,\nthen the resulting tensor have the reduced dimension pruned. The above behavior is similar to numpy,\nwith the exception that numpy default keepdims to False instead of True.", "attributes": [ { "name": "axes", "type": "int64[]", "required": true, "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": true, "description": "Keep the reduced dimension or not, default 1 mean keep reduced dimension." } ], "inputs": [ { "name": "data", "type": "T1", "description": "An input tensor." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "reduced", "type": "T2", "description": "Reduced output tensor." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input type to 8-bit integer tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int8)", "tensor(uint8)" ] }, { "description": "Constrain output data type to 32-bit integer tensor.T2 must be tensor(uint32) when T1 is tensor(uint8),or must be tensor(int32) when T1 is tensor(int8).", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int32)", "tensor(uint32)" ] } ] }, { "name": "ReduceSumSquare", "module": "ai.onnx", "version": 1, "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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.sum(np.square(data), axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[650.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_keepdims_random\",\n)" }, { "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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceSumSquare", "module": "ai.onnx", "version": 11, "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 = np.array([], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.sum(np.square(data), axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[650.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_keepdims_random\",\n)" }, { "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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceSumSquare", "module": "ai.onnx", "version": 13, "description": "Computes the sum square of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.sum(np.square(data), axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[650.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_keepdims_random\",\n)" }, { "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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_negative_axes_keepdims_random\",\n)" } ] }, { "name": "ReduceSumSquare", "module": "ai.onnx", "version": 18, "description": "Computes the sum square of the input tensor's elements 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. Input tensors of rank zero are\nvalid. Reduction over an empty set of values yields 0.\n\n\nThe above behavior is similar to numpy, with the exception that numpy defaults `keepdims`\nto `False` 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 behavior when axes is not provided or is empty. If false (default), reduction happens over all axes (similar to the case when `axis=None` in numpy). If true, reduction happens over an empty set of axes (similar to the case when `axis=()` in numpy). Note that reduction over an empty set of axes means that the reduction step behaves like a no-op (identity function), but composite-reduction operators will still perform the non-reduction steps as needed. Thus, ReduceLogSum returns the Log of input tensor, and ReduceSumSquare returns the Square of the input tensor, in this case." } ], "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 empty axes. When axes is empty (either not provided or explicitly empty), behavior depends on 'noop_with_empty_axes': reduction over all axes if 'noop_with_empty_axes' is false, and reduction over the empty set of axes 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 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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\nreduced = np.sum(np.square(data), axis=None, keepdims=keepdims == 1)\n# print(reduced)\n# [[[650.]]]\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_default_axes_keepdims_example\",\n)\n\nnp.random.seed(0)\ndata = np.random.uniform(-10, 10, shape).astype(np.float32)\nreduced = np.sum(np.square(data), axis=None, keepdims=keepdims == 1)\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_default_axes_keepdims_random\",\n)" }, { "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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_do_not_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_do_not_keepdims_random\",\n)" }, { "summary": "empty_set", "code": "shape = [2, 0, 4]\nkeepdims = 1\nreduced_shape = [2, 1, 4]\n\nnode = onnx.helper.make_node(\n \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array([], dtype=np.float32).reshape(shape)\naxes = np.array([1], dtype=np.int64)\nreduced = np.array(np.zeros(reduced_shape, dtype=np.float32))\n\nexpect(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_empty_set\",\n)" }, { "summary": "keepdims", "code": "shape = [3, 2, 2]\naxes = np.array([1], dtype=np.int64)\nkeepdims = 1\n\nnode = onnx.helper.make_node(\n \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_keepdims_random\",\n)" }, { "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 \"ReduceSumSquare\",\n inputs=[\"data\", \"axes\"],\n outputs=[\"reduced\"],\n keepdims=keepdims,\n)\n\ndata = np.array(\n [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]], dtype=np.float32\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_negative_axes_keepdims_example\",\n)\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(\n node,\n inputs=[data, axes],\n outputs=[reduced],\n name=\"test_reduce_sum_square_negative_axes_keepdims_random\",\n)" } ] }, { "name": "RegexFullMatch", "module": "ai.onnx", "version": 20, "description": "RegexFullMatch performs a full regex match on each element of the input tensor. If an element fully matches the regex pattern specified as an attribute, the corresponding element in the output is True and it is False otherwise. [RE2](https://github.com/google/re2/wiki/Syntax) regex syntax is used.", "attributes": [ { "name": "pattern", "type": "string", "required": false, "description": "Regex pattern to match on. This must be valid RE2 syntax." } ], "inputs": [ { "name": "X", "type": "T1", "description": "Tensor with strings to match on." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T2", "description": "Tensor of bools indicating if each input string fully matches the regex pattern specified." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Inputs must be UTF-8 strings", "type_param_str": "T1", "allowed_type_strs": [ "tensor(string)" ] }, { "description": "Outputs are bools and are True where there is a full regex match and False otherwise.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(bool)" ] } ], "examples": [ { "summary": "basic", "code": "node = onnx.helper.make_node(\n \"RegexFullMatch\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n pattern=r\"www\\.[\\w.-]+\\.\\bcom\\b\",\n)\n\nx = np.array([\"www.google.com\", \"www.facebook.com\", \"www.bbc.co.uk\"]).astype(\n object\n)\nresult = np.array([True, True, False])\nexpect(node, inputs=[x], outputs=[result], name=\"test_regex_full_match_basic\")" }, { "summary": "match_email_domain", "code": "node = onnx.helper.make_node(\n \"RegexFullMatch\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n pattern=r\"(\\W|^)[\\w.\\-]{0,25}@(yahoo|gmail)\\.com(\\W|$)\",\n)\n\nx = np.array(\n [\n [\"account@gmail.com\", \"account@hotmail.com\"],\n [\"not email\", \"account2@yahoo.com\"],\n ]\n).astype(object)\nresult = np.array([[True, False], [False, True]])\nexpect(\n node,\n inputs=[x],\n outputs=[result],\n name=\"test_regex_full_match_email_domain\",\n)" }, { "summary": "match_empty", "code": "node = onnx.helper.make_node(\n \"RegexFullMatch\",\n inputs=[\"X\"],\n outputs=[\"Y\"],\n pattern=r\"(\\W|^)[\\w.\\-]{0,25}@(yahoo|gmail)\\.com(\\W|$)\",\n)\n\nx = np.array([[], []]).astype(object)\nresult = np.array([[], []]).astype(bool)\nexpect(\n node,\n inputs=[x],\n outputs=[result],\n name=\"test_regex_full_match_empty\",\n)" } ] }, { "name": "RelativePositionBias", "module": "com.microsoft", "version": 1, "description": "Compute binned relative position bias for T5 model. ref: https://arxiv.org/abs/1803.02155v2", "attributes": [ { "name": "is_bidirectional", "type": "int64", "required": false, "description": "Default value is 0." }, { "name": "max_distance", "type": "int64", "required": true, "description": "Max distance" } ], "inputs": [ { "name": "bias_table", "type": "T", "description": "2D input tensor with shape (num_buckets, num_heads), COL-major(See UT for example)" }, { "name": "query_length", "type": "U", "description": "The length of query. Self Attention requires query_length = key_length" }, { "name": "key_length", "type": "U", "description": "The length of key." } ], "min_input": 3, "max_input": 3, "outputs": [ { "name": "output", "type": "T", "description": "4D output tensor with shape (1, num_heads, sequence_length, sequence_length)" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to float or half tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain sequence_length to int tensors.", "type_param_str": "U", "allowed_type_strs": [ "tensor(int64)" ] } ] }, { "name": "Relu", "module": "ai.onnx", "version": 1, "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], name=\"test_relu\")" } ], "category": "Activation" }, { "name": "Relu", "module": "ai.onnx", "version": 6, "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], name=\"test_relu\")" } ], "category": "Activation" }, { "name": "Relu", "module": "ai.onnx", "version": 13, "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], name=\"test_relu\")" } ], "category": "Activation" }, { "name": "Relu", "module": "ai.onnx", "version": 14, "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], name=\"test_relu\")" } ], "category": "Activation" }, { "name": "RemovePadding", "module": "com.microsoft", "version": 1, "description": "Compress transformer input by removing paddings. It assumes padding is on the right side of sequence.\n\nThe input has padding with shape (batch_size, sequence_length, hidden_size). This will generate two outputs:\noutput has shape (total_tokens, hidden_size); token_offset with shape (batch_size, sequence_length).\n\ntoken_offset has offsets of all non-padding tokens first, then offset of all padding tokens. It is\na list of batch_size * sequence_length elements, which is reshaped to 2D for convenience of shape inference.\n", "inputs": [ { "name": "input", "type": "T", "description": "Input tensor with shape (batch_size, sequence_length, hidden_size)" }, { "name": "sequence_token_count", "type": "M", "description": "Number of non-padding tokens in each sequence with shape (batch_size)." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "output", "type": "T", "description": "output tensor with shape (total_tokens, hidden_size)" }, { "name": "token_offset", "type": "M", "description": "Offset of non-padding tokens, and those of padding tokens. Its shape is (batch_size, sequence_length)" }, { "name": "cumulated_seq_len", "type": "M", "description": "Cumulated sequence lengths. Its shape is (batch_size + 1)" }, { "name": "max_seq_len", "type": "M", "description": "Max sequence length without padding. Its shape is (1)" } ], "min_output": 4, "max_output": 4, "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain sequence_token_count and token_offset to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "ReorderInput", "module": "com.microsoft.nchwc", "version": 1, "attributes": [ { "name": "channels_last", "type": "int64", "required": false } ], "inputs": [ { "name": "X", "type": "T" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T" } ], "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(float)" ] } ] }, { "name": "ReorderOutput", "module": "com.microsoft.nchwc", "version": 1, "attributes": [ { "name": "channels", "type": "int64", "required": false }, { "name": "channels_last", "type": "int64", "required": false } ], "inputs": [ { "name": "X", "type": "T" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T" } ], "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(float)" ] } ] }, { "name": "Reshape", "module": "ai.onnx", "version": 1, "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" }, { "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" } ], "category": "Shape" }, { "name": "Reshape", "module": "ai.onnx", "version": 5, "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" }, { "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" } ], "category": "Shape" }, { "name": "Reshape", "module": "ai.onnx", "version": 13, "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" }, { "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" } ], "category": "Shape" }, { "name": "Reshape", "module": "ai.onnx", "version": 14, "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.\n\nIf the attribute 'allowzero' is set, it is invalid for the specified shape to\ncontain both a zero value and -1, as the value of the dimension corresponding\nto -1 cannot be determined uniquely.\n", "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" }, { "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" } ], "category": "Shape" }, { "name": "Reshape", "module": "ai.onnx", "version": 19, "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.\n\nIf the attribute 'allowzero' is set, it is invalid for the specified shape to\ncontain both a zero value and -1, as the value of the dimension corresponding\nto -1 cannot be determined uniquely.\n", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] } ], "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" }, { "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" } ], "category": "Shape" }, { "name": "Reshape", "module": "ai.onnx", "version": 21, "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.\n\nIf the attribute 'allowzero' is set, it is invalid for the specified shape to\ncontain both a zero value and -1, as the value of the dimension corresponding\nto -1 cannot be determined uniquely.\n", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" }, { "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" } ], "category": "Shape" }, { "name": "Reshape", "module": "ai.onnx", "version": 23, "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.\n\nIf the attribute 'allowzero' is set, it is invalid for the specified shape to\ncontain both a zero value and -1, as the value of the dimension corresponding\nto -1 cannot be determined uniquely.\n", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" }, { "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" } ], "category": "Shape" }, { "name": "Reshape", "module": "ai.onnx", "version": 24, "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.\n\nIf the attribute 'allowzero' is set, it is invalid for the specified shape to\ncontain both a zero value and -1, as the value of the dimension corresponding\nto -1 cannot be determined uniquely.\n", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" }, { "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" } ], "category": "Shape" }, { "name": "Reshape", "module": "ai.onnx", "version": 25, "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.\n\nIf the attribute 'allowzero' is set, it is invalid for the specified shape to\ncontain both a zero value and -1, as the value of the dimension corresponding\nto -1 cannot be determined uniquely.\n", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" }, { "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(\n node,\n inputs=[data, shape],\n outputs=[reshaped],\n name=\"test_reshape_\" + test_name,\n )" } ], "category": "Shape" }, { "name": "Resize", "module": "ai.onnx", "version": 10, "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_align_corners\",\n)" }, { "summary": "resize_downsample_scales_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.5180721 4.2858863]\n# [ 9.589329 11.357142 ]]]]\noutput = interpolate_nd(\n data, cubic_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_downsample_scales_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.875 4.5 ]\n# [ 9.375 11. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_antialias\",\n)" }, { "summary": "resize_downsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2, 3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 1.0, 0.6], dtype=np.float32)\n\n# [[[[1.6666667, 3.3333333]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_nearest\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic\",\n)" }, { "summary": "resize_downsample_sizes_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 1.7750092 3.1200073 4.4650054]\n# [ 7.1550016 8.5 9.844998 ]\n# [12.534994 13.8799925 15.224991 ]]]]\noutput = interpolate_nd(data, cubic_coeffs_antialias, output_size=sizes).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic_antialias\",\n)" }, { "summary": "resize_downsample_sizes_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 2.3636363 3.590909 4.818182 ]\n# [ 7.2727275 8.5 9.727273 ]\n# [12.181818 13.409091 14.636364 ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n coordinate_transformation_mode=\"pytorch_half_pixel\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_pytorch_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 1, 3], dtype=np.int64)\n\n# [[[[1. 2. 4.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 1x2\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 2x3\n\n# [[[[1. 2. 4.]\n# [5. 6. 8.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_2_3", "code": "axes = [2, 3]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.4, 0.6, 0.6, 0.8], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_2_3\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_3_2", "code": "axes = [3, 2]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.6, 0.4, 0.8, 0.6], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n extrapolation_value=10.0,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_extrapolation_value\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_align_corners\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.75),\n scale_factors=scales,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_asymmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_upsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2], [3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 2.3, 2.94], dtype=np.float32)\n\n# [[[[1. , 1.15986395, 1.5 , 1.84013605, 2. ],\n# [1.56521738, 1.72508133, 2.06521738, 2.40535343, 2.56521738],\n# [2.43478262, 2.59464657, 2.93478262, 3.27491867, 3.43478262],\n# [3. , 3.15986395, 3.5 , 3.84013605, 4. ]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([3.0, 2.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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([8, 7], 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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_ceil_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"floor\"),\n output_size=sizes,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_floor_align_corners\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 7x7\n\n# [[[[1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 8x8\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,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"round_prefer_ceil\"),\n output_size=sizes,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric\",\n)" } ] }, { "name": "Resize", "module": "ai.onnx", "version": 11, "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_align_corners\",\n)" }, { "summary": "resize_downsample_scales_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.5180721 4.2858863]\n# [ 9.589329 11.357142 ]]]]\noutput = interpolate_nd(\n data, cubic_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_downsample_scales_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.875 4.5 ]\n# [ 9.375 11. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_antialias\",\n)" }, { "summary": "resize_downsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2, 3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 1.0, 0.6], dtype=np.float32)\n\n# [[[[1.6666667, 3.3333333]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_nearest\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic\",\n)" }, { "summary": "resize_downsample_sizes_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 1.7750092 3.1200073 4.4650054]\n# [ 7.1550016 8.5 9.844998 ]\n# [12.534994 13.8799925 15.224991 ]]]]\noutput = interpolate_nd(data, cubic_coeffs_antialias, output_size=sizes).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic_antialias\",\n)" }, { "summary": "resize_downsample_sizes_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 2.3636363 3.590909 4.818182 ]\n# [ 7.2727275 8.5 9.727273 ]\n# [12.181818 13.409091 14.636364 ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n coordinate_transformation_mode=\"pytorch_half_pixel\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_pytorch_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 1, 3], dtype=np.int64)\n\n# [[[[1. 2. 4.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 1x2\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 2x3\n\n# [[[[1. 2. 4.]\n# [5. 6. 8.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_2_3", "code": "axes = [2, 3]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.4, 0.6, 0.6, 0.8], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_2_3\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_3_2", "code": "axes = [3, 2]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.6, 0.4, 0.8, 0.6], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n extrapolation_value=10.0,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_extrapolation_value\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_align_corners\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.75),\n scale_factors=scales,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_asymmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_upsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2], [3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 2.3, 2.94], dtype=np.float32)\n\n# [[[[1. , 1.15986395, 1.5 , 1.84013605, 2. ],\n# [1.56521738, 1.72508133, 2.06521738, 2.40535343, 2.56521738],\n# [2.43478262, 2.59464657, 2.93478262, 3.27491867, 3.43478262],\n# [3. , 3.15986395, 3.5 , 3.84013605, 4. ]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([3.0, 2.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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([8, 7], 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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_ceil_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"floor\"),\n output_size=sizes,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_floor_align_corners\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 7x7\n\n# [[[[1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 8x8\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,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"round_prefer_ceil\"),\n output_size=sizes,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric\",\n)" } ] }, { "name": "Resize", "module": "ai.onnx", "version": 13, "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_align_corners\",\n)" }, { "summary": "resize_downsample_scales_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.5180721 4.2858863]\n# [ 9.589329 11.357142 ]]]]\noutput = interpolate_nd(\n data, cubic_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_downsample_scales_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.875 4.5 ]\n# [ 9.375 11. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_antialias\",\n)" }, { "summary": "resize_downsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2, 3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 1.0, 0.6], dtype=np.float32)\n\n# [[[[1.6666667, 3.3333333]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_nearest\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic\",\n)" }, { "summary": "resize_downsample_sizes_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 1.7750092 3.1200073 4.4650054]\n# [ 7.1550016 8.5 9.844998 ]\n# [12.534994 13.8799925 15.224991 ]]]]\noutput = interpolate_nd(data, cubic_coeffs_antialias, output_size=sizes).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic_antialias\",\n)" }, { "summary": "resize_downsample_sizes_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 2.3636363 3.590909 4.818182 ]\n# [ 7.2727275 8.5 9.727273 ]\n# [12.181818 13.409091 14.636364 ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n coordinate_transformation_mode=\"pytorch_half_pixel\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_pytorch_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 1, 3], dtype=np.int64)\n\n# [[[[1. 2. 4.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 1x2\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 2x3\n\n# [[[[1. 2. 4.]\n# [5. 6. 8.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_2_3", "code": "axes = [2, 3]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.4, 0.6, 0.6, 0.8], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_2_3\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_3_2", "code": "axes = [3, 2]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.6, 0.4, 0.8, 0.6], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n extrapolation_value=10.0,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_extrapolation_value\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_align_corners\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.75),\n scale_factors=scales,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_asymmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_upsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2], [3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 2.3, 2.94], dtype=np.float32)\n\n# [[[[1. , 1.15986395, 1.5 , 1.84013605, 2. ],\n# [1.56521738, 1.72508133, 2.06521738, 2.40535343, 2.56521738],\n# [2.43478262, 2.59464657, 2.93478262, 3.27491867, 3.43478262],\n# [3. , 3.15986395, 3.5 , 3.84013605, 4. ]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([3.0, 2.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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([8, 7], 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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_ceil_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"floor\"),\n output_size=sizes,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_floor_align_corners\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 7x7\n\n# [[[[1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 8x8\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,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"round_prefer_ceil\"),\n output_size=sizes,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric\",\n)" } ] }, { "name": "Resize", "module": "ai.onnx", "version": 18, "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)`
\nif input \\\"sizes\\\" is not specified.\n", "attributes": [ { "name": "antialias", "type": "int64", "required": false, "description": "If set to 1, \"linear\" and \"cubic\" interpolation modes will use an antialiasing filter when downscaling. Antialiasing is achieved by stretching the resampling filter by a factor max(1, 1 / scale), which means that when downsampling, more input pixels contribute to an output pixel." }, { "name": "axes", "type": "int64[]", "required": false, "description": "If provided, it specifies a subset of axes that 'roi', 'scales' and 'sizes' refer to. If not provided, all axes are assumed [0, 1, ..., r-1], where r = rank(data). Non-specified dimensions are interpreted as non-resizable. 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": "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\"`,
\n`x_original = (x_resized + 0.5) / scale - 0.5`
\n\nif coordinate_transformation_mode is `\"pytorch_half_pixel\"`,
\n`x_original = length_resized > 1 ? (x_resized + 0.5) / scale - 0.5 : 0`
\n\nif coordinate_transformation_mode is `\"align_corners\"`,
\n`x_original = x_resized * (length_original - 1) / (length_resized - 1)`
\n\nif coordinate_transformation_mode is `\"asymmetric\"`,
\n`x_original = x_resized / scale`
\n\nif coordinate_transformation_mode is `\"tf_crop_and_resize\"`,
\n`x_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)`\n." }, { "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": "keep_aspect_ratio_policy", "type": "string", "required": false, "default": "stretch", "description": "\nThis attribute describes how to interpret the `sizes` input with regard to keeping the original aspect ratio of the input, and it is not applicable when\nthe `scales` input is used.
\n\nGiven a set of `sizes`, associated with a subset of `axes` (explicitly provided or default), and assuming `d = axes[i]`, with `i` being the index of the provided `sizes`.
\n\nIf `keep_aspect_ratio_policy` is `\"stretch\"`, the original aspect ratio is disregarded, and the input is resized to the specified size:
\n`out_size[d] = sizes[i]`
\n\nIf `keep_aspect_ratio_policy` is `\"not_larger\"`, the sizes are adjusted so that no extent of the output is larger than the specified size, while keeping the original aspect ratio:
\n`scale = Min(sizes[i] / in_size[d])`
\n`out_size[d] = round_int(scale * in_size[d])`
\n\nIf `keep_aspect_ratio_policy` is `\"not_smaller\"`, the sizes are adjusted so that no extent of the output is smaller than the specified size, while keeping the original aspect ratio:
\n`scale = Max(sizes[i] / in_size[d])`
\n`out_size[d] = round_int(scale * in_size[d])`
\n\nFor non-resizable axes (those not specified in `axes`), the output size will be equal to the input size.\n\nNote: `round_int` stands for computing the nearest integer value, rounding halfway cases up." }, { "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 or the length of axes, if provided. 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' or the length of 'axes', if provided. 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": "Target size of the output tensor. Its interpretation depends on the 'keep_aspect_ratio_policy' value.The number of elements of 'sizes' should be the same as the rank of input 'X', or the length of 'axes', if provided. 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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_align_corners\",\n)" }, { "summary": "resize_downsample_scales_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.5180721 4.2858863]\n# [ 9.589329 11.357142 ]]]]\noutput = interpolate_nd(\n data, cubic_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_downsample_scales_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.875 4.5 ]\n# [ 9.375 11. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_antialias\",\n)" }, { "summary": "resize_downsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2, 3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 1.0, 0.6], dtype=np.float32)\n\n# [[[[1.6666667, 3.3333333]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_nearest\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic\",\n)" }, { "summary": "resize_downsample_sizes_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 1.7750092 3.1200073 4.4650054]\n# [ 7.1550016 8.5 9.844998 ]\n# [12.534994 13.8799925 15.224991 ]]]]\noutput = interpolate_nd(data, cubic_coeffs_antialias, output_size=sizes).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic_antialias\",\n)" }, { "summary": "resize_downsample_sizes_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 2.3636363 3.590909 4.818182 ]\n# [ 7.2727275 8.5 9.727273 ]\n# [12.181818 13.409091 14.636364 ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n coordinate_transformation_mode=\"pytorch_half_pixel\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_pytorch_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 1, 3], dtype=np.int64)\n\n# [[[[1. 2. 4.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 1x2\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 2x3\n\n# [[[[1. 2. 4.]\n# [5. 6. 8.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_2_3", "code": "axes = [2, 3]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.4, 0.6, 0.6, 0.8], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_2_3\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_3_2", "code": "axes = [3, 2]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.6, 0.4, 0.8, 0.6], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n extrapolation_value=10.0,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_extrapolation_value\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_align_corners\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.75),\n scale_factors=scales,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_asymmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_upsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2], [3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 2.3, 2.94], dtype=np.float32)\n\n# [[[[1. , 1.15986395, 1.5 , 1.84013605, 2. ],\n# [1.56521738, 1.72508133, 2.06521738, 2.40535343, 2.56521738],\n# [2.43478262, 2.59464657, 2.93478262, 3.27491867, 3.43478262],\n# [3. , 3.15986395, 3.5 , 3.84013605, 4. ]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([3.0, 2.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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([8, 7], 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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_ceil_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"floor\"),\n output_size=sizes,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_floor_align_corners\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 7x7\n\n# [[[[1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 8x8\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,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"round_prefer_ceil\"),\n output_size=sizes,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric\",\n)" } ] }, { "name": "Resize", "module": "ai.onnx", "version": 19, "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```\noutput_dimension = floor(input_dimension * (roi_end - roi_start) * scale)\n```\nif input \\\"sizes\\\" is not specified.\n", "attributes": [ { "name": "antialias", "type": "int64", "required": false, "description": "If set to 1, \"linear\" and \"cubic\" interpolation modes will use an antialiasing filter when downscaling. Antialiasing is achieved by stretching the resampling filter by a factor max(1, 1 / scale), which means that when downsampling, more input pixels contribute to an output pixel." }, { "name": "axes", "type": "int64[]", "required": false, "description": "If provided, it specifies a subset of axes that 'roi', 'scales' and 'sizes' refer to. If not provided, all axes are assumed [0, 1, ..., r-1], where r = rank(data). Non-specified dimensions are interpreted as non-resizable. 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": "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,\n `x_original` as the coordinate of axis x in the original tensor,\n `length_original` as the length of the original tensor in axis x,\n `length_resized` as the length of the resized tensor in axis x,\n `scale = length_resized / length_original`,\n `output_width` the target length on the axis x which can be a fractional number when it is calculated out of a scale factor,\n and `output_width_int` the effective output width as an integer.\n\nif coordinate_transformation_mode is `\"half_pixel\"`,\n```\nx_original = (x_resized + 0.5) / scale - 0.5\n```\n\nif coordinate_transformation_mode is `\"half_pixel_symmetric\"`,\n```\nadjustment = output_width_int / output_width\ncenter = input_width / 2\noffset = center * (1 - adjustment)\nx_ori = offset + (x + 0.5) / scale - 0.5\n```\n\nif coordinate_transformation_mode is `\"pytorch_half_pixel\"`,\n```\nx_original = length_resized > 1 ? (x_resized + 0.5) / scale - 0.5 : 0\n```\n\nif coordinate_transformation_mode is `\"align_corners\"`,\n```\nx_original = x_resized * (length_original - 1) / (length_resized - 1)\n```\n\nif coordinate_transformation_mode is `\"asymmetric\"`,\n```\nx_original = x_resized / scale\n```\n\nif coordinate_transformation_mode is `\"tf_crop_and_resize\"`,\n```\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)\n```\n." }, { "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": "keep_aspect_ratio_policy", "type": "string", "required": false, "default": "stretch", "description": "\nThis attribute describes how to interpret the `sizes` input with regard to keeping the original aspect ratio of the input, and it is not applicable when\nthe `scales` input is used.\n\nGiven a set of `sizes`, associated with a subset of `axes` (explicitly provided or default), and assuming `d = axes[i]`, with `i` being the index of the provided `sizes`.\n\nIf `keep_aspect_ratio_policy` is `\"stretch\"`, the original aspect ratio is disregarded, and the input is resized to the specified size:\n`out_size[d] = sizes[i]`\n\nIf `keep_aspect_ratio_policy` is `\"not_larger\"`, the sizes are adjusted so that no extent of the output is larger than the specified size, while keeping the original aspect ratio:\n```\nscale = Min(sizes[i] / in_size[d])\nout_size[d] = round_int(scale * in_size[d])\n```\n\nIf `keep_aspect_ratio_policy` is `\"not_smaller\"`, the sizes are adjusted so that no extent of the output is smaller than the specified size, while keeping the original aspect ratio:\n```\nscale = Max(sizes[i] / in_size[d])\nout_size[d] = round_int(scale * in_size[d])\n```\n\nFor non-resizable axes (those not specified in `axes`), the output size will be equal to the input size.\n\nNote: `round_int` stands for computing the nearest integer value, rounding halfway cases up." }, { "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 or the length of axes, if provided. 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' or the length of 'axes', if provided. 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": "Target size of the output tensor. Its interpretation depends on the 'keep_aspect_ratio_policy' value.The number of elements of 'sizes' should be the same as the rank of input 'X', or the length of 'axes', if provided. 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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_align_corners\",\n)" }, { "summary": "resize_downsample_scales_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.5180721 4.2858863]\n# [ 9.589329 11.357142 ]]]]\noutput = interpolate_nd(\n data, cubic_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_cubic_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_downsample_scales_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[ 2.875 4.5 ]\n# [ 9.375 11. ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_antialias\",\n)" }, { "summary": "resize_downsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2, 3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 1.0, 0.6], dtype=np.float32)\n\n# [[[[1.6666667, 3.3333333]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 0.6, 0.6], dtype=np.float32)\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_downsample_scales_nearest\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic\",\n)" }, { "summary": "resize_downsample_sizes_cubic_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"cubic\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 1.7750092 3.1200073 4.4650054]\n# [ 7.1550016 8.5 9.844998 ]\n# [12.534994 13.8799925 15.224991 ]]]]\noutput = interpolate_nd(data, cubic_coeffs_antialias, output_size=sizes).astype(\n np.float32\n)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_cubic_antialias\",\n)" }, { "summary": "resize_downsample_sizes_linear_antialias", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n antialias=1,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 3, 3], dtype=np.int64)\n\n# [[[[ 2.3636363 3.590909 4.818182 ]\n# [ 7.2727275 8.5 9.727273 ]\n# [12.181818 13.409091 14.636364 ]]]]\noutput = interpolate_nd(\n data, linear_coeffs_antialias, output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_antialias\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n coordinate_transformation_mode=\"pytorch_half_pixel\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_linear_pytorch_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 1, 1, 3], dtype=np.int64)\n\n# [[[[1. 2. 4.]]]]\noutput = interpolate_nd(\n data, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 1x2\n\n# [[[[1. 3.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_downsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([1, 3], dtype=np.int64) # Results in 2x3\n\n# [[[[1. 2. 4.]\n# [5. 6. 8.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_downsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_2_3", "code": "axes = [2, 3]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.4, 0.6, 0.6, 0.8], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_2_3\",\n)" }, { "summary": "resize_tf_crop_and_resize_axes_3_2", "code": "axes = [3, 2]\nnode = 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 axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\n# Note: for some rois, the result may be different with that of TF for inaccurate floating point\nroi = np.array([0.6, 0.4, 0.8, 0.6], dtype=np.float32)\nsizes = np.array([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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n axes=axes,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: linear_coeffs(x),\n output_size=sizes,\n roi=roi,\n coordinate_transformation_mode=\"tf_crop_and_resize\",\n extrapolation_value=10.0,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, roi, sizes],\n outputs=[output],\n name=\"test_resize_tf_crop_and_resize_extrapolation_value\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.5),\n scale_factors=scales,\n exclude_outside=True,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_A_n0p5_exclude_outside\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: cubic_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_align_corners\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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(\n data,\n lambda x, _: cubic_coeffs(x, A=-0.75),\n scale_factors=scales,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_cubic_asymmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: linear_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_align_corners\",\n)" }, { "summary": "resize_upsample_scales_linear_half_pixel_symmetric", "code": "node = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"linear\",\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n)\n\ndata = np.array([[[[1, 2], [3, 4]]]], dtype=np.float32)\nscales = np.array([1.0, 1.0, 2.3, 2.94], dtype=np.float32)\n\n# [[[[1. , 1.15986395, 1.5 , 1.84013605, 2. ],\n# [1.56521738, 1.72508133, 2.06521738, 2.40535343, 2.56521738],\n# [2.43478262, 2.59464657, 2.93478262, 3.27491867, 3.43478262],\n# [3. , 3.15986395, 3.5 , 3.84013605, 4. ]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: linear_coeffs(x),\n scale_factors=scales,\n coordinate_transformation_mode=\"half_pixel_symmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_linear_half_pixel_symmetric\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), scale_factors=scales\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_scales_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"scales\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([3.0, 2.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, lambda x, _: nearest_coeffs(x), scale_factors=scales, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_resize_upsample_scales_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: cubic_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_cubic\",\n)" }, { "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\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, lambda x, _: nearest_coeffs(x), output_size=sizes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_2_3", "code": "axes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_2_3\",\n)" }, { "summary": "resize_upsample_sizes_nearest_axes_3_2", "code": "axes = [3, 2]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([8, 7], 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, lambda x, _: nearest_coeffs(x), output_size=sizes, axes=axes\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_axes_3_2\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_ceil_half_pixel\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"floor\"),\n output_size=sizes,\n coordinate_transformation_mode=\"align_corners\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_floor_align_corners\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_larger", "code": "keep_aspect_ratio_policy = \"not_larger\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 7x7\n\n# [[[[1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [1. 1. 1. 1. 2. 2. 2.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]\n# [3. 3. 3. 3. 4. 4. 4.]]]]\noutput = interpolate_nd(\n data,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_larger\",\n)" }, { "summary": "resize_upsample_sizes_nearest_not_smaller", "code": "keep_aspect_ratio_policy = \"not_smaller\"\naxes = [2, 3]\nnode = onnx.helper.make_node(\n \"Resize\",\n inputs=[\"X\", \"\", \"\", \"sizes\"],\n outputs=[\"Y\"],\n mode=\"nearest\",\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n)\n\ndata = np.array(\n [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nsizes = np.array([7, 8], dtype=np.int64) # Results in 8x8\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,\n lambda x, _: nearest_coeffs(x),\n output_size=sizes,\n axes=axes,\n keep_aspect_ratio_policy=keep_aspect_ratio_policy,\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_not_smaller\",\n)" }, { "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 [\n [\n [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n [9, 10, 11, 12],\n [13, 14, 15, 16],\n ]\n ]\n ],\n dtype=np.float32,\n)\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,\n lambda x, _: nearest_coeffs(x, mode=\"round_prefer_ceil\"),\n output_size=sizes,\n coordinate_transformation_mode=\"asymmetric\",\n).astype(np.float32)\n\nexpect(\n node,\n inputs=[data, sizes],\n outputs=[output],\n name=\"test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric\",\n)" } ] }, { "name": "Resize", "module": "com.ms.internal.nhwc", "version": 11, "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)" ] } ] }, { "name": "Resize", "module": "com.ms.internal.nhwc", "version": 13, "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)" ] } ] }, { "name": "Resize", "module": "com.ms.internal.nhwc", "version": 18, "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)`
\nif input \\\"sizes\\\" is not specified.\n", "attributes": [ { "name": "antialias", "type": "int64", "required": false, "description": "If set to 1, \"linear\" and \"cubic\" interpolation modes will use an antialiasing filter when downscaling. Antialiasing is achieved by stretching the resampling filter by a factor max(1, 1 / scale), which means that when downsampling, more input pixels contribute to an output pixel." }, { "name": "axes", "type": "int64[]", "required": false, "description": "If provided, it specifies a subset of axes that 'roi', 'scales' and 'sizes' refer to. If not provided, all axes are assumed [0, 1, ..., r-1], where r = rank(data). Non-specified dimensions are interpreted as non-resizable. 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": "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\"`,
\n`x_original = (x_resized + 0.5) / scale - 0.5`
\n\nif coordinate_transformation_mode is `\"pytorch_half_pixel\"`,
\n`x_original = length_resized > 1 ? (x_resized + 0.5) / scale - 0.5 : 0`
\n\nif coordinate_transformation_mode is `\"align_corners\"`,
\n`x_original = x_resized * (length_original - 1) / (length_resized - 1)`
\n\nif coordinate_transformation_mode is `\"asymmetric\"`,
\n`x_original = x_resized / scale`
\n\nif coordinate_transformation_mode is `\"tf_crop_and_resize\"`,
\n`x_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)`\n." }, { "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": "keep_aspect_ratio_policy", "type": "string", "required": false, "default": "stretch", "description": "\nThis attribute describes how to interpret the `sizes` input with regard to keeping the original aspect ratio of the input, and it is not applicable when\nthe `scales` input is used.
\n\nGiven a set of `sizes`, associated with a subset of `axes` (explicitly provided or default), and assuming `d = axes[i]`, with `i` being the index of the provided `sizes`.
\n\nIf `keep_aspect_ratio_policy` is `\"stretch\"`, the original aspect ratio is disregarded, and the input is resized to the specified size:
\n`out_size[d] = sizes[i]`
\n\nIf `keep_aspect_ratio_policy` is `\"not_larger\"`, the sizes are adjusted so that no extent of the output is larger than the specified size, while keeping the original aspect ratio:
\n`scale = Min(sizes[i] / in_size[d])`
\n`out_size[d] = round_int(scale * in_size[d])`
\n\nIf `keep_aspect_ratio_policy` is `\"not_smaller\"`, the sizes are adjusted so that no extent of the output is smaller than the specified size, while keeping the original aspect ratio:
\n`scale = Max(sizes[i] / in_size[d])`
\n`out_size[d] = round_int(scale * in_size[d])`
\n\nFor non-resizable axes (those not specified in `axes`), the output size will be equal to the input size.\n\nNote: `round_int` stands for computing the nearest integer value, rounding halfway cases up." }, { "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 or the length of axes, if provided. 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' or the length of 'axes', if provided. 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": "Target size of the output tensor. Its interpretation depends on the 'keep_aspect_ratio_policy' value.The number of elements of 'sizes' should be the same as the rank of input 'X', or the length of 'axes', if provided. 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)" ] } ] }, { "name": "Resize", "module": "com.ms.internal.nhwc", "version": 19, "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```\noutput_dimension = floor(input_dimension * (roi_end - roi_start) * scale)\n```\nif input \\\"sizes\\\" is not specified.\n", "attributes": [ { "name": "antialias", "type": "int64", "required": false, "description": "If set to 1, \"linear\" and \"cubic\" interpolation modes will use an antialiasing filter when downscaling. Antialiasing is achieved by stretching the resampling filter by a factor max(1, 1 / scale), which means that when downsampling, more input pixels contribute to an output pixel." }, { "name": "axes", "type": "int64[]", "required": false, "description": "If provided, it specifies a subset of axes that 'roi', 'scales' and 'sizes' refer to. If not provided, all axes are assumed [0, 1, ..., r-1], where r = rank(data). Non-specified dimensions are interpreted as non-resizable. 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": "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,\n `x_original` as the coordinate of axis x in the original tensor,\n `length_original` as the length of the original tensor in axis x,\n `length_resized` as the length of the resized tensor in axis x,\n `scale = length_resized / length_original`,\n `output_width` the target length on the axis x which can be a fractional number when it is calculated out of a scale factor,\n and `output_width_int` the effective output width as an integer.\n\nif coordinate_transformation_mode is `\"half_pixel\"`,\n```\nx_original = (x_resized + 0.5) / scale - 0.5\n```\n\nif coordinate_transformation_mode is `\"half_pixel_symmetric\"`,\n```\nadjustment = output_width_int / output_width\ncenter = input_width / 2\noffset = center * (1 - adjustment)\nx_ori = offset + (x + 0.5) / scale - 0.5\n```\n\nif coordinate_transformation_mode is `\"pytorch_half_pixel\"`,\n```\nx_original = length_resized > 1 ? (x_resized + 0.5) / scale - 0.5 : 0\n```\n\nif coordinate_transformation_mode is `\"align_corners\"`,\n```\nx_original = x_resized * (length_original - 1) / (length_resized - 1)\n```\n\nif coordinate_transformation_mode is `\"asymmetric\"`,\n```\nx_original = x_resized / scale\n```\n\nif coordinate_transformation_mode is `\"tf_crop_and_resize\"`,\n```\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)\n```\n." }, { "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": "keep_aspect_ratio_policy", "type": "string", "required": false, "default": "stretch", "description": "\nThis attribute describes how to interpret the `sizes` input with regard to keeping the original aspect ratio of the input, and it is not applicable when\nthe `scales` input is used.\n\nGiven a set of `sizes`, associated with a subset of `axes` (explicitly provided or default), and assuming `d = axes[i]`, with `i` being the index of the provided `sizes`.\n\nIf `keep_aspect_ratio_policy` is `\"stretch\"`, the original aspect ratio is disregarded, and the input is resized to the specified size:\n`out_size[d] = sizes[i]`\n\nIf `keep_aspect_ratio_policy` is `\"not_larger\"`, the sizes are adjusted so that no extent of the output is larger than the specified size, while keeping the original aspect ratio:\n```\nscale = Min(sizes[i] / in_size[d])\nout_size[d] = round_int(scale * in_size[d])\n```\n\nIf `keep_aspect_ratio_policy` is `\"not_smaller\"`, the sizes are adjusted so that no extent of the output is smaller than the specified size, while keeping the original aspect ratio:\n```\nscale = Max(sizes[i] / in_size[d])\nout_size[d] = round_int(scale * in_size[d])\n```\n\nFor non-resizable axes (those not specified in `axes`), the output size will be equal to the input size.\n\nNote: `round_int` stands for computing the nearest integer value, rounding halfway cases up." }, { "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 or the length of axes, if provided. 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' or the length of 'axes', if provided. 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": "Target size of the output tensor. Its interpretation depends on the 'keep_aspect_ratio_policy' value.The number of elements of 'sizes' should be the same as the rank of input 'X', or the length of 'axes', if provided. 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)" ] } ] }, { "name": "RestorePadding", "module": "com.microsoft", "version": 1, "description": "Restore paddings and fill padding with zeros.\n\nThe input has padding with shape (total_tokens, hidden_size) and token_offset with shape (batch_size, sequence_length).\nThe output has shape (batch_size, sequence_length, hidden_size).\n", "inputs": [ { "name": "input", "type": "T", "description": "Input tensor with shape (total_tokens, hidden_size)" }, { "name": "token_offset", "type": "M", "description": "Offset of non-padding tokens and paddings. Its shape is (batch_size, sequence_length)" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "output", "type": "T", "description": "output tensor with shape (batch_size, sequence_length, hidden_size)" } ], "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(float)", "tensor(float16)" ] }, { "description": "Constrain token_offset to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "ReverseSequence", "module": "ai.onnx", "version": 10, "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(\n [\n [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 ],\n dtype=np.float32,\n)\nsequence_lens = np.array([1, 2, 3, 4], dtype=np.int64)\n\ny = np.array(\n [\n [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 ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[x, sequence_lens],\n outputs=[y],\n name=\"test_reversesequence_batch\",\n)" }, { "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(\n [\n [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 ],\n dtype=np.float32,\n)\nsequence_lens = np.array([4, 3, 2, 1], dtype=np.int64)\n\ny = np.array(\n [\n [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 ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[x, sequence_lens],\n outputs=[y],\n name=\"test_reversesequence_time\",\n)" } ] }, { "name": "Rfft", "module": "com.microsoft", "version": 1, "description": "This function computes the n-point one dimensional Fourier transform for a real-valued input where n is an even number.", "attributes": [ { "name": "normalized", "type": "int64", "required": false, "description": "must be 0, normalization currently not supported" }, { "name": "onesided", "type": "int64", "required": false, "default": 1, "description": "must be 1, only one sided FFTs supported" }, { "name": "signal_ndim", "type": "int64", "required": false, "default": 1, "description": "number of dimensions comprising the signal, collected in reverse order (e.g. 1 = last dimension is the signal)" } ], "inputs": [ { "name": "X", "type": "T", "description": "input tensor of size n in the signal dim" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "output tensor of size (n//2 + 1) in the signal dim and 2 in the last dimension for the real and complex parts" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to float or half tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(double)", "tensor(float16)" ] } ] }, { "name": "RoiAlign", "module": "ai.onnx", "version": 10, "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(\n node,\n inputs=[X, rois, batch_indices],\n outputs=[Y],\n name=\"test_roialign_aligned_false\",\n)" }, { "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(\n node,\n inputs=[X, rois, batch_indices],\n outputs=[Y],\n name=\"test_roialign_aligned_true\",\n)" }, { "summary": "roialign_mode_max", "code": "X = np.array(\n [\n [\n [\n [\n 0.2764,\n 0.715,\n 0.1958,\n 0.3416,\n 0.4638,\n 0.0259,\n 0.2963,\n 0.6518,\n 0.4856,\n 0.725,\n ],\n [\n 0.9637,\n 0.0895,\n 0.2919,\n 0.6753,\n 0.0234,\n 0.6132,\n 0.8085,\n 0.5324,\n 0.8992,\n 0.4467,\n ],\n [\n 0.3265,\n 0.8479,\n 0.9698,\n 0.2471,\n 0.9336,\n 0.1878,\n 0.4766,\n 0.4308,\n 0.34,\n 0.2162,\n ],\n [\n 0.0206,\n 0.172,\n 0.2155,\n 0.4394,\n 0.0653,\n 0.3406,\n 0.7724,\n 0.3921,\n 0.2541,\n 0.5799,\n ],\n [\n 0.4062,\n 0.2194,\n 0.4473,\n 0.4687,\n 0.7109,\n 0.9327,\n 0.9815,\n 0.632,\n 0.1728,\n 0.6119,\n ],\n [\n 0.3097,\n 0.1283,\n 0.4984,\n 0.5068,\n 0.4279,\n 0.0173,\n 0.4388,\n 0.043,\n 0.4671,\n 0.7119,\n ],\n [\n 0.1011,\n 0.8477,\n 0.4726,\n 0.1777,\n 0.9923,\n 0.4042,\n 0.1869,\n 0.7795,\n 0.9946,\n 0.9689,\n ],\n [\n 0.1366,\n 0.3671,\n 0.7011,\n 0.6234,\n 0.9867,\n 0.5585,\n 0.6985,\n 0.5609,\n 0.8788,\n 0.9928,\n ],\n [\n 0.5697,\n 0.8511,\n 0.6711,\n 0.9406,\n 0.8751,\n 0.7496,\n 0.165,\n 0.1049,\n 0.1559,\n 0.2514,\n ],\n [\n 0.7012,\n 0.4056,\n 0.7879,\n 0.3461,\n 0.0415,\n 0.2998,\n 0.5094,\n 0.3727,\n 0.5482,\n 0.0502,\n ],\n ]\n ]\n ],\n dtype=np.float32,\n)\nrois = np.array(\n [[0.0, 0.0, 9.0, 9.0], [0.0, 5.0, 4.0, 9.0], [5.0, 5.0, 9.0, 9.0]],\n dtype=np.float32,\n)\nbatch_indices = np.array([0, 0, 0], dtype=np.int64)\n\nY = np.array(\n [\n [\n [\n [0.3445228, 0.37310338, 0.37865096, 0.446696, 0.37991184],\n [0.4133513, 0.5455125, 0.6651902, 0.55805874, 0.27110294],\n [0.21223956, 0.40924096, 0.8417618, 0.792561, 0.37196714],\n [0.46835402, 0.39741728, 0.8012819, 0.4969306, 0.5495158],\n [0.3595896, 0.5196813, 0.5403741, 0.23814403, 0.19992709],\n ]\n ],\n [\n [\n [0.30517197, 0.5086199, 0.3189761, 0.4054401, 0.47630402],\n [0.50862, 0.8477, 0.37808004, 0.24936005, 0.79384017],\n [0.17620805, 0.29368007, 0.44870415, 0.4987201, 0.63148826],\n [0.51066005, 0.8511, 0.5368801, 0.9406, 0.70008016],\n [0.4487681, 0.51066035, 0.5042561, 0.5643603, 0.42004836],\n ]\n ],\n [\n [\n [0.21062402, 0.3510401, 0.37416005, 0.5967599, 0.46507207],\n [0.32336006, 0.31180006, 0.6236001, 0.9946, 0.7751202],\n [0.35744014, 0.5588001, 0.35897616, 0.7030401, 0.6353923],\n [0.5996801, 0.27940005, 0.17948808, 0.35152006, 0.31769615],\n [0.3598083, 0.40752012, 0.2385281, 0.43856013, 0.26313624],\n ]\n ],\n ],\n dtype=np.float32,\n)\n\nnode = onnx.helper.make_node(\n \"RoiAlign\",\n inputs=[\"X\", \"rois\", \"batch_indices\"],\n mode=\"max\",\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\nexpect(\n node,\n inputs=[X, rois, batch_indices],\n outputs=[Y],\n name=\"test_roialign_mode_max\",\n)" } ] }, { "name": "RoiAlign", "module": "ai.onnx", "version": 16, "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(\n node,\n inputs=[X, rois, batch_indices],\n outputs=[Y],\n name=\"test_roialign_aligned_false\",\n)" }, { "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(\n node,\n inputs=[X, rois, batch_indices],\n outputs=[Y],\n name=\"test_roialign_aligned_true\",\n)" }, { "summary": "roialign_mode_max", "code": "X = np.array(\n [\n [\n [\n [\n 0.2764,\n 0.715,\n 0.1958,\n 0.3416,\n 0.4638,\n 0.0259,\n 0.2963,\n 0.6518,\n 0.4856,\n 0.725,\n ],\n [\n 0.9637,\n 0.0895,\n 0.2919,\n 0.6753,\n 0.0234,\n 0.6132,\n 0.8085,\n 0.5324,\n 0.8992,\n 0.4467,\n ],\n [\n 0.3265,\n 0.8479,\n 0.9698,\n 0.2471,\n 0.9336,\n 0.1878,\n 0.4766,\n 0.4308,\n 0.34,\n 0.2162,\n ],\n [\n 0.0206,\n 0.172,\n 0.2155,\n 0.4394,\n 0.0653,\n 0.3406,\n 0.7724,\n 0.3921,\n 0.2541,\n 0.5799,\n ],\n [\n 0.4062,\n 0.2194,\n 0.4473,\n 0.4687,\n 0.7109,\n 0.9327,\n 0.9815,\n 0.632,\n 0.1728,\n 0.6119,\n ],\n [\n 0.3097,\n 0.1283,\n 0.4984,\n 0.5068,\n 0.4279,\n 0.0173,\n 0.4388,\n 0.043,\n 0.4671,\n 0.7119,\n ],\n [\n 0.1011,\n 0.8477,\n 0.4726,\n 0.1777,\n 0.9923,\n 0.4042,\n 0.1869,\n 0.7795,\n 0.9946,\n 0.9689,\n ],\n [\n 0.1366,\n 0.3671,\n 0.7011,\n 0.6234,\n 0.9867,\n 0.5585,\n 0.6985,\n 0.5609,\n 0.8788,\n 0.9928,\n ],\n [\n 0.5697,\n 0.8511,\n 0.6711,\n 0.9406,\n 0.8751,\n 0.7496,\n 0.165,\n 0.1049,\n 0.1559,\n 0.2514,\n ],\n [\n 0.7012,\n 0.4056,\n 0.7879,\n 0.3461,\n 0.0415,\n 0.2998,\n 0.5094,\n 0.3727,\n 0.5482,\n 0.0502,\n ],\n ]\n ]\n ],\n dtype=np.float32,\n)\nrois = np.array(\n [[0.0, 0.0, 9.0, 9.0], [0.0, 5.0, 4.0, 9.0], [5.0, 5.0, 9.0, 9.0]],\n dtype=np.float32,\n)\nbatch_indices = np.array([0, 0, 0], dtype=np.int64)\n\nY = np.array(\n [\n [\n [\n [0.3445228, 0.37310338, 0.37865096, 0.446696, 0.37991184],\n [0.4133513, 0.5455125, 0.6651902, 0.55805874, 0.27110294],\n [0.21223956, 0.40924096, 0.8417618, 0.792561, 0.37196714],\n [0.46835402, 0.39741728, 0.8012819, 0.4969306, 0.5495158],\n [0.3595896, 0.5196813, 0.5403741, 0.23814403, 0.19992709],\n ]\n ],\n [\n [\n [0.30517197, 0.5086199, 0.3189761, 0.4054401, 0.47630402],\n [0.50862, 0.8477, 0.37808004, 0.24936005, 0.79384017],\n [0.17620805, 0.29368007, 0.44870415, 0.4987201, 0.63148826],\n [0.51066005, 0.8511, 0.5368801, 0.9406, 0.70008016],\n [0.4487681, 0.51066035, 0.5042561, 0.5643603, 0.42004836],\n ]\n ],\n [\n [\n [0.21062402, 0.3510401, 0.37416005, 0.5967599, 0.46507207],\n [0.32336006, 0.31180006, 0.6236001, 0.9946, 0.7751202],\n [0.35744014, 0.5588001, 0.35897616, 0.7030401, 0.6353923],\n [0.5996801, 0.27940005, 0.17948808, 0.35152006, 0.31769615],\n [0.3598083, 0.40752012, 0.2385281, 0.43856013, 0.26313624],\n ]\n ],\n ],\n dtype=np.float32,\n)\n\nnode = onnx.helper.make_node(\n \"RoiAlign\",\n inputs=[\"X\", \"rois\", \"batch_indices\"],\n mode=\"max\",\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\nexpect(\n node,\n inputs=[X, rois, batch_indices],\n outputs=[Y],\n name=\"test_roialign_mode_max\",\n)" } ] }, { "name": "RoiAlign", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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(\n node,\n inputs=[X, rois, batch_indices],\n outputs=[Y],\n name=\"test_roialign_aligned_false\",\n)" }, { "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(\n node,\n inputs=[X, rois, batch_indices],\n outputs=[Y],\n name=\"test_roialign_aligned_true\",\n)" }, { "summary": "roialign_mode_max", "code": "X = np.array(\n [\n [\n [\n [\n 0.2764,\n 0.715,\n 0.1958,\n 0.3416,\n 0.4638,\n 0.0259,\n 0.2963,\n 0.6518,\n 0.4856,\n 0.725,\n ],\n [\n 0.9637,\n 0.0895,\n 0.2919,\n 0.6753,\n 0.0234,\n 0.6132,\n 0.8085,\n 0.5324,\n 0.8992,\n 0.4467,\n ],\n [\n 0.3265,\n 0.8479,\n 0.9698,\n 0.2471,\n 0.9336,\n 0.1878,\n 0.4766,\n 0.4308,\n 0.34,\n 0.2162,\n ],\n [\n 0.0206,\n 0.172,\n 0.2155,\n 0.4394,\n 0.0653,\n 0.3406,\n 0.7724,\n 0.3921,\n 0.2541,\n 0.5799,\n ],\n [\n 0.4062,\n 0.2194,\n 0.4473,\n 0.4687,\n 0.7109,\n 0.9327,\n 0.9815,\n 0.632,\n 0.1728,\n 0.6119,\n ],\n [\n 0.3097,\n 0.1283,\n 0.4984,\n 0.5068,\n 0.4279,\n 0.0173,\n 0.4388,\n 0.043,\n 0.4671,\n 0.7119,\n ],\n [\n 0.1011,\n 0.8477,\n 0.4726,\n 0.1777,\n 0.9923,\n 0.4042,\n 0.1869,\n 0.7795,\n 0.9946,\n 0.9689,\n ],\n [\n 0.1366,\n 0.3671,\n 0.7011,\n 0.6234,\n 0.9867,\n 0.5585,\n 0.6985,\n 0.5609,\n 0.8788,\n 0.9928,\n ],\n [\n 0.5697,\n 0.8511,\n 0.6711,\n 0.9406,\n 0.8751,\n 0.7496,\n 0.165,\n 0.1049,\n 0.1559,\n 0.2514,\n ],\n [\n 0.7012,\n 0.4056,\n 0.7879,\n 0.3461,\n 0.0415,\n 0.2998,\n 0.5094,\n 0.3727,\n 0.5482,\n 0.0502,\n ],\n ]\n ]\n ],\n dtype=np.float32,\n)\nrois = np.array(\n [[0.0, 0.0, 9.0, 9.0], [0.0, 5.0, 4.0, 9.0], [5.0, 5.0, 9.0, 9.0]],\n dtype=np.float32,\n)\nbatch_indices = np.array([0, 0, 0], dtype=np.int64)\n\nY = np.array(\n [\n [\n [\n [0.3445228, 0.37310338, 0.37865096, 0.446696, 0.37991184],\n [0.4133513, 0.5455125, 0.6651902, 0.55805874, 0.27110294],\n [0.21223956, 0.40924096, 0.8417618, 0.792561, 0.37196714],\n [0.46835402, 0.39741728, 0.8012819, 0.4969306, 0.5495158],\n [0.3595896, 0.5196813, 0.5403741, 0.23814403, 0.19992709],\n ]\n ],\n [\n [\n [0.30517197, 0.5086199, 0.3189761, 0.4054401, 0.47630402],\n [0.50862, 0.8477, 0.37808004, 0.24936005, 0.79384017],\n [0.17620805, 0.29368007, 0.44870415, 0.4987201, 0.63148826],\n [0.51066005, 0.8511, 0.5368801, 0.9406, 0.70008016],\n [0.4487681, 0.51066035, 0.5042561, 0.5643603, 0.42004836],\n ]\n ],\n [\n [\n [0.21062402, 0.3510401, 0.37416005, 0.5967599, 0.46507207],\n [0.32336006, 0.31180006, 0.6236001, 0.9946, 0.7751202],\n [0.35744014, 0.5588001, 0.35897616, 0.7030401, 0.6353923],\n [0.5996801, 0.27940005, 0.17948808, 0.35152006, 0.31769615],\n [0.3598083, 0.40752012, 0.2385281, 0.43856013, 0.26313624],\n ]\n ],\n ],\n dtype=np.float32,\n)\n\nnode = onnx.helper.make_node(\n \"RoiAlign\",\n inputs=[\"X\", \"rois\", \"batch_indices\"],\n mode=\"max\",\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\nexpect(\n node,\n inputs=[X, rois, batch_indices],\n outputs=[Y],\n name=\"test_roialign_mode_max\",\n)" } ] }, { "name": "RotaryEmbedding", "module": "ai.onnx", "version": 23, "description": "RotaryEmbedding is the implementation of rotary positional embeddings (RoPE) based on the paper https://arxiv.org/pdf/2104.09864.\nThe key advantage of RoPE is that it allows the model to understand both the absolute position of a token and the relative distances\nbetween tokens. This is achieved through a rotational mechanism where the extent of rotation is computed based on the token's absolute position (position_ids).\n\nThe rotational mechanism is defined by sine and cosine functions that are used to represent the rotation angles.\nFor each token in the sequence, its positional embedding is computed by rotating its embedding vector. This is done by splitting the\nembedding vector either into two halves or interleaving every alternate token and applying the rotation matrix to each half of the embedding vector.\nThe rotation matrix is parameterized by the token's position in the sequence. The rotated halves of the embedding vector are concatenated\nto form the final positional embedding for each token. The rotated positional embeddings are used in the self-attention mechanism.\nThe rotation ensures that the model captures both absolute and relative positional information.\n\nRotary embeddings are defined using the following algorithm:\n\n```python\ndef rotary_embedding(\n input: np.ndarray,\n cos_cache: np.ndarray,\n sin_cache: np.ndarray,\n position_ids: np.ndarray | None = None,\n interleaved=None,\n rotary_embedding_dim=None,\n num_heads=None,\n) -> np.ndarray:\n original_input_shape = input.shape\n # First ensure input to be processed has shape [batch_size, seq_len, num_heads, head_size]\n if len(input.shape) == 4:\n input = np.transpose(input, (0, 2, 1, 3))\n batch_size = input.shape[0]\n sequence_length = input.shape[1]\n if len(input.shape) == 3:\n hidden_size = input.shape[2]\n assert num_heads != 0\n head_size = int(hidden_size / num_heads)\n new_shape = [batch_size, sequence_length, num_heads, head_size]\n input = np.reshape(input, new_shape)\n assert len(input.shape) == 4\n head_size = input.shape[3]\n\n # Fully or partially perform rotation on input based on rotary_embedding_dim attribute\n if rotary_embedding_dim is None or rotary_embedding_dim == 0:\n # If rotary_embedding_dim not provided, perform full rotation by using head_size\n rotary_embedding_dim = head_size\n x_rotate = input[:, :, :, :rotary_embedding_dim]\n x_not_rotate = input[:, :, :, rotary_embedding_dim:]\n rotary_embedding_dim_half = int(rotary_embedding_dim / 2)\n\n # Retrieve sin and cos caches using position ids\n if position_ids is not None:\n cos_cache = cos_cache[\n position_ids\n ] # Shape: [batch_size, sequence_length, rotary_embedding_dim/2]\n sin_cache = sin_cache[\n position_ids\n ] # Shape: [batch_size, sequence_length, rotary_embedding_dim/2]\n\n # Shape: [batch_size, sequence_length, rotary_embedding_dim/2]\n if cos_cache.shape[-1] != rotary_embedding_dim_half:\n raise ValueError(\n f\"Last dimension of cos cache ({cos_cache.shape[-1]}) does not match rotary_embedding_dim/2 ({rotary_embedding_dim_half}).\"\n )\n if sin_cache.shape[-1] != rotary_embedding_dim_half:\n raise ValueError(\n f\"Last dimension of sin cache ({sin_cache.shape[-1]}) does not match rotary_embedding_dim/2 ({rotary_embedding_dim_half}).\"\n )\n\n cos_cache = np.expand_dims(\n cos_cache, axis=2\n ) # Shape: [batch_size, sequence_length, 1, rotary_embedding_dim/2]\n sin_cache = np.expand_dims(\n sin_cache, axis=2\n ) # Shape: [batch_size, sequence_length, 1, rotary_embedding_dim/2]\n\n # Either divide the input in halves or interleave (based on interleaved attribute)\n if interleaved:\n x1 = x_rotate[:, :, :, 0::2]\n x2 = x_rotate[:, :, :, 1::2]\n else:\n x1, x2 = np.split(x_rotate, 2, axis=-1)\n\n # Calculate real and imaginary values\n real = (cos_cache * x1) - (sin_cache * x2)\n imag = (sin_cache * x1) + (cos_cache * x2)\n\n # Inserted rotated embeddings back to the original input\n if interleaved:\n # x_rotate[:, :, :, 0::2] = real\n # x_rotate[:, :, :, 1::2] = imag\n real = np.expand_dims(real, axis=-1)\n imag = np.expand_dims(imag, axis=-1)\n x_rotate_concat = np.concatenate((real, imag), axis=-1)\n x_rotate = np.reshape(x_rotate_concat, x_rotate.shape)\n else:\n x_rotate = np.concatenate((real, imag), axis=-1)\n output = np.concatenate((x_rotate, x_not_rotate), axis=-1)\n if len(original_input_shape) == 3:\n output = np.reshape(output, original_input_shape)\n else:\n output = np.transpose(output, (0, 2, 1, 3))\n return output\n```\n", "attributes": [ { "name": "interleaved", "type": "int64", "required": false, "description": "Rotate using interleaved pattern. Default value is 0 (False)." }, { "name": "num_heads", "type": "int64", "required": false, "description": "Number of attention heads. Must be provided when input is a 3D tensor. " }, { "name": "rotary_embedding_dim", "type": "int64", "required": false, "description": "Rotary embedding dimension used to apply partial rotary embeddings." } ], "inputs": [ { "name": "X", "type": "T", "description": "The input tensor representing the token embeddings. 4D tensor with shape `(batch_size, num_heads, sequence_length, head_size)` or 3D tensor with shape `(batch_size, sequence_length, hidden_size)`. For cases with a 4D input tensor, `head_size` has to be even. For cases with a 3D input tensor, `num_heads` attribute must be provided and `hidden_size` must be an even multiple of `num_heads` where `hidden_size = num_heads * head_size`" }, { "name": "cos_cache", "type": "T", "description": "The cosine values for the rotation. 2D tensor with shape `(max_position_id_plus_1, head_size / 2)` for full rotation or `(max_position_id_plus_1, rotary_embedding_dim / 2)` for partial rotation when `position_ids` are provided. 3D tensor with shape `(batch_size, sequence_length, head_size / 2)` for full rotation or `(batch_size, sequence_length, rotary_embedding_dim / 2)` for partial rotation when `position_ids` are not provided. `max_position_id_plus_1` is a parameter to the model." }, { "name": "sin_cache", "type": "T", "description": "The sine values for the rotation. 2D tensor with shape `(max_position_id_plus_1, head_size / 2)` for full rotation or `(max_position_id_plus_1, rotary_embedding_dim / 2)` for partial rotation when `position_ids` are provided. 3D tensor with shape `(batch_size, sequence_length, head_size / 2)` for full rotation or `(batch_size, sequence_length, rotary_embedding_dim / 2)` for partial rotation when `position_ids` are not provided. `max_position_id_plus_1` is a parameter to the model." }, { "name": "position_ids", "type": "M", "option": "optional", "description": "The position indices for the tokens. 2D tensor with shape `(batch_size, sequence_length)`" } ], "min_input": 3, "max_input": 4, "outputs": [ { "name": "Y", "type": "T", "description": "Tensor with same shape as input." } ], "min_output": 1, "max_output": 1, "inputs_range": "3 - 4", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain input and output types to integer tensors.", "type_param_str": "M", "allowed_type_strs": [ "tensor(int64)" ] } ], "examples": [ { "summary": "rotary_embedding", "code": "node = onnx.helper.make_node(\n \"RotaryEmbedding\",\n inputs=[\"input\", \"cos_cache\", \"sin_cache\", \"position_ids\"],\n outputs=[\"output\"],\n)\n\ninput_data = np.random.rand(2, 4, 3, 8).astype(np.float32)\nposition_ids_data = np.random.uniform(0, 50, (2, 3)).astype(np.int64)\nsin_cache_data = np.random.rand(50, 4).astype(np.float32)\ncos_cache_data = np.random.rand(50, 4).astype(np.float32)\n\nexpected_output = rotary_embedding(\n input_data, cos_cache_data, sin_cache_data, position_ids=position_ids_data\n)\n\nexpect(\n node,\n inputs=[input_data, cos_cache_data, sin_cache_data, position_ids_data],\n outputs=[expected_output],\n name=\"test_rotary_embedding\",\n)" }, { "summary": "rotary_embedding_3d_input", "code": "num_heads = 4\nnode = onnx.helper.make_node(\n \"RotaryEmbedding\",\n inputs=[\"input\", \"cos_cache\", \"sin_cache\", \"position_ids\"],\n outputs=[\"output\"],\n num_heads=num_heads,\n)\n\ninput_data = np.random.rand(2, 3, 32).astype(np.float32)\nposition_ids_data = np.random.uniform(0, 50, (2, 3)).astype(np.int64)\nsin_cache_data = np.random.rand(50, 4).astype(np.float32)\ncos_cache_data = np.random.rand(50, 4).astype(np.float32)\n\nexpected_output = rotary_embedding(\n input_data,\n cos_cache_data,\n sin_cache_data,\n position_ids=position_ids_data,\n num_heads=num_heads,\n)\n\nexpect(\n node,\n inputs=[input_data, cos_cache_data, sin_cache_data, position_ids_data],\n outputs=[expected_output],\n name=\"test_rotary_embedding_3d_input\",\n)" }, { "summary": "rotary_embedding_interleaved", "code": "node = onnx.helper.make_node(\n \"RotaryEmbedding\",\n inputs=[\"input\", \"cos_cache\", \"sin_cache\", \"position_ids\"],\n outputs=[\"output\"],\n interleaved=1,\n)\n\ninput_data = np.random.rand(2, 4, 3, 8).astype(np.float32)\nposition_ids_data = np.random.uniform(0, 50, (2, 3)).astype(np.int64)\nsin_cache_data = np.random.rand(50, 4).astype(np.float32)\ncos_cache_data = np.random.rand(50, 4).astype(np.float32)\n\nexpected_output = rotary_embedding(\n input_data,\n cos_cache_data,\n sin_cache_data,\n position_ids=position_ids_data,\n interleaved=1,\n)\n\nexpect(\n node,\n inputs=[input_data, cos_cache_data, sin_cache_data, position_ids_data],\n outputs=[expected_output],\n name=\"test_rotary_embedding_interleaved\",\n)" }, { "summary": "rotary_embedding_no_position_ids", "code": "node = onnx.helper.make_node(\n \"RotaryEmbedding\",\n inputs=[\"input\", \"cos_cache\", \"sin_cache\"],\n outputs=[\"output\"],\n)\n\ninput_data = np.random.rand(2, 4, 3, 8).astype(np.float32)\nsin_cache_data = np.random.rand(2, 3, 4).astype(np.float32)\ncos_cache_data = np.random.rand(2, 3, 4).astype(np.float32)\n\nexpected_output = rotary_embedding(input_data, cos_cache_data, sin_cache_data)\n\nexpect(\n node,\n inputs=[input_data, cos_cache_data, sin_cache_data],\n outputs=[expected_output],\n name=\"test_rotary_embedding_no_position_ids\",\n)" }, { "summary": "rotary_embedding_no_position_ids_interleaved", "code": "node = onnx.helper.make_node(\n \"RotaryEmbedding\",\n inputs=[\"input\", \"cos_cache\", \"sin_cache\"],\n outputs=[\"output\"],\n interleaved=1,\n)\n\ninput_data = np.random.rand(2, 4, 3, 8).astype(np.float32)\nsin_cache_data = np.random.rand(2, 3, 4).astype(np.float32)\ncos_cache_data = np.random.rand(2, 3, 4).astype(np.float32)\n\nexpected_output = rotary_embedding(\n input_data,\n cos_cache_data,\n sin_cache_data,\n interleaved=1,\n)\n\nexpect(\n node,\n inputs=[input_data, cos_cache_data, sin_cache_data],\n outputs=[expected_output],\n name=\"test_rotary_embedding_no_position_ids_interleaved\",\n)" }, { "summary": "rotary_embedding_no_position_ids_rotary_dim", "code": "node = onnx.helper.make_node(\n \"RotaryEmbedding\",\n inputs=[\"input\", \"cos_cache\", \"sin_cache\"],\n outputs=[\"output\"],\n rotary_embedding_dim=4,\n)\n\ninput_data = np.random.rand(2, 4, 3, 8).astype(np.float32)\nsin_cache_data = np.random.rand(2, 3, 2).astype(np.float32)\ncos_cache_data = np.random.rand(2, 3, 2).astype(np.float32)\n\nexpected_output = rotary_embedding(\n input_data,\n cos_cache_data,\n sin_cache_data,\n rotary_embedding_dim=4,\n)\n\nexpect(\n node,\n inputs=[input_data, cos_cache_data, sin_cache_data],\n outputs=[expected_output],\n name=\"test_rotary_embedding_no_position_ids_rotary_dim\",\n)" }, { "summary": "rotary_embedding_with_interleaved_rotary_dim", "code": "node = onnx.helper.make_node(\n \"RotaryEmbedding\",\n inputs=[\"input\", \"cos_cache\", \"sin_cache\", \"position_ids\"],\n outputs=[\"output\"],\n rotary_embedding_dim=4,\n interleaved=1,\n)\n\ninput_data = np.random.rand(2, 4, 3, 8).astype(np.float32)\nposition_ids_data = np.random.uniform(0, 50, (2, 3)).astype(np.int64)\nsin_cache_data = np.random.rand(50, 2).astype(np.float32)\ncos_cache_data = np.random.rand(50, 2).astype(np.float32)\n\nexpected_output = rotary_embedding(\n input_data,\n cos_cache_data,\n sin_cache_data,\n position_ids=position_ids_data,\n interleaved=1,\n rotary_embedding_dim=4,\n)\n\nexpect(\n node,\n inputs=[input_data, cos_cache_data, sin_cache_data, position_ids_data],\n outputs=[expected_output],\n name=\"test_rotary_embedding_with_interleaved_rotary_dim\",\n)" }, { "summary": "rotary_embedding_with_rotary_dim", "code": "node = onnx.helper.make_node(\n \"RotaryEmbedding\",\n inputs=[\"input\", \"cos_cache\", \"sin_cache\", \"position_ids\"],\n outputs=[\"output\"],\n rotary_embedding_dim=4,\n)\n\ninput_data = np.random.rand(2, 4, 3, 8).astype(np.float32)\nposition_ids_data = np.random.uniform(0, 50, (2, 3)).astype(np.int64)\nsin_cache_data = np.random.rand(50, 2).astype(np.float32)\ncos_cache_data = np.random.rand(50, 2).astype(np.float32)\n\nexpected_output = rotary_embedding(\n input_data,\n cos_cache_data,\n sin_cache_data,\n position_ids=position_ids_data,\n rotary_embedding_dim=4,\n)\n\nexpect(\n node,\n inputs=[input_data, cos_cache_data, sin_cache_data, position_ids_data],\n outputs=[expected_output],\n name=\"test_rotary_embedding_with_rotary_dim\",\n)" } ], "category": "Transform" }, { "name": "RotaryEmbedding", "module": "com.microsoft", "version": 1, "description": "RotaryEmbedding is the implementation of rotary positional embeddings (RoPE). The positions are represented as rotation matrices\nthat are multiplied to query and key before the inner product of query and key is taken.\n", "attributes": [ { "name": "interleaved", "type": "int64", "required": false, "description": "Indicates whether the input has real and imaginary parts interleaved. Default value is 0 (False), meaning the first half of the input consists of real values and the second half consists of imaginary values." }, { "name": "is_packed_batching", "type": "int64", "required": false, "description": "ragged batch inputs or not. Default value is 0" }, { "name": "num_heads", "type": "int64", "required": false, "description": "Number of attention heads. Default value is 0. Must use with rotary_embedding_dim" }, { "name": "rotary_embedding_dim", "type": "int64", "required": false, "description": "Rotary embedding dimension. Default value is 0." }, { "name": "scale", "type": "float32", "required": false, "description": "Custom scale will be used if specified. Default value is 1.0" } ], "inputs": [ { "name": "input", "type": "T", "description": "3D tensor with shape (batch_size, sequence_length, hidden_size) or 4D with shape (batch_size, num_heads, sequence_length, head_size)" }, { "name": "position_ids", "type": "M", "description": "1D tensor with shape (1) or 2D tensor with shape (batch_size, sequence_length)" }, { "name": "cos_cache", "type": "T", "description": "2D tensor with shape (max_sequence_length, head_size / 2) or (max_sequence_length, rotary_embedding_dim / 2)" }, { "name": "sin_cache", "type": "T", "description": "2D tensor with shape (max_sequence_length, head_size / 2) or (max_sequence_length, rotary_embedding_dim / 2)" } ], "min_input": 4, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "tensor with 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(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain input and output types to integer tensors", "type_param_str": "M", "allowed_type_strs": [ "tensor(int64)" ] } ], "category": "Transform" }, { "name": "Round", "module": "ai.onnx", "version": 11, "description": "Round takes one input Tensor and rounds the values, element-wise, meaning\nit finds the nearest integer for each value.\nIn case of halves, the rule is to round them to the nearest even integer.\nIf input x is integral, +0, -0, NaN, or infinite, x itself is returned.\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(\n [\n 0.1,\n 0.5,\n 0.9,\n 1.2,\n 1.5,\n 1.8,\n 2.3,\n 2.5,\n 2.7,\n -1.1,\n -1.5,\n -1.9,\n -2.2,\n -2.5,\n -2.8,\n ]\n).astype(np.float32)\n\n# expected output\ny = np.array(\n [\n 0.0,\n 0.0,\n 1.0,\n 1.0,\n 2.0,\n 2.0,\n 2.0,\n 2.0,\n 3.0,\n -1.0,\n -2.0,\n -2.0,\n -2.0,\n -2.0,\n -3.0,\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_round\")" } ] }, { "name": "Round", "module": "ai.onnx", "version": 22, "description": "Round takes one input Tensor and rounds the values, element-wise, meaning\nit finds the nearest integer for each value.\nIn case of halves, the rule is to round them to the nearest even integer.\nIf input x is integral, +0, -0, NaN, or infinite, x itself is returned.\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(bfloat16)", "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(\n [\n 0.1,\n 0.5,\n 0.9,\n 1.2,\n 1.5,\n 1.8,\n 2.3,\n 2.5,\n 2.7,\n -1.1,\n -1.5,\n -1.9,\n -2.2,\n -2.5,\n -2.8,\n ]\n).astype(np.float32)\n\n# expected output\ny = np.array(\n [\n 0.0,\n 0.0,\n 1.0,\n 1.0,\n 2.0,\n 2.0,\n 2.0,\n 2.0,\n 3.0,\n -1.0,\n -2.0,\n -2.0,\n -2.0,\n -2.0,\n -3.0,\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], name=\"test_round\")" } ] }, { "name": "STFT", "module": "ai.onnx", "version": 17, "description": "Computes the Short-time Fourier Transform of the signal.", "attributes": [ { "name": "onesided", "type": "int64", "required": false, "default": 1, "description": "If onesided is 1, only values for w in [0, 1, 2, ..., floor(n_fft/2) + 1] are returned because the real-to-complex Fourier transform satisfies the conjugate symmetry, i.e., X[m, w] = X[m,w]=X[m,n_fft-w]*. Note if the input or window tensors are complex, then onesided output is not possible. Enabling onesided with real inputs performs a Real-valued fast Fourier transform (RFFT).When invoked with real or complex valued input, the default value is 1. Values can be 0 or 1." } ], "inputs": [ { "name": "signal", "type": "T1", "description": "Input tensor representing a real or complex valued signal. For real input, the following shape is expected: [batch_size][signal_length][1]. For complex input, the following shape is expected: [batch_size][signal_length][2], where [batch_size][signal_length][0] represents the real component and [batch_size][signal_length][1] represents the imaginary component of the signal." }, { "name": "frame_step", "type": "T2", "description": "The number of samples to step between successive DFTs." }, { "name": "window", "type": "T1", "option": "optional", "description": "A tensor representing the window that will be slid over the signal.The window must have rank 1 with shape: [window_shape]. It's an optional value. " }, { "name": "frame_length", "type": "T2", "option": "optional", "description": "A scalar representing the size of the DFT. It's an optional value." } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "output", "type": "T1", "description": "The Short-time Fourier Transform of the signals.If onesided is 1, the output has the shape: [batch_size][frames][dft_unique_bins][2], where dft_unique_bins is frame_length // 2 + 1 (the unique components of the DFT) If onesided is 0, the output has the shape: [batch_size][frames][frame_length][2], where frame_length is the length of the DFT." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "type_constraints": [ { "description": "Constrain signal and output to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(double)", "tensor(bfloat16)" ] }, { "description": "Constrain scalar length types to int64_t.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(int32)", "tensor(int64)" ] } ], "examples": [ { "summary": "stft", "code": "signal = np.arange(0, 128, dtype=np.float32).reshape(1, 128, 1)\nlength = np.array(16).astype(np.int64)\nonesided_length = (length >> 1) + 1\nstep = np.array(8).astype(np.int64)\n\nno_window = \"\" # optional input, not supplied\nnode = onnx.helper.make_node(\n \"STFT\",\n inputs=[\"signal\", \"frame_step\", no_window, \"frame_length\"],\n outputs=[\"output\"],\n)\n\nnstfts = ((signal.shape[1] - length) // step) + 1\n# [batch_size][frames][frame_length][2]\noutput = np.empty([1, nstfts, onesided_length, 2], dtype=np.float32)\nfor i in range(nstfts):\n start = i * step\n stop = i * step + length\n complex_out = np.fft.fft(signal[0, start:stop, 0])[0:onesided_length]\n output[0, i] = np.stack((complex_out.real, complex_out.imag), axis=1)\n\noutput = output.astype(signal.dtype)\nexpect(node, inputs=[signal, step, length], outputs=[output], name=\"test_stft\")\n\nnode = onnx.helper.make_node(\n \"STFT\",\n inputs=[\"signal\", \"frame_step\", \"window\"],\n outputs=[\"output\"],\n)\n\n# Test with window\na0 = 0.5\na1 = 0.5\nwindow = a0 + a1 * np.cos(\n 2 * np.pi * np.arange(0, length, 1, dtype=np.float32) / length\n)\nnstfts = 1 + (signal.shape[1] - window.shape[0]) // step\n\n# [batch_size][frames][frame_length][2]\noutput = np.empty([1, nstfts, onesided_length, 2], dtype=np.float32)\nfor i in range(nstfts):\n start = i * step\n stop = i * step + length\n complex_out = np.fft.fft(signal[0, start:stop, 0] * window)[\n 0:onesided_length\n ]\n output[0, i] = np.stack((complex_out.real, complex_out.imag), axis=1)\nwindow = window.astype(signal.dtype)\noutput = output.astype(signal.dtype)\nexpect(\n node,\n inputs=[signal, step, window],\n outputs=[output],\n name=\"test_stft_with_window\",\n)" } ] }, { "name": "SVMClassifier", "module": "ai.onnx.ml", "version": 1, "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 }, { "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 }, { "name": "support_vectors", "type": "float32[]", "required": false }, { "name": "vectors_per_class", "type": "int64[]", "required": false } ], "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 classlabels_* attributes is used. Its size will match the batch size of the input.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(string)", "tensor(int64)" ] } ] }, { "name": "SVMRegressor", "module": "ai.onnx.ml", "version": 1, "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 }, { "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": "SampleOp", "module": "com.microsoft", "version": 1, "description": "Sample echo operator.", "inputs": [ { "name": "X", "type": "T", "description": "input" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "output" } ], "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": "T", "allowed_type_strs": [ "tensor(uint32)", "tensor(uint64)", "tensor(int32)", "tensor(int64)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ] }, { "name": "Sampling", "module": "com.microsoft", "version": 1, "description": "Greedy Sampling for text generation.", "attributes": [ { "name": "custom", "type": "int64", "required": false, "description": "If 1 custom sampling logic" }, { "name": "decoder", "type": "graph", "required": true, "description": "Decoder subgraph to execute in a loop." }, { "name": "decoder_start_token_id", "type": "int64", "required": false, "default": -1, "description": "The id of the token that indicates decoding starts." }, { "name": "encoder", "type": "graph", "required": false, "description": "The subgraph for initialization of encoder and decoder. It will be called once before decoder subgraph." }, { "name": "eos_token_id", "type": "int64", "required": true, "description": "The id of the end-of-sequence token" }, { "name": "filter_value", "type": "float32", "required": false, "default": -1.0000000200408773e+20, "description": "All filtered values will be set to this float value." }, { "name": "init_decoder", "type": "graph", "required": false, "description": "The subgraph for the first decoding run. It will be called once before `decoder` subgraph. This is relevant only for the GPT2 model. If this attribute is missing, the `decoder` subgraph will be used for all decoding runs" }, { "name": "min_tokens_to_keep", "type": "int64", "required": false, "description": "Minimumber of tokens we keep per batch example in the output." }, { "name": "model_type", "type": "int64", "required": false, "description": "Model type: 0 for decoder only like GPT-2; 1 for encoder decoder like Bart" }, { "name": "no_repeat_ngram_size", "type": "int64", "required": false, "description": "no repeat ngrams size" }, { "name": "pad_token_id", "type": "int64", "required": true, "description": "The id of the padding token" }, { "name": "presence_penalty", "type": "float32", "required": false, "description": "Presence penalty for custom sampling" }, { "name": "temperature", "type": "float32", "required": false, "default": 1.0, "description": "The value used to module the next token probabilities." }, { "name": "top_p", "type": "float32", "required": false, "description": "If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or higher are kept for generation." }, { "name": "vocab_size", "type": "int64", "required": false, "default": -1, "description": "Size of the vocabulary. If not provided, it will be inferred from the decoder subgraph's output shape" } ], "inputs": [ { "name": "input_ids", "type": "I", "description": "The sequence used as a prompt for the generation. Shape is (batch_size, sequence_length)" }, { "name": "max_length", "type": "I", "description": "The maximum length of the sequence to be generated. Shape is (1)" }, { "name": "min_length", "type": "I", "option": "optional", "description": "The minimum length below which the score of eos_token_id is set to -Inf. Shape is (1)" }, { "name": "repetition_penalty", "type": "T", "option": "optional", "description": "The parameter for repetition penalty. Default value 1.0 means no penalty. Accepts value > 0.0. Shape is (1)" }, { "name": "vocab_mask", "type": "I", "option": "optional", "description": "Mask of vocabulary. Words that masked with 0 are not allowed to be generated, and 1 is allowed. Shape is (vocab_size)" }, { "name": "prefix_vocab_mask", "type": "I", "option": "optional", "description": "Mask of vocabulary for first step. Words that masked with 0 are not allowed to be generated, and 1 is allowed. Shape is (batch_size, vocab_size)" }, { "name": "attention_mask", "type": "I", "option": "optional", "description": "Custom attention mask. Shape is (batch_size, sequence_length)" }, { "name": "presence_mask", "type": "I", "option": "optional", "description": "Presence penalty mask. Shape is (batch_size, vocab_size)" }, { "name": "seed", "type": "I", "option": "optional", "description": "Seed for random number generator. Shape is (1)" } ], "min_input": 2, "max_input": 9, "outputs": [ { "name": "sequences", "type": "I", "description": "Word IDs of generated sequences. Shape is (batch_size, max_sequence_length)" }, { "name": "filtered_logits", "type": "T", "option": "optional", "description": "Filtered logits as input to the mutinomial function for debug purpose. Shape is (batch_size, vocab_size)" } ], "min_output": 1, "max_output": 2, "inputs_range": "2 - 9", "outputs_range": "1 - 2", "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)" ] }, { "description": "Constrain to integer types", "type_param_str": "I", "allowed_type_strs": [ "tensor(int32)" ] } ] }, { "name": "Scale", "module": "ai.onnx", "version": 1, "description": "Scale takes one input data (Tensor) and produces one output data\n(Tensor) whose value is the input data tensor scaled element-wise.\n", "attributes": [ { "name": "scale", "type": "float32", "required": false, "default": 1.0, "description": "The scale to apply." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input data to be scaled" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "Output data after scaling" } ], "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)" ] } ] }, { "name": "Scale", "module": "ai.onnx", "version": 10, "description": "Scale takes one input data (Tensor) and produces one output data\n(Tensor) whose value is the input data tensor scaled element-wise.\n", "attributes": [ { "name": "scale", "type": "float32", "required": false, "default": 1.0, "description": "The scale to apply." } ], "inputs": [ { "name": "input", "type": "T", "description": "Input data to be scaled" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "Output data after scaling" } ], "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)" ] } ] }, { "name": "ScaledTanh", "module": "ai.onnx", "version": 1, "attributes": [ { "name": "alpha", "type": "float32", "required": false, "description": "Scaling value" }, { "name": "beta", "type": "float32", "required": false, "description": "Scaling value" } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "The scaled 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)" ] } ] }, { "name": "ScaledTanh", "module": "ai.onnx", "version": 10, "attributes": [ { "name": "alpha", "type": "float32", "required": false, "description": "Scaling value" }, { "name": "beta", "type": "float32", "required": false, "description": "Scaling value" } ], "inputs": [ { "name": "input", "type": "T", "description": "Input tensor" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "The scaled 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)" ] } ] }, { "name": "Scaler", "module": "ai.onnx.ml", "version": 1, "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, "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# Note: the first input (sequence_lens) is optional and omitted via \"\".\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (\"\", initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 8)],\n)" }, { "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]\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_multi_state", "code": "# Scan with two state variables: running sum and running product.\n# This exercises the case where num_loop_state_vars (2) differs from\n# num_scan_inputs (1).\n#\n# Body inputs: sum_in (state), prod_in (state), next (scan)\n# Body outputs: sum_out (state), prod_out (state), scan_out (scan)\nnode = onnx.parser.parse_node(\n \"\"\"\n y_sum, y_prod, z = Scan (initial_sum, initial_prod, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] prod_in, float[2] next)\n => (float[2] sum_out, float[2] prod_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n prod_out = Mul(prod_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\n# x = [[1, 2], [3, 4], [5, 6]]\ninitial_sum = np.array([0, 0]).astype(np.float32)\ninitial_prod = np.array([1, 1]).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final sum = [1+3+5, 2+4+6] = [9, 12]\ny_sum = np.array([9, 12]).astype(np.float32)\n# final product = [1*3*5, 2*4*6] = [15, 48]\ny_prod = np.array([15, 48]).astype(np.float32)\n# scan output (running sum) = [[1,2], [4,6], [9,12]]\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(\n node,\n inputs=[initial_sum, initial_prod, x],\n outputs=[y_sum, y_prod, z],\n name=\"test_scan9_multi_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_scalar", "code": "# Scan with scalar state and scan output to verify that output\n# shapes are not distorted (e.g. (T,) not (T, 1)).\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float sum_in, float next)\n => (float sum_out, float scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\ninitial = np.float32(0.0)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n# final state = 1+2+3+4+5 = 15\ny = np.float32(15.0)\n# scan output = [1, 3, 6, 10, 15], shape (5,)\nz = np.array([1, 3, 6, 10, 15]).astype(np.float32)\n\nexpect(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_scalar\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" } ] }, { "name": "Scan", "module": "ai.onnx", "version": 9, "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# Note: the first input (sequence_lens) is optional and omitted via \"\".\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (\"\", initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 8)],\n)" }, { "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]\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_multi_state", "code": "# Scan with two state variables: running sum and running product.\n# This exercises the case where num_loop_state_vars (2) differs from\n# num_scan_inputs (1).\n#\n# Body inputs: sum_in (state), prod_in (state), next (scan)\n# Body outputs: sum_out (state), prod_out (state), scan_out (scan)\nnode = onnx.parser.parse_node(\n \"\"\"\n y_sum, y_prod, z = Scan (initial_sum, initial_prod, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] prod_in, float[2] next)\n => (float[2] sum_out, float[2] prod_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n prod_out = Mul(prod_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\n# x = [[1, 2], [3, 4], [5, 6]]\ninitial_sum = np.array([0, 0]).astype(np.float32)\ninitial_prod = np.array([1, 1]).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final sum = [1+3+5, 2+4+6] = [9, 12]\ny_sum = np.array([9, 12]).astype(np.float32)\n# final product = [1*3*5, 2*4*6] = [15, 48]\ny_prod = np.array([15, 48]).astype(np.float32)\n# scan output (running sum) = [[1,2], [4,6], [9,12]]\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(\n node,\n inputs=[initial_sum, initial_prod, x],\n outputs=[y_sum, y_prod, z],\n name=\"test_scan9_multi_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_scalar", "code": "# Scan with scalar state and scan output to verify that output\n# shapes are not distorted (e.g. (T,) not (T, 1)).\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float sum_in, float next)\n => (float sum_out, float scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\ninitial = np.float32(0.0)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n# final state = 1+2+3+4+5 = 15\ny = np.float32(15.0)\n# scan output = [1, 3, 6, 10, 15], shape (5,)\nz = np.array([1, 3, 6, 10, 15]).astype(np.float32)\n\nexpect(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_scalar\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" } ] }, { "name": "Scan", "module": "ai.onnx", "version": 11, "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# Note: the first input (sequence_lens) is optional and omitted via \"\".\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (\"\", initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 8)],\n)" }, { "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]\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_multi_state", "code": "# Scan with two state variables: running sum and running product.\n# This exercises the case where num_loop_state_vars (2) differs from\n# num_scan_inputs (1).\n#\n# Body inputs: sum_in (state), prod_in (state), next (scan)\n# Body outputs: sum_out (state), prod_out (state), scan_out (scan)\nnode = onnx.parser.parse_node(\n \"\"\"\n y_sum, y_prod, z = Scan (initial_sum, initial_prod, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] prod_in, float[2] next)\n => (float[2] sum_out, float[2] prod_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n prod_out = Mul(prod_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\n# x = [[1, 2], [3, 4], [5, 6]]\ninitial_sum = np.array([0, 0]).astype(np.float32)\ninitial_prod = np.array([1, 1]).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final sum = [1+3+5, 2+4+6] = [9, 12]\ny_sum = np.array([9, 12]).astype(np.float32)\n# final product = [1*3*5, 2*4*6] = [15, 48]\ny_prod = np.array([15, 48]).astype(np.float32)\n# scan output (running sum) = [[1,2], [4,6], [9,12]]\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(\n node,\n inputs=[initial_sum, initial_prod, x],\n outputs=[y_sum, y_prod, z],\n name=\"test_scan9_multi_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_scalar", "code": "# Scan with scalar state and scan output to verify that output\n# shapes are not distorted (e.g. (T,) not (T, 1)).\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float sum_in, float next)\n => (float sum_out, float scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\ninitial = np.float32(0.0)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n# final state = 1+2+3+4+5 = 15\ny = np.float32(15.0)\n# scan output = [1, 3, 6, 10, 15], shape (5,)\nz = np.array([1, 3, 6, 10, 15]).astype(np.float32)\n\nexpect(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_scalar\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" } ] }, { "name": "Scan", "module": "ai.onnx", "version": 16, "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 up to IRv4.", "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# Note: the first input (sequence_lens) is optional and omitted via \"\".\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (\"\", initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 8)],\n)" }, { "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]\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_multi_state", "code": "# Scan with two state variables: running sum and running product.\n# This exercises the case where num_loop_state_vars (2) differs from\n# num_scan_inputs (1).\n#\n# Body inputs: sum_in (state), prod_in (state), next (scan)\n# Body outputs: sum_out (state), prod_out (state), scan_out (scan)\nnode = onnx.parser.parse_node(\n \"\"\"\n y_sum, y_prod, z = Scan (initial_sum, initial_prod, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] prod_in, float[2] next)\n => (float[2] sum_out, float[2] prod_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n prod_out = Mul(prod_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\n# x = [[1, 2], [3, 4], [5, 6]]\ninitial_sum = np.array([0, 0]).astype(np.float32)\ninitial_prod = np.array([1, 1]).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final sum = [1+3+5, 2+4+6] = [9, 12]\ny_sum = np.array([9, 12]).astype(np.float32)\n# final product = [1*3*5, 2*4*6] = [15, 48]\ny_prod = np.array([15, 48]).astype(np.float32)\n# scan output (running sum) = [[1,2], [4,6], [9,12]]\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(\n node,\n inputs=[initial_sum, initial_prod, x],\n outputs=[y_sum, y_prod, z],\n name=\"test_scan9_multi_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_scalar", "code": "# Scan with scalar state and scan output to verify that output\n# shapes are not distorted (e.g. (T,) not (T, 1)).\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float sum_in, float next)\n => (float sum_out, float scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\ninitial = np.float32(0.0)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n# final state = 1+2+3+4+5 = 15\ny = np.float32(15.0)\n# scan output = [1, 3, 6, 10, 15], shape (5,)\nz = np.array([1, 3, 6, 10, 15]).astype(np.float32)\n\nexpect(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_scalar\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" } ] }, { "name": "Scan", "module": "ai.onnx", "version": 19, "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 up to IRv9.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] } ], "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# Note: the first input (sequence_lens) is optional and omitted via \"\".\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (\"\", initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 8)],\n)" }, { "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]\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_multi_state", "code": "# Scan with two state variables: running sum and running product.\n# This exercises the case where num_loop_state_vars (2) differs from\n# num_scan_inputs (1).\n#\n# Body inputs: sum_in (state), prod_in (state), next (scan)\n# Body outputs: sum_out (state), prod_out (state), scan_out (scan)\nnode = onnx.parser.parse_node(\n \"\"\"\n y_sum, y_prod, z = Scan (initial_sum, initial_prod, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] prod_in, float[2] next)\n => (float[2] sum_out, float[2] prod_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n prod_out = Mul(prod_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\n# x = [[1, 2], [3, 4], [5, 6]]\ninitial_sum = np.array([0, 0]).astype(np.float32)\ninitial_prod = np.array([1, 1]).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final sum = [1+3+5, 2+4+6] = [9, 12]\ny_sum = np.array([9, 12]).astype(np.float32)\n# final product = [1*3*5, 2*4*6] = [15, 48]\ny_prod = np.array([15, 48]).astype(np.float32)\n# scan output (running sum) = [[1,2], [4,6], [9,12]]\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(\n node,\n inputs=[initial_sum, initial_prod, x],\n outputs=[y_sum, y_prod, z],\n name=\"test_scan9_multi_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_scalar", "code": "# Scan with scalar state and scan output to verify that output\n# shapes are not distorted (e.g. (T,) not (T, 1)).\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float sum_in, float next)\n => (float sum_out, float scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\ninitial = np.float32(0.0)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n# final state = 1+2+3+4+5 = 15\ny = np.float32(15.0)\n# scan output = [1, 3, 6, 10, 15], shape (5,)\nz = np.array([1, 3, 6, 10, 15]).astype(np.float32)\n\nexpect(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_scalar\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" } ] }, { "name": "Scan", "module": "ai.onnx", "version": 21, "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 up to IRv10.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "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# Note: the first input (sequence_lens) is optional and omitted via \"\".\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (\"\", initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 8)],\n)" }, { "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]\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_multi_state", "code": "# Scan with two state variables: running sum and running product.\n# This exercises the case where num_loop_state_vars (2) differs from\n# num_scan_inputs (1).\n#\n# Body inputs: sum_in (state), prod_in (state), next (scan)\n# Body outputs: sum_out (state), prod_out (state), scan_out (scan)\nnode = onnx.parser.parse_node(\n \"\"\"\n y_sum, y_prod, z = Scan (initial_sum, initial_prod, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] prod_in, float[2] next)\n => (float[2] sum_out, float[2] prod_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n prod_out = Mul(prod_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\n# x = [[1, 2], [3, 4], [5, 6]]\ninitial_sum = np.array([0, 0]).astype(np.float32)\ninitial_prod = np.array([1, 1]).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final sum = [1+3+5, 2+4+6] = [9, 12]\ny_sum = np.array([9, 12]).astype(np.float32)\n# final product = [1*3*5, 2*4*6] = [15, 48]\ny_prod = np.array([15, 48]).astype(np.float32)\n# scan output (running sum) = [[1,2], [4,6], [9,12]]\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(\n node,\n inputs=[initial_sum, initial_prod, x],\n outputs=[y_sum, y_prod, z],\n name=\"test_scan9_multi_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_scalar", "code": "# Scan with scalar state and scan output to verify that output\n# shapes are not distorted (e.g. (T,) not (T, 1)).\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float sum_in, float next)\n => (float sum_out, float scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\ninitial = np.float32(0.0)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n# final state = 1+2+3+4+5 = 15\ny = np.float32(15.0)\n# scan output = [1, 3, 6, 10, 15], shape (5,)\nz = np.array([1, 3, 6, 10, 15]).astype(np.float32)\n\nexpect(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_scalar\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" } ] }, { "name": "Scan", "module": "ai.onnx", "version": 23, "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 up to IRv11.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "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# Note: the first input (sequence_lens) is optional and omitted via \"\".\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (\"\", initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 8)],\n)" }, { "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]\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_multi_state", "code": "# Scan with two state variables: running sum and running product.\n# This exercises the case where num_loop_state_vars (2) differs from\n# num_scan_inputs (1).\n#\n# Body inputs: sum_in (state), prod_in (state), next (scan)\n# Body outputs: sum_out (state), prod_out (state), scan_out (scan)\nnode = onnx.parser.parse_node(\n \"\"\"\n y_sum, y_prod, z = Scan (initial_sum, initial_prod, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] prod_in, float[2] next)\n => (float[2] sum_out, float[2] prod_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n prod_out = Mul(prod_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\n# x = [[1, 2], [3, 4], [5, 6]]\ninitial_sum = np.array([0, 0]).astype(np.float32)\ninitial_prod = np.array([1, 1]).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final sum = [1+3+5, 2+4+6] = [9, 12]\ny_sum = np.array([9, 12]).astype(np.float32)\n# final product = [1*3*5, 2*4*6] = [15, 48]\ny_prod = np.array([15, 48]).astype(np.float32)\n# scan output (running sum) = [[1,2], [4,6], [9,12]]\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(\n node,\n inputs=[initial_sum, initial_prod, x],\n outputs=[y_sum, y_prod, z],\n name=\"test_scan9_multi_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_scalar", "code": "# Scan with scalar state and scan output to verify that output\n# shapes are not distorted (e.g. (T,) not (T, 1)).\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float sum_in, float next)\n => (float sum_out, float scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\ninitial = np.float32(0.0)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n# final state = 1+2+3+4+5 = 15\ny = np.float32(15.0)\n# scan output = [1, 3, 6, 10, 15], shape (5,)\nz = np.array([1, 3, 6, 10, 15]).astype(np.float32)\n\nexpect(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_scalar\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" } ] }, { "name": "Scan", "module": "ai.onnx", "version": 24, "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 up to IRv12.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "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# Note: the first input (sequence_lens) is optional and omitted via \"\".\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (\"\", initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 8)],\n)" }, { "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]\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_multi_state", "code": "# Scan with two state variables: running sum and running product.\n# This exercises the case where num_loop_state_vars (2) differs from\n# num_scan_inputs (1).\n#\n# Body inputs: sum_in (state), prod_in (state), next (scan)\n# Body outputs: sum_out (state), prod_out (state), scan_out (scan)\nnode = onnx.parser.parse_node(\n \"\"\"\n y_sum, y_prod, z = Scan (initial_sum, initial_prod, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] prod_in, float[2] next)\n => (float[2] sum_out, float[2] prod_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n prod_out = Mul(prod_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\n# x = [[1, 2], [3, 4], [5, 6]]\ninitial_sum = np.array([0, 0]).astype(np.float32)\ninitial_prod = np.array([1, 1]).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final sum = [1+3+5, 2+4+6] = [9, 12]\ny_sum = np.array([9, 12]).astype(np.float32)\n# final product = [1*3*5, 2*4*6] = [15, 48]\ny_prod = np.array([15, 48]).astype(np.float32)\n# scan output (running sum) = [[1,2], [4,6], [9,12]]\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(\n node,\n inputs=[initial_sum, initial_prod, x],\n outputs=[y_sum, y_prod, z],\n name=\"test_scan9_multi_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_scalar", "code": "# Scan with scalar state and scan output to verify that output\n# shapes are not distorted (e.g. (T,) not (T, 1)).\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float sum_in, float next)\n => (float sum_out, float scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\ninitial = np.float32(0.0)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n# final state = 1+2+3+4+5 = 15\ny = np.float32(15.0)\n# scan output = [1, 3, 6, 10, 15], shape (5,)\nz = np.array([1, 3, 6, 10, 15]).astype(np.float32)\n\nexpect(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_scalar\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" } ] }, { "name": "Scan", "module": "ai.onnx", "version": 25, "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 up to IRv13.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "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# Note: the first input (sequence_lens) is optional and omitted via \"\".\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (\"\", initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 8)],\n)" }, { "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]\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] next)\n => (float[2] sum_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\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(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_sum\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_multi_state", "code": "# Scan with two state variables: running sum and running product.\n# This exercises the case where num_loop_state_vars (2) differs from\n# num_scan_inputs (1).\n#\n# Body inputs: sum_in (state), prod_in (state), next (scan)\n# Body outputs: sum_out (state), prod_out (state), scan_out (scan)\nnode = onnx.parser.parse_node(\n \"\"\"\n y_sum, y_prod, z = Scan (initial_sum, initial_prod, x) <\n num_scan_inputs = 1,\n body = scan_body (float[2] sum_in, float[2] prod_in, float[2] next)\n => (float[2] sum_out, float[2] prod_out, float[2] scan_out)\n {\n sum_out = Add(sum_in, next)\n prod_out = Mul(prod_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\n# x = [[1, 2], [3, 4], [5, 6]]\ninitial_sum = np.array([0, 0]).astype(np.float32)\ninitial_prod = np.array([1, 1]).astype(np.float32)\nx = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32).reshape((3, 2))\n# final sum = [1+3+5, 2+4+6] = [9, 12]\ny_sum = np.array([9, 12]).astype(np.float32)\n# final product = [1*3*5, 2*4*6] = [15, 48]\ny_prod = np.array([15, 48]).astype(np.float32)\n# scan output (running sum) = [[1,2], [4,6], [9,12]]\nz = np.array([1, 2, 4, 6, 9, 12]).astype(np.float32).reshape((3, 2))\n\nexpect(\n node,\n inputs=[initial_sum, initial_prod, x],\n outputs=[y_sum, y_prod, z],\n name=\"test_scan9_multi_state\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" }, { "summary": "scan_9_scalar", "code": "# Scan with scalar state and scan output to verify that output\n# shapes are not distorted (e.g. (T,) not (T, 1)).\nnode = onnx.parser.parse_node(\n \"\"\"\n y, z = Scan (initial, x) <\n num_scan_inputs = 1,\n body = scan_body (float sum_in, float next)\n => (float sum_out, float scan_out)\n {\n sum_out = Add(sum_in, next)\n scan_out = Identity(sum_out)\n }\n >\n \"\"\"\n)\ninitial = np.float32(0.0)\nx = np.array([1, 2, 3, 4, 5]).astype(np.float32)\n# final state = 1+2+3+4+5 = 15\ny = np.float32(15.0)\n# scan output = [1, 3, 6, 10, 15], shape (5,)\nz = np.array([1, 3, 6, 10, 15]).astype(np.float32)\n\nexpect(\n node,\n inputs=[initial, x],\n outputs=[y, z],\n name=\"test_scan9_scalar\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 9)],\n)" } ] }, { "name": "Scatter", "module": "ai.onnx", "version": 9, "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_with_axis\",\n opset_imports=[helper.make_opsetid(\"\", 10)],\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_without_axis\",\n opset_imports=[helper.make_opsetid(\"\", 10)],\n)" } ] }, { "name": "Scatter", "module": "ai.onnx", "version": 11, "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_with_axis\",\n opset_imports=[helper.make_opsetid(\"\", 10)],\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_without_axis\",\n opset_imports=[helper.make_opsetid(\"\", 10)],\n)" } ] }, { "name": "ScatterElements", "module": "ai.onnx", "version": 11, "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_axis\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_duplicate_indices\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_negative_indices\",\n)" }, { "summary": "scatter_elements_with_reduction_max", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"max\",\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=\"max\")\n# print(y) produces\n# [[1.0, 2.1, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_max\",\n)" }, { "summary": "scatter_elements_with_reduction_min", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"min\",\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=\"min\")\n# print(y) produces\n# [[1.0, 1.1, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_min\",\n)" }, { "summary": "scatter_elements_with_reduction_mul", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"mul\",\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=\"mul\")\n# print(y) produces\n# [[1.0, 4.62, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_mul\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_without_axis\",\n)" } ] }, { "name": "ScatterElements", "module": "ai.onnx", "version": 13, "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_axis\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_duplicate_indices\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_negative_indices\",\n)" }, { "summary": "scatter_elements_with_reduction_max", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"max\",\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=\"max\")\n# print(y) produces\n# [[1.0, 2.1, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_max\",\n)" }, { "summary": "scatter_elements_with_reduction_min", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"min\",\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=\"min\")\n# print(y) produces\n# [[1.0, 1.1, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_min\",\n)" }, { "summary": "scatter_elements_with_reduction_mul", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"mul\",\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=\"mul\")\n# print(y) produces\n# [[1.0, 4.62, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_mul\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_without_axis\",\n)" } ] }, { "name": "ScatterElements", "module": "ai.onnx", "version": 16, "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`.\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`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```\nThis operator is the inverse of GatherElements. It is similar to Torch's Scatter operation.\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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_axis\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_duplicate_indices\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_negative_indices\",\n)" }, { "summary": "scatter_elements_with_reduction_max", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"max\",\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=\"max\")\n# print(y) produces\n# [[1.0, 2.1, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_max\",\n)" }, { "summary": "scatter_elements_with_reduction_min", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"min\",\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=\"min\")\n# print(y) produces\n# [[1.0, 1.1, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_min\",\n)" }, { "summary": "scatter_elements_with_reduction_mul", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"mul\",\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=\"mul\")\n# print(y) produces\n# [[1.0, 4.62, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_mul\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_without_axis\",\n)" } ] }, { "name": "ScatterElements", "module": "ai.onnx", "version": 18, "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```\noutput[indices[i][j]][j] = updates[i][j] if axis = 0,\noutput[i][indices[i][j]] = updates[i][j] if axis = 1,\n```\nWhen `reduction` is set to some reduction function `f`, the update corresponding to the [i][j] entry is performed as below:\n```\noutput[indices[i][j]][j] = f(output[indices[i][j]][j], updates[i][j]) if axis = 0,\noutput[i][indices[i][j]] = f(output[i][indices[i][j]], updates[i][j]) if axis = 1,\n```\nwhere the `f` is `+`, `*`, `max` or `min` as specified.\n\nThis operator is the inverse of GatherElements. It is similar to Torch's Scatter operation.\n\n(Opset 18 change): Adds max/min to the set of allowed reduction ops.\n\nExample 1:\n```\ndata = [\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n [0.0, 0.0, 0.0],\n]\nindices = [\n [1, 0, 2],\n [0, 2, 1],\n]\nupdates = [\n [1.0, 1.1, 1.2],\n [2.0, 2.1, 2.2],\n]\noutput = [\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```\ndata = [[1.0, 2.0, 3.0, 4.0, 5.0]]\nindices = [[1, 3]]\nupdates = [[1.1, 2.1]]\naxis = 1\noutput = [[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, max, min. 'none': no reduction applied. 'add': reduction using the addition operation. 'mul': reduction using the multiplication operation.'max': reduction using the maximum operation.'min': reduction using the minimum 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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_axis\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_duplicate_indices\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_negative_indices\",\n)" }, { "summary": "scatter_elements_with_reduction_max", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"max\",\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=\"max\")\n# print(y) produces\n# [[1.0, 2.1, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_max\",\n)" }, { "summary": "scatter_elements_with_reduction_min", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"min\",\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=\"min\")\n# print(y) produces\n# [[1.0, 1.1, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_min\",\n)" }, { "summary": "scatter_elements_with_reduction_mul", "code": "axis = 1\nnode = onnx.helper.make_node(\n \"ScatterElements\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n axis=axis,\n reduction=\"mul\",\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=\"mul\")\n# print(y) produces\n# [[1.0, 4.62, 3.0, 4.0, 5.0]]\n\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_with_reduction_mul\",\n)" }, { "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(\n node,\n inputs=[data, indices, updates],\n outputs=[y],\n name=\"test_scatter_elements_without_axis\",\n)" } ] }, { "name": "ScatterND", "module": "ai.onnx", "version": 11, "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. Index values are allowed to be negative, as per the usual\nconvention for counting backwards from the end, but are expected in the valid range.\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[tuple(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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [2]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd\",\n)" }, { "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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_add\",\n)" }, { "summary": "scatternd_max", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"max\",\n)\ndata = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\n# Expecting output as np.array(\n# [[[5, 5, 5, 5], [6, 6, 7, 8], [8, 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, reduction=\"max\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_max\",\n)" }, { "summary": "scatternd_max_with_element_indices", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"max\",\n)\ndata = np.array([[1, 2], [3, 4]], dtype=np.float32)\n# Indices address individual elements (index rank == data rank),\n# which exercises the reduction at the element level.\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\nupdates = np.array([5, 1], dtype=np.float32)\n# Expecting output as np.array([[5, 2], [3, 4]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction=\"max\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_max_with_element_indices\",\n)" }, { "summary": "scatternd_min", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"min\",\n)\ndata = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\n# Expecting output as np.array(\n# [[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 3, 2, 1]],\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=\"min\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_min\",\n)" }, { "summary": "scatternd_min_with_element_indices", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"min\",\n)\ndata = np.array([[1, 2], [3, 4]], dtype=np.float32)\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\nupdates = np.array([5, 1], dtype=np.float32)\n# Expecting output as np.array([[1, 2], [3, 1]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction=\"min\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_min_with_element_indices\",\n)" }, { "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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_multiply\",\n)" } ] }, { "name": "ScatterND", "module": "ai.onnx", "version": 13, "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. Index values are allowed to be negative, as per the usual\nconvention for counting backwards from the end, but are expected in the valid range.\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[tuple(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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [2]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd\",\n)" }, { "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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_add\",\n)" }, { "summary": "scatternd_max", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"max\",\n)\ndata = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\n# Expecting output as np.array(\n# [[[5, 5, 5, 5], [6, 6, 7, 8], [8, 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, reduction=\"max\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_max\",\n)" }, { "summary": "scatternd_max_with_element_indices", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"max\",\n)\ndata = np.array([[1, 2], [3, 4]], dtype=np.float32)\n# Indices address individual elements (index rank == data rank),\n# which exercises the reduction at the element level.\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\nupdates = np.array([5, 1], dtype=np.float32)\n# Expecting output as np.array([[5, 2], [3, 4]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction=\"max\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_max_with_element_indices\",\n)" }, { "summary": "scatternd_min", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"min\",\n)\ndata = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\n# Expecting output as np.array(\n# [[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 3, 2, 1]],\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=\"min\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_min\",\n)" }, { "summary": "scatternd_min_with_element_indices", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"min\",\n)\ndata = np.array([[1, 2], [3, 4]], dtype=np.float32)\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\nupdates = np.array([5, 1], dtype=np.float32)\n# Expecting output as np.array([[1, 2], [3, 1]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction=\"min\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_min_with_element_indices\",\n)" }, { "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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_multiply\",\n)" } ] }, { "name": "ScatterND", "module": "ai.onnx", "version": 16, "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`.\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. Index values are allowed to be negative, as per the usual\nconvention for counting backwards from the end, but are expected in the valid range.\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 output = np.copy(data)\n update_indices = indices.shape[:-1]\n for idx in np.ndindex(update_indices):\n output[tuple(indices[idx])] = updates[idx]\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 output = np.copy(data)\n update_indices = indices.shape[:-1]\n for idx in np.ndindex(update_indices):\n output[tuple(indices[idx])] += updates[idx]\nWhen `reduction` is set to \"mul\", `output` is calculated as follows:\n output = np.copy(data)\n update_indices = indices.shape[:-1]\n for idx in np.ndindex(update_indices):\n output[tuple(indices[idx])] *= updates[idx]\nThis operator is the inverse of GatherND.\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```\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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [2]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd\",\n)" }, { "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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_add\",\n)" }, { "summary": "scatternd_max", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"max\",\n)\ndata = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\n# Expecting output as np.array(\n# [[[5, 5, 5, 5], [6, 6, 7, 8], [8, 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, reduction=\"max\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_max\",\n)" }, { "summary": "scatternd_max_with_element_indices", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"max\",\n)\ndata = np.array([[1, 2], [3, 4]], dtype=np.float32)\n# Indices address individual elements (index rank == data rank),\n# which exercises the reduction at the element level.\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\nupdates = np.array([5, 1], dtype=np.float32)\n# Expecting output as np.array([[5, 2], [3, 4]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction=\"max\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_max_with_element_indices\",\n)" }, { "summary": "scatternd_min", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"min\",\n)\ndata = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\n# Expecting output as np.array(\n# [[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 3, 2, 1]],\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=\"min\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_min\",\n)" }, { "summary": "scatternd_min_with_element_indices", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"min\",\n)\ndata = np.array([[1, 2], [3, 4]], dtype=np.float32)\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\nupdates = np.array([5, 1], dtype=np.float32)\n# Expecting output as np.array([[1, 2], [3, 1]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction=\"min\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_min_with_element_indices\",\n)" }, { "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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_multiply\",\n)" } ] }, { "name": "ScatterND", "module": "ai.onnx", "version": 18, "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`.\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. Index values are allowed to be negative, as per the usual\nconvention for counting backwards from the end, but are expected in the valid range.\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```\noutput = np.copy(data)\nupdate_indices = indices.shape[:-1]\nfor idx in np.ndindex(update_indices):\n output[tuple(indices[idx])] = updates[idx]\n```\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 some reduction function `f`, `output` is calculated as follows:\n\n```\noutput = np.copy(data)\nupdate_indices = indices.shape[:-1]\nfor idx in np.ndindex(update_indices):\n output[tuple(indices[idx])] = f(output[tuple(indices[idx])], updates[idx])\n```\n\nwhere the `f` is `+`, `*`, `max` or `min` as specified.\n\nThis operator is the inverse of GatherND.\n\n(Opset 18 change): Adds max/min to the set of allowed reduction ops.\n\nExample 1:\n```\ndata = [1, 2, 3, 4, 5, 6, 7, 8]\nindices = [[4], [3], [1], [7]]\nupdates = [9, 10, 11, 12]\noutput = [1, 11, 3, 10, 9, 6, 7, 12]\n```\n\nExample 2:\n```\ndata = [[[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]]]\nindices = [[0], [2]]\nupdates = [[[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]]]\noutput = [[[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, max, min. 'none': no reduction applied. 'add': reduction using the addition operation. 'mul': reduction using the addition operation. 'max': reduction using the maximum operation.'min': reduction using the minimum 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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [2]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd\",\n)" }, { "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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_add\",\n)" }, { "summary": "scatternd_max", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"max\",\n)\ndata = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\n# Expecting output as np.array(\n# [[[5, 5, 5, 5], [6, 6, 7, 8], [8, 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, reduction=\"max\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_max\",\n)" }, { "summary": "scatternd_max_with_element_indices", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"max\",\n)\ndata = np.array([[1, 2], [3, 4]], dtype=np.float32)\n# Indices address individual elements (index rank == data rank),\n# which exercises the reduction at the element level.\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\nupdates = np.array([5, 1], dtype=np.float32)\n# Expecting output as np.array([[5, 2], [3, 4]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction=\"max\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_max_with_element_indices\",\n)" }, { "summary": "scatternd_min", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"min\",\n)\ndata = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\n# Expecting output as np.array(\n# [[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 3, 2, 1]],\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=\"min\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_min\",\n)" }, { "summary": "scatternd_min_with_element_indices", "code": "node = onnx.helper.make_node(\n \"ScatterND\",\n inputs=[\"data\", \"indices\", \"updates\"],\n outputs=[\"y\"],\n reduction=\"min\",\n)\ndata = np.array([[1, 2], [3, 4]], dtype=np.float32)\nindices = np.array([[0, 0], [1, 1]], dtype=np.int64)\nupdates = np.array([5, 1], dtype=np.float32)\n# Expecting output as np.array([[1, 2], [3, 1]], dtype=np.float32)\noutput = scatter_nd_impl(data, indices, updates, reduction=\"min\")\nexpect(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_min_with_element_indices\",\n)" }, { "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 [\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]],\n ],\n dtype=np.float32,\n)\nindices = np.array([[0], [0]], dtype=np.int64)\nupdates = np.array(\n [\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]],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[data, indices, updates],\n outputs=[output],\n name=\"test_scatternd_multiply\",\n)" } ] }, { "name": "Selu", "module": "ai.onnx", "version": 1, "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\", inputs=[\"x\"], outputs=[\"y\"], alpha=2.0, gamma=3.0\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-3.79272318, 0., 3.]\ny = (\n np.clip(x, 0, np.inf) * 3.0\n + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\n)\nexpect(node, inputs=[x], outputs=[y], name=\"test_selu_example\")\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = (\n np.clip(x, 0, np.inf) * 3.0\n + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\n)\nexpect(node, inputs=[x], outputs=[y], 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 = (\n np.clip(x, 0, np.inf) * default_gamma\n + (np.exp(np.clip(x, -np.inf, 0)) - 1) * default_alpha * default_gamma\n)\nexpect(node, inputs=[x], outputs=[y], name=\"test_selu_default\")" } ], "category": "Activation" }, { "name": "Selu", "module": "ai.onnx", "version": 6, "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\", inputs=[\"x\"], outputs=[\"y\"], alpha=2.0, gamma=3.0\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-3.79272318, 0., 3.]\ny = (\n np.clip(x, 0, np.inf) * 3.0\n + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\n)\nexpect(node, inputs=[x], outputs=[y], name=\"test_selu_example\")\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = (\n np.clip(x, 0, np.inf) * 3.0\n + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\n)\nexpect(node, inputs=[x], outputs=[y], 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 = (\n np.clip(x, 0, np.inf) * default_gamma\n + (np.exp(np.clip(x, -np.inf, 0)) - 1) * default_alpha * default_gamma\n)\nexpect(node, inputs=[x], outputs=[y], name=\"test_selu_default\")" } ], "category": "Activation" }, { "name": "Selu", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "selu", "code": "node = onnx.helper.make_node(\n \"Selu\", inputs=[\"x\"], outputs=[\"y\"], alpha=2.0, gamma=3.0\n)\n\nx = np.array([-1, 0, 1]).astype(np.float32)\n# expected output [-3.79272318, 0., 3.]\ny = (\n np.clip(x, 0, np.inf) * 3.0\n + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\n)\nexpect(node, inputs=[x], outputs=[y], name=\"test_selu_example\")\n\nx = np.random.randn(3, 4, 5).astype(np.float32)\ny = (\n np.clip(x, 0, np.inf) * 3.0\n + (np.exp(np.clip(x, -np.inf, 0)) - 1) * 2.0 * 3.0\n)\nexpect(node, inputs=[x], outputs=[y], 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 = (\n np.clip(x, 0, np.inf) * default_gamma\n + (np.exp(np.clip(x, -np.inf, 0)) - 1) * default_alpha * default_gamma\n)\nexpect(node, inputs=[x], outputs=[y], name=\"test_selu_default\")" } ], "category": "Activation" }, { "name": "SequenceAt", "module": "ai.onnx", "version": 11, "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, "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, "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, "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, "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 = [\n np.array([1, 2, 3, 4]).astype(np.int64),\n np.array([5, 6, 7]).astype(np.int64),\n np.array([8, 9]).astype(np.int64),\n]\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(\n sequence, tensor, position\n )\n expect(\n node,\n inputs=[sequence, tensor, position],\n outputs=[inserted],\n name=\"test_sequence_insert_\" + test_name,\n )\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(\n node,\n inputs=[sequence, tensor],\n outputs=[inserted],\n name=\"test_sequence_insert_\" + test_name,\n )" } ] }, { "name": "SequenceLength", "module": "ai.onnx", "version": 11, "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": 17, "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 [\n onnx.helper.make_tensor_value_info(\n \"in0\", onnx.TensorProto.FLOAT, [\"N\"]\n ),\n onnx.helper.make_tensor_value_info(\n \"in1\", onnx.TensorProto.FLOAT, [\"N\"]\n ),\n ],\n [onnx.helper.make_tensor_value_info(\"out0\", onnx.TensorProto.FLOAT, [\"N\"])],\n)\n\nnode = onnx.helper.make_node(\n \"SequenceMap\", inputs=[\"x0\", \"x1\"], outputs=[\"y0\"], 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 ),\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 ),\n]\nexpect(\n node,\n inputs=[x0, x1],\n 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\",\n)" }, { "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 [\n onnx.helper.make_tensor_value_info(\n \"in0\", onnx.TensorProto.FLOAT, [\"N\"]\n ),\n onnx.helper.make_tensor_value_info(\n \"in1\", onnx.TensorProto.FLOAT, [\"N\"]\n ),\n ],\n [onnx.helper.make_tensor_value_info(\"out0\", onnx.TensorProto.FLOAT, [\"N\"])],\n)\n\nnode = onnx.helper.make_node(\n \"SequenceMap\", inputs=[\"x0\", \"x1\"], outputs=[\"y0\"], 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) for k in range(3)]\nx1 = [np.random.uniform(0.0, 1.0, N[k]).astype(np.float32) 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 ),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [\"N\"])\n ),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [\"N\"])\n ),\n]\nexpect(\n node,\n inputs=[x0, x1],\n outputs=[y0],\n input_type_protos=input_type_protos,\n output_type_protos=output_type_protos,\n name=\"test_sequence_map_add_2_sequences\",\n)" }, { "summary": "sequence_map_extract_shapes", "code": "body = onnx.helper.make_graph(\n [onnx.helper.make_node(\"Shape\", [\"x\"], [\"shape\"])],\n \"seq_map_body\",\n [\n onnx.helper.make_tensor_value_info(\n \"x\", onnx.TensorProto.FLOAT, [\"H\", \"W\", \"C\"]\n )\n ],\n [onnx.helper.make_tensor_value_info(\"shape\", onnx.TensorProto.INT64, [3])],\n)\n\nnode = onnx.helper.make_node(\n \"SequenceMap\", inputs=[\"in_seq\"], outputs=[\"shapes\"], 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(\n onnx.TensorProto.FLOAT, [\"H\", \"W\", \"C\"]\n )\n ),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.INT64, [3])\n ),\n]\nexpect(\n node,\n inputs=[x0],\n outputs=[shapes],\n input_type_protos=input_type_protos,\n output_type_protos=output_type_protos,\n name=\"test_sequence_map_extract_shapes\",\n)" }, { "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(\"in0\", onnx.TensorProto.FLOAT, [\"N\"])],\n [onnx.helper.make_tensor_value_info(\"out0\", onnx.TensorProto.FLOAT, [\"M\"])],\n)\n\nnode = onnx.helper.make_node(\n \"SequenceMap\", inputs=[\"x\"], outputs=[\"y\"], body=body\n)\n\nx = [np.random.uniform(0.0, 1.0, 10).astype(np.float32) 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 ),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [\"N\"])\n ),\n]\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n input_type_protos=input_type_protos,\n output_type_protos=output_type_protos,\n name=\"test_sequence_map_identity_1_sequence\",\n)" }, { "summary": "sequence_map_identity_1_sequence_1_tensor", "code": "body = onnx.helper.make_graph(\n [\n onnx.helper.make_node(\"Identity\", [\"in0\"], [\"out0\"]),\n onnx.helper.make_node(\"Identity\", [\"in1\"], [\"out1\"]),\n ],\n \"seq_map_body\",\n [\n onnx.helper.make_tensor_value_info(\n \"in0\", onnx.TensorProto.FLOAT, [\"N\"]\n ),\n onnx.helper.make_tensor_value_info(\n \"in1\", onnx.TensorProto.FLOAT, [\"M\"]\n ),\n ],\n [\n onnx.helper.make_tensor_value_info(\n \"out0\", onnx.TensorProto.FLOAT, [\"N\"]\n ),\n onnx.helper.make_tensor_value_info(\n \"out1\", onnx.TensorProto.FLOAT, [\"M\"]\n ),\n ],\n)\n\nnode = onnx.helper.make_node(\n \"SequenceMap\", inputs=[\"x0\", \"x1\"], outputs=[\"y0\", \"y1\"], body=body\n)\n\nx0 = [\n np.random.uniform(0.0, 1.0, np.random.randint(1, 10)).astype(np.float32)\n for _ in range(3)\n]\nx1 = np.random.uniform(0.0, 1.0, np.random.randint(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 ),\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 ),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [\"M\"])\n ),\n]\nexpect(\n node,\n inputs=[x0, x1],\n 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\",\n)" }, { "summary": "sequence_map_identity_2_sequences", "code": "body = onnx.helper.make_graph(\n [\n onnx.helper.make_node(\"Identity\", [\"in0\"], [\"out0\"]),\n onnx.helper.make_node(\"Identity\", [\"in1\"], [\"out1\"]),\n ],\n \"seq_map_body\",\n [\n onnx.helper.make_tensor_value_info(\n \"in0\", onnx.TensorProto.FLOAT, [\"N\"]\n ),\n onnx.helper.make_tensor_value_info(\n \"in1\", onnx.TensorProto.FLOAT, [\"M\"]\n ),\n ],\n [\n onnx.helper.make_tensor_value_info(\n \"out0\", onnx.TensorProto.FLOAT, [\"N\"]\n ),\n onnx.helper.make_tensor_value_info(\n \"out1\", onnx.TensorProto.FLOAT, [\"M\"]\n ),\n ],\n)\n\nnode = onnx.helper.make_node(\n \"SequenceMap\", inputs=[\"x0\", \"x1\"], outputs=[\"y0\", \"y1\"], body=body\n)\n\nx0 = [\n np.random.uniform(0.0, 1.0, np.random.randint(1, 10)).astype(np.float32)\n for _ in range(3)\n]\nx1 = [\n np.random.uniform(0.0, 1.0, np.random.randint(1, 10)).astype(np.float32)\n for _ in range(3)\n]\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 ),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [\"M\"])\n ),\n]\noutput_type_protos = [\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [\"N\"])\n ),\n onnx.helper.make_sequence_type_proto(\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.FLOAT, [\"M\"])\n ),\n]\nexpect(\n node,\n inputs=[x0, x1],\n 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\",\n)" } ] }, { "name": "Shape", "module": "ai.onnx", "version": 1, "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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\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)\n\ntest_shape(\"_start_greater_than_end\", x, start=2, end=1)" } ] }, { "name": "Shape", "module": "ai.onnx", "version": 13, "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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\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)\n\ntest_shape(\"_start_greater_than_end\", x, start=2, end=1)" } ] }, { "name": "Shape", "module": "ai.onnx", "version": 15, "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], 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. If start > end, the result will be an empty shape.\n\nExamples:\n\n```\nInput tensor with shape: [2, 3, 4]\nNo attributes specified.\nOutput: [2, 3, 4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: -1\nOutput: [4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nend: -1\nOutput: [2, 3]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: 1\nend: 2\nOutput: [3]\n```\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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\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)\n\ntest_shape(\"_start_greater_than_end\", x, start=2, end=1)" } ] }, { "name": "Shape", "module": "ai.onnx", "version": 19, "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], 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. If start > end, the result will be an empty shape.\n\nExamples:\n\n```\nInput tensor with shape: [2, 3, 4]\nNo attributes specified.\nOutput: [2, 3, 4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: -1\nOutput: [4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nend: -1\nOutput: [2, 3]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: 1\nend: 2\nOutput: [3]\n```\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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "description": "Constrain output to int64 tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int64)" ] } ], "examples": [ { "summary": "shape", "code": "x = np.array(\n [\n [1, 2, 3],\n [4, 5, 6],\n ]\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)\n\ntest_shape(\"_start_greater_than_end\", x, start=2, end=1)" } ] }, { "name": "Shape", "module": "ai.onnx", "version": 21, "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], 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. If start > end, the result will be an empty shape.\n\nExamples:\n\n```\nInput tensor with shape: [2, 3, 4]\nNo attributes specified.\nOutput: [2, 3, 4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: -1\nOutput: [4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nend: -1\nOutput: [2, 3]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: 1\nend: 2\nOutput: [3]\n```\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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] }, { "description": "Constrain output to int64 tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int64)" ] } ], "examples": [ { "summary": "shape", "code": "x = np.array(\n [\n [1, 2, 3],\n [4, 5, 6],\n ]\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)\n\ntest_shape(\"_start_greater_than_end\", x, start=2, end=1)" } ] }, { "name": "Shape", "module": "ai.onnx", "version": 23, "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], 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. If start > end, the result will be an empty shape.\n\nExamples:\n\n```\nInput tensor with shape: [2, 3, 4]\nNo attributes specified.\nOutput: [2, 3, 4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: -1\nOutput: [4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nend: -1\nOutput: [2, 3]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: 1\nend: 2\nOutput: [3]\n```\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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] }, { "description": "Constrain output to int64 tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int64)" ] } ], "examples": [ { "summary": "shape", "code": "x = np.array(\n [\n [1, 2, 3],\n [4, 5, 6],\n ]\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)\n\ntest_shape(\"_start_greater_than_end\", x, start=2, end=1)" } ] }, { "name": "Shape", "module": "ai.onnx", "version": 24, "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], 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. If start > end, the result will be an empty shape.\n\nExamples:\n\n```\nInput tensor with shape: [2, 3, 4]\nNo attributes specified.\nOutput: [2, 3, 4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: -1\nOutput: [4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nend: -1\nOutput: [2, 3]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: 1\nend: 2\nOutput: [3]\n```\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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] }, { "description": "Constrain output to int64 tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int64)" ] } ], "examples": [ { "summary": "shape", "code": "x = np.array(\n [\n [1, 2, 3],\n [4, 5, 6],\n ]\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)\n\ntest_shape(\"_start_greater_than_end\", x, start=2, end=1)" } ] }, { "name": "Shape", "module": "ai.onnx", "version": 25, "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], 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. If start > end, the result will be an empty shape.\n\nExamples:\n\n```\nInput tensor with shape: [2, 3, 4]\nNo attributes specified.\nOutput: [2, 3, 4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: -1\nOutput: [4]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nend: -1\nOutput: [2, 3]\n```\n\n```\nInput tensor with shape: [2, 3, 4]\nstart: 1\nend: 2\nOutput: [3]\n```\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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] }, { "description": "Constrain output to int64 tensor.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(int64)" ] } ], "examples": [ { "summary": "shape", "code": "x = np.array(\n [\n [1, 2, 3],\n [4, 5, 6],\n ]\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)\n\ntest_shape(\"_start_greater_than_end\", x, start=2, end=1)" } ] }, { "name": "Shrink", "module": "ai.onnx", "version": 9, "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], 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], name=\"test_shrink_soft\")" } ] }, { "name": "Sigmoid", "module": "ai.onnx", "version": 1, "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 / (\n 1.0 + np.exp(np.negative(x))\n) # expected output [0.26894143, 0.5, 0.7310586]\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_sigmoid\")" } ], "category": "Activation" }, { "name": "Sigmoid", "module": "ai.onnx", "version": 6, "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 / (\n 1.0 + np.exp(np.negative(x))\n) # expected output [0.26894143, 0.5, 0.7310586]\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_sigmoid\")" } ], "category": "Activation" }, { "name": "Sigmoid", "module": "ai.onnx", "version": 13, "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 / (\n 1.0 + np.exp(np.negative(x))\n) # expected output [0.26894143, 0.5, 0.7310586]\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_sigmoid\")" } ], "category": "Activation" }, { "name": "Sign", "module": "ai.onnx", "version": 9, "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], name=\"test_sign\")" } ] }, { "name": "Sign", "module": "ai.onnx", "version": 13, "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], name=\"test_sign\")" } ] }, { "name": "SimplifiedLayerNormalization", "module": "ai.onnx", "version": 1, "status": "experimental", "description": "SimplifiedLayerNormalization", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": -1, "description": "The first normalization dimension: normalization will be performed along dimensions axis : rank(inputs)." }, { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "The epsilon value to use to avoid division by zero." }, { "name": "stash_type", "type": "int64", "required": false, "default": 1, "description": "type used for stash mean/inv_std_var" } ], "inputs": [ { "name": "X", "type": "T", "description": "Input data tensor from the previous layer." }, { "name": "scale", "type": "V", "description": "Scale tensor." } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "V", "description": "Output data tensor." }, { "name": "inv_std_var", "type": "U", "option": "optional", "description": "Saved inverse standard variance used during training to speed up gradient computation." } ], "min_output": 1, "max_output": 2, "outputs_range": "1 - 2", "type_constraints": [ { "description": "Constrain input X type to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] }, { "description": "Constrain mean and inv_std_var to be float tensors.", "type_param_str": "U", "allowed_type_strs": [ "tensor(float)", "tensor(double)" ] }, { "description": "Constrain output Y and scale type to float tensors.", "type_param_str": "V", "allowed_type_strs": [ "tensor(float16)", "tensor(float)", "tensor(double)", "tensor(bfloat16)" ] } ], "category": "Normalization" }, { "name": "Sin", "module": "ai.onnx", "version": 7, "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], 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], name=\"test_sin\")" } ] }, { "name": "Sin", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_sin\")" } ] }, { "name": "Sinh", "module": "ai.onnx", "version": 9, "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], 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], name=\"test_sinh\")" } ] }, { "name": "Sinh", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_sinh\")" } ] }, { "name": "Size", "module": "ai.onnx", "version": 1, "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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\n).astype(np.float32)\ny = np.array(6).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_size\")" } ] }, { "name": "Size", "module": "ai.onnx", "version": 13, "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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\n).astype(np.float32)\ny = np.array(6).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_size\")" } ] }, { "name": "Size", "module": "ai.onnx", "version": 19, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)" ] }, { "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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\n).astype(np.float32)\ny = np.array(6).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_size\")" } ] }, { "name": "Size", "module": "ai.onnx", "version": 21, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] }, { "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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\n).astype(np.float32)\ny = np.array(6).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_size\")" } ] }, { "name": "Size", "module": "ai.onnx", "version": 23, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] }, { "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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\n).astype(np.float32)\ny = np.array(6).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_size\")" } ] }, { "name": "Size", "module": "ai.onnx", "version": 24, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] }, { "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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\n).astype(np.float32)\ny = np.array(6).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_size\")" } ] }, { "name": "Size", "module": "ai.onnx", "version": 25, "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] }, { "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 [\n [1, 2, 3],\n [4, 5, 6],\n ]\n).astype(np.float32)\ny = np.array(6).astype(np.int64)\n\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_size\")" } ] }, { "name": "SkipGroupNorm", "module": "com.microsoft", "version": 1, "description": "This operator element-wise adds x, skip and bias, then apply group normalization and optional activation.\n\nThis operator transforms input according to\n s = x + skip + bias\n y = gamma * (s - mean) / sqrt(variance + epsilon) + beta\n\nThe input channels are separated into num_groups groups, each containing num_channels / num_groups channels.\nThe num_channels must be divisible by num_groups.\nThe mean and standard-deviation of s are calculated separately over the each group.\nThe weight and bias are per-channel affine transform parameter vectors of size num_channels.\n\nThe activation attribute can be used to enable activation after group normalization.\n", "attributes": [ { "name": "activation", "type": "int64", "required": true, "description": "Activation after group normalization: 0 for None, 1 for SiLU" }, { "name": "channels_last", "type": "int64", "required": false, "default": 1, "description": "1 if the input and output are in the NHWC layout, 0 if it is in the NCHW layout. Defaults to 1." }, { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999747378752e-06, "description": "The epsilon value to use to avoid division by zero" }, { "name": "groups", "type": "int64", "required": true, "description": "The number of groups of channels. It should be a divisor of the number of channels C" } ], "inputs": [ { "name": "X", "type": "T", "description": "Input data tensor. Dimensions are (N x H x W x C) when channels_last is 1 or (N x C x H x W) otherwise, where N is the batch size, C is the number of channels, and H and W are the height and width of the data" }, { "name": "gamma", "type": "M", "description": "1D gamma tensor for normalization with shape (C), where C is number of channels" }, { "name": "beta", "type": "M", "description": "1D beta tensor for normalization with shape (C), where C is number of channels" }, { "name": "skip", "type": "T", "description": "4D or 2D skip tensor. The shape can be (N x H x W x C) or (N x 1 x 1 x C) or (N x C)" }, { "name": "bias", "type": "T", "option": "optional", "description": "1D bias tensor. Dimensions are (C), where C is number of channels" } ], "min_input": 4, "max_input": 5, "outputs": [ { "name": "Y", "type": "T", "description": "The output tensor of the same shape as X" }, { "name": "S", "type": "T", "option": "optional", "description": "The element-wise sum of input x, skip and bias tensors. It has the same shape as X" } ], "min_output": 1, "max_output": 2, "inputs_range": "4 - 5", "outputs_range": "1 - 2", "type_constraints": [ { "description": "Constrain input X, skip, bias and output Y, S types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] }, { "description": "Constrain gamma and beta to float tensors.", "type_param_str": "M", "allowed_type_strs": [ "tensor(float16)", "tensor(float)" ] } ] }, { "name": "SkipLayerNormalization", "module": "com.microsoft", "version": 1, "description": "Skip and Layer Normalization Fusion", "attributes": [ { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999960041972e-13, "description": "The epsilon value to use to avoid division by zero." } ], "inputs": [ { "name": "input", "type": "T", "description": "3D input tensor with shape (batch_size, sequence_length, hidden_size)" }, { "name": "skip", "type": "T", "description": "3D skip tensor with shape (batch_size, sequence_length, hidden_size) or (1, sequence_length, hidden_size) or (sequence_length, hidden_size)" }, { "name": "gamma", "type": "T", "description": "1D input tensor with shape (hidden_size)" }, { "name": "beta", "type": "T", "option": "optional", "description": "1D skip tensor with shape (hidden_size" }, { "name": "bias", "type": "T", "option": "optional", "description": "1D bias tensor with shape (hidden_size" } ], "min_input": 3, "max_input": 5, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, hidden_size)" }, { "name": "mean", "type": "U", "option": "optional", "description": "Saved mean used during training to speed up gradient computation" }, { "name": "inv_std_var", "type": "U", "option": "optional", "description": "Saved inverse standard variance used during training to speed up gradient computation." }, { "name": "input_skip_bias_sum", "type": "T", "option": "optional", "description": "Sum of the input and skip inputs (and bias if it exists) with shape (batch_size, sequence_length, hidden_size)." } ], "min_output": 1, "max_output": 4, "inputs_range": "3 - 5", "outputs_range": "1 - 4", "type_constraints": [ { "description": "Constrain input and output types to float or half tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain mean and inv_std_var to float tensors.", "type_param_str": "U", "allowed_type_strs": [ "tensor(float)" ] } ], "category": "Normalization" }, { "name": "SkipSimplifiedLayerNormalization", "module": "com.microsoft", "version": 1, "description": "Skip and Root Mean Square Layer Normalization", "attributes": [ { "name": "epsilon", "type": "float32", "required": false, "default": 9.999999960041972e-13, "description": "The epsilon value to use to avoid division by zero." } ], "inputs": [ { "name": "input", "type": "T", "description": "3D input tensor with shape (batch_size, sequence_length, hidden_size)Or 2D input tensor with shape (token_count, hidden_size)" }, { "name": "skip", "type": "T", "description": "3D input tensor with shape (batch_size, sequence_length, hidden_size)Or 2D input tensor with shape (token_count, hidden_size)" }, { "name": "gamma", "type": "T", "description": "1D input tensor with shape (hidden_size)" }, { "name": "bias", "type": "T", "option": "optional", "description": "1D bias tensor with shape (hidden_size" } ], "min_input": 3, "max_input": 4, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, hidden_size)Or 2D output tensor with shape (token_count, hidden_size)" }, { "name": "mean", "type": "U", "option": "optional", "description": "Saved mean used during training to speed up gradient computation" }, { "name": "inv_std_var", "type": "U", "option": "optional", "description": "Saved inverse standard variance used during training to speed up gradient computation." }, { "name": "input_skip_bias_sum", "type": "T", "option": "optional", "description": "Sum of the input and skip inputs (and bias if it exists)with shape (batch_size, sequence_length, hidden_size) or (token_count, hidden_size)." } ], "min_output": 1, "max_output": 4, "inputs_range": "3 - 4", "outputs_range": "1 - 4", "type_constraints": [ { "description": "Constrain input and output to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain mean and inv_std_var to float tensors.", "type_param_str": "U", "allowed_type_strs": [ "tensor(float)" ] } ], "category": "Normalization" }, { "name": "Slice", "module": "ai.onnx", "version": 1, "description": "Produces a slice of the input tensor along multiple axes. Similar to numpy:\nhttps://numpy.org/doc/stable/reference/routines.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(\n node, inputs=[x, starts, ends, axes, steps], outputs=[y], name=\"test_slice\"\n)" }, { "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(\n node, inputs=[x, starts, ends], outputs=[y], name=\"test_slice_default_axes\"\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes],\n outputs=[y],\n name=\"test_slice_default_steps\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_end_out_of_bounds\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_neg\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_neg_steps\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes],\n outputs=[y],\n name=\"test_slice_negative_axes\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_start_out_of_bounds\",\n)" } ], "category": "Tensor" }, { "name": "Slice", "module": "ai.onnx", "version": 10, "description": "Produces a slice of the input tensor along multiple axes. Similar to numpy:\nhttps://numpy.org/doc/stable/reference/routines.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(\n node, inputs=[x, starts, ends, axes, steps], outputs=[y], name=\"test_slice\"\n)" }, { "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(\n node, inputs=[x, starts, ends], outputs=[y], name=\"test_slice_default_axes\"\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes],\n outputs=[y],\n name=\"test_slice_default_steps\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_end_out_of_bounds\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_neg\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_neg_steps\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes],\n outputs=[y],\n name=\"test_slice_negative_axes\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_start_out_of_bounds\",\n)" } ], "category": "Tensor" }, { "name": "Slice", "module": "ai.onnx", "version": 11, "description": "Produces a slice of the input tensor along multiple axes. Similar to numpy:\nhttps://numpy.org/doc/stable/reference/routines.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 slicing 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(\n node, inputs=[x, starts, ends, axes, steps], outputs=[y], name=\"test_slice\"\n)" }, { "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(\n node, inputs=[x, starts, ends], outputs=[y], name=\"test_slice_default_axes\"\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes],\n outputs=[y],\n name=\"test_slice_default_steps\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_end_out_of_bounds\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_neg\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_neg_steps\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes],\n outputs=[y],\n name=\"test_slice_negative_axes\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_start_out_of_bounds\",\n)" } ], "category": "Tensor" }, { "name": "Slice", "module": "ai.onnx", "version": 13, "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 `starts[i]`, `ends[i]`, and `steps[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`, `ends[i] = dims[i]` where\n`dims` are the dimensions of `input` and `steps[i] = 1`.\n\nAll negative elements of `axes` are made non-negative 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`ends[axes[i]]` is clamped to `[0, dims[axes[i]]]`, while for negative stepping it\nis clamped to `[-1, dims[axes[i]]-1]`.\n\nFinally, `steps[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\n```\ndata = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]\naxes = [0, 1]\nstarts = [1, 0]\nends = [2, 3]\nsteps = [1, 2]\nresult = [\n [5, 7],\n]\n```\n\nExample 2:\n\n```\ndata = [\n [1, 2, 3, 4],\n [5, 6, 7, 8],\n]\nstarts = [0, 1]\nends = [-1, 1000]\nresult = [\n [2, 3, 4],\n]\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(\n node, inputs=[x, starts, ends, axes, steps], outputs=[y], name=\"test_slice\"\n)" }, { "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(\n node, inputs=[x, starts, ends], outputs=[y], name=\"test_slice_default_axes\"\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes],\n outputs=[y],\n name=\"test_slice_default_steps\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_end_out_of_bounds\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_neg\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_neg_steps\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes],\n outputs=[y],\n name=\"test_slice_negative_axes\",\n)" }, { "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(\n node,\n inputs=[x, starts, ends, axes, steps],\n outputs=[y],\n name=\"test_slice_start_out_of_bounds\",\n)" } ], "category": "Tensor" }, { "name": "Snpe", "module": "com.microsoft", "version": 1, "description": "Onnx node for SNPE.", "attributes": [ { "name": "DLC", "type": "string", "required": true, "description": "payload of the SNPE DLC file." }, { "name": "notes", "type": "string", "required": false, "description": "(Optional) Some notes for the model" }, { "name": "snpe_version", "type": "string", "required": false, "description": "(Optional) SNPE version used to convert the model." }, { "name": "target_device", "type": "string", "required": false, "description": "(Optional) Target device like CPU, DSP, etc." } ], "inputs": [ { "name": "inputs", "type": "T", "list": true, "description": "List of tensors for SNPE DLC input" } ], "min_input": 1, "max_input": 2147483647, "outputs": [ { "name": "outputs", "type": "T", "list": true, "description": "One or more outputs, list of tensors for DLC output" } ], "min_output": 1, "max_output": 2147483647, "inputs_range": "1 - ∞", "outputs_range": "1 - ∞", "type_constraints": [ { "description": "Constrain input and output types to uint8, uint16, float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(uint8)", "tensor(uint16)", "tensor(float)" ] } ] }, { "name": "Softmax", "module": "ai.onnx", "version": 1, "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], name=\"test_softmax_example\")" }, { "summary": "softmax_axis", "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]).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], 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], 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], 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], 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], 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], name=\"test_softmax_default_axis\")" } ], "category": "Activation" }, { "name": "Softmax", "module": "ai.onnx", "version": 11, "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], name=\"test_softmax_example\")" }, { "summary": "softmax_axis", "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]).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], 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], 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], 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], 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], 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], name=\"test_softmax_default_axis\")" } ], "category": "Activation" }, { "name": "Softmax", "module": "ai.onnx", "version": 13, "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], name=\"test_softmax_example\")" }, { "summary": "softmax_axis", "code": "x = np.array([[0, 1, 2, 3], [10000, 10001, 10002, 10003]]).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], 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], 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], 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], 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], 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], name=\"test_softmax_default_axis\")" } ], "category": "Activation" }, { "name": "SoftmaxCrossEntropyLoss", "module": "ai.onnx", "version": 12, "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 calculated 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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[sce],\n name=\"test_sce_NCd1_mean_weight_negative_ii\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True,\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1_mean_weight_negative_ii_log_prob\",\n)" }, { "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 \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n np.int64\n)\nlabels[0][0][0][0] = -5\n\nsce = softmaxcrossentropy(\n x, labels, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[x, labels],\n outputs=[sce],\n name=\"test_sce_NCd1d2d3_none_no_weight_negative_ii\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n np.int64\n)\nlabels[0][0][0][0] = -5\n\nloss, log_prob = softmaxcrossentropy(\n x, labels, reduction=reduction, ignore_index=ignore_index, get_log_prob=True\n)\n\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1d2d3_none_no_weight_negative_ii_log_prob\",\n)" }, { "summary": "input_shape_is_NCd1d2d3_sum_weight_high_ii", "code": "reduction = \"sum\"\nignore_index = np.int64(10)\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[sce],\n name=\"test_sce_NCd1d2d3_sum_weight_high_ii\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True,\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1d2d3_sum_weight_high_ii_log_prob\",\n)" }, { "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight", "code": "reduction = \"mean\"\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nsce = softmaxcrossentropy(x, labels, weight=weight, reduction=reduction)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[sce],\n name=\"test_sce_NCd1d2d3d4d5_mean_weight\",\n)" }, { "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight_log_prob", "code": "reduction = \"mean\"\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nloss, log_prob = softmaxcrossentropy(\n x, labels, weight=weight, reduction=reduction, get_log_prob=True\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1d2d3d4d5_mean_weight_log_prob\",\n)" }, { "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight", "code": "reduction = \"none\"\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\n\nsce = softmaxcrossentropy(x, labels, reduction=reduction)\n\nexpect(\n node,\n inputs=[x, labels],\n outputs=[sce],\n name=\"test_sce_NCd1d2d3d4d5_none_no_weight\",\n)" }, { "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight_log_prob", "code": "reduction = \"none\"\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\n\nloss, log_prob = softmaxcrossentropy(\n x, labels, reduction=reduction, get_log_prob=True\n)\n\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1d2d3d4d5_none_no_weight_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_mean", "code": "# Define operator attributes.\nreduction = \"mean\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n node,\n inputs=[x, y],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_3d_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_mean_log_prob", "code": "# Define operator attributes.\nreduction = \"mean\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n node, inputs=[x, labels], outputs=[sce], name=\"test_sce_mean_no_weight_ii\"\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n node,\n inputs=[x, labels],\n outputs=[sce],\n name=\"test_sce_mean_no_weight_ii_3d\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_no_weight_ii_3d_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, reduction=reduction, ignore_index=ignore_index\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[sce],\n name=\"test_sce_mean_no_weight_ii_4d\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, reduction=reduction, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_no_weight_ii_4d_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_no_weight_ii_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_mean_weights", "code": "# Define operator attributes.\nreduction = \"mean\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_mean_weight\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_mean_weight_ii\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_mean_weight_ii_3d\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, weight=weights, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_weight_ii_3d_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, reduction=reduction, weight=weights, ignore_index=ignore_index\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_mean_weight_ii_4d\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x,\n labels,\n reduction=reduction,\n weight=weights,\n ignore_index=ignore_index,\n get_log_prob=True,\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_weight_ii_4d_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, weight=weights, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_weight_ii_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_mean_weights_log_prob", "code": "# Define operator attributes.\nreduction = \"mean\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n x, labels, weight=weights, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_weight_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_none", "code": "# Define operator attributes.\nreduction = \"none\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n x, labels, reduction=\"none\", get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_none_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_none_weights", "code": "# Define operator attributes.\nreduction = \"none\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_none_weights\",\n)" }, { "summary": "softmaxcrossentropy_none_weights_log_prob", "code": "# Define operator attributes.\nreduction = \"none\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n x, labels, weight=weights, reduction=\"none\", get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_none_weights_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_sum", "code": "# Define operator attributes.\nreduction = \"sum\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n x, labels, reduction=\"sum\", get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_sum_log_prob\",\n)" } ] }, { "name": "SoftmaxCrossEntropyLoss", "module": "ai.onnx", "version": 13, "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\n* shape(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.\n* shape(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 calculated as follows:\n```\nl[i][d1][d2]...[dk] = -y[i][c][d1][d2]..[dk], where i is the index of classes.\n```\nor\n```\nl[i][d1][d2]...[dk] = -y[i][c][d1][d2]..[dk] * weights[c], if 'weights' is provided.\n```\n\nloss is zero for the case when label-value equals ignore_index.\n```\nl[i][d1][d2]...[dk] = 0, when labels[n][d1][d2]...[dk] = ignore_index\n```\n\nwhere:\n```\np = Softmax(scores)\ny = Log(p)\nc = labels[i][d1][d2]...[dk]\n```\n\nFinally, L is optionally reduced:\n\n* If reduction = 'none', the output is L with shape (N, D1, D2, ..., Dk).\n* If reduction = 'sum', the output is scalar: Sum(L).\n* If reduction = 'mean', the output is scalar: ReduceMean(L), or if weight is provided: `ReduceSum(L) / ReduceSum(W)`,\n where 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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[sce],\n name=\"test_sce_NCd1_mean_weight_negative_ii\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True,\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1_mean_weight_negative_ii_log_prob\",\n)" }, { "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 \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n np.int64\n)\nlabels[0][0][0][0] = -5\n\nsce = softmaxcrossentropy(\n x, labels, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[x, labels],\n outputs=[sce],\n name=\"test_sce_NCd1d2d3_none_no_weight_negative_ii\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n np.int64\n)\nlabels[0][0][0][0] = -5\n\nloss, log_prob = softmaxcrossentropy(\n x, labels, reduction=reduction, ignore_index=ignore_index, get_log_prob=True\n)\n\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1d2d3_none_no_weight_negative_ii_log_prob\",\n)" }, { "summary": "input_shape_is_NCd1d2d3_sum_weight_high_ii", "code": "reduction = \"sum\"\nignore_index = np.int64(10)\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, weight=weight, reduction=reduction, ignore_index=ignore_index\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[sce],\n name=\"test_sce_NCd1d2d3_sum_weight_high_ii\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x,\n labels,\n weight=weight,\n reduction=reduction,\n ignore_index=ignore_index,\n get_log_prob=True,\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1d2d3_sum_weight_high_ii_log_prob\",\n)" }, { "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight", "code": "reduction = \"mean\"\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nsce = softmaxcrossentropy(x, labels, weight=weight, reduction=reduction)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[sce],\n name=\"test_sce_NCd1d2d3d4d5_mean_weight\",\n)" }, { "summary": "input_shape_is_NCd1d2d3d4d5_mean_weight_log_prob", "code": "reduction = \"mean\"\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\nweight = np.random.rand(C).astype(np.float32)\n\nloss, log_prob = softmaxcrossentropy(\n x, labels, weight=weight, reduction=reduction, get_log_prob=True\n)\n\nexpect(\n node,\n inputs=[x, labels, weight],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1d2d3d4d5_mean_weight_log_prob\",\n)" }, { "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight", "code": "reduction = \"none\"\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\n\nsce = softmaxcrossentropy(x, labels, reduction=reduction)\n\nexpect(\n node,\n inputs=[x, labels],\n outputs=[sce],\n name=\"test_sce_NCd1d2d3d4d5_none_no_weight\",\n)" }, { "summary": "input_shape_is_NCd1d2d3d4d5_none_no_weight_log_prob", "code": "reduction = \"none\"\n\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n 0, high=C, size=(N, dim1, dim2, dim3, dim4, dim5)\n).astype(np.int64)\n\nloss, log_prob = softmaxcrossentropy(\n x, labels, reduction=reduction, get_log_prob=True\n)\n\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_NCd1d2d3d4d5_none_no_weight_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_mean", "code": "# Define operator attributes.\nreduction = \"mean\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n node,\n inputs=[x, y],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_3d_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_mean_log_prob", "code": "# Define operator attributes.\nreduction = \"mean\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n node, inputs=[x, labels], outputs=[sce], name=\"test_sce_mean_no_weight_ii\"\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n node,\n inputs=[x, labels],\n outputs=[sce],\n name=\"test_sce_mean_no_weight_ii_3d\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_no_weight_ii_3d_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, reduction=reduction, ignore_index=ignore_index\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[sce],\n name=\"test_sce_mean_no_weight_ii_4d\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, reduction=reduction, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_no_weight_ii_4d_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_no_weight_ii_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_mean_weights", "code": "# Define operator attributes.\nreduction = \"mean\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_mean_weight\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_mean_weight_ii\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_mean_weight_ii_3d\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, weight=weights, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_weight_ii_3d_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, reduction=reduction, weight=weights, ignore_index=ignore_index\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_mean_weight_ii_4d\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x,\n labels,\n reduction=reduction,\n weight=weights,\n ignore_index=ignore_index,\n get_log_prob=True,\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_weight_ii_4d_log_prob\",\n)" }, { "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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n ignore_index=ignore_index,\n)\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(\n x, labels, weight=weights, ignore_index=ignore_index, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_weight_ii_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_mean_weights_log_prob", "code": "# Define operator attributes.\nreduction = \"mean\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n x, labels, weight=weights, get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_mean_weight_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_none", "code": "# Define operator attributes.\nreduction = \"none\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n x, labels, reduction=\"none\", get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_none_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_none_weights", "code": "# Define operator attributes.\nreduction = \"none\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n node,\n inputs=[x, labels, weights],\n outputs=[sce],\n name=\"test_sce_none_weights\",\n)" }, { "summary": "softmaxcrossentropy_none_weights_log_prob", "code": "# Define operator attributes.\nreduction = \"none\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\", \"w\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n x, labels, weight=weights, reduction=\"none\", get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels, weights],\n outputs=[loss, log_prob],\n name=\"test_sce_none_weights_log_prob\",\n)" }, { "summary": "softmaxcrossentropy_sum", "code": "# Define operator attributes.\nreduction = \"sum\"\n\n# Create operator.\nnode = onnx.helper.make_node(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\"],\n reduction=reduction,\n)\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(\n \"SoftmaxCrossEntropyLoss\",\n inputs=[\"x\", \"y\"],\n outputs=[\"z\", \"log_prob\"],\n reduction=reduction,\n)\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(\n x, labels, reduction=\"sum\", get_log_prob=True\n)\n\n# Check results\nexpect(\n node,\n inputs=[x, labels],\n outputs=[loss, log_prob],\n name=\"test_sce_sum_log_prob\",\n)" } ] }, { "name": "Softplus", "module": "ai.onnx", "version": 1, "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": "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": "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(\n np.exp(x) + 1\n) # expected output [0.31326166, 0.69314718, 1.31326163]\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_softplus\")" } ], "category": "Activation" }, { "name": "Softplus", "module": "ai.onnx", "version": 22, "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": "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": "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(\n np.exp(x) + 1\n) # expected output [0.31326166, 0.69314718, 1.31326163]\nexpect(node, inputs=[x], outputs=[y], 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], name=\"test_softplus\")" } ], "category": "Activation" }, { "name": "Softsign", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_softsign\")" } ], "category": "Activation" }, { "name": "Softsign", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_softsign\")" } ], "category": "Activation" }, { "name": "SpaceToDepth", "module": "ai.onnx", "version": 1, "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(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\n# (1, 4, 2, 3) output tensor\ny = np.array(\n [\n [\n [[0, 1, 2], [3, 4, 5]],\n [[6, 7, 8], [9, 10, 11]],\n [[12, 13, 14], [15, 16, 17]],\n [[18, 19, 20], [21, 22, 23]],\n ]\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], 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(\n x, [b, c, h // blocksize, blocksize, w // blocksize, blocksize]\n)\ntmp = np.transpose(tmp, [0, 3, 5, 1, 2, 4])\ny = np.reshape(tmp, [b, c * (blocksize**2), h // blocksize, w // blocksize])\nexpect(node, inputs=[x], outputs=[y], name=\"test_spacetodepth\")" } ] }, { "name": "SpaceToDepth", "module": "ai.onnx", "version": 13, "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(\n [\n [\n [\n [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],\n ]\n ]\n ]\n).astype(np.float32)\n\n# (1, 4, 2, 3) output tensor\ny = np.array(\n [\n [\n [[0, 1, 2], [3, 4, 5]],\n [[6, 7, 8], [9, 10, 11]],\n [[12, 13, 14], [15, 16, 17]],\n [[18, 19, 20], [21, 22, 23]],\n ]\n ]\n).astype(np.float32)\nexpect(node, inputs=[x], outputs=[y], 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(\n x, [b, c, h // blocksize, blocksize, w // blocksize, blocksize]\n)\ntmp = np.transpose(tmp, [0, 3, 5, 1, 2, 4])\ny = np.reshape(tmp, [b, c * (blocksize**2), h // blocksize, w // blocksize])\nexpect(node, inputs=[x], outputs=[y], name=\"test_spacetodepth\")" } ] }, { "name": "SpaceToDepth", "module": "com.ms.internal.nhwc", "version": 1, "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)" ] } ] }, { "name": "SpaceToDepth", "module": "com.ms.internal.nhwc", "version": 13, "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)" ] } ] }, { "name": "SparseAttention", "module": "com.microsoft", "version": 1, "description": "Block Sparse Attention used in Phi-3-small (https://arxiv.org/pdf/2404.14219).\n\nIt is inspired by Sparse Transformers (https://arxiv.org/pdf/1904.10509) and BigBird (https://arxiv.org/pdf/2007.14062).\n\nblock_mask can be used to configure sparse layout for different head.\nWhen number of sparse layout is 1, all heads have same sparse layout. Otherwise, different layouts are used cyclically.\nFor example, given 4 layouts (S0, S1, S2, S3), 8 heads will have layouts like (S0, S1, S2, S3, S0, S1, S2, S3).\n\nThe block_row_indices and block_col_indices are the CSR representation of block mask. The block_col_indices might contain\npaddings at the right side when different layout has different number of non-zeros in block mask.\n\nAn example of block mask with 2 layouts where each layout is 4 x 4 blocks:\n [[[1, 0, 0, 0],\n [1, 1, 0, 0],\n [0, 1, 1, 0],\n [0, 1, 1, 1]],\n\n [[1, 0, 0, 0],\n [1, 1, 0, 0],\n [1, 1, 1, 0],\n [1, 0, 1, 1]]]\n\nThe corresponding CSR format:\n block_col_indices = [[0, 0, 1, 1, 2, 1, 2, 3, -1], [0, 0, 1, 0, 1, 2, 0, 2, 3]]\n block_row_indices = [[0, 1, 3, 5, 8], [0, 1, 3, 6, 9]]\n\nWhen do_rotary is True, cos_cache and sin_cache are required. Note that the maximum sequence length supported by cos\nor sin cache can be different from the maximum sequence length used by kv cache.\n\nOnly supports unidirectional attention with cache of past key and value in linear buffers.\n\nFor performance, past_key and present_key share same memory buffer, and past_value and present_value too.\n", "attributes": [ { "name": "do_rotary", "type": "int64", "required": false, "description": "Whether to use rotary position embedding. Default value is 0." }, { "name": "kv_num_heads", "type": "int64", "required": true, "description": "Number of attention heads for key and value" }, { "name": "num_heads", "type": "int64", "required": true, "description": "Number of attention heads for query" }, { "name": "rotary_interleaved", "type": "int64", "required": false, "description": "Rotary use interleaved pattern or not. Default value is 0." }, { "name": "scale", "type": "float32", "required": false, "description": "Scaling factor applied prior to softmax. The default value is 1/sqrt(head_size)" }, { "name": "sparse_block_size", "type": "int64", "required": true, "description": "Number of tokens per sparse block. Choices: 16, 32, 64, 128" } ], "inputs": [ { "name": "query", "type": "T", "description": "Query with shape (batch_size, sequence_length, num_heads * head_size), or packed QKV with shape is(batch_size, sequence_length, d) where d is (num_heads + 2 * kv_num_heads) * head_size." }, { "name": "key", "type": "T", "option": "optional", "description": "Key with shape (batch_size, sequence_length, kv_num_heads * head_size)" }, { "name": "value", "type": "T", "option": "optional", "description": "Value with shape (batch_size, sequence_length, kv_num_heads * head_size)" }, { "name": "past_key", "type": "T", "description": "Key cache with shape (batch_size, kv_num_heads, max_cache_sequence_length, head_size)" }, { "name": "past_value", "type": "T", "description": "Value cache with shape (batch_size, kv_num_heads, max_cache_sequence_length, head_size)" }, { "name": "block_row_indices", "type": "M", "description": "The row indices of CSR format of block mask with shape (num_layout, max_blocks + 1).The num_heads is divisible by num_layout, and max_blocks is max_sequence_length / sparse_block_size." }, { "name": "block_col_indices", "type": "M", "description": "The col indices of CSR format of block mask with shape (num_layout, max_nnz_blocks).The max_nnz_blocks is the maximum number of non-zeros per layout in block mask." }, { "name": "total_sequence_length", "type": "M", "description": "Scalar tensor of maximum total sequence length (past_sequence_length + sequence_length) among keys." }, { "name": "key_total_sequence_lengths", "type": "M", "description": "1D tensor with shape (batch_size) where each value is total sequence length of key excluding paddings." }, { "name": "cos_cache", "type": "T", "option": "optional", "description": "Cos cache of rotary with shape (max_rotary_sequence_length, head_size / 2)." }, { "name": "sin_cache", "type": "T", "option": "optional", "description": "Sin cache of rotary with shape (max_rotary_sequence_length, head_size / 2)." } ], "min_input": 9, "max_input": 11, "outputs": [ { "name": "output", "type": "T", "description": "3D output tensor with shape (batch_size, sequence_length, num_heads * head_size)" }, { "name": "present_key", "type": "T", "description": "Updated key cache with shape (batch_size, kv_num_heads, max_cache_sequence_length, head_size)." }, { "name": "present_value", "type": "T", "description": "Updated value cache with shape (batch_size, kv_num_heads, max_cache_sequence_length, head_size)." } ], "min_output": 3, "max_output": 3, "inputs_range": "9 - 11", "type_constraints": [ { "description": "Constrain input and output to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)", "tensor(bfloat16)" ] }, { "description": "Constrain integer type.", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] } ], "category": "Attention" }, { "name": "SparseToDenseMatMul", "module": "com.microsoft", "version": 1, "attributes": [ { "name": "alpha", "type": "float32", "required": false, "default": 1.0, "description": "Scalar multiplier for the product of the input tensors." }, { "name": "transA", "type": "int64", "required": false, "description": "Whether A should be transposed on the last two dimensions before doing multiplication" }, { "name": "transB", "type": "int64", "required": false, "description": "Whether B should be transposed on the last two dimensions before doing multiplication" } ], "inputs": [ { "name": "A", "type": "T", "description": "2-dimensional sparse matrix A. Either COO or CSR format" }, { "name": "B", "type": "T1", "description": "N-dimensional dense matrix B" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Y", "type": "T1", "description": "Matrix multiply results" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain input and output types to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "sparse_tensor(float)", "sparse_tensor(double)", "sparse_tensor(int64)", "sparse_tensor(int32)", "sparse_tensor(uint64)", "sparse_tensor(uint32)" ] }, { "description": "Constrain input and output types to float tensors.", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)", "tensor(double)", "tensor(int64)", "tensor(int32)", "tensor(uint64)", "tensor(uint32)" ] } ] }, { "name": "Split", "module": "ai.onnx", "version": 1, "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_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "1d_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset18\",\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset18\",\n)" }, { "summary": "1d_uneven_split_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]).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\", \"output_4\"],\n num_outputs=4,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n np.array([7.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_1d_uneven_split_opset18\",\n)" }, { "summary": "2d_opset13", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\"], axis=1\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "2d_opset18", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\"],\n axis=1,\n num_outputs=2,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d\",\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset18\",\n)" }, { "summary": "2d_uneven_split_opset18", "code": "node_input = np.array(\n [\n [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0],\n [9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\", \"output_3\"],\n axis=1,\n num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [9.0, 10.0, 11.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [12.0, 13.0, 14.0]]).astype(np.float32),\n np.array([[7.0, 8.0], [15.0, 16.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_2d_uneven_split_opset18\",\n)" }, { "summary": "default_values_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float32)\n\n# If axis is not specified, split is applied on default axis 0\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\", \"output_3\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "default_values_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset18\",\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset18\",\n)" }, { "summary": "zero_size_splits_opset13", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "zero_size_splits_opset18", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset18\",\n)" } ], "category": "Tensor" }, { "name": "Split", "module": "ai.onnx", "version": 2, "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_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "1d_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset18\",\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset18\",\n)" }, { "summary": "1d_uneven_split_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]).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\", \"output_4\"],\n num_outputs=4,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n np.array([7.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_1d_uneven_split_opset18\",\n)" }, { "summary": "2d_opset13", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\"], axis=1\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "2d_opset18", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\"],\n axis=1,\n num_outputs=2,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d\",\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset18\",\n)" }, { "summary": "2d_uneven_split_opset18", "code": "node_input = np.array(\n [\n [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0],\n [9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\", \"output_3\"],\n axis=1,\n num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [9.0, 10.0, 11.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [12.0, 13.0, 14.0]]).astype(np.float32),\n np.array([[7.0, 8.0], [15.0, 16.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_2d_uneven_split_opset18\",\n)" }, { "summary": "default_values_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float32)\n\n# If axis is not specified, split is applied on default axis 0\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\", \"output_3\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "default_values_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset18\",\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset18\",\n)" }, { "summary": "zero_size_splits_opset13", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "zero_size_splits_opset18", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset18\",\n)" } ], "category": "Tensor" }, { "name": "Split", "module": "ai.onnx", "version": 11, "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_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "1d_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset18\",\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset18\",\n)" }, { "summary": "1d_uneven_split_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]).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\", \"output_4\"],\n num_outputs=4,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n np.array([7.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_1d_uneven_split_opset18\",\n)" }, { "summary": "2d_opset13", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\"], axis=1\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "2d_opset18", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\"],\n axis=1,\n num_outputs=2,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d\",\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset18\",\n)" }, { "summary": "2d_uneven_split_opset18", "code": "node_input = np.array(\n [\n [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0],\n [9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\", \"output_3\"],\n axis=1,\n num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [9.0, 10.0, 11.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [12.0, 13.0, 14.0]]).astype(np.float32),\n np.array([[7.0, 8.0], [15.0, 16.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_2d_uneven_split_opset18\",\n)" }, { "summary": "default_values_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float32)\n\n# If axis is not specified, split is applied on default axis 0\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\", \"output_3\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "default_values_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset18\",\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset18\",\n)" }, { "summary": "zero_size_splits_opset13", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "zero_size_splits_opset18", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset18\",\n)" } ], "category": "Tensor" }, { "name": "Split", "module": "ai.onnx", "version": 13, "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_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "1d_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset18\",\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset18\",\n)" }, { "summary": "1d_uneven_split_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]).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\", \"output_4\"],\n num_outputs=4,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n np.array([7.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_1d_uneven_split_opset18\",\n)" }, { "summary": "2d_opset13", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\"], axis=1\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "2d_opset18", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\"],\n axis=1,\n num_outputs=2,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d\",\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset18\",\n)" }, { "summary": "2d_uneven_split_opset18", "code": "node_input = np.array(\n [\n [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0],\n [9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\", \"output_3\"],\n axis=1,\n num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [9.0, 10.0, 11.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [12.0, 13.0, 14.0]]).astype(np.float32),\n np.array([[7.0, 8.0], [15.0, 16.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_2d_uneven_split_opset18\",\n)" }, { "summary": "default_values_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float32)\n\n# If axis is not specified, split is applied on default axis 0\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\", \"output_3\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "default_values_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset18\",\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset18\",\n)" }, { "summary": "zero_size_splits_opset13", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "zero_size_splits_opset18", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset18\",\n)" } ], "category": "Tensor" }, { "name": "Split", "module": "ai.onnx", "version": 18, "description": "Split a tensor into a list of tensors, along the specified 'axis'.\nEither input 'split' or the attribute 'num_outputs' should be specified, but not both.\nIf the attribute 'num_outputs' is specified, then the tensor is split into equal sized parts.\nIf the tensor is not evenly splittable into `num_outputs`, the last chunk will be smaller.\nIf the input 'split' is specified, it indicates the sizes of each output in the split.\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": "num_outputs", "type": "int64", "required": false, "description": "Number of outputs to split parts of the tensor into. If the tensor is not evenly splittable the last chunk will be smaller." } ], "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_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "1d_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_1d_opset18\",\n)\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 = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_1d_opset18\",\n)" }, { "summary": "1d_uneven_split_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]).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\", \"output_4\"],\n num_outputs=4,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n np.array([7.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_1d_uneven_split_opset18\",\n)" }, { "summary": "2d_opset13", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\"], axis=1\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "2d_opset18", "code": "node_input = np.array(\n [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0, 10.0, 11.0, 12.0]]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\"],\n axis=1,\n num_outputs=2,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [7.0, 8.0, 9.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [10.0, 11.0, 12.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_2d\",\n)\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 = [\n np.array([[1.0, 2.0], [7.0, 8.0]]).astype(np.float32),\n np.array([[3.0, 4.0, 5.0, 6.0], [9.0, 10.0, 11.0, 12.0]]).astype(\n np.float32\n ),\n]\n\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_2d_opset18\",\n)" }, { "summary": "2d_uneven_split_opset18", "code": "node_input = np.array(\n [\n [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0],\n [9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0],\n ]\n).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Split\",\n inputs=[\"input\"],\n outputs=[\"output_1\", \"output_2\", \"output_3\"],\n axis=1,\n num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([[1.0, 2.0, 3.0], [9.0, 10.0, 11.0]]).astype(np.float32),\n np.array([[4.0, 5.0, 6.0], [12.0, 13.0, 14.0]]).astype(np.float32),\n np.array([[7.0, 8.0], [15.0, 16.0]]).astype(np.float32),\n]\n\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_2d_uneven_split_opset18\",\n)" }, { "summary": "default_values_opset13", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).astype(np.float32)\n\n# If axis is not specified, split is applied on default axis 0\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\"], outputs=[\"output_1\", \"output_2\", \"output_3\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "default_values_opset18", "code": "node_input = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).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 num_outputs=3,\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0]).astype(np.float32),\n np.array([5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input],\n outputs=expected_outputs,\n name=\"test_split_equal_parts_default_axis_opset18\",\n)\n\nsplit = np.array([2, 4]).astype(np.int64)\nnode = onnx.helper.make_node(\n \"Split\", inputs=[\"input\", \"split\"], outputs=[\"output_1\", \"output_2\"]\n)\n\nexpected_outputs = [\n np.array([1.0, 2.0]).astype(np.float32),\n np.array([3.0, 4.0, 5.0, 6.0]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_variable_parts_default_axis_opset18\",\n)" }, { "summary": "zero_size_splits_opset13", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset13\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 13)],\n)" }, { "summary": "zero_size_splits_opset18", "code": "# 1-dimensional tensor with dimension_size=0\nnode_input = np.array([]).astype(np.float32)\n\n# Split empty 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 = [\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n np.array([]).astype(np.float32),\n]\nexpect(\n node,\n inputs=[node_input, split],\n outputs=expected_outputs,\n name=\"test_split_zero_size_splits_opset18\",\n)" } ], "category": "Tensor" }, { "name": "SplitToSequence", "module": "ai.onnx", "version": 11, "description": "Split a tensor into a sequence of tensors, along the specified 'axis'.\nLengths of the parts can be specified using the optional argument 'split'.\nIf the argument `split' is not specified, a default scalar value of 1\nis used as the value of `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 chunks all of size 'split'\nif possible. The last chunk alone may be smaller than 'split' if the 'input' size\nalong the given axis 'axis' is not divisible by 'split'.\nIf 'split' is a 1-dimensional tensor, the input tensor is split into 'size(split)' chunks,\nwith lengths of the parts on 'axis' specified in 'split'. In this scenario, the sum of entries\nin 'split' must be equal to the dimension 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))" ] } ], "examples": [ { "summary": "nokeepdims", "code": "data = np.arange(18).reshape((3, 6)).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"SplitToSequence\",\n [\"data\"],\n [\"seq\"],\n axis=1,\n keepdims=0,\n)\n\nexpected_outputs = [[data[:, i] for i in range(data.shape[1])]]\n\nexpect(\n node,\n inputs=[data],\n outputs=expected_outputs,\n name=\"test_split_to_sequence_nokeepdims\",\n)" }, { "summary": "with_split_1", "code": "data = np.arange(18).reshape((3, 6)).astype(np.float32)\nsplit = np.array(2, dtype=np.int64)\n\nnode = onnx.helper.make_node(\n \"SplitToSequence\", [\"data\", \"split\"], [\"seq\"], axis=1\n)\n\nexpected_outputs = [\n [\n np.array([[0.0, 1.0], [6.0, 7.0], [12.0, 13.0]], dtype=np.float32),\n np.array([[2.0, 3.0], [8.0, 9.0], [14.0, 15.0]], dtype=np.float32),\n np.array([[4.0, 5.0], [10.0, 11.0], [16.0, 17.0]], dtype=np.float32),\n ]\n]\n\nexpect(\n node,\n inputs=[data, split],\n outputs=expected_outputs,\n name=\"test_split_to_sequence_1\",\n)" }, { "summary": "with_split_2", "code": "data = np.arange(18).reshape((3, 6)).astype(np.float32)\nsplit = np.array([1, 2], dtype=np.int64)\n\nnode = onnx.helper.make_node(\n \"SplitToSequence\", [\"data\", \"split\"], [\"seq\"], axis=0\n)\n\nexpected_outputs = [\n [\n data[:1],\n data[1:],\n ]\n]\n\nexpect(\n node,\n inputs=[data, split],\n outputs=expected_outputs,\n name=\"test_split_to_sequence_2\",\n)" } ] }, { "name": "SplitToSequence", "module": "ai.onnx", "version": 24, "description": "Split a tensor into a sequence of tensors, along the specified 'axis'.\nLengths of the parts can be specified using the optional argument 'split'.\nIf the argument `split' is not specified, a default scalar value of 1\nis used as the value of `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 chunks all of size 'split'\nif possible. The last chunk alone may be smaller than 'split' if the 'input' size\nalong the given axis 'axis' is not divisible by 'split'.\nIf 'split' is a 1-dimensional tensor, the input tensor is split into 'size(split)' chunks,\nwith lengths of the parts on 'axis' specified in 'split'. In this scenario, the sum of entries\nin 'split' must be equal to the dimension 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(bfloat16)", "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(bfloat16))", "seq(tensor(float16))", "seq(tensor(float))", "seq(tensor(double))", "seq(tensor(string))", "seq(tensor(bool))", "seq(tensor(complex64))", "seq(tensor(complex128))" ] } ], "examples": [ { "summary": "nokeepdims", "code": "data = np.arange(18).reshape((3, 6)).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"SplitToSequence\",\n [\"data\"],\n [\"seq\"],\n axis=1,\n keepdims=0,\n)\n\nexpected_outputs = [[data[:, i] for i in range(data.shape[1])]]\n\nexpect(\n node,\n inputs=[data],\n outputs=expected_outputs,\n name=\"test_split_to_sequence_nokeepdims\",\n)" }, { "summary": "with_split_1", "code": "data = np.arange(18).reshape((3, 6)).astype(np.float32)\nsplit = np.array(2, dtype=np.int64)\n\nnode = onnx.helper.make_node(\n \"SplitToSequence\", [\"data\", \"split\"], [\"seq\"], axis=1\n)\n\nexpected_outputs = [\n [\n np.array([[0.0, 1.0], [6.0, 7.0], [12.0, 13.0]], dtype=np.float32),\n np.array([[2.0, 3.0], [8.0, 9.0], [14.0, 15.0]], dtype=np.float32),\n np.array([[4.0, 5.0], [10.0, 11.0], [16.0, 17.0]], dtype=np.float32),\n ]\n]\n\nexpect(\n node,\n inputs=[data, split],\n outputs=expected_outputs,\n name=\"test_split_to_sequence_1\",\n)" }, { "summary": "with_split_2", "code": "data = np.arange(18).reshape((3, 6)).astype(np.float32)\nsplit = np.array([1, 2], dtype=np.int64)\n\nnode = onnx.helper.make_node(\n \"SplitToSequence\", [\"data\", \"split\"], [\"seq\"], axis=0\n)\n\nexpected_outputs = [\n [\n data[:1],\n data[1:],\n ]\n]\n\nexpect(\n node,\n inputs=[data, split],\n outputs=expected_outputs,\n name=\"test_split_to_sequence_2\",\n)" } ] }, { "name": "Sqrt", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_sqrt\")" } ] }, { "name": "Sqrt", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_sqrt\")" } ] }, { "name": "Sqrt", "module": "ai.onnx", "version": 13, "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], 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], name=\"test_sqrt\")" } ] }, { "name": "Squeeze", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_squeeze_negative_axes\")" } ], "category": "Transform" }, { "name": "Squeeze", "module": "ai.onnx", "version": 11, "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], 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], name=\"test_squeeze_negative_axes\")" } ], "category": "Transform" }, { "name": "Squeeze", "module": "ai.onnx", "version": 13, "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], 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], name=\"test_squeeze_negative_axes\")" } ], "category": "Transform" }, { "name": "Squeeze", "module": "ai.onnx", "version": 21, "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 up to IRv10.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "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], 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], name=\"test_squeeze_negative_axes\")" } ], "category": "Transform" }, { "name": "Squeeze", "module": "ai.onnx", "version": 23, "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": "1D tensor 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 up to IRv11.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "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], 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], name=\"test_squeeze_negative_axes\")" } ], "category": "Transform" }, { "name": "Squeeze", "module": "ai.onnx", "version": 24, "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": "1D tensor 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 up to IRv12.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "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], 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], name=\"test_squeeze_negative_axes\")" } ], "category": "Transform" }, { "name": "Squeeze", "module": "ai.onnx", "version": 25, "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": "1D tensor 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 up to IRv13.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "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], 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], name=\"test_squeeze_negative_axes\")" } ], "category": "Transform" }, { "name": "StringConcat", "module": "ai.onnx", "version": 20, "description": "StringConcat concatenates string tensors elementwise (with NumPy-style broadcasting support)", "inputs": [ { "name": "X", "type": "T", "description": "Tensor to prepend in concatenation" }, { "name": "Y", "type": "T", "description": "Tensor to append in concatenation" } ], "min_input": 2, "max_input": 2, "outputs": [ { "name": "Z", "type": "T", "description": "Concatenated string tensor" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Inputs and outputs must be UTF-8 strings", "type_param_str": "T", "allowed_type_strs": [ "tensor(string)" ] } ], "examples": [ { "summary": "stringconcat", "code": "node = onnx.helper.make_node(\n \"StringConcat\",\n inputs=[\"x\", \"y\"],\n outputs=[\"result\"],\n)\nx = np.array([\"abc\", \"def\"]).astype(\"object\")\ny = np.array([\".com\", \".net\"]).astype(\"object\")\nresult = np.array([\"abc.com\", \"def.net\"]).astype(\"object\")\n\nexpect(node, inputs=[x, y], outputs=[result], name=\"test_string_concat\")\n\nx = np.array([\"cat\", \"dog\", \"snake\"]).astype(\"object\")\ny = np.array([\"s\"]).astype(\"object\")\nresult = np.array([\"cats\", \"dogs\", \"snakes\"]).astype(\"object\")\n\nexpect(\n node,\n inputs=[x, y],\n outputs=[result],\n name=\"test_string_concat_broadcasting\",\n)\n\nx = np.array(\"cat\").astype(\"object\")\ny = np.array(\"s\").astype(\"object\")\nresult = np.array(\"cats\").astype(\"object\")\n\nexpect(\n node,\n inputs=[x, y],\n outputs=[result],\n name=\"test_string_concat_zero_dimensional\",\n)\n\nx = np.array([\"abc\", \"\"]).astype(\"object\")\ny = np.array([\"\", \"abc\"]).astype(\"object\")\nresult = np.array([\"abc\", \"abc\"]).astype(\"object\")\n\nexpect(\n node,\n inputs=[x, y],\n outputs=[result],\n name=\"test_string_concat_empty_string\",\n)\n\nx = np.array([\"\u7684\", \"\u4e2d\"]).astype(\"object\")\ny = np.array([\"\u7684\", \"\u4e2d\"]).astype(\"object\")\nresult = np.array([\"\u7684\u7684\", \"\u4e2d\u4e2d\"]).astype(\"object\")\n\nexpect(\n node,\n inputs=[x, y],\n outputs=[result],\n name=\"test_string_concat_utf8\",\n)" } ] }, { "name": "StringNormalizer", "module": "ai.onnx", "version": 10, "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([\"monday\", \"tuesday\", \"wednesday\", \"thursday\"]).astype(object)\noutput = np.array([\"tuesday\", \"wednesday\", \"thursday\"]).astype(object)\nstopwords = [\"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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_strnormalizer_export_monday_casesensintive_lower\",\n)" }, { "summary": "monday_casesensintive_nochangecase", "code": "input = np.array([\"monday\", \"tuesday\", \"wednesday\", \"thursday\"]).astype(object)\noutput = np.array([\"tuesday\", \"wednesday\", \"thursday\"]).astype(object)\nstopwords = [\"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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_strnormalizer_export_monday_casesensintive_nochangecase\",\n)" }, { "summary": "monday_casesensintive_upper", "code": "input = np.array([\"monday\", \"tuesday\", \"wednesday\", \"thursday\"]).astype(object)\noutput = np.array([\"TUESDAY\", \"WEDNESDAY\", \"THURSDAY\"]).astype(object)\nstopwords = [\"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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_strnormalizer_export_monday_casesensintive_upper\",\n)" }, { "summary": "monday_empty_output", "code": "input = np.array([\"monday\", \"monday\"]).astype(object)\noutput = np.array([\"\"]).astype(object)\nstopwords = [\"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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_strnormalizer_export_monday_empty_output\",\n)" }, { "summary": "monday_insensintive_upper_twodim", "code": "input = (\n np.array(\n [\"Monday\", \"tuesday\", \"wednesday\", \"Monday\", \"tuesday\", \"wednesday\"]\n )\n .astype(object)\n .reshape([1, 6])\n)\n\n# It does upper case cecedille, accented E\n# and german umlaut but fails\n# with german eszett\noutput = (\n np.array([\"TUESDAY\", \"WEDNESDAY\", \"TUESDAY\", \"WEDNESDAY\"])\n .astype(object)\n .reshape([1, 4])\n)\nstopwords = [\"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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_strnormalizer_export_monday_insensintive_upper_twodim\",\n)" }, { "summary": "nostopwords_nochangecase", "code": "input = np.array([\"monday\", \"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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_strnormalizer_nostopwords_nochangecase\",\n)" } ] }, { "name": "StringSplit", "module": "ai.onnx", "version": 20, "description": "StringSplit splits a string tensor's elements into substrings based on a delimiter attribute and a maxsplit attribute.\n\nThe first output of this operator is a tensor of strings representing the substrings from splitting each input string on the `delimiter` substring. This tensor has one additional rank compared to the input tensor in order to store the substrings for each input element (where the input tensor is not empty). Note that, in order to ensure the same number of elements are present in the final dimension, this tensor will pad empty strings as illustrated in the examples below. Consecutive delimiters are not grouped together and are deemed to delimit empty strings, except if the `delimiter` is unspecified or is the empty string (\"\"). In the case where the `delimiter` is unspecified or the empty string, consecutive whitespace characters are regarded as a single separator and leading or trailing whitespace is removed in the output.\n\nThe second output tensor represents the number of substrings generated. `maxsplit` can be used to limit the number of splits performed - after the `maxsplit`th split if the string is not fully split, the trailing suffix of input string after the final split point is also added. For elements where fewer splits are possible than specified in `maxsplit`, it has no effect.", "attributes": [ { "name": "delimiter", "type": "string", "required": false, "description": "Delimiter to split on. If left unset or set to the empty string (\"\"), the input is split on consecutive whitespace." }, { "name": "maxsplit", "type": "int64", "required": false, "description": "Maximum number of splits (from left to right). If left unset (or if the number of possible splits are less than maxsplit), it will make as many splits as possible. Note that the maximum possible number of substrings returned with `maxsplit` specified is `maxsplit+1` since the remaining suffix after the `maxsplit`th split is included in the output." } ], "inputs": [ { "name": "X", "type": "T1", "description": "Tensor of strings to split." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T2", "description": "Tensor of substrings representing the outcome of splitting the strings in the input on the delimiter. Note that to ensure the same number of elements are present in the final rank, this tensor will pad any necessary empty strings." }, { "name": "Z", "type": "T3", "description": "The number of substrings generated for each input element." } ], "min_output": 2, "max_output": 2, "type_constraints": [ { "description": "The input must be a UTF-8 string tensor", "type_param_str": "T1", "allowed_type_strs": [ "tensor(string)" ] }, { "description": "Tensor of substrings.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(string)" ] }, { "description": "The number of substrings generated.", "type_param_str": "T3", "allowed_type_strs": [ "tensor(int64)" ] } ], "examples": [ { "summary": "basic", "code": "node = onnx.helper.make_node(\n \"StringSplit\",\n inputs=[\"x\"],\n outputs=[\"substrings\", \"length\"],\n delimiter=\".\",\n maxsplit=None,\n)\n\nx = np.array([\"abc.com\", \"def.net\"]).astype(object)\n\nsubstrings = np.array([[\"abc\", \"com\"], [\"def\", \"net\"]]).astype(object)\n\nlength = np.array([2, 2], dtype=np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[substrings, length],\n name=\"test_string_split_basic\",\n)" }, { "summary": "consecutive_delimiters", "code": "node = onnx.helper.make_node(\n \"StringSplit\",\n inputs=[\"x\"],\n outputs=[\"substrings\", \"length\"],\n delimiter=\"-\",\n maxsplit=None,\n)\n\nx = np.array([\"o-n-n--x-\", \"o-n----nx\"]).astype(object)\n\nsubstrings = np.array(\n [[\"o\", \"n\", \"n\", \"\", \"x\", \"\"], [\"o\", \"n\", \"\", \"\", \"\", \"nx\"]]\n).astype(object)\n\nlength = np.array([6, 6], dtype=np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[substrings, length],\n name=\"test_string_split_consecutive_delimiters\",\n)" }, { "summary": "empty_string_delimiter", "code": "for delimiter, test_name in (\n (\"\", \"test_string_split_empty_string_delimiter\"),\n (None, \"test_string_split_no_delimiter\"),\n):\n node = onnx.helper.make_node(\n \"StringSplit\",\n inputs=[\"x\"],\n outputs=[\"substrings\", \"length\"],\n delimiter=delimiter,\n maxsplit=None,\n )\n\n x = np.array(\n [\"hello world !\", \" hello world !\", \" hello world ! \"]\n ).astype(object)\n\n substrings = np.array(\n [\n [\"hello\", \"world\", \"!\"],\n [\"hello\", \"world\", \"!\"],\n [\"hello\", \"world\", \"!\"],\n ]\n ).astype(object)\n\n length = np.array([3, 3, 3], dtype=np.int64)\n\n expect(\n node,\n inputs=[x],\n outputs=[substrings, length],\n name=test_name,\n )" }, { "summary": "empty_string_split", "code": "node = onnx.helper.make_node(\n \"StringSplit\",\n inputs=[\"x\"],\n outputs=[\"substrings\", \"length\"],\n delimiter=None,\n maxsplit=None,\n)\n\nx = np.array([]).astype(object)\n\nsubstrings = np.array([]).astype(object).reshape(0, 0)\n\nlength = np.array([], dtype=np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[substrings, length],\n name=\"test_string_split_empty_tensor\",\n output_type_protos=[\n onnx.helper.make_tensor_type_proto(onnx.TensorProto.STRING, (0, None)),\n None,\n ],\n)" }, { "summary": "maxsplit", "code": "node = onnx.helper.make_node(\n \"StringSplit\",\n inputs=[\"x\"],\n outputs=[\"substrings\", \"length\"],\n maxsplit=2,\n)\n\nx = np.array(\n [[\"hello world\", \"def.net\"], [\"o n n x\", \"the quick brown fox\"]]\n).astype(object)\n\nsubstrings = np.array(\n [\n [[\"hello\", \"world\", \"\"], [\"def.net\", \"\", \"\"]],\n [[\"o\", \"n\", \"n x\"], [\"the\", \"quick\", \"brown fox\"]],\n ]\n).astype(object)\n\nlength = np.array([[2, 1], [3, 3]], np.int64)\n\nexpect(\n node,\n inputs=[x],\n outputs=[substrings, length],\n name=\"test_string_split_maxsplit\",\n)" } ] }, { "name": "Sub", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_sub\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int16\")\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], name=\"test_sub_uint8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint16\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint32\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint64)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint64\")" }, { "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], name=\"test_sub_bcast\")" } ] }, { "name": "Sub", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_sub\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int16\")\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], name=\"test_sub_uint8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint16\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint32\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint64)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint64\")" }, { "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], name=\"test_sub_bcast\")" } ] }, { "name": "Sub", "module": "ai.onnx", "version": 7, "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], 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], name=\"test_sub\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int16\")\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], name=\"test_sub_uint8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint16\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint32\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint64)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint64\")" }, { "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], name=\"test_sub_bcast\")" } ] }, { "name": "Sub", "module": "ai.onnx", "version": 13, "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], 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], name=\"test_sub\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int16\")\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], name=\"test_sub_uint8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint16\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint32\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint64)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint64\")" }, { "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], name=\"test_sub_bcast\")" } ] }, { "name": "Sub", "module": "ai.onnx", "version": 14, "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], 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], name=\"test_sub\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int8)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int8)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.int16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.int16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_int16\")\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], name=\"test_sub_uint8\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint16)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint16)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint16\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint32)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint32)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint32\")\n\nx = np.random.randint(12, 24, size=(3, 4, 5), dtype=np.uint64)\ny = np.random.randint(12, size=(3, 4, 5), dtype=np.uint64)\nz = x - y\nexpect(node, inputs=[x, y], outputs=[z], name=\"test_sub_uint64\")" }, { "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], name=\"test_sub_bcast\")" } ] }, { "name": "Sum", "module": "ai.onnx", "version": 1, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_sum_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Sum\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_sum_two_inputs\"\n)" } ] }, { "name": "Sum", "module": "ai.onnx", "version": 6, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_sum_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Sum\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_sum_two_inputs\"\n)" } ] }, { "name": "Sum", "module": "ai.onnx", "version": 8, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_sum_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Sum\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_sum_two_inputs\"\n)" } ] }, { "name": "Sum", "module": "ai.onnx", "version": 13, "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(\n node,\n inputs=[data_0, data_1, data_2],\n outputs=[result],\n name=\"test_sum_example\",\n)\n\nnode = onnx.helper.make_node(\n \"Sum\",\n inputs=[\"data_0\"],\n outputs=[\"result\"],\n)\nexpect(node, inputs=[data_0], outputs=[data_0], 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(\n node, inputs=[data_0, data_1], outputs=[result], name=\"test_sum_two_inputs\"\n)" } ] }, { "name": "Swish", "module": "ai.onnx", "version": 24, "description": "Swish function takes one input data (Tensor) and produces one output data (Tensor) of the same shape,\nwhere $Swish(x) = x * sigmoid(alpha * x)$.\n", "attributes": [ { "name": "alpha", "type": "float32", "required": false, "default": 1.0, "description": "Coefficient to multiply with input before sigmoid." } ], "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(bfloat16)", "tensor(double)" ] } ], "examples": [ { "summary": "swish", "code": "node = onnx.helper.make_node(\n \"Swish\",\n inputs=[\"x\"],\n outputs=[\"y\"],\n alpha=1.0, # pass alpha as attribute\n)\n\nx = np.array([3, 4, 5], dtype=np.float32)\ny = swish(x, alpha=1.0)\n\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_swish\",\n opset_imports=[onnx.helper.make_opsetid(\"\", 24)],\n)" } ] }, { "name": "Tan", "module": "ai.onnx", "version": 7, "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], 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], name=\"test_tan\")" } ] }, { "name": "Tan", "module": "ai.onnx", "version": 22, "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(bfloat16)", "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], 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], name=\"test_tan\")" } ] }, { "name": "Tanh", "module": "ai.onnx", "version": 1, "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], 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], name=\"test_tanh\")" } ], "category": "Activation" }, { "name": "Tanh", "module": "ai.onnx", "version": 6, "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], 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], name=\"test_tanh\")" } ], "category": "Activation" }, { "name": "Tanh", "module": "ai.onnx", "version": 13, "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(bfloat16)", "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], 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], name=\"test_tanh\")" } ], "category": "Activation" }, { "name": "TensorScatter", "module": "ai.onnx", "version": 24, "description": "TensorScatter is a generic tensor update operation, motivated by the requirements for KV cache updates for Attention\nops commonly found in LLMs. It is a functional operation that models an in-place update to a KV cache buffer.\n\nThe past and present cache tensors have the same shape (batch_size, D1, D2, ..., max_sequence_length, ..., Dn), with\nthe sequence dimension (indicated by the `axis` attribute) being max_sequence_length, so the sizes of these tensors do\nnot need to grow between iterations. The `update` tensor's shape only differs from the cache tensors in the sequence\ndimension: (batch_size, D1, D2, ..., sequence_length, ..., Dn), where sequence_length <= max_sequence_length.\n\nThe optional `write_indices` input indicates the write index for each sample in the batch, assumed to be zero\nif not provided. When the `mode` attribute is set to \"circular\", the write index is modulo max_sequence_length.\nThe operation can be described using the following pseudocode:\n\n```\nfor prefix_idx in np.ndindex(past_cache.shape[:axis]):\n batch_idx = prefix_idx[0]\n for sequence_idx in range(sequence_length):\n cache_idx = (*prefix_idx, write_indices[batch_idx] + sequence_idx)\n if mode == \"circular\":\n cache_idx = tuple(np.mod(np.asarray(cache_idx), max_sequence_length))\n update_idx = (*prefix_idx, sequence_idx)\n present_cache[cache_idx] = update[update_idx]\n```\n\nDuring the prefill phase of attention, only the first two inputs are needed. During the decode phase, `write_indices`\nis also needed so that the incoming key or value update can be appended after the last valid token for each sample\nin the batch.\n", "attributes": [ { "name": "axis", "type": "int64", "required": false, "default": -2, "description": "Sequence dimension of the `past_cache` and `update` tensors. It cannot be 0 (the batch dimension). Default is -2." }, { "name": "mode", "type": "string", "required": false, "default": "linear", "description": "Write mode of cache update. Supported modes include `linear` and `circular`. `linear` mode requires write_indices+sequence_length<=max_sequence_length. For `circular` mode, the updates happen in wrap-around fashion, ie, the update index is modulo `max_sequence_length`" } ], "inputs": [ { "name": "past_cache", "type": "T", "description": "Past state cache for key or value with shape `(batch_size, D1, D2, ..., max_sequence_length, ..., Dn)`." }, { "name": "update", "type": "T", "description": "New update tensor with shape `(batch_size, D1, D2, ..., sequence_length, ..., Dn)`." }, { "name": "write_indices", "type": "tensor(int64)", "option": "optional", "description": "Write indices for the incoming update tensor in the cache. Shape is `(batch_size,)`. Assumed to be all zeros if not provided." } ], "min_input": 2, "max_input": 3, "outputs": [ { "name": "present_cache", "type": "T", "description": "Updated cache. Same shape as `past_cache`." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 3", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "examples": [ { "summary": "tensorscatter", "code": "node = onnx.helper.make_node(\n \"TensorScatter\",\n inputs=[\"past_cache\", \"update\", \"write_indices\"],\n outputs=[\"present_cache\"],\n mode=\"linear\",\n)\npast_cache = np.array(\n [\n [[[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], [8, 7, 6, 5, 4], [4, 3, 2, 1, 0]]],\n [[[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], [8, 7, 6, 5, 4], [4, 3, 2, 1, 0]]],\n ],\n dtype=np.float32,\n)\nupdate = np.array(\n [\n [[[5, 5, 5, 5, 5]]],\n [[[1, 1, 1, 1, 1]]],\n ],\n dtype=np.float32,\n)\nwrite_indices = np.array([1, 2], dtype=np.int64)\npresent_cache = np.array(\n [\n [[[1, 2, 3, 4, 5], [5, 5, 5, 5, 5], [8, 7, 6, 5, 4], [4, 3, 2, 1, 0]]],\n [[[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], [1, 1, 1, 1, 1], [4, 3, 2, 1, 0]]],\n ],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[past_cache, update, write_indices],\n outputs=[present_cache],\n name=\"test_tensorscatter\",\n)" }, { "summary": "tensorscatter_3d", "code": "node = onnx.helper.make_node(\n \"TensorScatter\",\n inputs=[\"past_cache\", \"update\", \"write_indices\"],\n outputs=[\"present_cache\"],\n)\npast_cache = np.array(\n [\n [\n [1, 2, 3, 4, 5],\n [5, 6, 7, 8, 9],\n [8, 7, 6, 5, 4],\n [5, 4, 3, 2, 1],\n ],\n [\n [1, 2, 3, 4, 5],\n [5, 6, 7, 8, 9],\n [8, 7, 6, 5, 4],\n [5, 4, 3, 2, 1],\n ],\n [\n [1, 2, 3, 4, 5],\n [5, 6, 7, 8, 9],\n [8, 7, 6, 5, 4],\n [5, 4, 3, 2, 1],\n ],\n ],\n dtype=np.float32,\n)\nupdate = np.array(\n [\n [\n [4, 4, 4, 4, 4],\n [5, 5, 5, 5, 5],\n ],\n [\n [6, 6, 6, 6, 6],\n [7, 7, 7, 7, 7],\n ],\n [\n [2, 2, 2, 2, 2],\n [3, 3, 3, 3, 3],\n ],\n ],\n dtype=np.float32,\n)\nwrite_indices = np.array([1, 2, 0], dtype=np.int64)\npresent_cache = np.array(\n [\n [\n [1, 2, 3, 4, 5],\n [4, 4, 4, 4, 4],\n [5, 5, 5, 5, 5],\n [5, 4, 3, 2, 1],\n ],\n [\n [1, 2, 3, 4, 5],\n [5, 6, 7, 8, 9],\n [6, 6, 6, 6, 6],\n [7, 7, 7, 7, 7],\n ],\n [\n [2, 2, 2, 2, 2],\n [3, 3, 3, 3, 3],\n [8, 7, 6, 5, 4],\n [5, 4, 3, 2, 1],\n ],\n ],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[past_cache, update, write_indices],\n outputs=[present_cache],\n name=\"test_tensorscatter_3d\",\n)" }, { "summary": "tensorscatter_circular", "code": "node = onnx.helper.make_node(\n \"TensorScatter\",\n inputs=[\"past_cache\", \"update\", \"write_indices\"],\n outputs=[\"present_cache\"],\n mode=\"circular\",\n)\npast_cache = np.array(\n [\n [[[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], [8, 7, 6, 5, 4], [4, 3, 2, 1, 0]]],\n [[[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], [8, 7, 6, 5, 4], [4, 3, 2, 1, 0]]],\n ],\n dtype=np.float32,\n)\nupdate = np.array(\n [\n [\n [\n [5, 5, 5, 5, 5],\n [6, 6, 6, 6, 6],\n ]\n ],\n [\n [\n [1, 1, 1, 1, 1],\n [2, 2, 2, 2, 2],\n ]\n ],\n ],\n dtype=np.float32,\n)\nwrite_indices = np.array([1, 3], dtype=np.int64)\npresent_cache = np.array(\n [\n [[[1, 2, 3, 4, 5], [5, 5, 5, 5, 5], [6, 6, 6, 6, 6], [4, 3, 2, 1, 0]]],\n [[[2, 2, 2, 2, 2], [5, 6, 7, 8, 9], [8, 7, 6, 5, 4], [1, 1, 1, 1, 1]]],\n ],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[past_cache, update, write_indices],\n outputs=[present_cache],\n name=\"test_tensorscatter_circular\",\n)" } ] }, { "name": "TfIdfVectorizer", "module": "ai.onnx", "version": 9, "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 either 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(\n [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0]]\n).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, 5, 6, 7, 8, 6, 7]).astype( # unigrams\n np.int64\n) # 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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_tfidfvectorizer_tf_batch_onlybigrams_skip0\",\n)" }, { "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(\n [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0]]\n).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, 5, 6, 7, 8, 6, 7]).astype( # unigrams\n np.int64\n) # 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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_tfidfvectorizer_tf_batch_onlybigrams_skip5\",\n)" }, { "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(\n [[0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0]]\n).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, 5, 6, 7, 8, 6, 7]).astype( # unigrams\n np.int64\n) # 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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_tfidfvectorizer_tf_batch_uniandbigrams_skip5\",\n)" }, { "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, 0.0, 0.0, 1.0, 1.0, 1.0]).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, 5, 6, 7, 8, 6, 7]).astype( # unigrams\n np.int64\n) # 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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_tfidfvectorizer_tf_only_bigrams_skip0\",\n)" }, { "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.0, 1.0, 1.0]).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([5, 6, 7, 8, 6, 7]).astype( # unigrams none\n np.int64\n) # 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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_tfidfvectorizer_tf_onlybigrams_levelempty\",\n)" }, { "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, 0.0, 0.0, 1.0, 3.0, 1.0]).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, 5, 6, 7, 8, 6, 7]).astype( # unigrams\n np.int64\n) # 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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_tfidfvectorizer_tf_onlybigrams_skip5\",\n)" }, { "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.0, 3.0, 1.0, 0.0, 1.0, 3.0, 1.0]).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, 5, 6, 7, 8, 6, 7]).astype( # unigrams\n np.int64\n) # 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(\n node,\n inputs=[input],\n outputs=[output],\n name=\"test_tfidfvectorizer_tf_uniandbigrams_skip5\",\n)" } ] }, { "name": "ThresholdedRelu", "module": "ai.onnx", "version": 1, "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. ", "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)" ] } ], "category": "Activation" }, { "name": "ThresholdedRelu", "module": "ai.onnx", "version": 10, "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(\"ThresholdedRelu\", inputs=[\"x\"], outputs=[\"y\"])\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], name=\"test_thresholdedrelu_default\")" }, { "summary": "thresholdedrelu", "code": "alpha = 2.0\nnode = onnx.helper.make_node(\n \"ThresholdedRelu\", inputs=[\"x\"], outputs=[\"y\"], alpha=alpha\n)\n\nx = np.array([-1.5, 0.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], 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], name=\"test_thresholdedrelu\")" } ], "category": "Activation" }, { "name": "ThresholdedRelu", "module": "ai.onnx", "version": 22, "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(bfloat16)", "tensor(float16)", "tensor(float)", "tensor(double)" ] } ], "examples": [ { "summary": "default", "code": "default_alpha = 1.0\nnode = onnx.helper.make_node(\"ThresholdedRelu\", inputs=[\"x\"], outputs=[\"y\"])\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], name=\"test_thresholdedrelu_default\")" }, { "summary": "thresholdedrelu", "code": "alpha = 2.0\nnode = onnx.helper.make_node(\n \"ThresholdedRelu\", inputs=[\"x\"], outputs=[\"y\"], alpha=alpha\n)\n\nx = np.array([-1.5, 0.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], 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], name=\"test_thresholdedrelu\")" } ], "category": "Activation" }, { "name": "Tile", "module": "ai.onnx", "version": 1, "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(\"Tile\", inputs=[\"x\", \"y\"], outputs=[\"z\"])\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, inputs=[x, repeats], outputs=[z], name=\"test_tile\")" }, { "summary": "tile_precomputed", "code": "node = onnx.helper.make_node(\"Tile\", inputs=[\"x\", \"y\"], outputs=[\"z\"])\n\nx = np.array([[0, 1], [2, 3]], dtype=np.float32)\n\nrepeats = np.array([2, 2], dtype=np.int64)\n\nz = np.array(\n [[0, 1, 0, 1], [2, 3, 2, 3], [0, 1, 0, 1], [2, 3, 2, 3]], dtype=np.float32\n)\n\nexpect(node, inputs=[x, repeats], outputs=[z], name=\"test_tile_precomputed\")" } ], "category": "Shape" }, { "name": "Tile", "module": "ai.onnx", "version": 6, "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(\"Tile\", inputs=[\"x\", \"y\"], outputs=[\"z\"])\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, inputs=[x, repeats], outputs=[z], name=\"test_tile\")" }, { "summary": "tile_precomputed", "code": "node = onnx.helper.make_node(\"Tile\", inputs=[\"x\", \"y\"], outputs=[\"z\"])\n\nx = np.array([[0, 1], [2, 3]], dtype=np.float32)\n\nrepeats = np.array([2, 2], dtype=np.int64)\n\nz = np.array(\n [[0, 1, 0, 1], [2, 3, 2, 3], [0, 1, 0, 1], [2, 3, 2, 3]], dtype=np.float32\n)\n\nexpect(node, inputs=[x, repeats], outputs=[z], name=\"test_tile_precomputed\")" } ], "category": "Shape" }, { "name": "Tile", "module": "ai.onnx", "version": 13, "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(\"Tile\", inputs=[\"x\", \"y\"], outputs=[\"z\"])\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, inputs=[x, repeats], outputs=[z], name=\"test_tile\")" }, { "summary": "tile_precomputed", "code": "node = onnx.helper.make_node(\"Tile\", inputs=[\"x\", \"y\"], outputs=[\"z\"])\n\nx = np.array([[0, 1], [2, 3]], dtype=np.float32)\n\nrepeats = np.array([2, 2], dtype=np.int64)\n\nz = np.array(\n [[0, 1, 0, 1], [2, 3, 2, 3], [0, 1, 0, 1], [2, 3, 2, 3]], dtype=np.float32\n)\n\nexpect(node, inputs=[x, repeats], outputs=[z], name=\"test_tile_precomputed\")" } ], "category": "Shape" }, { "name": "Tokenizer", "module": "com.microsoft", "version": 1, "description": "Tokenizer divides each string in X into a vector of strings along the last axis. Allowed input shapes are [C] and [N, C].\n If the maximum number of tokens found per input string is D, the output shape would be [N, C, D] when input shape is [N, C].\n Similarly, if input shape is [C] then the output should be [C, D]. Tokenizer has two different operation modes.\n The first mode is selected when \"tokenexp\" is not set and \"separators\" is set. If \"tokenexp\" is set and \"separators\" is not set,\n the second mode will be used. The first mode breaks each input string into tokens by matching and removing separators.\n \"separators\" is a list of strings which are RE2 regular expressions. \"tokenexp\" is a single RE2 regular expression.\n Let's assume \"separators\" is [\" \"] and consider an example.\n If input is\n [\"Hello World\", \"I love computer science !\"] whose shape is [2],\n then the output would be\n [[\"Hello\", \"World\", padvalue, padvalue, padvalue],\n [\"I\", \"love\", \"computer\", \"science\", \"!\"]]\n whose shape is [2, 5] because you can find at most 5 tokens per input string.\n Note that the input at most can have two axes, so 3-D and higher dimension are not supported.\n If \"separators\" contains a single empty string, the Tokenizer will enter into character tokenization mode. This means all strings\n will be broken apart into individual characters.\n Similarly, if \"tokenexp\" is set to \".\" (match any single character), character tokenization mode is used.\n For each input string, the second mode searches matches of \"tokenexp\" and each match will be a token in Y.\n The matching of \"tokenexp\" is conducted greedily (i.e., a match should be as long as possible).\n This operator searches for the first match starting from the beginning of the considered string,\n and then launches another search starting from the first remained character after the first matched token.\n If no match found, this operator will remove the first character from the remained string and do another search.\n This procedure will be repeated until reaching the end of the considered string.\n Let's consider another example to illustrate the effect of setting \"mark\" to true.\n If input is [\"Hello\", \"World\"],\n then the corresponding output would be [0x02, \"Hello\", \"World\", 0x03].\n This implies that if mark is true, [C]/[N, C] - input's output shape becomes [C, D+2]/[N, C, D+2].\nIf tokenizer removes the entire content of [C]-input, it will produce [[]].\nI.e. the output shape should be [C][0] or [N][C][0] if input shape was [N][C].\nIf the tokenizer receives empty input of [0] then the output is [0] if empty input\nof [N, 0] then [N, 0].\n", "attributes": [ { "name": "mark", "type": "int64", "required": true, "description": "Boolean whether to mark the beginning/end character with start of text character (0x02)/end of text character (0x03)." }, { "name": "mincharnum", "type": "int64", "required": true, "description": "Minimum number of characters allowed in the output. For example, if mincharnum is 2, tokens such as \"A\" and \"B\" would be ignored" }, { "name": "pad_value", "type": "string", "required": true, "description": "The string used to pad output tensors when the tokens extracted doesn't match the maximum number of tokens found. If start/end markers are needed, padding will appear outside the markers." }, { "name": "separators", "type": "string[]", "required": false, "description": "an optional list of strings attribute that contains a list of separators - RE2 regular expressions to match separators. Two consecutive segments in X connected by a separator would be divided into two tokens. For example, if the input is \"Hello World!\" and this attribute contains only one space character, the corresponding output would be [\"Hello\", \"World!\"]. To achieve character-level tokenization, one should set the 'separators' to [\"\"], which contains an empty string." }, { "name": "tokenexp", "type": "string", "required": false, "description": "An optional string. Token's regular expression in RE2 format (https://github.com/google/re2/wiki/Syntax). If set, tokenizer may produce tokens matching the specified pattern. Note that one and only one of 'tokenexp' and 'separators' should be set. If tokenexp is \".\", the tokenizer enters character tokenization mode." } ], "inputs": [ { "name": "X", "type": "T", "description": "Strings to tokenize" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "Tokenized strings" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Input/Output is a string tensor", "type_param_str": "T", "allowed_type_strs": [ "tensor(string)" ] } ] }, { "name": "TopK", "module": "ai.onnx", "version": 1, "description": "Retrieve the top-K elements along a specified axis. Given an input tensor of\nshape [a_0, a_1, ..., a_{n-1}] and integer argument k, return two outputs:\n -Value tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}]\n which contains the values of the top k elements along the specified axis\n -Index tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] 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_0, a_1, ..., a_{n-1}]" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Values", "type": "T", "description": "Tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] containing top K values from the input tensor" }, { "name": "Indices", "type": "I", "description": "Tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] 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\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.float32,\n)\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(\n node, inputs=[X, K], outputs=[values_ref, indices_ref], name=\"test_top_k\"\n)" }, { "summary": "top_k_negative_axis", "code": "axis = -1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_negative_axis\",\n)" }, { "summary": "top_k_same_values", "code": "axis = 0\nlargest = 0\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [0, 0, 0, 0],\n dtype=np.int64,\n)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n# (Pdb) print(values_ref)\n# [0 0 0]\n# (Pdb) print(indices_ref)\n# [0 1 2]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values\",\n)" }, { "summary": "top_k_same_values_2d", "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 1, 1]],\n dtype=np.int64,\n)\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 0 0]\n# [1 1 1]\n# [1 1 2]]\n# print(indices_ref)\n# [[0 1 2]\n# [0 1 2]\n# [2 3 0]]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values_2d\",\n)" }, { "summary": "top_k_same_values_largest", "code": "axis = 0\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [0, 0, 0, 0],\n dtype=np.int64,\n)\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 0 0]\n# print(indices_ref)\n# [0 1 2]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values_largest\",\n)" }, { "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 [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [11, 10, 9, 8],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_smallest\",\n)" }, { "summary": "top_k_uint64", "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.uint64,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_uint64\",\n)" } ] }, { "name": "TopK", "module": "ai.onnx", "version": 10, "description": "Retrieve the top-K elements along a specified axis. Given an input tensor of\nshape [a_0, a_1, ..., a_{n-1}] and integer argument k, return two outputs:\n -Value tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}]\n which contains the values of the top k elements along the specified axis\n -Index tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] 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_0, a_1, ..., a_{n-1}]" }, { "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_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] containing top K values from the input tensor" }, { "name": "Indices", "type": "I", "description": "Tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] 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\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.float32,\n)\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(\n node, inputs=[X, K], outputs=[values_ref, indices_ref], name=\"test_top_k\"\n)" }, { "summary": "top_k_negative_axis", "code": "axis = -1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_negative_axis\",\n)" }, { "summary": "top_k_same_values", "code": "axis = 0\nlargest = 0\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [0, 0, 0, 0],\n dtype=np.int64,\n)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n# (Pdb) print(values_ref)\n# [0 0 0]\n# (Pdb) print(indices_ref)\n# [0 1 2]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values\",\n)" }, { "summary": "top_k_same_values_2d", "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 1, 1]],\n dtype=np.int64,\n)\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 0 0]\n# [1 1 1]\n# [1 1 2]]\n# print(indices_ref)\n# [[0 1 2]\n# [0 1 2]\n# [2 3 0]]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values_2d\",\n)" }, { "summary": "top_k_same_values_largest", "code": "axis = 0\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [0, 0, 0, 0],\n dtype=np.int64,\n)\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 0 0]\n# print(indices_ref)\n# [0 1 2]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values_largest\",\n)" }, { "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 [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [11, 10, 9, 8],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_smallest\",\n)" }, { "summary": "top_k_uint64", "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.uint64,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_uint64\",\n)" } ] }, { "name": "TopK", "module": "ai.onnx", "version": 11, "description": "Retrieve the top-K largest or smallest elements along a specified axis. Given an input tensor of\nshape [a_0, a_1, ..., a_{n-1}] and integer argument k, return two outputs:\n\n* Value tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}]\n which contains the values of the top k elements along the specified axis\n* Index tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] which\n contains the indices of the top k elements (original indices from the input\n tensor).\n\n* If \"largest\" is 1 (the default value) then the k largest elements are returned.\n* If \"sorted\" is 1 (the default value) then the resulting k elements will be sorted.\n* If \"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\na 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_0, a_1, ..., a_{n-1}]" }, { "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_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] containing top K values from the input tensor" }, { "name": "Indices", "type": "I", "description": "Tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] 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\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.float32,\n)\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(\n node, inputs=[X, K], outputs=[values_ref, indices_ref], name=\"test_top_k\"\n)" }, { "summary": "top_k_negative_axis", "code": "axis = -1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_negative_axis\",\n)" }, { "summary": "top_k_same_values", "code": "axis = 0\nlargest = 0\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [0, 0, 0, 0],\n dtype=np.int64,\n)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n# (Pdb) print(values_ref)\n# [0 0 0]\n# (Pdb) print(indices_ref)\n# [0 1 2]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values\",\n)" }, { "summary": "top_k_same_values_2d", "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 1, 1]],\n dtype=np.int64,\n)\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 0 0]\n# [1 1 1]\n# [1 1 2]]\n# print(indices_ref)\n# [[0 1 2]\n# [0 1 2]\n# [2 3 0]]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values_2d\",\n)" }, { "summary": "top_k_same_values_largest", "code": "axis = 0\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [0, 0, 0, 0],\n dtype=np.int64,\n)\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 0 0]\n# print(indices_ref)\n# [0 1 2]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values_largest\",\n)" }, { "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 [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [11, 10, 9, 8],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_smallest\",\n)" }, { "summary": "top_k_uint64", "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.uint64,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_uint64\",\n)" } ] }, { "name": "TopK", "module": "ai.onnx", "version": 24, "description": "Retrieve the top-K largest or smallest elements along a specified axis. Given an input tensor of\nshape [a_0, a_1, ..., a_{n-1}] and integer argument k, return two outputs:\n\n* Value tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}]\n which contains the values of the top k elements along the specified axis\n* Index tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] which\n contains the indices of the top k elements (original indices from the input\n tensor).\n\n* If \"largest\" is 1 (the default value) then the k largest elements are returned.\n* If \"sorted\" is 1 (the default value) then the resulting k elements will be sorted.\n* If \"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\na 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_0, a_1, ..., a_{n-1}]" }, { "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_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] containing top K values from the input tensor" }, { "name": "Indices", "type": "I", "description": "Tensor of shape [a_0, a_1, ..., a_{axis-1}, k, a_{axis+1}, ... a_{n-1}] 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)", "tensor(bfloat16)" ] }, { "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\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.float32,\n)\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(\n node, inputs=[X, K], outputs=[values_ref, indices_ref], name=\"test_top_k\"\n)" }, { "summary": "top_k_negative_axis", "code": "axis = -1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_negative_axis\",\n)" }, { "summary": "top_k_same_values", "code": "axis = 0\nlargest = 0\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [0, 0, 0, 0],\n dtype=np.int64,\n)\nK = np.array([k], dtype=np.int64)\nvalues_ref, indices_ref = topk_sorted_implementation(X, k, axis, largest)\n\n# (Pdb) print(values_ref)\n# [0 0 0]\n# (Pdb) print(indices_ref)\n# [0 1 2]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values\",\n)" }, { "summary": "top_k_same_values_2d", "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 1, 1]],\n dtype=np.int64,\n)\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 0 0]\n# [1 1 1]\n# [1 1 2]]\n# print(indices_ref)\n# [[0 1 2]\n# [0 1 2]\n# [2 3 0]]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values_2d\",\n)" }, { "summary": "top_k_same_values_largest", "code": "axis = 0\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [0, 0, 0, 0],\n dtype=np.int64,\n)\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 0 0]\n# print(indices_ref)\n# [0 1 2]\n\nexpect(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_same_values_largest\",\n)" }, { "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 [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [11, 10, 9, 8],\n ],\n dtype=np.float32,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_smallest\",\n)" }, { "summary": "top_k_uint64", "code": "axis = 1\nlargest = 1\n\nk = 3\nnode = onnx.helper.make_node(\n \"TopK\", inputs=[\"x\", \"k\"], outputs=[\"values\", \"indices\"], axis=axis\n)\nX = np.array(\n [\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [8, 9, 10, 11],\n ],\n dtype=np.uint64,\n)\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(\n node,\n inputs=[X, K],\n outputs=[values_ref, indices_ref],\n name=\"test_top_k_uint64\",\n)" } ] }, { "name": "TorchEmbedding", "module": "com.microsoft", "version": 1, "description": "Based on Torch operator Embedding, creates a lookup table of embedding vectors of fixed size,\n for a dictionary of fixed size.\n ", "inputs": [ { "name": "weight", "type": "T", "description": "The embedding matrix of size N x M. 'N' is equal to the maximum possible index + 1, and 'M' is equal to the embedding size" }, { "name": "indices", "type": "tensor(int64)", "description": "Long tensor containing the indices to extract from embedding matrix." }, { "name": "padding_idx", "type": "tensor(int64)", "option": "optional", "description": "A 0-D scalar tensor. If specified, the entries at `padding_idx` do not contribute to the gradient; therefore, the embedding vector at `padding_idx` is not updated during training, i.e. it remains as a fixed pad." }, { "name": "scale_grad_by_freq", "type": "tensor(bool)", "option": "optional", "description": "A 0-D bool tensor. If given, this will scale gradients by the inverse of frequency of the indices (words) in the mini-batch. Default is ``False``" } ], "min_input": 2, "max_input": 4, "outputs": [ { "name": "Y", "type": "T", "description": "Output tensor of the same type as the input tensor. Shape of the output is * x M, where '*' is the shape of input indices, and 'M' is the embedding size." } ], "min_output": 1, "max_output": 1, "inputs_range": "2 - 4", "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)", "tensor(uint8)", "tensor(uint16)", "tensor(uint32)", "tensor(uint64)", "tensor(int8)", "tensor(int16)", "tensor(int32)", "tensor(int64)" ] } ] }, { "name": "Transpose", "module": "ai.onnx", "version": 1, "description": "Returns a transpose of the input tensor. (Similar to `numpy.transpose`).\nThe optional attribute `perm` must be a permutation of the dimensions of\nthe input tensor. Axis `i` of the output tensor corresponds to the axis\n`perm[i]` of the input tensor.\nFor example, when perm=(1, 0, 2), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 1, 3).\nWhen perm=(1, 2, 0), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 3, 1).\nIf the attribute `perm` is omitted, its default value is `(n-1, ..., 0)`,\nwhere `n` is the rank of the input tensor.\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, permutation in enumerate(permutations):\n node = onnx.helper.make_node(\n \"Transpose\",\n inputs=[\"data\"],\n outputs=[\"transposed\"],\n perm=permutation,\n )\n transposed = np.transpose(data, permutation)\n expect(\n node,\n inputs=[data],\n outputs=[transposed],\n name=f\"test_transpose_all_permutations_{i}\",\n )" }, { "summary": "default", "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Transpose\", inputs=[\"data\"], outputs=[\"transposed\"]\n)\n\ntransposed = np.transpose(data)\nexpect(node, inputs=[data], outputs=[transposed], name=\"test_transpose_default\")" } ], "category": "Transform" }, { "name": "Transpose", "module": "ai.onnx", "version": 13, "description": "Returns a transpose of the input tensor. (Similar to `numpy.transpose`).\nThe optional attribute `perm` must be a permutation of the dimensions of\nthe input tensor. Axis `i` of the output tensor corresponds to the axis\n`perm[i]` of the input tensor.\nFor example, when perm=(1, 0, 2), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 1, 3).\nWhen perm=(1, 2, 0), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 3, 1).\nIf the attribute `perm` is omitted, its default value is `(n-1, ..., 0)`,\nwhere `n` is the rank of the input tensor.\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, permutation in enumerate(permutations):\n node = onnx.helper.make_node(\n \"Transpose\",\n inputs=[\"data\"],\n outputs=[\"transposed\"],\n perm=permutation,\n )\n transposed = np.transpose(data, permutation)\n expect(\n node,\n inputs=[data],\n outputs=[transposed],\n name=f\"test_transpose_all_permutations_{i}\",\n )" }, { "summary": "default", "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Transpose\", inputs=[\"data\"], outputs=[\"transposed\"]\n)\n\ntransposed = np.transpose(data)\nexpect(node, inputs=[data], outputs=[transposed], name=\"test_transpose_default\")" } ], "category": "Transform" }, { "name": "Transpose", "module": "ai.onnx", "version": 21, "description": "Returns a transpose of the input tensor. (Similar to `numpy.transpose`).\nThe optional attribute `perm` must be a permutation of the dimensions of\nthe input tensor. Axis `i` of the output tensor corresponds to the axis\n`perm[i]` of the input tensor.\nFor example, when perm=(1, 0, 2), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 1, 3).\nWhen perm=(1, 2, 0), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 3, 1).\nIf the attribute `perm` is omitted, its default value is `(n-1, ..., 0)`,\nwhere `n` is the rank of the input tensor.\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. Its length must be equal to the rank of the input." } ], "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "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, permutation in enumerate(permutations):\n node = onnx.helper.make_node(\n \"Transpose\",\n inputs=[\"data\"],\n outputs=[\"transposed\"],\n perm=permutation,\n )\n transposed = np.transpose(data, permutation)\n expect(\n node,\n inputs=[data],\n outputs=[transposed],\n name=f\"test_transpose_all_permutations_{i}\",\n )" }, { "summary": "default", "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Transpose\", inputs=[\"data\"], outputs=[\"transposed\"]\n)\n\ntransposed = np.transpose(data)\nexpect(node, inputs=[data], outputs=[transposed], name=\"test_transpose_default\")" } ], "category": "Transform" }, { "name": "Transpose", "module": "ai.onnx", "version": 23, "description": "Returns a transpose of the input tensor. (Similar to `numpy.transpose`).\nThe optional attribute `perm` must be a permutation of the dimensions of\nthe input tensor. Axis `i` of the output tensor corresponds to the axis\n`perm[i]` of the input tensor.\nFor example, when perm=(1, 0, 2), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 1, 3).\nWhen perm=(1, 2, 0), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 3, 1).\nIf the attribute `perm` is omitted, its default value is `(n-1, ..., 0)`,\nwhere `n` is the rank of the input tensor.\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. Its length must be equal to the rank of the input." } ], "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "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, permutation in enumerate(permutations):\n node = onnx.helper.make_node(\n \"Transpose\",\n inputs=[\"data\"],\n outputs=[\"transposed\"],\n perm=permutation,\n )\n transposed = np.transpose(data, permutation)\n expect(\n node,\n inputs=[data],\n outputs=[transposed],\n name=f\"test_transpose_all_permutations_{i}\",\n )" }, { "summary": "default", "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Transpose\", inputs=[\"data\"], outputs=[\"transposed\"]\n)\n\ntransposed = np.transpose(data)\nexpect(node, inputs=[data], outputs=[transposed], name=\"test_transpose_default\")" } ], "category": "Transform" }, { "name": "Transpose", "module": "ai.onnx", "version": 24, "description": "Returns a transpose of the input tensor. (Similar to `numpy.transpose`).\nThe optional attribute `perm` must be a permutation of the dimensions of\nthe input tensor. Axis `i` of the output tensor corresponds to the axis\n`perm[i]` of the input tensor.\nFor example, when perm=(1, 0, 2), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 1, 3).\nWhen perm=(1, 2, 0), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 3, 1).\nIf the attribute `perm` is omitted, its default value is `(n-1, ..., 0)`,\nwhere `n` is the rank of the input tensor.\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. Its length must be equal to the rank of the input." } ], "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "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, permutation in enumerate(permutations):\n node = onnx.helper.make_node(\n \"Transpose\",\n inputs=[\"data\"],\n outputs=[\"transposed\"],\n perm=permutation,\n )\n transposed = np.transpose(data, permutation)\n expect(\n node,\n inputs=[data],\n outputs=[transposed],\n name=f\"test_transpose_all_permutations_{i}\",\n )" }, { "summary": "default", "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Transpose\", inputs=[\"data\"], outputs=[\"transposed\"]\n)\n\ntransposed = np.transpose(data)\nexpect(node, inputs=[data], outputs=[transposed], name=\"test_transpose_default\")" } ], "category": "Transform" }, { "name": "Transpose", "module": "ai.onnx", "version": 25, "description": "Returns a transpose of the input tensor. (Similar to `numpy.transpose`).\nThe optional attribute `perm` must be a permutation of the dimensions of\nthe input tensor. Axis `i` of the output tensor corresponds to the axis\n`perm[i]` of the input tensor.\nFor example, when perm=(1, 0, 2), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 1, 3).\nWhen perm=(1, 2, 0), given an input tensor of shape (1, 2, 3),\nthe output shape will be (2, 3, 1).\nIf the attribute `perm` is omitted, its default value is `(n-1, ..., 0)`,\nwhere `n` is the rank of the input tensor.\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. Its length must be equal to the rank of the input." } ], "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "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, permutation in enumerate(permutations):\n node = onnx.helper.make_node(\n \"Transpose\",\n inputs=[\"data\"],\n outputs=[\"transposed\"],\n perm=permutation,\n )\n transposed = np.transpose(data, permutation)\n expect(\n node,\n inputs=[data],\n outputs=[transposed],\n name=f\"test_transpose_all_permutations_{i}\",\n )" }, { "summary": "default", "code": "shape = (2, 3, 4)\ndata = np.random.random_sample(shape).astype(np.float32)\n\nnode = onnx.helper.make_node(\n \"Transpose\", inputs=[\"data\"], outputs=[\"transposed\"]\n)\n\ntransposed = np.transpose(data)\nexpect(node, inputs=[data], outputs=[transposed], name=\"test_transpose_default\")" } ], "category": "Transform" }, { "name": "TransposeMatMul", "module": "com.microsoft", "version": 1, "description": "Duplicate of FusedMatMul. Going forward FusedMatMul should be used. This OP will be supported for backward compatibility.\nMatrix product that behaves like numpy.matmul: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matmul.html\n", "attributes": [ { "name": "alpha", "type": "float32", "required": false, "default": 1.0, "description": "Scalar multiplier for the product of the input tensors." }, { "name": "transA", "type": "int64", "required": false, "description": "Whether A should be transposed on the last two dimensions before doing multiplication" }, { "name": "transB", "type": "int64", "required": false, "description": "Whether B should be transposed on the last two dimensions before doing multiplication" } ], "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" } ], "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)" ] } ] }, { "name": "TreeEnsemble", "module": "ai.onnx.ml", "version": 5, "description": "Tree Ensemble operator. Returns the regressed values for each input in a batch.\n Inputs have dimensions `[N, F]` where `N` is the input batch size and `F` is the number of input features.\n Outputs have dimensions `[N, num_targets]` where `N` is the batch size and `num_targets` is the number of targets, which is a configurable attribute.\n\n The encoding of this attribute is split along interior nodes and the leaves of the trees. Notably, attributes with the prefix `nodes_*` are associated with interior nodes, and attributes with the prefix `leaf_*` are associated with leaves.\n The attributes `nodes_*` must all have the same length and encode a sequence of tuples, as defined by taking all the `nodes_*` fields at a given position.\n\n All fields prefixed with `leaf_*` represent tree leaves, and similarly define tuples of leaves and must have identical length.\n\n This operator can be used to implement both the previous `TreeEnsembleRegressor` and `TreeEnsembleClassifier` nodes.\n The `TreeEnsembleRegressor` node maps directly to this node and requires changing how the nodes are represented.\n The `TreeEnsembleClassifier` node can be implemented by adding a `ArgMax` node after this node to determine the top class.\n To encode class labels, a `LabelEncoder` or `GatherND` operator may be used.\n", "attributes": [ { "name": "aggregate_function", "type": "int64", "required": false, "default": 1, "description": "Defines how to aggregate leaf values within a target.
One of 'AVERAGE' (0) 'SUM' (1) 'MIN' (2) 'MAX (3) defaults to 'SUM' (1)" }, { "name": "leaf_targetids", "type": "int64[]", "required": true, "description": "The index of the target that this leaf contributes to (this must be in range `[0, n_targets)`)." }, { "name": "leaf_weights", "type": "tensor", "required": true, "description": "The weight for each leaf." }, { "name": "membership_values", "type": "tensor", "required": false, "description": "Members to test membership of for each set membership node. List all of the members to test again in the order that the 'BRANCH_MEMBER' mode appears in `node_modes`, delimited by `NaN`s. Will have the same number of sets of values as nodes with mode 'BRANCH_MEMBER'. This may be omitted if the node doesn't contain any 'BRANCH_MEMBER' nodes." }, { "name": "n_targets", "type": "int64", "required": false, "description": "The total number of targets." }, { "name": "nodes_falseleafs", "type": "int64[]", "required": true, "description": "1 if false branch is leaf for each node and 0 if an interior node. To represent a tree that is a leaf (only has one node), one can do so by having a single `nodes_*` entry with true and false branches referencing the same `leaf_*` entry" }, { "name": "nodes_falsenodeids", "type": "int64[]", "required": true, "description": "If `nodes_falseleafs` is false at an entry, this represents the position of the false branch node. This position can be used to index into a `nodes_*` entry. If `nodes_falseleafs` is false, it is an index into the leaf_* attributes." }, { "name": "nodes_featureids", "type": "int64[]", "required": true, "description": "Feature id for each node." }, { "name": "nodes_hitrates", "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 whether to follow the true branch (if attribute value is 1) or false branch (if attribute value is 0) in the presence of a NaN input feature. This attribute may be left undefined and the default value is false (0) for all nodes." }, { "name": "nodes_modes", "type": "tensor", "required": true, "description": "The comparison operation performed by the node. This is encoded as an enumeration of 0 ('BRANCH_LEQ'), 1 ('BRANCH_LT'), 2 ('BRANCH_GTE'), 3 ('BRANCH_GT'), 4 ('BRANCH_EQ'), 5 ('BRANCH_NEQ'), and 6 ('BRANCH_MEMBER'). Note this is a tensor of type uint8." }, { "name": "nodes_splits", "type": "tensor", "required": true, "description": "Thresholds to do the splitting on for each node with mode that is not 'BRANCH_MEMBER'." }, { "name": "nodes_trueleafs", "type": "int64[]", "required": true, "description": "1 if true branch is leaf for each node and 0 an interior node. To represent a tree that is a leaf (only has one node), one can do so by having a single `nodes_*` entry with true and false branches referencing the same `leaf_*` entry" }, { "name": "nodes_truenodeids", "type": "int64[]", "required": true, "description": "If `nodes_trueleafs` is false at an entry, this represents the position of the true branch node. This position can be used to index into a `nodes_*` entry. If `nodes_trueleafs` is false, it is an index into the leaf_* attributes." }, { "name": "post_transform", "type": "int64", "required": false, "description": "Indicates the transform to apply to the score.
One of 'NONE' (0), 'SOFTMAX' (1), 'LOGISTIC' (2), 'SOFTMAX_ZERO' (3) or 'PROBIT' (4), defaults to 'NONE' (0)" }, { "name": "tree_roots", "type": "int64[]", "required": true, "description": "Index into `nodes_*` for the root of each tree. The tree structure is derived from the branching of each node." } ], "inputs": [ { "name": "X", "type": "T", "description": "Input of shape [Batch Size, Number of Features]" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T", "description": "Output of shape [Batch Size, Number of targets]" } ], "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(float16)" ] } ], "examples": [ { "summary": "tree_ensemble_set_membership", "code": "node = onnx.helper.make_node(\n \"TreeEnsemble\",\n [\"X\"],\n [\"Y\"],\n domain=\"ai.onnx.ml\",\n n_targets=4,\n aggregate_function=1,\n membership_values=make_tensor(\n \"membership_values\",\n onnx.TensorProto.FLOAT,\n (8,),\n [1.2, 3.7, 8, 9, np.nan, 12, 7, np.nan],\n ),\n nodes_missing_value_tracks_true=None,\n nodes_hitrates=None,\n post_transform=0,\n tree_roots=[0],\n nodes_modes=make_tensor(\n \"nodes_modes\",\n onnx.TensorProto.UINT8,\n (3,),\n np.array([0, 6, 6], dtype=np.uint8),\n ),\n nodes_featureids=[0, 0, 0],\n nodes_splits=make_tensor(\n \"nodes_splits\",\n onnx.TensorProto.FLOAT,\n (3,),\n np.array([11, 232344.0, np.nan], dtype=np.float32),\n ),\n nodes_trueleafs=[0, 1, 1],\n nodes_truenodeids=[1, 0, 1],\n nodes_falseleafs=[1, 0, 1],\n nodes_falsenodeids=[2, 2, 3],\n leaf_targetids=[0, 1, 2, 3],\n leaf_weights=make_tensor(\n \"leaf_weights\", onnx.TensorProto.FLOAT, (4,), [1, 10, 1000, 100]\n ),\n)\n\nx = np.array([1.2, 3.4, -0.12, np.nan, 12, 7], np.float32).reshape(-1, 1)\nexpected = np.array(\n [\n [1, 0, 0, 0],\n [0, 0, 0, 100],\n [0, 0, 0, 100],\n [0, 0, 1000, 0],\n [0, 0, 1000, 0],\n [0, 10, 0, 0],\n ],\n dtype=np.float32,\n)\nexpect(\n node,\n inputs=[x],\n outputs=[expected],\n name=\"test_ai_onnx_ml_tree_ensemble_set_membership\",\n)" }, { "summary": "tree_ensemble_single_tree", "code": "node = onnx.helper.make_node(\n \"TreeEnsemble\",\n [\"X\"],\n [\"Y\"],\n domain=\"ai.onnx.ml\",\n n_targets=2,\n membership_values=None,\n nodes_missing_value_tracks_true=None,\n nodes_hitrates=None,\n aggregate_function=1,\n post_transform=0,\n tree_roots=[0],\n nodes_modes=make_tensor(\n \"nodes_modes\",\n onnx.TensorProto.UINT8,\n (3,),\n np.array([0, 0, 0], dtype=np.uint8),\n ),\n nodes_featureids=[0, 0, 0],\n nodes_splits=make_tensor(\n \"nodes_splits\",\n onnx.TensorProto.DOUBLE,\n (3,),\n np.array([3.14, 1.2, 4.2], dtype=np.float64),\n ),\n nodes_truenodeids=[1, 0, 1],\n nodes_trueleafs=[0, 1, 1],\n nodes_falsenodeids=[2, 2, 3],\n nodes_falseleafs=[0, 1, 1],\n leaf_targetids=[0, 1, 0, 1],\n leaf_weights=make_tensor(\n \"leaf_weights\",\n onnx.TensorProto.DOUBLE,\n (4,),\n np.array([5.23, 12.12, -12.23, 7.21], dtype=np.float64),\n ),\n)\n\nx = np.array([1.2, 3.4, -0.12, 1.66, 4.14, 1.77], np.float64).reshape(3, 2)\ny = np.array([[5.23, 0], [5.23, 0], [0, 12.12]], dtype=np.float64)\nexpect(\n node,\n inputs=[x],\n outputs=[y],\n name=\"test_ai_onnx_ml_tree_ensemble_single_tree\",\n)" } ] }, { "name": "TreeEnsembleClassifier", "module": "ai.onnx.ml", "version": 1, "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 default 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 classlabels_* attributes is used.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(string)", "tensor(int64)" ] } ] }, { "name": "TreeEnsembleClassifier", "module": "ai.onnx.ml", "version": 3, "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 default 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 classlabels_* attributes is used.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(string)", "tensor(int64)" ] } ] }, { "name": "TreeEnsembleClassifier", "module": "ai.onnx.ml", "version": 5, "description": "This operator is DEPRECATED. Please use TreeEnsemble with provides similar functionality.\n In order to determine the top class, the ArgMax node can be applied to the output of TreeEnsemble.\n To encode class labels, use a LabelEncoder operator.\n 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 default 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 classlabels_* attributes is used.", "type_param_str": "T2", "allowed_type_strs": [ "tensor(string)", "tensor(int64)" ] } ] }, { "name": "TreeEnsembleRegressor", "module": "ai.onnx.ml", "version": 1, "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 default 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, "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 regression, added to final prediction after applying aggregate_function; 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 regression, added to final prediction after applying aggregate_function; 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 default 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": "TreeEnsembleRegressor", "module": "ai.onnx.ml", "version": 5, "description": "This operator is DEPRECATED. Please use TreeEnsemble instead which provides the same\n functionality.
\n 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 regression, added to final prediction after applying aggregate_function; 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 regression, added to final prediction after applying aggregate_function; 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 default 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, "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": "Trilu", "module": "com.microsoft", "version": 1, "description": "Returns the upper or lower triangular part of a 2-D matrix, or batches of 2-D matrices. If the attribute \"upper\" is set to true,\n the upper triangular matrix is retained. Lower triangular matrix is retained otherwise. Default value for upper is true.\n Trilu takes one input tensor of shape [*, N, M], where * is zero or more batch dimensions. The upper triangular part consists\n of the elements on and above the given diagonal (k). The lower triangular part consists of elements on and below the diagonal.\n All other elements in the matrix are set to zero.\n If k = 0, the triangular part on and above/below the main diagonal is retained.\n If upper is set to true, a positive k retains the upper triangular matrix excluding k diagonals above\n the main diagonal. A negative k value includes as many diagonals below the main diagonal.\n If upper is set to false, a positive k retains the lower triangular matrix including k diagonals above\n the main diagonal. A negative k value excludes as many diagonals below the main diagonal.\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": "X", "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 the main diagonal to exclude or include.Default value is 0 if it's not specified." } ], "min_input": 1, "max_input": 2, "outputs": [ { "name": "Y", "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 numeric tensors and bool tensors.", "type_param_str": "T", "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)" ] } ] }, { "name": "UnfoldTensor", "module": "com.microsoft", "version": 1, "description": "Returns a tensor which contains all slices of size `size` from input tensor in the dimension `dim`. Step between two slices is given by `step`. If `sizedim` is the size of dimension `dim` for input tensor, the size of dimension `dim` in the returned tensor will be `(sizedim - size) / step + 1`. An additional dimension of size `size` is appended in the returned tensor.", "attributes": [ { "name": "dim", "type": "int64", "required": false, "default": -1, "description": "specify the dimension to unfold" }, { "name": "size", "type": "int64", "required": true, "description": "specify the size" }, { "name": "step", "type": "int64", "required": false, "default": 1, "description": "specify the step." } ], "inputs": [ { "name": "input", "type": "T", "description": "input tensor" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "output", "type": "T", "description": "Output tensor." } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Allow inputs and outputs to be any kind of tensor.", "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)" ] } ] }, { "name": "Unique", "module": "ai.onnx", "version": 11, "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 occurrence 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```\ninput_X = [2, 1, 1, 3, 4, 3]\nattribute_sorted = 0\nattribute_axis = None\noutput_Y = [2, 1, 3, 4]\noutput_indices = [0, 1, 3, 4]\noutput_inverse_indices = [0, 1, 1, 2, 3, 2]\noutput_counts = [1, 2, 2, 1]\n```\n\nExample 2:\n```\ninput_X = [[1, 3], [2, 3]]\nattribute_sorted = 1\nattribute_axis = None\noutput_Y = [1, 2, 3]\noutput_indices = [0, 2, 1]\noutput_inverse_indices = [0, 2, 1, 2]\noutput_counts = [1, 1, 2]\n```\n\nExample 3:\n```\ninput_X = [[1, 0, 0], [1, 0, 0], [2, 3, 4]]\nattribute_sorted = 1\nattribute_axis = 0\noutput_Y = [[1, 0, 0], [2, 3, 4]]\noutput_indices = [0, 2]\noutput_inverse_indices = [0, 0, 1]\noutput_counts = [2, 1]\n```\n\nExample 4:\n```\ninput_x = [[[1., 1.], [0., 1.], [2., 1.], [0., 1.]],\n [[1., 1.], [0., 1.], [2., 1.], [0., 1.]]]\nattribute_sorted = 1\nattribute_axis = 1\n```\n\nintermediate data are presented below for better understanding:\nthere are 4 subtensors sliced along axis 1 of input_x (shape = (2, 4, 2)):\n```\nA: [[1, 1], [1, 1]],\n [[0, 1], [0, 1]],\n [[2, 1], [2, 1]],\n [[0, 1], [0, 1]].\n```\n\nthere are 3 unique subtensors:\n```\n[[1, 1], [1, 1]],\n[[0, 1], [0, 1]],\n[[2, 1], [2, 1]].\n```\n\nsorted unique subtensors:\n```\nB: [[0, 1], [0, 1]],\n [[1, 1], [1, 1]],\n [[2, 1], [2, 1]].\n```\n\noutput_Y is constructed from B:\n```\n[[[0. 1.], [1. 1.], [2. 1.]],\n [[0. 1.], [1. 1.], [2. 1.]]]\n```\n\noutput_indices is to map from B to A:\n```\n[1, 0, 2]\n```\n\noutput_inverse_indices is to map from A to B:\n```\n[1, 0, 2, 0]\n```\n\noutput_counts:\n```\n[2, 1, 1]\n```\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 occurrence 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": "length_1", "code": "node_sorted = onnx.helper.make_node(\n \"Unique\",\n inputs=[\"X\"],\n outputs=[\"Y\", \"indices\", \"inverse_indices\", \"counts\"],\n sorted=1,\n)\n\nx = np.array([0], dtype=np.int64)\ny, indices, inverse_indices, counts = np.unique(x, True, True, True)\nindices, inverse_indices, counts = specify_int64(\n indices, inverse_indices, counts\n)\n# behavior changed with numpy >= 2.0\ninverse_indices = inverse_indices.reshape(-1)\n# print(y)\n# [0]\n# print(indices)\n# [0]\n# print(inverse_indices)\n# [0]\n# print(counts)\n# [1]\n\nexpect(\n node_sorted,\n inputs=[x],\n outputs=[y, indices, inverse_indices, counts],\n name=\"test_unique_length_1\",\n)" }, { "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 = dict(\n zip(argsorted_indices, np.arange(len(argsorted_indices)), strict=True)\n)\n\nindices = indices[argsorted_indices]\ny = np.take(x, indices, axis=0)\ninverse_indices = np.asarray(\n [inverse_indices_map[i] for i in inverse_indices], dtype=np.int64\n)\ncounts = counts[argsorted_indices]\nindices, inverse_indices, counts = specify_int64(\n indices, inverse_indices, counts\n)\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(\n node_not_sorted,\n inputs=[x],\n outputs=[y, indices, inverse_indices, counts],\n name=\"test_unique_not_sorted_without_axis\",\n)" }, { "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(\n indices, inverse_indices, counts\n)\n# behavior changed with numpy >= 2.0\ninverse_indices = inverse_indices.reshape(-1)\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(\n node_sorted,\n inputs=[x],\n outputs=[y, indices, inverse_indices, counts],\n name=\"test_unique_sorted_with_axis\",\n)" }, { "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(\n [\n [[1.0, 1.0], [0.0, 1.0], [2.0, 1.0], [0.0, 1.0]],\n [[1.0, 1.0], [0.0, 1.0], [2.0, 1.0], [0.0, 1.0]],\n ],\n dtype=np.float32,\n)\ny, indices, inverse_indices, counts = np.unique(x, True, True, True, axis=1)\nindices, inverse_indices, counts = specify_int64(\n indices, inverse_indices, counts\n)\n# behavior changed with numpy >= 2.0\ninverse_indices = inverse_indices.reshape(-1)\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(\n node_sorted,\n inputs=[x],\n outputs=[y, indices, inverse_indices, counts],\n name=\"test_unique_sorted_with_axis_3d\",\n)" }, { "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(\n indices, inverse_indices, counts\n)\n# behavior changed with numpy >= 2.0\ninverse_indices = inverse_indices.reshape(-1)\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(\n node_sorted,\n inputs=[x],\n outputs=[y, indices, inverse_indices, counts],\n name=\"test_unique_sorted_with_negative_axis\",\n)" }, { "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(\n indices, inverse_indices, counts\n)\nexpect(\n node_sorted,\n inputs=[x],\n outputs=[y, indices, inverse_indices, counts],\n name=\"test_unique_sorted_without_axis\",\n)" } ] }, { "name": "Unique", "module": "com.microsoft", "version": 1, "description": "Finds all the unique values (deduped list) present in the given input tensor.\n This operator returns 3 outputs.\n The first output tensor 'uniques' contains all of the unique elements of the input,\n sorted in the same order that they occur in the input.\n The second output tensor 'idx' is the same size as the input and it contains the index\n of each value of the input in 'uniques'.\n The third output tensor 'counts' contains the count of each element of 'uniques' in the input.\n Example:\n input_x = [2, 1, 1, 3, 4, 3]\n output_uniques = [2, 1, 3, 4]\n output_idx = [0, 1, 1, 2, 3, 2]\n output_counts = [1, 2, 2, 1]\n ", "inputs": [ { "name": "x", "type": "T", "description": "A 1-D input tensor that is to be processed." } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "y", "type": "T", "description": "A 1-D tensor of the same type as 'x' containing all the unique values in 'x' sorted in the same order that they occur in the input 'x'" }, { "name": "idx", "type": "tensor(int64)", "description": "A 1-D INT64 tensor of the same size as 'x' containing the indices for each value in 'x' in the output 'uniques'" }, { "name": "counts", "type": "tensor(int64)", "description": "A 1-D INT64 tensor containing the the count of each element of 'uniques' in the input 'x'" } ], "min_output": 3, "max_output": 3, "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)" ] } ] }, { "name": "Unsqueeze", "module": "ai.onnx", "version": 1, "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], 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(\n node,\n inputs=[x, axes],\n outputs=[y],\n name=\"test_unsqueeze_axis_\" + str(i),\n )" }, { "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], 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], 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], name=\"test_unsqueeze_unsorted_axes\")" } ], "category": "Transform" }, { "name": "Unsqueeze", "module": "ai.onnx", "version": 11, "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], 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(\n node,\n inputs=[x, axes],\n outputs=[y],\n name=\"test_unsqueeze_axis_\" + str(i),\n )" }, { "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], 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], 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], name=\"test_unsqueeze_unsorted_axes\")" } ], "category": "Transform" }, { "name": "Unsqueeze", "module": "ai.onnx", "version": 13, "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, given an input tensor (`data`) of shape [3, 4, 5], then\nUnsqueeze(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", "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], 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(\n node,\n inputs=[x, axes],\n outputs=[y],\n name=\"test_unsqueeze_axis_\" + str(i),\n )" }, { "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], 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], 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], name=\"test_unsqueeze_unsorted_axes\")" } ], "category": "Transform" }, { "name": "Unsqueeze", "module": "ai.onnx", "version": 21, "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, given an input tensor (`data`) of shape [3, 4, 5], then\nUnsqueeze(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", "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 up to IRv10.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)" ] } ], "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], 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(\n node,\n inputs=[x, axes],\n outputs=[y],\n name=\"test_unsqueeze_axis_\" + str(i),\n )" }, { "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], 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], 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], name=\"test_unsqueeze_unsorted_axes\")" } ], "category": "Transform" }, { "name": "Unsqueeze", "module": "ai.onnx", "version": 23, "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, given an input tensor (`data`) of shape [3, 4, 5], then\nUnsqueeze(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", "inputs": [ { "name": "data", "type": "T", "description": "Original tensor" }, { "name": "axes", "type": "tensor(int64)", "description": "1D tensor 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 up to IRv11.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)" ] } ], "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], 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(\n node,\n inputs=[x, axes],\n outputs=[y],\n name=\"test_unsqueeze_axis_\" + str(i),\n )" }, { "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], 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], 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], name=\"test_unsqueeze_unsorted_axes\")" } ], "category": "Transform" }, { "name": "Unsqueeze", "module": "ai.onnx", "version": 24, "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, given an input tensor (`data`) of shape [3, 4, 5], then\nUnsqueeze(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", "inputs": [ { "name": "data", "type": "T", "description": "Original tensor" }, { "name": "axes", "type": "tensor(int64)", "description": "1D tensor 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 up to IRv12.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)" ] } ], "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], 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(\n node,\n inputs=[x, axes],\n outputs=[y],\n name=\"test_unsqueeze_axis_\" + str(i),\n )" }, { "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], 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], 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], name=\"test_unsqueeze_unsorted_axes\")" } ], "category": "Transform" }, { "name": "Unsqueeze", "module": "ai.onnx", "version": 25, "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, given an input tensor (`data`) of shape [3, 4, 5], then\nUnsqueeze(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", "inputs": [ { "name": "data", "type": "T", "description": "Original tensor" }, { "name": "axes", "type": "tensor(int64)", "description": "1D tensor 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 up to IRv13.", "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)", "tensor(float8e4m3fn)", "tensor(float8e4m3fnuz)", "tensor(float8e5m2)", "tensor(float8e5m2fnuz)", "tensor(uint4)", "tensor(int4)", "tensor(float4e2m1)", "tensor(float8e8m0)", "tensor(uint2)", "tensor(int2)" ] } ], "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], 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(\n node,\n inputs=[x, axes],\n outputs=[y],\n name=\"test_unsqueeze_axis_\" + str(i),\n )" }, { "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], 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], 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], name=\"test_unsqueeze_unsorted_axes\")" } ], "category": "Transform" }, { "name": "Upsample", "module": "ai.onnx", "version": 1, "status": "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\noutput = np.array(\n [\n [\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],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_upsample_nearest\",\n opset_imports=[helper.make_opsetid(\"\", 9)],\n)" } ], "category": "Data" }, { "name": "Upsample", "module": "ai.onnx", "version": 7, "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\noutput = np.array(\n [\n [\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],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_upsample_nearest\",\n opset_imports=[helper.make_opsetid(\"\", 9)],\n)" } ], "category": "Data" }, { "name": "Upsample", "module": "ai.onnx", "version": 9, "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\noutput = np.array(\n [\n [\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],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_upsample_nearest\",\n opset_imports=[helper.make_opsetid(\"\", 9)],\n)" } ], "category": "Data" }, { "name": "Upsample", "module": "ai.onnx", "version": 10, "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 [\n [\n [\n [1, 2],\n [3, 4],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nscales = np.array([1.0, 1.0, 2.0, 3.0], dtype=np.float32)\n\noutput = np.array(\n [\n [\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],\n ]\n ]\n ],\n dtype=np.float32,\n)\n\nexpect(\n node,\n inputs=[data, scales],\n outputs=[output],\n name=\"test_upsample_nearest\",\n opset_imports=[helper.make_opsetid(\"\", 9)],\n)" } ], "category": "Data" }, { "name": "Upsample", "module": "com.microsoft.nchwc", "version": 1, "attributes": [ { "name": "coordinate_transformation_mode", "type": "string", "required": false, "default": "asymmetric" }, { "name": "mode", "type": "string", "required": false, "default": "nearest" }, { "name": "scales", "type": "int64[]", "required": false } ], "inputs": [ { "name": "X", "type": "T" } ], "min_input": 1, "max_input": 1, "outputs": [ { "name": "Y", "type": "T" } ], "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(float)" ] } ], "category": "Data" }, { "name": "Where", "module": "ai.onnx", "version": 9, "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(\n node, inputs=[condition, x, y], outputs=[z], name=\"test_where_long_example\"\n)" }, { "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], name=\"test_where_example\")" } ] }, { "name": "Where", "module": "ai.onnx", "version": 16, "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 (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(\n node, inputs=[condition, x, y], outputs=[z], name=\"test_where_long_example\"\n)" }, { "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], name=\"test_where_example\")" } ] }, { "name": "WhisperBeamSearch", "module": "com.microsoft", "version": 1, "description": "Beam Search for whisper model, especially with cross_qk features etc.", "attributes": [ { "name": "beginning_timestamp_token_id", "type": "int64", "required": false, "description": "The id of the first timestamp" }, { "name": "decoder", "type": "graph", "required": true, "description": "Decoder subgraph to execute in a loop." }, { "name": "decoder_output_cross_qk", "type": "int64", "required": false, "description": "If nozero, decoder subgraph contains output Q*K from cross attentions. Default 0." }, { "name": "decoder_start_token_id", "type": "int64", "required": false, "default": -1, "description": "The id of the token that indicates decoding starts (i.e. the start of transcription token id)" }, { "name": "early_stopping", "type": "int64", "required": false, "description": "early stop or not" }, { "name": "encoder", "type": "graph", "required": false, "description": "The subgraph for initialization of encoder and decoder. It will be called once before decoder subgraph." }, { "name": "eos_token_id", "type": "int64", "required": true, "description": "The id of the end-of-sequence token" }, { "name": "init_decoder", "type": "graph", "required": false, "description": "The subgraph for the first decoding run. It will be called once before `decoder` subgraph. This is relevant only for the GPT2 model. If this attribute is missing, the `decoder` subgraph will be used for all decoding runs" }, { "name": "model_type", "type": "int64", "required": false, "default": 2, "description": "Must be 2 for whisper" }, { "name": "no_repeat_ngram_size", "type": "int64", "required": false, "description": "no repeat ngrams size" }, { "name": "no_speech_token_id", "type": "int64", "required": false, "description": "The token in whisper model that marks all sequence empty. With this model, whisper could output no_speech_prob after. Default -1." }, { "name": "no_timestamps_token_id", "type": "int64", "required": false, "description": "The id of the token that indicates no timestamps" }, { "name": "pad_token_id", "type": "int64", "required": true, "description": "The id of the padding token" }, { "name": "start_of_lm_token_id", "type": "int64", "required": false, "description": "The id of the token that indicates LM starts" }, { "name": "transcribe_token_id", "type": "int64", "required": false, "description": "The id of the transcribe task" }, { "name": "translate_token_id", "type": "int64", "required": false, "description": "The id of the translate task" }, { "name": "vocab_size", "type": "int64", "required": false, "default": -1, "description": "Size of the vocabulary. If not provided, it will be inferred from the decoder subgraph's output shape" } ], "inputs": [ { "name": "input_ids", "type": "F", "description": "The sequence used as a prompt for the generation in the encoder subgraph. Shape is (batch_size, sequence_length)" }, { "name": "max_length", "type": "I", "description": "The maximum length of the sequence to be generated. Shape is (1)" }, { "name": "min_length", "type": "I", "option": "optional", "description": "The minimum length below which the score of eos_token_id is set to -Inf. Shape is (1)" }, { "name": "num_beams", "type": "I", "description": "Number of beams for beam search. 1 means no beam search. Shape is (1)" }, { "name": "num_return_sequences", "type": "I", "description": "The number of returned sequences in the batch. Shape is (1)" }, { "name": "length_penalty", "type": "T", "option": "optional", "description": "Exponential penalty to the length. Default value 1.0 means no penalty. Value > 1.0 encourages longer sequences, while values < 1.0 produces shorter sequences. Shape is (1,)" }, { "name": "repetition_penalty", "type": "T", "option": "optional", "description": "The parameter for repetition penalty. Default value 1.0 means no penalty. Accepts value > 0.0. Shape is (1)" }, { "name": "vocab_mask", "type": "M", "option": "optional", "description": "Mask of vocabulary. Words that masked with 0 are not allowed to be generated, and 1 is allowed. Shape is (vocab_size)" }, { "name": "prefix_vocab_mask", "type": "M", "option": "optional", "description": "Mask of vocabulary for first step. Words that masked with 0 are not allowed to be generated, and 1 is allowed. Shape is (batch_size, vocab_size)" }, { "name": "attention_mask", "type": "I", "option": "optional", "description": "Custom attention mask. Shape is (batch_size, sequence_length)" }, { "name": "decoder_input_ids", "type": "I", "option": "optional", "description": "The forced input id sequence for the decoder subgraph. Shape is (batch_size, initial_sequence_length)" }, { "name": "logits_processor", "type": "I", "option": "optional", "description": "Specific logits processor for different types of beamsearch models. Default value 0 means no specific logit processor. Accepts value >= 0. Shape is (1)" }, { "name": "cross_qk_layer_head", "type": "I", "option": "optional", "description": "Only keep this list of (layer, head) of QK in the final cross_qk output when use_cross_qk is set. Default collect all its shape is (number of (layer, head) to keep, 2), i.e., [[layer_id1, head_id1], [layer_id2, head_id2]......]" }, { "name": "extra_decoding_ids", "type": "I", "option": "optional", "description": "Part of the decoder_input_ids that we need cross qk for it. it is of shape (batch_size, extra_decoding_ids_len).In such case, we should remove this from the tail of the decoder_input_ids, and put it here. ids < 0 in it (for multiple batch) are treated as stop of the extra_decoding_ids for corresponding batch." }, { "name": "temperature", "type": "T", "option": "optional", "description": "Temperature value to apply to logits processing during this execution's decoding. Shape is (1)" } ], "min_input": 5, "max_input": 15, "outputs": [ { "name": "sequences", "type": "I", "description": "Word IDs of generated sequences. Shape is (batch_size, num_return_sequences, max_sequence_length)" }, { "name": "sequences_scores", "type": "T", "option": "optional", "description": "Final beam score of the generated sequences. Shape is (batch_size, num_return_sequences)" }, { "name": "scores", "type": "T", "option": "optional", "description": "Processed beam scores for each vocabulary token at each generation step. Beam scores consisting of log softmax scores for each vocabulary token and sum of log softmax of previously generated tokens in this beam. Shape is (max_length - sequence_length, batch_size, num_beams, vocab_size)" }, { "name": "cross_qk", "type": "V", "option": "optional", "description": "Output the accumulated stacked Q*K in cross attentions. Let H = number of Head of cross attention, F = the frames or kv-seq-len of the cross attention input, T = real decoded token length, L = number of layers, B = batch size, R = num_return_sequences. It then should return tensor of shape [B, R, L*H, T, F]. If cross_qk_layer_head is given, shape is [B, R, cross_qk_layer_head.shape[0], T, F]" }, { "name": "non_speech_probs", "type": "T", "option": "optional", "description": "For whisper model, output the probabilities from logits after encoder and context decoding for the no_speech_token_id. The shape of non_speech_probs is [B]" } ], "min_output": 1, "max_output": 5, "inputs_range": "5 - 15", "outputs_range": "1 - 5", "type_constraints": [ { "description": "Constrain to float tensors.", "type_param_str": "T", "allowed_type_strs": [ "tensor(float)", "tensor(float16)" ] }, { "description": "Constrain input type to float or int tensors.", "type_param_str": "F", "allowed_type_strs": [ "tensor(float)", "tensor(int32)", "tensor(float16)" ] }, { "description": "Constrain to integer types", "type_param_str": "I", "allowed_type_strs": [ "tensor(int32)" ] }, { "description": "Constrain mask to integer types", "type_param_str": "M", "allowed_type_strs": [ "tensor(int32)" ] }, { "description": "Constrain cross_qk to float32 tensors.", "type_param_str": "V", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "WordConvEmbedding", "module": "com.microsoft", "version": 1, "description": "The WordConvEmbedding takes in a batch of sequence words and embed each word to a vector.", "attributes": [ { "name": "char_embedding_size", "type": "int64", "required": false, "description": "Integer representing the embedding vector size for each char.If not provide, use the char embedding size of embedding vector." }, { "name": "conv_window_size", "type": "int64", "required": false, "description": "This operator applies convolution to word from left to right with window equal to conv_window_size and stride to 1.Take word 'example' for example, with conv_window_size equal to 2, conv is applied to [ex],[xa], [am], [mp]...If not provide, use the first dimension of conv kernel shape." }, { "name": "embedding_size", "type": "int64", "required": false, "description": "Integer representing the embedding vector size for each word.If not provide, use the filter size of conv weight" } ], "inputs": [ { "name": "Sequence", "type": "T", "description": "Specify batchs of sequence words to embedding" }, { "name": "W", "type": "T1", "description": "Specify weights of conv" }, { "name": "B", "type": "T1", "description": "Specify bias of conv" }, { "name": "C", "type": "T1", "description": "Specify embedding vector of char" } ], "min_input": 4, "max_input": 4, "outputs": [ { "name": "Y", "type": "T1", "description": "output" } ], "min_output": 1, "max_output": 1, "type_constraints": [ { "description": "Constrain to tensor(int32).", "type_param_str": "T", "allowed_type_strs": [ "tensor(int32)" ] }, { "description": "Constrain to tensor(float).", "type_param_str": "T1", "allowed_type_strs": [ "tensor(float)" ] } ] }, { "name": "Xor", "module": "ai.onnx", "version": 1, "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], 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], 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], 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], 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], 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], 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], 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], name=\"test_xor_bcast4v4d\")" } ] }, { "name": "Xor", "module": "ai.onnx", "version": 7, "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], 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], 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], 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], 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], 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], 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], 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], name=\"test_xor_bcast4v4d\")" } ] }, { "name": "ZipMap", "module": "ai.onnx.ml", "version": 1, "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))" ] } ] } ]