parseBPWOrQuant takes a string and returns a float64 BPW value
(input string)
| 661 | |
| 662 | // parseBPWOrQuant takes a string and returns a float64 BPW value |
| 663 | func ParseBPWOrQuant(input string) (float64, error) { |
| 664 | // First, try to parse as a float64 (direct BPW value) |
| 665 | bpw, err := strconv.ParseFloat(input, 64) |
| 666 | if err == nil { |
| 667 | return bpw, nil |
| 668 | } |
| 669 | |
| 670 | // If parsing as float fails, check if it's a valid quantisation type |
| 671 | input = strings.ToUpper(input) // Convert to uppercase for case-insensitive matching |
| 672 | if bpw, ok := GGUFMapping[input]; ok { |
| 673 | return bpw, nil |
| 674 | } |
| 675 | |
| 676 | // If not found, try to find a close match |
| 677 | var closestMatch string |
| 678 | var minDistance int = len(input) |
| 679 | for key := range GGUFMapping { |
| 680 | distance := levenshteinDistance(input, key) |
| 681 | if distance < minDistance { |
| 682 | minDistance = distance |
| 683 | closestMatch = key |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if closestMatch != "" { |
| 688 | return 0, fmt.Errorf("invalid quantisation type: %s. Did you mean %s?", input, closestMatch) |
| 689 | } |
| 690 | |
| 691 | return 0, fmt.Errorf("invalid quantisation or BPW value: %s", input) |
| 692 | } |
| 693 | |
| 694 | // levenshteinDistance calculates the Levenshtein distance between two strings |
| 695 | func levenshteinDistance(s1, s2 string) int { |
no test coverage detected