-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
435 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#include "logging.h" | ||
#include "model.h" | ||
#include <cassert> | ||
|
||
namespace ark { | ||
|
||
extern const OpConfigMap EmbeddingConfigMap; | ||
|
||
EmbeddingOp::EmbeddingOp(OpPrecType prec_type, Tensor *input, Tensor *weight, | ||
Tensor *output, const std::string &name) | ||
: Op{OP_EMBEDDING, prec_type, {input, weight}, {output}, | ||
{}, name, &EmbeddingConfigMap, -1, | ||
true} | ||
{ | ||
} | ||
|
||
std::string EmbeddingOp::function_name(const OpConfig &cfg) const | ||
{ | ||
Tensor *input = this->inputs[0]; | ||
Tensor *weight = this->inputs[1]; | ||
Tensor *output = this->outputs[0]; | ||
|
||
auto in_dims = input->ldims.dims4(); | ||
auto in_shape = input->shape.dims4(); | ||
|
||
assert(in_dims[0] == 1); | ||
assert(in_shape[0] == 1); | ||
|
||
Dims new_in_dims{in_dims[1], in_dims[2], in_dims[3], 1}; | ||
Dims new_in_shape{in_shape[1], in_shape[2], in_shape[3], 1}; | ||
|
||
int emb_dim = weight->shape[-1]; | ||
return Op::function_name("ark::embedding", | ||
{{ | ||
new_in_dims, // InDims | ||
new_in_shape, // InShape | ||
weight->ldims.dims4(), // WeightDims | ||
weight->shape.dims4(), // WeightShape | ||
output->ldims.dims4(), // OutDims | ||
output->shape.dims4(), // OutShape | ||
emb_dim, // EmbeddingDim | ||
cfg.num_warps * 32, // NumThreads | ||
}}); | ||
} | ||
|
||
Tensor *Model::embedding(Tensor *input, Tensor *weight, Tensor *output, | ||
const std::string &name) | ||
{ | ||
assert(input != nullptr); | ||
assert(weight != nullptr); | ||
if (input->shape.ndims() > 3) { | ||
LOG(ERROR, "input shape ndims > 3: ", input->shape); | ||
} | ||
if (weight->shape.ndims() != 2) { | ||
LOG(ERROR, "weight shape ndims != 2: ", weight->shape); | ||
} | ||
OpPrecType pt = OP_PREC_NONE; | ||
if (weight->type == FP16) { | ||
pt = OP_PREC_FP16; | ||
} else if (weight->type == FP32) { | ||
pt = OP_PREC_FP32; | ||
} else { | ||
LOG(ERROR, "unsupported weight data type: ", weight->type); | ||
} | ||
auto emb_dim = weight->shape[-1]; | ||
|
||
std::vector<DimType> output_dims; | ||
for (int i = 0; i < input->shape.ndims(); ++i) { | ||
output_dims.push_back(input->shape[i]); | ||
} | ||
output_dims.push_back(emb_dim); | ||
Dims out_shape{output_dims}; | ||
if (output == nullptr) { | ||
output = this->tensor(out_shape, weight->type); | ||
} | ||
EmbeddingOp op{pt, input, weight, output, name}; | ||
return this->impl->add_op(op)[0]; | ||
} | ||
|
||
const OpConfigMap EmbeddingConfigMap = { | ||
{{OP_ARCH_CUDA_ANY, OP_PREC_ANY}, | ||
{ | ||
// NumWarps, SmemBytes, InDepsTiles, OutDepsTiles, SyncPre, SyncPost | ||
{1, 0, {{1, 1}, {1, -1}}, {{1, -1}}, true, false}, | ||
{2, 0, {{1, 1}, {1, -1}}, {{1, -1}}, true, false}, | ||
{4, 0, {{1, 1}, {1, -1}}, {{1, -1}}, true, false}, | ||
{8, 0, {{1, 1}, {1, -1}}, {{1, -1}}, true, false}, | ||
}}, | ||
}; | ||
|
||
} // namespace ark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#include "include/ark.h" | ||
#include "include/ark_utils.h" | ||
#include "ops_test_common.h" | ||
#include "unittest/unittest_utils.h" | ||
#include <cassert> | ||
#include <type_traits> | ||
|
||
template <typename T> | ||
void baseline_embedding(std::vector<void *> &outputs, | ||
const std::vector<ark::Dims> &output_shapes, | ||
const std::vector<void *> &inputs, | ||
const std::vector<ark::Dims> &input_shapes) | ||
{ | ||
T *out = static_cast<T *>(outputs[0]); | ||
int *in = static_cast<int *>(inputs[0]); | ||
T *weight = static_cast<T *>(inputs[1]); | ||
|
||
ark::Dims osh = output_shapes[0].dims4(); | ||
ark::Dims wsh = input_shapes[1].dims4(); | ||
|
||
assert(osh[3] == wsh[3]); | ||
|
||
int in_idx = 0; | ||
for (ark::DimType n = 0; n < osh[0]; ++n) { | ||
for (ark::DimType c = 0; c < osh[1]; ++c) { | ||
for (ark::DimType h = 0; h < osh[2]; ++h) { | ||
int weight_idx = in[in_idx++]; | ||
T *ptr = &weight[weight_idx * wsh[3]]; | ||
for (ark::DimType w = 0; w < osh[3]; ++w) { | ||
out[n * osh[1] * osh[2] * osh[3] + c * osh[2] * osh[3] + | ||
h * osh[3] + w] = ptr[w]; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
template <typename T> ark::unittest::State test_embedding() | ||
{ | ||
const int num_emb = 1000; | ||
const int emb_dim = 8192; | ||
|
||
ark::TensorType weight_type; | ||
if (std::is_same<T, float>::value) { | ||
weight_type = ark::FP32; | ||
} else { | ||
weight_type = ark::FP16; | ||
} | ||
|
||
ark::Model m; | ||
ark::Tensor *ti = m.tensor(ark::Dims(8, 3, 64), ark::INT32); | ||
ark::Tensor *tw = m.tensor(ark::Dims(num_emb, emb_dim), weight_type); | ||
ark::Tensor *to = m.embedding(ti, tw); | ||
|
||
ark::srand(); | ||
|
||
std::vector<int> ti_data; | ||
for (auto i = 0; i < ti->shape.size(); ++i) { | ||
// Random indices in [0, num_emb) | ||
ti_data.push_back(ark::rand() % num_emb); | ||
} | ||
auto tw_data = ark::utils::rand_array<T>(tw->shape.size(), 1.0); | ||
auto result = | ||
ark::op_test("embedding_fp32", m, {ti, tw}, {to}, baseline_embedding<T>, | ||
{ti_data.data(), tw_data.get()}); | ||
ark::op_test_log(result); | ||
return ark::unittest::SUCCESS; | ||
} | ||
|
||
ark::unittest::State test_embedding_fp32() | ||
{ | ||
return test_embedding<float>(); | ||
} | ||
|
||
ark::unittest::State test_embedding_fp16() | ||
{ | ||
return test_embedding<ark::half_t>(); | ||
} | ||
|
||
int main() | ||
{ | ||
ark::init(); | ||
UNITTEST(test_embedding_fp32); | ||
UNITTEST(test_embedding_fp16); | ||
return ark::unittest::SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.