-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data): Add support for managed dataset and provide shortcut for …
…common framework (#751) * add Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * refactor a little bit Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * add Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * fix Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * fix Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * fix Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * lint Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> * fix Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai> Signed-off-by: Jinjing.Zhou <allenzhou@tensorchord.ai>
- Loading branch information
Showing
10 changed files
with
274 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// 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. | ||
|
||
package data | ||
|
||
type DataSource interface { | ||
Init() error | ||
GetHostDir() (string, error) | ||
Type() string | ||
Hash() (uint32, error) | ||
} |
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,54 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// 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. | ||
|
||
package data | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tensorchord/envd/pkg/home" | ||
|
||
"go.starlark.net/starlark" | ||
) | ||
|
||
type EnvdManagedDataSource struct { | ||
name string | ||
hostDataDir string | ||
} | ||
|
||
func (e *EnvdManagedDataSource) Init() error { | ||
manager := home.GetManager() | ||
hostDataDir, err := manager.InitDataDir(e.name) | ||
if err != nil { | ||
return err | ||
} | ||
e.hostDataDir = hostDataDir | ||
return nil | ||
} | ||
|
||
func (e *EnvdManagedDataSource) GetHostDir() (string, error) { | ||
return e.hostDataDir, nil | ||
} | ||
|
||
func (e *EnvdManagedDataSource) Type() string { | ||
return "envd managed data source" | ||
} | ||
|
||
func (e *EnvdManagedDataSource) Hash() (uint32, error) { | ||
return starlark.String(fmt.Sprintf("envd://%s", e.name)).Hash() | ||
} | ||
|
||
func NewEnvdManagedDataSource(name string) *EnvdManagedDataSource { | ||
return &EnvdManagedDataSource{name: name} | ||
} |
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,41 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// 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. | ||
|
||
package home | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type dataManager interface { | ||
InitDataDir(name string) (string, error) | ||
} | ||
|
||
func (m *generalManager) InitDataDir(name string) (string, error) { | ||
newDataDir := filepath.Join(m.CacheDir(), "data", name) | ||
if _, err := os.Stat(newDataDir); !os.IsNotExist(err) { | ||
logrus.Infof("Data dir %s already exists, skipping creation", newDataDir) | ||
return newDataDir, nil | ||
} | ||
|
||
err := os.Mkdir(newDataDir, 0777) // Avoid UID/GID issues | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to create data dir") | ||
} | ||
return newDataDir, nil | ||
} |
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,21 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// 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. | ||
|
||
package data | ||
|
||
const ( | ||
ruleEnvdManagedDataSource = "data.envd" | ||
huggingFaceDatasetPath = "~/.cache/huggingface/datasets" | ||
dglFaceDatasetPath = "~/.dgl" | ||
) |
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,54 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// 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. | ||
|
||
package data | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"go.starlark.net/starlark" | ||
"go.starlark.net/starlarkstruct" | ||
|
||
envddata "github.com/tensorchord/envd/pkg/data" | ||
) | ||
|
||
var ( | ||
logger = logrus.WithField("frontend", "starlark") | ||
) | ||
|
||
var Module = &starlarkstruct.Module{ | ||
Name: "data", | ||
Members: starlark.StringDict{ | ||
"envd": starlark.NewBuiltin(ruleEnvdManagedDataSource, ruleValueEnvdManagedDataSource), | ||
"path": &starlarkstruct.Module{ | ||
Name: "path", | ||
Members: starlark.StringDict{ | ||
"huggingface": starlark.String(huggingFaceDatasetPath), | ||
"dgl": starlark.String(dglFaceDatasetPath), | ||
}}, | ||
}, | ||
} | ||
|
||
func ruleValueEnvdManagedDataSource(thread *starlark.Thread, _ *starlark.Builtin, | ||
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var name starlark.String | ||
|
||
if err := starlark.UnpackArgs(ruleEnvdManagedDataSource, args, kwargs, | ||
"name?", &name); err != nil { | ||
return nil, err | ||
} | ||
logger.Debugf("rule `%s` is invoked, name=%s", | ||
ruleEnvdManagedDataSource, name) | ||
|
||
return NewDataSourceValue(envddata.NewEnvdManagedDataSource(name.String())), nil | ||
} |
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,53 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// 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. | ||
|
||
package data | ||
|
||
import ( | ||
"go.starlark.net/starlark" | ||
|
||
envddata "github.com/tensorchord/envd/pkg/data" | ||
) | ||
|
||
type DataSourceValue struct { | ||
source envddata.DataSource | ||
} | ||
|
||
func (d DataSourceValue) Init() error { | ||
return d.source.Init() | ||
} | ||
|
||
func (d DataSourceValue) GetHostDir() (string, error) { | ||
return d.source.GetHostDir() | ||
} | ||
|
||
func (d DataSourceValue) String() string { | ||
return d.source.Type() | ||
} | ||
|
||
func (d DataSourceValue) Type() string { | ||
return d.source.Type() | ||
} | ||
|
||
func (d DataSourceValue) Freeze() {} | ||
|
||
func (d DataSourceValue) Truth() starlark.Bool { return true } | ||
|
||
func (d DataSourceValue) Hash() (uint32, error) { | ||
return d.source.Hash() | ||
} | ||
|
||
func NewDataSourceValue(source envddata.DataSource) *DataSourceValue { | ||
return &DataSourceValue{source: source} | ||
} |
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