SMAUG
Simulating Machine Learning Applications on gem5-Aladdin
|
1 #include "smaug/core/tensor.h"
4 #include "smaug/utility/thread_pool.h"
9 TensorShapeProto* shapeProto =
new TensorShapeProto();
10 *shapeProto->mutable_dims() = { dims_.begin(), dims_.end() };
11 shapeProto->set_layout(layout);
12 shapeProto->set_alignment(alignment);
17 TensorProto* tensorProto =
new TensorProto();
18 tensorProto->set_name(
name);
19 tensorProto->set_data_type(dataType);
23 TensorData* protoData =
new TensorData();
24 void* rawPtr = tensorData.get();
28 protoData->mutable_half_data()->Resize(
29 (
shape.storageSize() + 1) / 2, 0);
30 memcpy(protoData->mutable_half_data()->mutable_data(), rawPtr,
31 shape.storageSize() *
sizeof(float16));
34 protoData->mutable_float_data()->Resize(
shape.storageSize(), 0);
35 memcpy(protoData->mutable_float_data()->mutable_data(), rawPtr,
36 shape.storageSize() *
sizeof(
float));
39 protoData->mutable_double_data()->Resize(
shape.storageSize(), 0);
40 memcpy(protoData->mutable_double_data()->mutable_data(), rawPtr,
41 shape.storageSize() *
sizeof(
double));
44 protoData->mutable_int_data()->Resize(
shape.storageSize(), 0);
45 memcpy(protoData->mutable_int_data()->mutable_data(), rawPtr,
46 shape.storageSize() *
sizeof(
int));
49 protoData->mutable_int64_data()->Resize(
shape.storageSize(), 0);
50 memcpy(protoData->mutable_int64_data()->mutable_data(), rawPtr,
51 shape.storageSize() *
sizeof(int64_t));
54 protoData->mutable_bool_data()->Resize(
shape.storageSize(), 0);
55 memcpy(protoData->mutable_bool_data()->mutable_data(), rawPtr,
56 shape.storageSize() *
sizeof(
bool));
59 assert(
false &&
"Unknown data type!");
61 tensorProto->set_allocated_data(protoData);
72 const std::vector<int>& origin,
83 void* TiledTensor::tileCopyWorker(
void* _args) {
84 auto args =
reinterpret_cast<CopyTilesArgs*
>(_args);
86 int start = args->start;
87 int numTiles = args->numTiles;
89 for (
int i = start; i < start + numTiles; i++) {
90 Tile* tile = tiledTensor->getTile(i);
101 int totalNumTiles =
tiles.size();
102 int numTilesPerThread = std::ceil(totalNumTiles * 1.0 /
threadPool->
size());
103 int remainingTiles = totalNumTiles;
104 while (remainingTiles > 0) {
105 int numTiles = std::min(numTilesPerThread, remainingTiles);
107 this, totalNumTiles - remainingTiles, numTiles, op);
110 assert(cpuid != -1 &&
"Failed to dispatch thread!");
111 remainingTiles -= numTiles;
122 "TiledTensor must have the original tensor to copy data from!");
124 for (
auto index = startIndex(); !index.end(); ++index)
140 "Must set the tile's origin in the original tensor!");
144 tile->
tensor->getShape().storageSize());
146 std::vector<int> dstOrigin(tile->
tensor->ndims(), 0);
148 tile->
tensor->getShape().dims());
155 "TiledTensor must have the original tensor to copy data to!");
157 int ndims = tensorShape.ndims();
158 if (
tiles.size() == 1) {
164 for (
auto index = startIndex(); !index.end(); ++index)
174 "Must set the tile's origin in the original tensor!");
178 tile->
tensor->getShape().storageSize());
180 std::vector<int> srcOrigin(tile->
tensor->ndims(), 0);
185 tile->
tensor->getShape().dims());
Tensor represents a single multi-dimensional array of data.
TensorProto * asTensorProto()
Serializes this Tensor to a TensorProto.
A tile is a rectangular portion of a larger Tensor.
ThreadPool * threadPool
The user-space thread pool used by SMAUG to run multithreaded tasks.
void joinThreadPool()
Wait for all threads in the pool to finish work.
DataStorageFormat dataFormat
Indicates the compression format of the data.
std::string name
Name of of the Tensor.
Tensor * getTileWithData(int index)
Returns a Tensor at the specified tile position, with data copied from the original tensor.
TensorShape shape
Shape of the Tensor.
void copyTensorRegion(Tensor *dest, Tensor *src, std::vector< int > destOrigin, std::vector< int > srcOrigin, std::vector< int > regionSize)
Copies a region of a source Tensor to a corresponding region in a destination Tensor.
Tensor * origTensor
The original Tensor that was tiled into this TiledTensor.
bool dataFilled
True if all the tiles have data filled.
void gatherDataFromTile(Tile *tile)
Copy data from this tile to the original Tensor.
A multidimensional container of Tensors.
bool hasOrigin
True if the tile has its origin set.
Utility functions for copying/printing/tiling tensors.
void setTile(int index, const std::vector< int > &origin, Tensor *tensor, bool copyData)
Set the specified tile to the provided Tensor, and optionally copy data into it.
void copyRawTensorData(Tensor *dest, Tensor *src, int destOffset, int srcOffset, int copySize)
Directly copies a linear region of memory from dest to src, without taking dimensions/padding into ac...
bool fastForwardMode
True if we are simulating in fast-forward mode.
bool hasData
True if we have copied data to this tile.
TensorShapeProto * asTensorShapeProto()
Return a TensorShapeProto that serializes this TensorShape.
TensorShape describes the shape of a Tensor.
void parallelCopyTileData(TileDataOperation op)
Split the work (data filling or gathering) across multiple threads.
bool useRawTensor
True if we should use copyRawTensorData() for copying data.
The smaug namespace is the parent namespace of all C++ code in SMAUG.
TileDataOperation
Specifies what to do with the data in the original Tensor and tiles.
Tensor * tensor
The new smaller Tensor of this tile.
int size() const
Returns the number of worker threads.
void untile()
Copies data from the TiledTensor into the original Tensor.
int dispatchThread(WorkerThreadFunc func, void *args)
Dispatch the function to a worker in the thread pool.
void copyDataToAllTiles()
Copies data (if needed) to all the tiles from the original Tensor.
@ Scatter
Copies data from a contiguous Tensor to the tiles.
std::vector< int > origin
The tile's coordinate origins in the original tensor.
std::vector< Tile > tiles
The list of Tiles, indexed using a TensorIndexIterator.
void copyDataToTile(Tile *tile)
Copy data (if needed) to this tile from the original Tensor.
@ Gather
Copies data from the tiles to a contiguous Tensor.