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

chore: remove hyperparameters from projects table #9504

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 0 additions & 79 deletions master/internal/db/postgres_experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,85 +484,6 @@ func AddExperimentTx(
if err != nil {
return errors.Wrapf(err, "error inserting experiment %v", experiment)
}
if err = AddProjectHyperparameters(
ctx, idb, int32(experiment.ProjectID), []int32{int32(experiment.ID)}); err != nil {
return errors.Wrapf(err, "error updating hyperparameters")
}

return nil
}

// RemoveProjectHyperparameters take a list of experiment ids,
// recalculate their respective project hyper parameters.
func RemoveProjectHyperparameters(ctx context.Context, idb bun.IDB, experimentIDs []int32) error {
if idb == nil {
idb = Bun()
}
var projectIDs []int
err := idb.NewRaw(`WITH recursive flat (project_id, key, value) AS (
SELECT project_id, key, value
FROM experiments,
jsonb_each(config -> 'hyperparameters')
WHERE project_id IN (SELECT project_id WHERE id IN (?)) AND id NOT IN (?)
UNION
SELECT f.project_id, concat(f.key, '.', j.key), j.value
FROM flat f,
jsonb_each(f.value) j
WHERE jsonb_typeof(f.value) = 'object' AND f.value -> 'type' IS NULL
), flatten AS (
SELECT project_id, array_to_json(array_agg(DISTINCT key)) AS data
FROM flat
WHERE value -> 'type' IS NOT NULL
GROUP BY project_id), reset_hp AS (
UPDATE projects SET hyperparameters = '[]'::jsonb
WHERE id IN (SELECT project_id FROM experiments WHERE id IN (?))
)
UPDATE projects SET hyperparameters = flatten.data FROM flatten
WHERE flatten.project_id = projects.id`,
bun.In(experimentIDs), bun.In(experimentIDs), bun.In(experimentIDs)).Scan(ctx, &projectIDs)
if err != nil {
return err
}
if len(projectIDs) > 1 {
return fmt.Errorf("error removing experiment hyperparameters")
}
return nil
}

// AddProjectHyperparameters takes a list of project ids,
// combine their hyper parameters with existing one.
func AddProjectHyperparameters(
ctx context.Context, idb bun.IDB, projectID int32, experimentIDs []int32,
) error {
if idb == nil {
idb = Bun()
}
var projectIDs []int
err := idb.NewRaw(`WITH recursive flat (key, value) AS (
SELECT key, value
FROM experiments,
jsonb_each(config -> 'hyperparameters')
WHERE id IN (?)
UNION
SELECT concat(f.key, '.', j.key), j.value
FROM flat f,
jsonb_each(f.value) j
WHERE jsonb_typeof(f.value) = 'object' AND f.value -> 'type' IS NULL
), flatten AS (
SELECT key AS data
FROM flat WHERE value -> 'type' IS NOT NULL
UNION SELECT jsonb_array_elements_text(hyperparameters) FROM projects WHERE id = ?
), agg AS (
SELECT array_to_json(array_agg(DISTINCT flatten.data)) AS adata FROM flatten
)
UPDATE "projects" SET hyperparameters = agg.adata FROM agg WHERE (id = ?) RETURNING id`,
bun.In(experimentIDs), projectID, projectID).Scan(ctx, &projectIDs)
if err != nil {
return err
}
if len(projectIDs) > 1 {
return fmt.Errorf("error adding experiment hyperparameters")
}
return nil
}

Expand Down
52 changes: 0 additions & 52 deletions master/internal/db/postgres_experiments_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,58 +389,6 @@ func TestExperimentBestSearcherValidation(t *testing.T) {
require.InEpsilon(t, float32(5.0), val, 0.01)
}

func TestProjectHyperparameters(t *testing.T) {
ctx := context.Background()

require.NoError(t, etc.SetRootPath(RootFromDB))
db, closeDB := MustResolveTestPostgres(t)
defer closeDB()
MustMigrateTestPostgres(t, db, MigrationsFromDB)
user := RequireMockUser(t, db)

workspaceID, _ := RequireMockWorkspaceID(t, db, "")
projectID, _ := RequireMockProjectID(t, db, workspaceID, false)
exp0 := RequireMockExperimentParams(t, db, user, MockExperimentParams{
HParamNames: &[]string{"a", "b", "c"},
ProjectID: &projectID,
}, DefaultProjectID)
exp1 := RequireMockExperimentParams(t, db, user, MockExperimentParams{
HParamNames: &[]string{"b", "c", "d"},
ProjectID: &projectID,
}, DefaultProjectID)

require.ElementsMatch(t, []string{"a", "b", "c", "d"},
RequireGetProjectHParams(t, db, projectID))

require.NoError(t,
RemoveProjectHyperparameters(ctx, nil, []int32{int32(exp0.ID), int32(exp1.ID)}))
require.Empty(t, RequireGetProjectHParams(t, db, projectID))

require.NoError(t,
RemoveProjectHyperparameters(ctx, nil, []int32{int32(exp0.ID), int32(exp1.ID)}))
require.Empty(t, RequireGetProjectHParams(t, db, projectID))

require.NoError(t,
AddProjectHyperparameters(ctx, nil, int32(projectID), []int32{int32(exp0.ID)}))
require.ElementsMatch(t, []string{"a", "b", "c"},
RequireGetProjectHParams(t, db, projectID))

require.NoError(t,
AddProjectHyperparameters(ctx, nil, int32(projectID), []int32{int32(exp0.ID)}))
require.ElementsMatch(t, []string{"a", "b", "c"},
RequireGetProjectHParams(t, db, projectID))

require.NoError(t,
AddProjectHyperparameters(ctx, nil, int32(projectID), []int32{int32(exp1.ID)}))
require.ElementsMatch(t, []string{"a", "b", "c", "d"},
RequireGetProjectHParams(t, db, projectID))

require.NoError(t,
RemoveProjectHyperparameters(ctx, nil, []int32{int32(exp1.ID)}))
require.ElementsMatch(t, []string{}, // TODO(nickb): This is a bug in the query.
RequireGetProjectHParams(t, db, projectID))
}

func TestActiveLogPatternPolicies(t *testing.T) {
ctx := context.Background()
require.NoError(t, etc.SetRootPath(RootFromDB))
Expand Down
9 changes: 0 additions & 9 deletions master/internal/experiment/bulk_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,15 +772,6 @@ func MoveExperiments(
}
}()

err = db.RemoveProjectHyperparameters(ctx, tx, validIDs)
if err != nil {
return nil, err
}
err = db.AddProjectHyperparameters(ctx, tx, destinationProjectID, validIDs)
if err != nil {
return nil, err
}

var acceptedIDs []int32
if _, err = tx.NewUpdate().
ModelTableExpr("experiments as e").
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE projects DROP COLUMN hyperparameters;
Loading