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

fix(controller): add validation for eval import/export #3018

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
15 changes: 15 additions & 0 deletions client/starwhale/base/client/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,21 @@ class ResponseMessageMapObjectObject(SwBaseModel):
data: Dict[str, Dict[str, Any]]


class FineTuneMigrationRequest(SwBaseModel):
ids: List[str]


class MigrationResult(SwBaseModel):
success: Optional[int] = None
fail: Optional[int] = None


class ResponseMessageMigrationResult(SwBaseModel):
code: str
message: str
data: MigrationResult


class RecordValueDesc(SwBaseModel):
key: str
value: Optional[Dict[str, Any]] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

import ai.starwhale.mlops.api.protocol.Code;
import ai.starwhale.mlops.api.protocol.ResponseMessage;
import ai.starwhale.mlops.api.protocol.ft.FineTuneMigrationRequest;
import ai.starwhale.mlops.api.protocol.ft.FineTuneSpaceCreateRequest;
import ai.starwhale.mlops.api.protocol.ft.FineTuneSpaceVo;
import ai.starwhale.mlops.api.protocol.model.ModelViewVo;
import ai.starwhale.mlops.common.IdConverter;
import ai.starwhale.mlops.configuration.FeaturesProperties;
import ai.starwhale.mlops.domain.ft.FineTuneAppService;
import ai.starwhale.mlops.domain.ft.FineTuneSpaceService;
import ai.starwhale.mlops.domain.ft.bo.MigrationResult;
import ai.starwhale.mlops.domain.ft.vo.FineTuneVo;
import ai.starwhale.mlops.domain.job.converter.UserJobConverter;
import ai.starwhale.mlops.domain.project.ProjectService;
Expand Down Expand Up @@ -179,27 +181,29 @@ public ResponseEntity<ResponseMessage<String>> releaseFt(
value = "/project/{projectId}/ftspace/{spaceId}/eval/import", produces = MediaType.APPLICATION_JSON_VALUE
)
@PreAuthorize("hasAnyRole('OWNER', 'MAINTAINER')")
public ResponseEntity<ResponseMessage<String>> importEval(
public ResponseEntity<ResponseMessage<MigrationResult>> importEval(
@PathVariable("projectId") Long projectId,
@PathVariable("spaceId") Long spaceId,
@RequestParam("ids") List<String> ids
@RequestBody FineTuneMigrationRequest request
) {
fineTuneAppService.importEvalFromCommon(projectId, spaceId, ids);
return ResponseEntity.ok(Code.success.asResponse(""));
return ResponseEntity.ok(Code.success.asResponse(
fineTuneAppService.importEvalFromCommon(projectId, spaceId, request.getIds())
));
}

@Operation(summary = "export to common eval summary")
@PostMapping(
value = "/project/{projectId}/ftspace/{spaceId}/eval/export", produces = MediaType.APPLICATION_JSON_VALUE
)
@PreAuthorize("hasAnyRole('OWNER', 'MAINTAINER')")
public ResponseEntity<ResponseMessage<String>> exportEval(
public ResponseEntity<ResponseMessage<MigrationResult>> exportEval(
@PathVariable("projectId") Long projectId,
@PathVariable("spaceId") Long spaceId,
@RequestParam("ids") List<String> ids
@RequestBody FineTuneMigrationRequest request
) {
fineTuneAppService.exportEvalToCommon(projectId, spaceId, ids);
return ResponseEntity.ok(Code.success.asResponse(""));
return ResponseEntity.ok(Code.success.asResponse(
fineTuneAppService.exportEvalToCommon(projectId, spaceId, request.getIds())
));
}

@GetMapping(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2022 Starwhale, Inc. 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.
*/

package ai.starwhale.mlops.api.protocol.ft;

import java.util.List;
import javax.validation.constraints.NotEmpty;
import lombok.Data;
import org.springframework.validation.annotation.Validated;

@Data
@Validated
public class FineTuneMigrationRequest {
@NotEmpty
List<String> ids;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import ai.starwhale.mlops.domain.dataset.bo.DatasetVersion;
import ai.starwhale.mlops.domain.evaluation.storage.EvaluationRepo;
import ai.starwhale.mlops.domain.event.EventService;
import ai.starwhale.mlops.domain.ft.bo.MigrationResult;
import ai.starwhale.mlops.domain.ft.mapper.FineTuneMapper;
import ai.starwhale.mlops.domain.ft.mapper.FineTuneSpaceMapper;
import ai.starwhale.mlops.domain.ft.po.FineTuneEntity;
Expand All @@ -45,6 +46,7 @@
import ai.starwhale.mlops.domain.job.spec.Env;
import ai.starwhale.mlops.domain.job.spec.JobSpecParser;
import ai.starwhale.mlops.domain.job.spec.StepSpec;
import ai.starwhale.mlops.domain.job.status.JobStatusMachine;
import ai.starwhale.mlops.domain.model.ModelDao;
import ai.starwhale.mlops.domain.model.ModelService;
import ai.starwhale.mlops.domain.model.bo.ModelVersion;
Expand Down Expand Up @@ -87,6 +89,8 @@ public class FineTuneAppService {

final JobMapper jobMapper;

final JobStatusMachine jobStatusMachine;

private final JobSpecParser jobSpecParser;

private final IdConverter idConverter;
Expand Down Expand Up @@ -116,7 +120,7 @@ public FineTuneAppService(
JobCreator jobCreator,
FineTuneMapper fineTuneMapper,
JobMapper jobMapper,
JobSpecParser jobSpecParser,
JobStatusMachine jobStatusMachine, JobSpecParser jobSpecParser,
IdConverter idConverter,
ModelDao modelDao,
@Value("${sw.instance-uri}") String instanceUri,
Expand All @@ -133,6 +137,7 @@ public FineTuneAppService(
this.jobCreator = jobCreator;
this.fineTuneMapper = fineTuneMapper;
this.jobMapper = jobMapper;
this.jobStatusMachine = jobStatusMachine;
this.jobSpecParser = jobSpecParser;
this.idConverter = idConverter;
this.modelDao = modelDao;
Expand Down Expand Up @@ -301,20 +306,51 @@ private FineTuneVo buildFineTuneVo(FineTuneEntity fineTuneEntity) {
.build();
}

public int importEvalFromCommon(Long projectId, Long spaceId, List<String> uuids) {
return evaluationRepo.migration(
String.format(TABLE_NAME_FORMAT, projectId),
uuids,
String.format(FULL_EVALUATION_SUMMARY_TABLE_FORMAT, projectId, spaceId)
);
public MigrationResult importEvalFromCommon(Long projectId, Long spaceId, List<String> uuids) {
// validate the src rows
// 1. status should be final
var jobs = jobMapper.findJobByUuids(uuids, projectId);
var validates = jobs.stream()
.filter(jobEntity -> jobStatusMachine.isFinal(jobEntity.getJobStatus()))
.map(JobEntity::getJobUuid)
.collect(Collectors.toList());
var success = 0;
if (!validates.isEmpty()) {
success = evaluationRepo.migration(
String.format(TABLE_NAME_FORMAT, projectId),
validates,
String.format(FULL_EVALUATION_SUMMARY_TABLE_FORMAT, projectId, spaceId)
);
}
return MigrationResult.builder()
.success(success)
.fail(uuids.size() - success)
.build();
}

public int exportEvalToCommon(Long projectId, Long spaceId, List<String> uuids) {
return evaluationRepo.migration(
String.format(FULL_EVALUATION_SUMMARY_TABLE_FORMAT, projectId, spaceId),
uuids,
String.format(TABLE_NAME_FORMAT, projectId)
);
public MigrationResult exportEvalToCommon(Long projectId, Long spaceId, List<String> uuids) {
// validate the src rows
// 1. status should be final
// 2. model should not be draft
var jobs = jobMapper.findJobByUuids(uuids, projectId);
var validates = jobs.stream()
.filter(jobEntity -> jobStatusMachine.isFinal(jobEntity.getJobStatus())
&& !jobEntity.getModelVersion().getDraft()
)
.map(JobEntity::getJobUuid)
.collect(Collectors.toList());
var success = 0;
if (!validates.isEmpty()) {
success = evaluationRepo.migration(
String.format(FULL_EVALUATION_SUMMARY_TABLE_FORMAT, projectId, spaceId),
validates,
String.format(TABLE_NAME_FORMAT, projectId)
);
}
return MigrationResult.builder()
.success(success)
.fail(uuids.size() - success)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2022 Starwhale, Inc. 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.
*/

package ai.starwhale.mlops.domain.ft.bo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MigrationResult {

@Default
private int success = 0;

@Default
private int fail = 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ List<JobEntity> listBizJobs(

JobEntity findJobByUuid(@Param("uuid") String uuid);

List<JobEntity> findJobByUuids(@Param("uuids") List<String> uuids, @Param("projectId") Long projectId);

int addJob(@Param("job") JobEntity jobEntity);

List<JobEntity> findJobByStatusIn(@Param("jobStatuses") List<JobStatus> jobStatuses);
Expand Down
12 changes: 12 additions & 0 deletions server/controller/src/main/resources/mapper/JobMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@
</foreach>
</select>

<select id="findJobByUuids" resultMap="jobResultMap">
<include refid="select_jobs"/>
<where>
and project_id = #{projectId}
and job_uuid in
</where>
<foreach item="item" index="index" collection="uuids"
open="(" separator="," close=")">
#{item}
</foreach>
</select>

<update id="updateJobStatus">
update job_info set job_status = #{jobStatus} WHERE id in
<foreach item="item" index="index" collection="jobIds"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package ai.starwhale.mlops.domain.ft;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -48,6 +51,8 @@
import ai.starwhale.mlops.domain.job.po.JobEntity;
import ai.starwhale.mlops.domain.job.spec.JobSpecParser;
import ai.starwhale.mlops.domain.job.spec.StepSpec;
import ai.starwhale.mlops.domain.job.status.JobStatus;
import ai.starwhale.mlops.domain.job.status.JobStatusMachine;
import ai.starwhale.mlops.domain.model.ModelDao;
import ai.starwhale.mlops.domain.model.ModelService;
import ai.starwhale.mlops.domain.model.po.ModelEntity;
Expand All @@ -70,6 +75,7 @@ class FineTuneAppServiceTest {
FineTuneSpaceMapper fineTuneSpaceMapper;

JobMapper jobMapper;
JobStatusMachine jobStatusMachine = new JobStatusMachine();

JobSpecParser jobSpecParser;

Expand Down Expand Up @@ -108,6 +114,7 @@ public void setup() {
jobCreator,
fineTuneMapper,
jobMapper,
jobStatusMachine,
jobSpecParser,
new IdConverter(),
modelDao,
Expand Down Expand Up @@ -150,7 +157,7 @@ void createFt() throws JsonProcessingException {
void listFt() {
when(fineTuneMapper.list(anyLong())).thenReturn(List.of(FineTuneEntity.builder().jobId(1L).build()));
when(jobMapper.findJobById(1L)).thenReturn(JobEntity.builder().build());
Assertions.assertEquals(1, fineTuneAppService.list(1L, 1, 1).getSize());
assertEquals(1, fineTuneAppService.list(1L, 1, 1).getSize());
}

@Test
Expand All @@ -160,11 +167,57 @@ void ftInfo() {
JobVo jobVo = JobVo.builder().build();
when(jobConverter.convert(any())).thenReturn(jobVo);
FineTuneVo fineTuneVo = fineTuneAppService.ftInfo(1L, 1L);
Assertions.assertEquals(jobVo, fineTuneVo.getJob());
assertEquals(jobVo, fineTuneVo.getJob());
}

@Test
void evalFt() {
void ftImport() {
when(jobMapper.findJobByUuids(anyList(), anyLong())).thenReturn(List.of(
JobEntity.builder().jobUuid("uuid1").jobStatus(JobStatus.RUNNING).build(),
JobEntity.builder().jobUuid("uuid2").jobStatus(JobStatus.SUCCESS).build(),
JobEntity.builder().jobUuid("uuid3").jobStatus(JobStatus.FAIL).build()
));
when(evaluationRepo.migration(
"project/1/eval/summary",
List.of("uuid2", "uuid3"),
"project/1/ftspace/1/eval/summary")
).thenReturn(2);

var result = fineTuneAppService.importEvalFromCommon(1L, 1L, List.of("uuid1", "uuid2", "uuid3"));
assertEquals(2, result.getSuccess());
assertEquals(1, result.getFail());
verify(evaluationRepo, times(1)).migration(anyString(), eq(List.of("uuid2", "uuid3")), anyString());
}

@Test
void ftExport() {
when(jobMapper.findJobByUuids(anyList(), anyLong())).thenReturn(List.of(
JobEntity.builder()
.jobUuid("uuid1")
.jobStatus(JobStatus.RUNNING)
.modelVersion(ModelVersionEntity.builder().draft(true).build())
.build(),
JobEntity.builder()
.jobUuid("uuid2")
.jobStatus(JobStatus.SUCCESS)
.modelVersion(ModelVersionEntity.builder().draft(false).build())
.build(),
JobEntity.builder()
.jobUuid("uuid3")
.jobStatus(JobStatus.FAIL)
.modelVersion(ModelVersionEntity.builder().draft(true).build())
.build()
));
when(evaluationRepo.migration(
"project/1/ftspace/1/eval/summary",
List.of("uuid2"),
"project/1/eval/summary")
).thenReturn(1);

var result = fineTuneAppService.exportEvalToCommon(1L, 1L, List.of("uuid1", "uuid2", "uuid3"));
assertEquals(1, result.getSuccess());
assertEquals(2, result.getFail());
verify(evaluationRepo, times(1)).migration(anyString(), eq(List.of("uuid2")), anyString());
}

@Test
Expand Down Expand Up @@ -276,7 +329,8 @@ void testReleaseFtNotFound() {
@Test
void testFeatureDisabled() {
when(featuresProperties.isFineTuneEnabled()).thenReturn(false);
Assertions.assertThrows(StarwhaleApiException.class,
Assertions.assertThrows(
StarwhaleApiException.class,
() -> fineTuneAppService.createFineTune(
"1",
1L,
Expand Down
Loading
Loading