<!--
{
  "availability" : [
    "iOS: 14.0.0 - 17.4.0",
    "iPadOS: 14.0.0 - 17.4.0",
    "macCatalyst: 14.0.0 - 17.4.0",
    "macOS: 11.0.0 - 14.3.0",
    "tvOS: 14.0.0 - 17.4.0"
  ],
  "documentType" : "symbol",
  "framework" : "MLCompute",
  "identifier" : "/documentation/MLCompute/MLCTensor",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "ML Compute"
    ],
    "preciseIdentifier" : "c:objc(cs)MLCTensor"
  },
  "title" : "MLCTensor"
}
-->

# MLCTensor

The data object you use throughout the framework.

```
class MLCTensor
```

## Overview

Create a tensor with or without data. For example, create a tensor with data for weights used by convolution or mean, variance, beta, and gamma parameters with batch normalization. Create a tensor without data to use as an input tensor when you build a graph.

## Topics

### Creating Tensors with Descriptors

[`+  tensorWithDescriptor:`](/documentation/MLCompute/MLCTensor/init(descriptor:))

Creates a tensor without data, using the descriptor you specify.

[`+  tensorWithDescriptor:data:`](/documentation/MLCompute/MLCTensor/init(descriptor:data:))

Creates a tensor with the descriptor and data you specify.

[`+  tensorWithDescriptor:fillWithData:`](/documentation/MLCompute/MLCTensor/init(descriptor:fillWithData:))

Creates a tensor with the descriptor and scalar value you specify.

[`+  tensorWithDescriptor:randomInitializerType:`](/documentation/MLCompute/MLCTensor/init(descriptor:randomInitializerType:))

Creates a tensor with the descriptor and random initializer type you specify.

[`MLCTensorDescriptor`](/documentation/MLCompute/MLCTensorDescriptor)

A configuration object you use to create a tensor.

[`MLCDataType`](/documentation/MLCompute/MLCDataType)

A tensor data type.

[`MLCTensorData`](/documentation/MLCompute/MLCTensorData)

An encapsulation of the memory that tensor data uses.

[`MLCRandomInitializerType`](/documentation/MLCompute/MLCRandomInitializerType)

An initializer type you use to create a tensor with random data.

### Creating Tensors by Specifying Shape

[`init(shape:)`](/documentation/MLCompute/MLCTensor/init(shape:))

Creates a tensor without data, with the shape you specify.

[`init(shape:dataType:)`](/documentation/MLCompute/MLCTensor/init(shape:dataType:))

Creates a tensor without data, with the shape and data type you specify.

[`init(shape:data:dataType:)`](/documentation/MLCompute/MLCTensor/init(shape:data:dataType:))

Creates a tensor with the shape, data, and data type you specify.

[`init(shape:fillWithData:dataType:)`](/documentation/MLCompute/MLCTensor/init(shape:fillWithData:dataType:))

Creates a tensor with the shape, scalar value, and data type you specify.

[`init(shape:randomInitializerType:)`](/documentation/MLCompute/MLCTensor/init(shape:randomInitializerType:))

Creates a tensor with the shape and random initializer type you specify.

[`+  tensorWithShape:`](/documentation/MLCompute/MLCTensor/tensorWithShape:)

Creates a tensor without data, with the shape you specify.

[`+  tensorWithShape:dataType:`](/documentation/MLCompute/MLCTensor/tensorWithShape:dataType:)

Creates a tensor without data, with the shape and data type you specify.

[`+  tensorWithShape:data:dataType:`](/documentation/MLCompute/MLCTensor/tensorWithShape:data:dataType:)

Creates a tensor with the shape, data, and data type you specify.

[`+  tensorWithShape:fillWithData:dataType:`](/documentation/MLCompute/MLCTensor/tensorWithShape:fillWithData:dataType:)

Creates a tensor with the shape, scalar value, and data type you specify.

[`+  tensorWithShape:randomInitializerType:`](/documentation/MLCompute/MLCTensor/tensorWithShape:randomInitializerType:)

Creates a tensor with the shape and random initializer type you specify.

[`+  tensorWithShape:randomInitializerType:dataType:`](/documentation/MLCompute/MLCTensor/tensorWithShape:randomInitializerType:dataType:)

Creates a tensor with the shape, random initializer, and data type you specify.

[`+  tensorWithWidth:height:featureChannelCount:batchSize:`](/documentation/MLCompute/MLCTensor/init(width:height:featureChannelCount:batchSize:))

Creates a tensor without data, with the sizes and number of feature channels you specify.

[`+  tensorWithWidth:height:featureChannelCount:batchSize:data:`](/documentation/MLCompute/MLCTensor/init(width:height:featureChannelCount:batchSize:data:))

Creates a tensor with the sizes, number of feature channels, and data you specify.

[`+  tensorWithWidth:height:featureChannelCount:batchSize:data:dataType:`](/documentation/MLCompute/MLCTensor/init(width:height:featureChannelCount:batchSize:data:dataType:))

Creates a tensor with the sizes, number of feature channels, data, and data type you specify.

[`+  tensorWithWidth:height:featureChannelCount:batchSize:fillWithData:dataType:`](/documentation/MLCompute/MLCTensor/init(width:height:featureChannelCount:batchSize:fillWithData:dataType:))

Creates a tensor with the sizes and number of feature channels, and filled with the data and type you specify.

[`+  tensorWithWidth:height:featureChannelCount:batchSize:randomInitializerType:`](/documentation/MLCompute/MLCTensor/init(width:height:featureChannelCount:batchSize:randomInitializerType:))

Creates a tensor with the sizes, number of feature channels, and random data using the random initializer type you specify.

### Creating Tensors by Specifying Sequence Lengths

[`+  tensorWithSequenceLength:featureChannelCount:batchSize:`](/documentation/MLCompute/MLCTensor/init(sequenceLength:featureChannelCount:batchSize:))

Creates a tensor without data, with the sequence length, number of feature channels, and batch size you specify.

[`+  tensorWithSequenceLength:featureChannelCount:batchSize:data:`](/documentation/MLCompute/MLCTensor/init(sequenceLength:featureChannelCount:batchSize:data:))

Creates a tensor with the sequence length, number of feature channels, batch size, and data you specify.

[`init(sequenceLengths:sortedSequences:featureChannelCount:batchSize:data:)`](/documentation/MLCompute/MLCTensor/init(sequenceLengths:sortedSequences:featureChannelCount:batchSize:data:))

Creates a tensor with the sequence lengths, sorting indicator, number of feature channels, batch size, and data you specify.

[`+  tensorWithSequenceLengths:sortedSequences:featureChannelCount:batchSize:data:`](/documentation/MLCompute/MLCTensor/tensorWithSequenceLengths:sortedSequences:featureChannelCount:batchSize:data:)

Creates a tensor with the sequence lengths, sorting indicator, number of feature channels, batch size, and data you specify.

[`+  tensorWithSequenceLength:featureChannelCount:batchSize:randomInitializerType:`](/documentation/MLCompute/MLCTensor/init(sequenceLength:featureChannelCount:batchSize:randomInitializerType:))

Creates a tensor with the sequence length, number of feature channels, batch size, and random initializer type you specify.

[`init(sequenceLengths:sortedSequences:featureChannelCount:batchSize:randomInitializerType:)`](/documentation/MLCompute/MLCTensor/init(sequenceLengths:sortedSequences:featureChannelCount:batchSize:randomInitializerType:))

Creates a tensor with the sequence lengths, sorting indicator, number of feature channels, batch size, and random initializer type you specify.

[`+  tensorWithSequenceLengths:sortedSequences:featureChannelCount:batchSize:randomInitializerType:`](/documentation/MLCompute/MLCTensor/tensorWithSequenceLengths:sortedSequences:featureChannelCount:batchSize:randomInitializerType:)

Creates a tensor with the sequence lengths, sorting indicator, number of feature channels, batch size, and random initializer type you specify.

### Inspecting Tensors

[`tensorID`](/documentation/MLCompute/MLCTensor/tensorID)

A number that uniquely identifies the tensor, which the framework assigns when it creates a tensor.

[`descriptor`](/documentation/MLCompute/MLCTensor/descriptor)

The configuration object you use to create a tensor.

[`data`](/documentation/MLCompute/MLCTensor/data)

The tensor data.

[`label`](/documentation/MLCompute/MLCTensor/label)

A string that identifes this tensor.

[`device`](/documentation/MLCompute/MLCTensor/device)

The device associated with this tensor.

[`optimizerData`](/documentation/MLCompute/MLCTensor/optimizerData)

An array that contains optimizer buffers you specify when you create a tensor parameter.

[`optimizerDeviceData`](/documentation/MLCompute/MLCTensor/optimizerDeviceData)

An array that contains the device optimizer buffers you specify.

[`hasValidNumerics`](/documentation/MLCompute/MLCTensor/hasValidNumerics)

A Boolean that indicates whether a tensor contains NaN or INF values.

[`MLCTensorOptimizerDeviceData`](/documentation/MLCompute/MLCTensorOptimizerDeviceData)

An encapsulation of the device memory associated with a tensor that an optimizer uses.

### Converting Tensors

[`-  tensorByQuantizingToType:scale:bias:`](/documentation/MLCompute/MLCTensor/quantized(to:scale:bias:))

Converts a 32-bit floating-point tensor with the scale and bias you specify.

[`-  tensorByQuantizingToType:scale:bias:axis:`](/documentation/MLCompute/MLCTensor/quantized(to:scale:bias:axis:))

Converts a 32-bit floating-point tensor with the scale and bias you specify.

[`-  tensorByDequantizingToType:scale:bias:`](/documentation/MLCompute/MLCTensor/dequantized(to:scale:zeroPoint:))

Converts a tensor you quantize to a 32-bit floating-point tensor.

[`-  tensorByDequantizingToType:scale:bias:axis:`](/documentation/MLCompute/MLCTensor/dequantized(to:scale:bias:axis:))

Converts a tensor you quantize to a 32-bit floating-point tensor.

### Managing Tensor Data

[`-  synchronizeData`](/documentation/MLCompute/MLCTensor/synchronizeData())

Synchronizes the data in host memory.

[`-  synchronizeOptimizerData`](/documentation/MLCompute/MLCTensor/synchronizeOptimizerData())

Synchronizes the optimizer data in host memory.

[`-  copyDataFromDeviceMemoryToBytes:length:synchronizeWithDevice:`](/documentation/MLCompute/MLCTensor/copyDataFromDeviceMemory(toBytes:length:synchronizeWithDevice:))

Copies tensor data from device memory to user-specified memory.

[`-  bindAndWriteData:toDevice:`](/documentation/MLCompute/MLCTensor/bindAndWriteData(_:to:))

Associates the given data to the tensor, and if the device is a GPU, also copies the data to the device memory.

[`-  bindOptimizerData:deviceData:`](/documentation/MLCompute/MLCTensor/bindOptimizerData(_:deviceData:))

Associates the optimizer and device data buffers you specify to the tensor.

## Relationships

### Conforms To

[`CVarArg`](/documentation/Swift/CVarArg)

[`Hashable`](/documentation/Swift/Hashable)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)