Skip to content

Commit

Permalink
add argmax operator
Browse files Browse the repository at this point in the history
  • Loading branch information
xinhaoc committed Jul 26, 2023
1 parent d047aa6 commit d0d61b5
Show file tree
Hide file tree
Showing 14 changed files with 701 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/flexflow/ffconst.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ enum OperatorType {
OP_GATHER, // https://pytorch.org/docs/stable/generated/torch.gather.html
OP_RMS_NORM,
OP_BEAM_TOPK,
OP_ARGMAX,
OP_INC_MULTIHEAD_SELF_ATTENTION,
OP_SPEC_INC_MULTIHEAD_SELF_ATTENTION,
OP_TREE_INC_MULTIHEAD_SELF_ATTENTION,
Expand Down
6 changes: 6 additions & 0 deletions include/flexflow/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ enum TaskIDs {
ARG_TOPK_INF_TASK_ID,
SAMPLING_INIT_TASK_ID,
SAMPLING_INF_TASK_ID,
ARGMAX_INIT_TASK_ID,
ARGMAX_INF_TASK_ID,
TRANSPOSE_INIT_TASK_ID,
TRANSPOSE_FWD_TASK_ID,
TRANSPOSE_BWD_TASK_ID,
Expand Down Expand Up @@ -315,6 +317,7 @@ class BeamTopK;
class SpecIncMultiHeadSelfAttention;
class IncMultiQuerySelfAttention;
class Sampling;
class ArgMax;
class Combine;
class Repartition;
class Reduction;
Expand Down Expand Up @@ -615,6 +618,7 @@ class FFModel {
int k,
bool sorted,
char const *name = NULL);
Tensor argmax(const Tensor input, char const *name = NULL);
Tensor sampling(const Tensor input, float top_p, char const *name = NULL);
Tensor multihead_attention(const Tensor query,
const Tensor key,
Expand Down Expand Up @@ -1067,6 +1071,8 @@ class FFModel {
BeamTopK *>,
std::unordered_map<std::pair<ParallelTensorShape, SamplingParams>,
Sampling *>,
std::unordered_map<std::pair<ParallelTensorShape, ArgMaxParams>,
ArgMax *>,
std::unordered_map<
std::pair<ParallelTensorShape, SpecIncMultiHeadSelfAttentionParams>,
SpecIncMultiHeadSelfAttention *>,
Expand Down
2 changes: 2 additions & 0 deletions include/flexflow/operator_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "flexflow/ops/aggregate_params.h"
#include "flexflow/ops/aggregate_spec_params.h"
#include "flexflow/ops/arg_topk_params.h"
#include "flexflow/ops/argmax_params.h"
#include "flexflow/ops/attention_params.h"
#include "flexflow/ops/batch_matmul_params.h"
#include "flexflow/ops/beam_topk_params.h"
Expand Down Expand Up @@ -73,6 +74,7 @@ using OperatorParameters = mp::variant<AggregateParams,
TopKParams,
ArgTopKParams,
SamplingParams,
ArgMaxParams,
SoftmaxParams,
TransposeParams,
RepartitionParams,
Expand Down
95 changes: 95 additions & 0 deletions include/flexflow/ops/argmax.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifndef _FLEXFLOW_ARG_MAX_H_
#define _FLEXFLOW_ARG_MAX_H_

#include "flexflow/inference.h"
#include "flexflow/model.h"
#include "flexflow/node.h"
#include "flexflow/ops/argmax_params.h"

namespace FlexFlow {

class ArgMaxMeta : public OpMeta {
public:
#if defined(FF_USE_CUDA) || defined(FF_USE_HIP_CUDA)
cudnnTensorDescriptor_t inputTensor, outputTensor;
cudnnReduceTensorDescriptor_t reduceMaxDesc;
#else
miopenTensorDescriptor_t inputTensor, outputTensor;
miopenReduceTensorDescriptor_t reduceMaxDesc;
#endif
ArgMaxMeta(FFHandler handle,
Op const *op,
Legion::Domain const &input_domain,
Legion::Domain const &output_domain,
GenericTensorAccessorW input);
};

class ArgMax : public Op {
public:
using Params = ArgMaxParams;
using Input = ParallelTensor;
ArgMax(FFModel &model, const ParallelTensor input, char const *name);
ArgMax(FFModel &model, ArgMax const &other, const ParallelTensor input);
ArgMax(FFModel &model,
Params const &params,
Input const input,
char const *name = nullptr);
void init(FFModel const &) override;
void init_inference(FFModel const &,
std::vector<ParallelTensor> const &,
std::vector<ParallelTensor> const &,
MachineView const *mv = nullptr) override;
void forward(FFModel const &) override;
void backward(FFModel const &) override;
Legion::FutureMap inference(FFModel const &,
BatchConfig const &,
std::vector<ParallelTensor> const &,
std::vector<ParallelTensor> const &,
MachineView const *mv = nullptr) override;
void print_layer(FFModel const &model) override {
assert(0);
}
static Op *
create_operator_from_layer(FFModel &model,
Layer const *layer,
std::vector<ParallelTensor> const &inputs);

static OpMeta *init_task(Legion::Task const *task,
std::vector<Legion::PhysicalRegion> const &regions,
Legion::Context ctx,
Legion::Runtime *runtime);
static InferenceResult
inference_task(Legion::Task const *task,
std::vector<Legion::PhysicalRegion> const &regions,
Legion::Context ctx,
Legion::Runtime *runtime);
void serialize(Legion::Serializer &s) const override;
static PCG::Node deserialize(FFModel &ff,
Legion::Deserializer &d,
ParallelTensor inputs[],
int num_inputs);
Op *materialize(FFModel &ff,
ParallelTensor inputs[],
int num_inputs) const override;
bool measure_operator_cost(Simulator *sim,
MachineView const &pc,
CostMetrics &cost_metrics) const override;
template <typename DT>
static void forward_kernel(ArgMaxMeta const *m,
DT *input_ptr,
int *indices_ptr,
int length,
int batch_size,
ffStream_t stream);
static void forward_kernel_wrapper(ArgMaxMeta const *m,
GenericTensorAccessorW const &input,
GenericTensorAccessorW const &indices,
int batch_size);
Params get_params() const;

public:
};

}; // namespace FlexFlow

#endif
24 changes: 24 additions & 0 deletions include/flexflow/ops/argmax_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _FLEXFLOW_ARGMAX_PARAMS_H
#define _FLEXFLOW_ARGMAX_PARAMS_H

#include "flexflow/ffconst.h"
#include "flexflow/parallel_tensor.h"

namespace FlexFlow {

struct ArgMaxParams {
OperatorType op_type;
bool is_valid(ParallelTensorShape const &) const;
};
bool operator==(ArgMaxParams const &, ArgMaxParams const &);

} // namespace FlexFlow

namespace std {
template <>
struct hash<FlexFlow::ArgMaxParams> {
size_t operator()(FlexFlow::ArgMaxParams const &) const;
};
} // namespace std

#endif // _FLEXFLOW_ARGMAX_PARAMS_H
8 changes: 5 additions & 3 deletions inference/models/llama.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,18 @@ void LLAMA::create_llama_model(FFModel &ff,

Tensor output;
if (mode == BEAM_SEARCH_MODE) {
Tensor softmax = ff.softmax(dense, -1);
output = ff.beam_top_k(softmax, llama_config.max_beam_width, false);
// Tensor softmax = ff.softmax(dense, -1);
// output = ff.beam_top_k(softmax, llama_config.max_beam_width, false);
output = ff.argmax(dense);
} else {
// Tensor softmax = ff.softmax(dense, -1);
if (samplingConfig.do_sample) {
dense = ff.scalar_truediv(dense, samplingConfig.temperature, false);
Tensor softmax = ff.softmax(dense, -1);
output = ff.sampling(softmax, samplingConfig.topp);
} else {
output = ff.arg_top_k(dense, /*k=*/1, false);
// output = ff.arg_top_k(dense, /*k=*/1, false);
output = ff.argmax(dense);
}
}

Expand Down
8 changes: 5 additions & 3 deletions inference/models/opt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,12 @@ void OPT::create_opt_model(FFModel &ff,

Tensor output;
if (mode == BEAM_SEARCH_MODE) {
Tensor softmax = ff.softmax(lm_head, -1);
output = ff.beam_top_k(softmax, opt_config.max_beam_width, false);
// Tensor softmax = ff.softmax(lm_head, -1);
// output = ff.beam_top_k(softmax, opt_config.max_beam_width, false);
output = ff.argmax(lm_head);
} else {
output = ff.arg_top_k(lm_head, /*k=*/1, false);
// output = ff.arg_top_k(lm_head, /*k=*/1, false);
output = ff.argmax(lm_head);
}

//------------------- compile the model --------------------------------
Expand Down
Loading

0 comments on commit d0d61b5

Please sign in to comment.