forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
also add vect_add op for FPGA device, we can use follow code test it: import tensorflow as tf with tf.device("/fpga:0"): result = tf.user_ops.vect_add(4, 0) #result = tf.user_ops.vect_add([4, 3], [2, 1]) sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) print(sess.run(result)) +--------------------+ | python | | | +--------------------+ +--------------------+ | | | c/c++ | +--------------------+ +--------------------+ | opkernel(VectAdd) | +--------------------+ tensorflow +-------------------------------------+ +--------------------+ FPGA | API(fpga_mag.cc/ | | calc_vector_add) | +--------------------+ +--------------------+ | driver | | | +--------------------+ +--------+ +--------+ | FPGA | | FPGA | +--------+ +--------+
- Loading branch information
ZhaoHb
committed
Mar 22, 2019
1 parent
080d59b
commit 7456ddb
Showing
21 changed files
with
389 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "tensorflow/core/common_runtime/threadpiscine_device.h" | ||
|
||
#include "tensorflow/core/common_runtime/local_device.h" | ||
#include "tensorflow/core/framework/allocator.h" | ||
#include "tensorflow/core/framework/allocator_registry.h" | ||
#include "tensorflow/core/framework/device_base.h" | ||
#include "tensorflow/core/framework/op_kernel.h" | ||
#include "tensorflow/core/framework/tensor.pb_text.h" | ||
#include "tensorflow/core/framework/types.h" | ||
#include "tensorflow/core/graph/types.h" | ||
#include "tensorflow/core/lib/hash/hash.h" | ||
#include "tensorflow/core/platform/tracing.h" | ||
#include "tensorflow/core/platform/types.h" | ||
#include "tensorflow/core/public/session_options.h" | ||
|
||
namespace tensorflow { | ||
|
||
ThreadPiscineDevice::ThreadPiscineDevice(const SessionOptions& options, | ||
const string& name, Bytes memory_limit, | ||
const DeviceLocality& locality, | ||
Allocator* allocator) | ||
: LocalDevice(options, Device::BuildDeviceAttributes( | ||
name, DEVICE_FPGA, memory_limit, locality)), | ||
allocator_(allocator) {} | ||
|
||
ThreadPiscineDevice::~ThreadPiscineDevice() {} | ||
|
||
void ThreadPiscineDevice::Compute(OpKernel* op_kernel, OpKernelContext* context) { | ||
// When TraceMe profiling is off (which is the default), the | ||
// following TraceMe constructor is simply a conditional test of | ||
// false value. Measurements show that its overhead is negligible. | ||
port::Tracing::TraceMe trace_me(op_kernel->name(), op_kernel->type_string(), | ||
op_kernel->IsExpensive()); | ||
if (port::Tracing::IsActive()) { | ||
// TODO(pbar) We really need a useful identifier of the graph node. | ||
const uint64 id = Hash64(op_kernel->name()); | ||
port::Tracing::ScopedActivity region(port::Tracing::EventCategory::kCompute, | ||
id); | ||
op_kernel->Compute(context); | ||
} else { | ||
op_kernel->Compute(context); | ||
} | ||
} | ||
|
||
Allocator* ThreadPiscineDevice::GetAllocator(AllocatorAttributes attr) { | ||
return allocator_; | ||
} | ||
|
||
Status ThreadPiscineDevice::MakeTensorFromProto( | ||
const TensorProto& tensor_proto, const AllocatorAttributes alloc_attrs, | ||
Tensor* tensor) { | ||
if (tensor_proto.dtype() > 0 && tensor_proto.dtype() <= DataType_MAX) { | ||
Tensor parsed(tensor_proto.dtype()); | ||
if (parsed.FromProto(cpu_allocator(), tensor_proto)) { | ||
*tensor = std::move(parsed); | ||
return Status::OK(); | ||
} | ||
} | ||
return errors::InvalidArgument("Cannot parse tensor from proto: ", | ||
ProtoDebugString(tensor_proto)); | ||
} | ||
|
||
} // namespace tensorflow |
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,46 @@ | ||
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef TENSORFLOW_COMMON_RUNTIME_THREADPISCINE_DEVICE_H_ | ||
#define TENSORFLOW_COMMON_RUNTIME_THREADPISCINE_DEVICE_H_ | ||
|
||
#include "tensorflow/core/common_runtime/device_factory.h" | ||
#include "tensorflow/core/common_runtime/local_device.h" | ||
|
||
namespace tensorflow { | ||
|
||
// CPU device implementation. | ||
class ThreadPiscineDevice : public LocalDevice { | ||
public: | ||
ThreadPiscineDevice(const SessionOptions& options, const string& name, | ||
Bytes memory_limit, const DeviceLocality& locality, | ||
Allocator* allocator); | ||
~ThreadPiscineDevice() override; | ||
|
||
void Compute(OpKernel* op_kernel, OpKernelContext* context) override; | ||
Allocator* GetAllocator(AllocatorAttributes attr) override; | ||
Status MakeTensorFromProto(const TensorProto& tensor_proto, | ||
const AllocatorAttributes alloc_attrs, | ||
Tensor* tensor) override; | ||
|
||
Status Sync() override { return Status::OK(); } | ||
|
||
private: | ||
Allocator* allocator_; // Not owned | ||
}; | ||
|
||
} // namespace tensorflow | ||
|
||
#endif // TENSORFLOW_COMMON_RUNTIME_THREADPOOL_DEVICE_H_ |
50 changes: 50 additions & 0 deletions
50
tensorflow/core/common_runtime/threadpiscine_device_factory.cc
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,50 @@ | ||
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
// Register a factory that provides CPU devices. | ||
#include "tensorflow/core/common_runtime/threadpiscine_device.h" | ||
|
||
#include <vector> | ||
#include "tensorflow/core/common_runtime/device_factory.h" | ||
#include "tensorflow/core/framework/allocator.h" | ||
#include "tensorflow/core/public/session_options.h" | ||
|
||
namespace tensorflow { | ||
|
||
// TODO(zhifengc/tucker): Figure out the bytes of available RAM. | ||
class ThreadPiscineDeviceFactory : public DeviceFactory { | ||
public: | ||
Status CreateDevices(const SessionOptions& options, const string& name_prefix, | ||
std::vector<Device*>* devices) override { | ||
// TODO(zhifengc/tucker): Figure out the number of available CPUs | ||
// and/or NUMA configuration. | ||
int n = 1; | ||
auto iter = options.config.device_count().find("FPGA"); | ||
if (iter != options.config.device_count().end()) { | ||
n = iter->second; | ||
} | ||
for (int i = 0; i < n; i++) { | ||
string name = strings::StrCat(name_prefix, "/device:FPGA:", i); | ||
devices->push_back(new ThreadPiscineDevice( | ||
options, name, Bytes(256 << 20), DeviceLocality(), cpu_allocator())); | ||
} | ||
|
||
return Status::OK(); | ||
} | ||
}; | ||
|
||
REGISTER_LOCAL_DEVICE_FACTORY("FPGA", ThreadPiscineDeviceFactory, 60); | ||
|
||
} // namespace tensorflow |
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
Oops, something went wrong.