-
Notifications
You must be signed in to change notification settings - Fork 39
Make device interface generic #606
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
Changes from all commits
0143d4e
afdca1f
3edf0ae
32d9598
79c84c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include "src/torchcodec/_core/DeviceInterface.h" | ||
|
||
namespace facebook::torchcodec { | ||
|
||
class CudaDevice : public DeviceInterface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The term "device" should be reserved for the concept of pytorch device object. Here, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NicolasHug : no problem, will do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NicolasHug : filed #626 |
||
public: | ||
CudaDevice(const torch::Device& device); | ||
|
||
virtual ~CudaDevice(); | ||
|
||
std::optional<const AVCodec*> findCodec(const AVCodecID& codecId) override; | ||
|
||
void initializeContext(AVCodecContext* codecContext) override; | ||
|
||
void convertAVFrameToFrameOutput( | ||
const SingleStreamDecoder::VideoStreamOptions& videoStreamOptions, | ||
UniqueAVFrame& avFrame, | ||
SingleStreamDecoder::FrameOutput& frameOutput, | ||
std::optional<torch::Tensor> preAllocatedOutputTensor = | ||
std::nullopt) override; | ||
|
||
private: | ||
AVBufferRef* ctx_ = nullptr; | ||
}; | ||
|
||
} // namespace facebook::torchcodec |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include "src/torchcodec/_core/DeviceInterface.h" | ||
#include <map> | ||
#include <mutex> | ||
|
||
namespace facebook::torchcodec { | ||
|
||
namespace { | ||
std::mutex g_interface_mutex; | ||
std::map<torch::DeviceType, CreateDeviceInterfaceFn> g_interface_map; | ||
|
||
std::string getDeviceType(const std::string& device) { | ||
size_t pos = device.find(':'); | ||
if (pos == std::string::npos) { | ||
return device; | ||
} | ||
return device.substr(0, pos); | ||
} | ||
|
||
} // namespace | ||
|
||
bool registerDeviceInterface( | ||
torch::DeviceType deviceType, | ||
CreateDeviceInterfaceFn createInterface) { | ||
std::scoped_lock lock(g_interface_mutex); | ||
TORCH_CHECK( | ||
g_interface_map.find(deviceType) == g_interface_map.end(), | ||
"Device interface already registered for ", | ||
deviceType); | ||
g_interface_map.insert({deviceType, createInterface}); | ||
return true; | ||
} | ||
|
||
torch::Device createTorchDevice(const std::string device) { | ||
// TODO: remove once DeviceInterface for CPU is implemented | ||
if (device == "cpu") { | ||
return torch::kCPU; | ||
} | ||
|
||
std::scoped_lock lock(g_interface_mutex); | ||
std::string deviceType = getDeviceType(device); | ||
auto deviceInterface = std::find_if( | ||
g_interface_map.begin(), | ||
g_interface_map.end(), | ||
[&](const std::pair<torch::DeviceType, CreateDeviceInterfaceFn>& arg) { | ||
return device.rfind( | ||
torch::DeviceTypeName(arg.first, /*lcase*/ true), 0) == 0; | ||
}); | ||
TORCH_CHECK( | ||
dvrogozh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
deviceInterface != g_interface_map.end(), "Unsupported device: ", device); | ||
|
||
return torch::Device(device); | ||
} | ||
|
||
std::unique_ptr<DeviceInterface> createDeviceInterface( | ||
const torch::Device& device) { | ||
auto deviceType = device.type(); | ||
// TODO: remove once DeviceInterface for CPU is implemented | ||
if (deviceType == torch::kCPU) { | ||
return nullptr; | ||
} | ||
|
||
std::scoped_lock lock(g_interface_mutex); | ||
TORCH_CHECK( | ||
g_interface_map.find(deviceType) != g_interface_map.end(), | ||
"Unsupported device: ", | ||
device); | ||
|
||
return std::unique_ptr<DeviceInterface>(g_interface_map[deviceType](device)); | ||
} | ||
|
||
} // namespace facebook::torchcodec |
Uh oh!
There was an error while loading. Please reload this page.