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

Describe problems for functor #3893

Closed

Conversation

reyoung
Copy link
Collaborator

@reyoung reyoung commented Sep 5, 2017

Part one of Functor Design Doc. It describes what problems should be resolved.

Related issue #3894

@reyoung reyoung force-pushed the feature/design_doc_of_functor branch 2 times, most recently from 7a7b90e to 131d961 Compare September 5, 2017 21:34
@@ -0,0 +1,17 @@
# Design Doc: Operator Functor

Copy link
Collaborator

@wangkuiyi wangkuiyi Sep 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions or Functors

#include <algorithm>
#include <iostream>

template <typename T>
T tanh(T x) {
  return static_cast<T>(1);
}

template <typename T, typename S>
T sigmoid(T x, S s) {
  return (tanh(x) + 1) * s;
}

template <typename Place>
class SigmodOpKernel /*: public framework::OpKernel*/ {
template <typename T, typename S>
class Sigmoid {
 public:
  Sigmoid(S scale) : scale_(scale) {}
  /*__host__ __device__*/ T operator(T x) const { return sigmoid(x, scale_); }
 private:
  const S scale_;
}
};

template <>
class SigmodOpKernel<CPUPlace> : public framework::OpKernel {};
 public:
  void Compute(ctx) {
    std::transform(
        ctx.Input<Tensor>("X")->data(),
        ctx.Input<Tensor>("X")->data() + product(ctx.Input<Tensor>("X")->dims()),
        ctx.Output<Tensor>("Out")->mutable_data(),
        Sigmoid(ctx->Attr<float>("scale")));
  }
};

// template <>
// class SigmodOpKernel<GPUPlace> : public framework::OpKernel {};
//  public:
//   void Compute(ctx) {
//     thrust::transform(
//         ctx.Input<Tensor>("X")->data(),
//         ctx.Input<Tensor>("X")->data() + product(ctx.Input<Tensor>("X")->dims()),
//         ctx.Output<Tensor>("Out")->mutable_data(),
//         Sigmoid(ctx->Attr<float>("scale")));
//   }
// };

@@ -0,0 +1,17 @@
# Design Doc: Operator Functor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design Doc: Functions

Following the overall design, this document is about the functions and how they compose into operators.

Basically, to the ease of definitions, functions operate on scalar values. But most operators operate on Tensors. For example:

@QiJune
Copy link
Member

QiJune commented Sep 6, 2017

Dose this doc only focus on element-wise operators or try to propose a general solution?
std::transform is not suitable for all operations.

@reyoung
Copy link
Collaborator Author

reyoung commented Sep 6, 2017

Closed since #3901

@reyoung reyoung closed this Sep 6, 2017
@reyoung reyoung deleted the feature/design_doc_of_functor branch September 6, 2017 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants