original_docstring: Helper function for scalar arithmetic operations
docstring: Helper function for scalar arithmetic operations
docstring_tokens: Helper function for scalar arithmetic operations
code: def scalar_op_helper(node, op_name, **kwargs):
      """Helper function for scalar arithmetic operations"""
      onnx = import_onnx_modules()
      name = node["name"]
      proc_nodes = kwargs["proc_nodes"]
      inputs = node["inputs"]
      scalar_value = [float(node.get("attrs", {}).get("scalar", 1))]
  
      input_name_id = kwargs["index_lookup"][inputs[0][0]]
      input_node = proc_nodes[input_name_id].name
  
      initializer = kwargs["initializer"]
      flag = True
      # If the input value is in initializer, just multiply with scalar input
      # and create a new initializer
      for i in initializer:
          if i.name == input_node:
              if op_name == 'Mul':
                  new_initializer = onnx.numpy_helper.to_array(i) * scalar_value[0]
              elif op_name == 'Sub':
                  new_initializer = onnx.numpy_helper.to_array(i) - scalar_value[0]
              elif op_name == 'Add':
                  new_initializer = onnx.numpy_helper.to_array(i) + scalar_value[0]
              elif op_name == 'Div':
                  new_initializer = onnx.numpy_helper.to_array(i) / scalar_value[0]
              flag = False
              break
  
      # else create a new tensor of the scalar value, add it in initializer
      if flag is True:
          np_arr = np.array(scalar_value)
          data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np_arr.dtype]
          dims = np.shape(np_arr)
  
          scalar_op_name = "scalar_op" + str(kwargs["idx"])
          tensor_node = onnx.helper.make_tensor_value_info(scalar_op_name, data_type, dims)
  
          initializer.append(
              onnx.helper.make_tensor(
                  name=scalar_op_name,
                  data_type=data_type,
                  dims=dims,
                  vals=scalar_value,
                  raw=False,
              )
          )
  
          mul_node = onnx.helper.make_node(
              op_name,
              [input_node, scalar_op_name],
              [name],
              name=name
          )
  
          return [tensor_node, mul_node]
      else:
          data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[new_initializer.dtype]
          dims = np.shape(new_initializer)
  
          new_a_node = input_node + str(kwargs["idx"])
          tensor_node = onnx.helper.make_tensor_value_info(new_a_node, data_type, dims)
  
          initializer.append(
              onnx.helper.make_tensor(
                  name=new_a_node,
                  data_type=data_type,
                  dims=dims,
                  vals=new_initializer,
                  raw=False,
              )
          )
          return [tensor_node]
code_tokens: def scalar_op_helper ( node , op_name , ** kwargs ) : onnx = import_onnx_modules ( ) name = node [ "name" ] proc_nodes = kwargs [ "proc_nodes" ] inputs = node [ "inputs" ] scalar_value = [ float ( node . get ( "attrs" , { } ) . get ( "scalar" , 1 ) ) ] input_name_id = kwargs [ "index_lookup" ] [ inputs [ 0 ] [ 0 ] ] input_node = proc_nodes [ input_name_id ] . name initializer = kwargs [ "initializer" ] flag = True for i in initializer : if i . name == input_node : if op_name == 'Mul' : new_initializer = onnx . numpy_helper . to_array ( i ) * scalar_value [ 0 ] elif op_name == 'Sub' : new_initializer = onnx . numpy_helper . to_array ( i ) - scalar_value [ 0 ] elif op_name == 'Add' : new_initializer = onnx . numpy_helper . to_array ( i ) + scalar_value [ 0 ] elif op_name == 'Div' : new_initializer = onnx . numpy_helper . to_array ( i ) / scalar_value [ 0 ] flag = False break if flag is True : np_arr = np . array ( scalar_value ) data_type = onnx . mapping . NP_TYPE_TO_TENSOR_TYPE [ np_arr . dtype ] dims = np . shape ( np_arr ) scalar_op_name = "scalar_op" + str ( kwargs [ "idx" ] ) tensor_node = onnx . helper . make_tensor_value_info ( scalar_op_name , data_type , dims ) initializer . append ( onnx . helper . make_tensor ( name = scalar_op_name , data_type = data_type , dims = dims , vals = scalar_value , raw = False , ) ) mul_node = onnx . helper . make_node ( op_name , [ input_node , scalar_op_name ] , [ name ] , name = name ) return [ tensor_node , mul_node ] else : data_type = onnx . mapping . NP_TYPE_TO_TENSOR_TYPE [ new_initializer . dtype ] dims = np . shape ( new_initializer ) new_a_node = input_node + str ( kwargs [ "idx" ] ) tensor_node = onnx . helper . make_tensor_value_info ( new_a_node , data_type , dims ) initializer . append ( onnx . helper . make_tensor ( name = new_a_node , data_type = data_type , dims = dims , vals = new_initializer , raw = False , ) ) return [ tensor_node ]
short_docstring: Helper function for scalar arithmetic operations
short_docstring_tokens: Helper function for scalar arithmetic operations
comment: """Helper function for scalar arithmetic operations"""$SEP$# If the input value is in initializer, just multiply with scalar input$SEP$# and create a new initializer$SEP$# else create a new tensor of the scalar value, add it in initializer