Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tensor] Refactorize Tensor Class to TensorV2 @open sesame 03/26 12:26 #2500

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Applications/LLaMA/jni/custom_multi_head_attention_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,66 +272,66 @@ void MultiHeadAttentionLayer::finalize(InitLayerContext &context) {
{batch_size, 1, query_height, num_heads * projected_query_dim_prop},
activation_type);
weight_idx[AttentionParams::projected_query] = context.requestTensor(
projected_query_dim, "projected_query", Tensor::Initializer::NONE, true,
projected_query_dim, "projected_query", Initializer::NONE, true,
TensorLifespan::ITERATION_LIFESPAN);
/** tensor for output of key fc */
TensorDim projected_key_dim(
{batch_size, 1, key_height, num_heads * projected_key_dim_prop},
activation_type);
weight_idx[AttentionParams::projected_key] = context.requestTensor(
projected_key_dim, "projected_key", Tensor::Initializer::NONE, true,
TensorLifespan::ITERATION_LIFESPAN);
weight_idx[AttentionParams::projected_key] =
context.requestTensor(projected_key_dim, "projected_key", Initializer::NONE,
true, TensorLifespan::ITERATION_LIFESPAN);
/** tensor for output of value fc */
TensorDim projected_value_dim(
{batch_size, 1, value_height, num_heads * projected_value_dim_prop},
activation_type);
weight_idx[AttentionParams::projected_value] = context.requestTensor(
projected_value_dim, "projected_value", Tensor::Initializer::NONE, true,
projected_value_dim, "projected_value", Initializer::NONE, true,
TensorLifespan::ITERATION_LIFESPAN);

TensorDim cache_key_dim(
{batch_size, 1, max_timestep, num_heads * projected_key_dim_prop},
activation_type);
weight_idx[AttentionParams::cache_key] =
context.requestTensor(cache_key_dim, "cache_key", Tensor::Initializer::NONE,
true, TensorLifespan::MAX_LIFESPAN);
context.requestTensor(cache_key_dim, "cache_key", Initializer::NONE, true,
TensorLifespan::MAX_LIFESPAN);

TensorDim cache_value_dim(
{batch_size, 1, max_timestep, num_heads * projected_value_dim_prop},
activation_type);
weight_idx[AttentionParams::cache_value] = context.requestTensor(
cache_value_dim, "cache_value", Tensor::Initializer::NONE, true,
TensorLifespan::MAX_LIFESPAN);
weight_idx[AttentionParams::cache_value] =
context.requestTensor(cache_value_dim, "cache_value", Initializer::NONE,
true, TensorLifespan::MAX_LIFESPAN);

if (provide_attention_mask) {
/** Intended comment for bool type mask */
// TensorDim attention_mask_dim(
// {batch_size, num_heads, query_height, key_height});
// weight_idx[AttentionParams::attention_mask] = context.requestTensor(
// attention_mask_dim, "attention_mask", Tensor::Initializer::NONE, false,
// attention_mask_dim, "attention_mask", Initializer::NONE, false,
// TensorLifespan::FORWARD_FUNC_LIFESPAN);
}
/** tensor for attention weight */
TensorDim attention_weight_dim(
{batch_size, num_heads, query_height, key_height}, activation_type);
weight_idx[AttentionParams::attention_weight] = context.requestTensor(
attention_weight_dim, "attention_weight", Tensor::Initializer::NONE, true,
attention_weight_dim, "attention_weight", Initializer::NONE, true,
TensorLifespan::ITERATION_LIFESPAN);
if (dropout_rate > epsilon) {
/** tensor for dropout mask */
TensorDim dropout_mask_dim(
{batch_size, num_heads, query_height, key_height}, activation_type);
weight_idx[AttentionParams::dropout_mask] = context.requestTensor(
dropout_mask_dim, "dropout_mask", Tensor::Initializer::NONE, false,
TensorLifespan::ITERATION_LIFESPAN);
weight_idx[AttentionParams::dropout_mask] =
context.requestTensor(dropout_mask_dim, "dropout_mask", Initializer::NONE,
false, TensorLifespan::ITERATION_LIFESPAN);
}

/** tensor for attention output */
TensorDim attention_output_dim(
{batch_size, 1, query_height, num_heads * projected_value_dim_prop},
activation_type);
weight_idx[AttentionParams::attention_output] = context.requestTensor(
attention_output_dim, "attention_output", Tensor::Initializer::NONE, true,
attention_output_dim, "attention_output", Initializer::NONE, true,
TensorLifespan::ITERATION_LIFESPAN);

TensorDim output_dim({batch_size, 1, query_height, output_shape},
Expand Down
4 changes: 2 additions & 2 deletions Applications/LLaMA/jni/rms_norm.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class RMS_NORM_GAMMA_INIT final
/**
* @brief Construct a RMS_NORM_GAMMA_INIT object
*/
RMS_NORM_GAMMA_INIT(nntrainer::Tensor::Initializer value =
nntrainer::Tensor::Initializer::ONES) {
RMS_NORM_GAMMA_INIT(
nntrainer::Initializer value = nntrainer::Initializer::ONES) {
set(value);
};

Expand Down
75 changes: 35 additions & 40 deletions Applications/YOLOv2/jni/yolo_v2_loss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,141 +319,136 @@ void YoloV2LossLayer::finalize(nntrainer::InitLayerContext &context) {
nntrainer::TensorDim bbox_x_pred_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_x_pred] = context.requestTensor(
bbox_x_pred_dim, "bbox_x_pred", nntrainer::Tensor::Initializer::NONE, true,
bbox_x_pred_dim, "bbox_x_pred", nntrainer::Initializer::NONE, true,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_y_pred_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_y_pred] = context.requestTensor(
bbox_y_pred_dim, "bbox_y_pred", nntrainer::Tensor::Initializer::NONE, true,
bbox_y_pred_dim, "bbox_y_pred", nntrainer::Initializer::NONE, true,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_w_pred_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_w_pred] = context.requestTensor(
bbox_w_pred_dim, "bbox_w_pred", nntrainer::Tensor::Initializer::NONE, true,
bbox_w_pred_dim, "bbox_w_pred", nntrainer::Initializer::NONE, true,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_h_pred_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_h_pred] = context.requestTensor(
bbox_h_pred_dim, "bbox_h_pred", nntrainer::Tensor::Initializer::NONE, true,
bbox_h_pred_dim, "bbox_h_pred", nntrainer::Initializer::NONE, true,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim confidence_pred_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::confidence_pred] =
context.requestTensor(confidence_pred_dim, "confidence_pred",
nntrainer::Tensor::Initializer::NONE, true,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);
wt_idx[YoloV2LossParams::confidence_pred] = context.requestTensor(
confidence_pred_dim, "confidence_pred", nntrainer::Initializer::NONE, true,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim class_pred_dim(batch_size,
grid_height_number * grid_width_number,
NUM_ANCHOR, class_number);
wt_idx[YoloV2LossParams::class_pred] = context.requestTensor(
class_pred_dim, "class_pred", nntrainer::Tensor::Initializer::NONE, true,
class_pred_dim, "class_pred", nntrainer::Initializer::NONE, true,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_w_pred_anchor_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_w_pred_anchor] =
context.requestTensor(bbox_w_pred_anchor_dim, "bbox_w_pred_anchor",
nntrainer::Tensor::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);
wt_idx[YoloV2LossParams::bbox_w_pred_anchor] = context.requestTensor(
bbox_w_pred_anchor_dim, "bbox_w_pred_anchor", nntrainer::Initializer::NONE,
false, nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_h_pred_anchor_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_h_pred_anchor] =
context.requestTensor(bbox_h_pred_anchor_dim, "bbox_h_pred_anchor",
nntrainer::Tensor::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);
wt_idx[YoloV2LossParams::bbox_h_pred_anchor] = context.requestTensor(
bbox_h_pred_anchor_dim, "bbox_h_pred_anchor", nntrainer::Initializer::NONE,
false, nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_x_gt_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_x_gt] = context.requestTensor(
bbox_x_gt_dim, "bbox_x_gt", nntrainer::Tensor::Initializer::NONE, false,
bbox_x_gt_dim, "bbox_x_gt", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_y_gt_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_y_gt] = context.requestTensor(
bbox_y_gt_dim, "bbox_y_gt", nntrainer::Tensor::Initializer::NONE, false,
bbox_y_gt_dim, "bbox_y_gt", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_w_gt_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_w_gt] = context.requestTensor(
bbox_w_gt_dim, "bbox_w_gt", nntrainer::Tensor::Initializer::NONE, false,
bbox_w_gt_dim, "bbox_w_gt", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_h_gt_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_h_gt] = context.requestTensor(
bbox_h_gt_dim, "bbox_h_gt", nntrainer::Tensor::Initializer::NONE, false,
bbox_h_gt_dim, "bbox_h_gt", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim confidence_gt_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::confidence_gt] = context.requestTensor(
confidence_gt_dim, "confidence_gt", nntrainer::Tensor::Initializer::NONE,
false, nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);
confidence_gt_dim, "confidence_gt", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim class_gt_dim(batch_size,
grid_height_number * grid_width_number,
NUM_ANCHOR, class_number);
wt_idx[YoloV2LossParams::class_gt] = context.requestTensor(
class_gt_dim, "class_gt", nntrainer::Tensor::Initializer::NONE, false,
class_gt_dim, "class_gt", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox_class_mask_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox_class_mask] =
context.requestTensor(bbox_class_mask_dim, "bbox_class_mask",
nntrainer::Tensor::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);
wt_idx[YoloV2LossParams::bbox_class_mask] = context.requestTensor(
bbox_class_mask_dim, "bbox_class_mask", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim iou_mask_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::iou_mask] = context.requestTensor(
iou_mask_dim, "iou_mask", nntrainer::Tensor::Initializer::NONE, false,
iou_mask_dim, "iou_mask", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox1_width_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox1_width] = context.requestTensor(
bbox1_width_dim, "bbox1_width", nntrainer::Tensor::Initializer::NONE, false,
bbox1_width_dim, "bbox1_width", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim bbox1_height_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::bbox1_height] = context.requestTensor(
bbox1_height_dim, "bbox1_height", nntrainer::Tensor::Initializer::NONE,
false, nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);
bbox1_height_dim, "bbox1_height", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim is_xy_min_max_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 4);
wt_idx[YoloV2LossParams::is_xy_min_max] = context.requestTensor(
is_xy_min_max_dim, "is_xy_min_max", nntrainer::Tensor::Initializer::NONE,
false, nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);
is_xy_min_max_dim, "is_xy_min_max", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim intersection_width_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::intersection_width] =
context.requestTensor(intersection_width_dim, "intersection_width",
nntrainer::Tensor::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);
wt_idx[YoloV2LossParams::intersection_width] = context.requestTensor(
intersection_width_dim, "intersection_width", nntrainer::Initializer::NONE,
false, nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim intersection_height_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::intersection_height] =
context.requestTensor(intersection_height_dim, "intersection_height",
nntrainer::Tensor::Initializer::NONE, false,
nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);

nntrainer::TensorDim unions_dim(
batch_size, grid_height_number * grid_width_number, NUM_ANCHOR, 1);
wt_idx[YoloV2LossParams::unions] = context.requestTensor(
unions_dim, "unions", nntrainer::Tensor::Initializer::NONE, false,
unions_dim, "unions", nntrainer::Initializer::NONE, false,
nntrainer::TensorLifespan::FORWARD_DERIV_LIFESPAN);
}

Expand Down
Loading
Loading