@@ -730,166 +730,4 @@ class EmbeddingVar : public ResourceBase {
730730
731731} // namespace tensorflow
732732
733- #if GOOGLE_CUDA
734- namespace tensorflow {
735-
736- template <class K , class V >
737- class EmbeddingVarGPU : public ResourceBase {
738- public:
739- EmbeddingVarGPU (const string& name,
740- embedding::GPUHashMapKV<K, V>* kv,
741- Allocator* alloc,
742- const EmbeddingConfig& emb_cfg = EmbeddingConfig()):
743- name_ (name),
744- kv_ (kv),
745- default_value_ (nullptr ),
746- value_len_ (0 ),
747- emb_config_ (emb_cfg) {
748- alloc_ =
749- DisableGPUEVAllocatorFromEnvironment () ? alloc : gpu_ev_allocator ();
750- }
751-
752- Status Init () {
753- if (kv_ == nullptr ) {
754- return errors::InvalidArgument (" Error to construct EmbeddingVarGPU" );
755- } else {
756- return Status::OK ();
757- }
758- }
759-
760- Status Init (const Tensor& default_tensor,
761- int64 default_value_dim=1 ) {
762- if (DataTypeToEnum<V>::v () != default_tensor.dtype ()) {
763- return errors::InvalidArgument (
764- " EV's default_tensor DTYPE must be same as EmbeddingVar Value Type" );
765- } else if (kv_ == nullptr ) {
766- return errors::InvalidArgument (" Error to construct EmbeddingVarGPU" );
767- } else {
768- emb_config_.default_value_dim = default_value_dim;
769- value_len_ =
770- default_tensor.NumElements () / emb_config_.default_value_dim ;
771- kv_->SetValueLen (value_len_);
772- default_value_ = TypedAllocator::Allocate<V>(
773- alloc_, default_tensor.NumElements (), AllocationAttributes ());
774- auto default_tensor_flat = default_tensor.flat <V>();
775- cudaMemcpy (default_value_, &default_tensor_flat (0 ),
776- default_tensor.TotalBytes (), cudaMemcpyDeviceToDevice);
777- return Status::OK ();
778- }
779- }
780-
781- void SetInitialized () {
782- is_initialized_ = true ;
783- }
784-
785- bool IsInitialized () const {
786- return is_initialized_;
787- }
788-
789- void LookupOrCreateKey (const K* key, int32* item_idxs, size_t n,
790- const Eigen::GpuDevice& device, int64 update_version = -1 ) {
791- kv_->BatchLookupOrCreateKeys (key, n, item_idxs, device);
792- }
793-
794- void LookupOrCreate (const K* key, V* val, V* default_v,
795- int32 default_v_num, bool is_use_default_value_tensor,
796- size_t n, const Eigen::GpuDevice& device) {
797- kv_->BatchLookupOrCreate (key, val, default_v, default_v_num,
798- is_use_default_value_tensor, n, device);
799- }
800-
801- void GetSnapshot (K* keys, V* values, const Eigen::GpuDevice& device) {
802- kv_->GetSnapshot (keys, values, device);
803- }
804-
805- int64 Size () const {
806- return kv_->Size ();
807- }
808-
809- int64 ValueLen () const {
810- return value_len_;
811- }
812-
813- std::string DebugString () const {
814- return emb_config_.DebugString ();
815- }
816-
817- embedding::GPUHashMapKV<K, V>* kv () {
818- return kv_;
819- }
820-
821- int64 MinFreq () {
822- return emb_config_.filter_freq ;
823- }
824-
825- float GetL2WeightThreshold () {
826- return emb_config_.l2_weight_threshold ;
827- }
828-
829- int32 SlotNum () {
830- return (emb_config_.block_num * (1 + emb_config_.slot_num ));
831- }
832-
833- int32 EmbIdx () {
834- return emb_config_.emb_index ;
835- }
836-
837- V* DefaultValuePtr () {
838- return default_value_;
839- }
840-
841- void SetSlotNum (int64 slot_num) {
842- emb_config_.slot_num = slot_num;
843- }
844-
845- int64 GetSlotNum () {
846- return emb_config_.slot_num ;
847- }
848-
849- V* GetDefaultValuePtr () {
850- return default_value_;
851- }
852-
853- int64 GetDefaultValueDim () {
854- return emb_config_.default_value_dim ;
855- }
856-
857- Status Import (RestoreBuffer& restore_buff, int64 key_num,
858- int bucket_num, int64 partition_id, int64 partition_num,
859- bool is_filter, const Eigen::GpuDevice& device) {
860- return kv_->Import (restore_buff, key_num, bucket_num,
861- partition_id, partition_num, is_filter, device);
862- }
863-
864- private:
865- bool DisableGPUEVAllocatorFromEnvironment () {
866- bool disable_gpu_ev_allocator = false ;
867- ReadBoolFromEnvVar (" TF_DISABLE_GPU_EV_ALLOCATOR" , true ,
868- &disable_gpu_ev_allocator);
869- return disable_gpu_ev_allocator;
870- }
871-
872- private:
873- ~EmbeddingVarGPU () override {
874- if (emb_config_.is_primary () && emb_config_.primary_emb_index == 0 ) {
875- delete kv_;
876- }
877- TypedAllocator::Deallocate (alloc_, default_value_, value_len_);
878- }
879- TF_DISALLOW_COPY_AND_ASSIGN (EmbeddingVarGPU);
880-
881- private:
882- bool is_initialized_ = false ;
883- std::string name_;
884- embedding::GPUHashMapKV<K, V>* kv_ = nullptr ;
885- Allocator* alloc_ = nullptr ;
886- EmbeddingConfig emb_config_;
887- V* default_value_ = nullptr ;
888- int64 value_len_;
889- };
890-
891- } // namespace tensorflow
892-
893- #endif // GOOGLE_CUDA
894-
895733#endif // TENSORFLOW_CORE_FRAMEWORK_EMBEDDING_EMBEDDING_VAR_H_
0 commit comments